Course contentsShow
← System Design
Lesson 340 of 1,91910. NoSQL: Key-Value StoresFree lesson

In-Memory vs Persistent Key-Value Stores

Comparing in-memory stores like Redis for speed versus persistent stores like RocksDB for durability.

In-Memory vs Persistent Key-Value Stores

What you'll learn: The fundamental trade-off between speed-focused in-memory key-value stores and durability-focused persistent stores.

The Core Trade-Off

Key-value stores come in two main flavors based on where they primarily store data: in-memory (RAM) or on-disk (persistent storage). This choice creates a fundamental trade-off between speed and durability.

In-memory stores like Redis keep all data in RAM. Think of RAM as your desk's work surface—everything is immediately accessible, but if you lose power (the system crashes), anything not saved elsewhere disappears. These stores are blazingly fast because RAM access takes nanoseconds, making them ideal for caching, session management, and real-time leaderboards where milliseconds matter.

Persistent stores like RocksDB write data to disk first. Imagine a filing cabinet—it takes longer to retrieve items, but they're safe even if the building loses power. These stores prioritize durability and can handle datasets larger than available RAM, making them suitable for system-of-record data where loss is unacceptable.

The Hybrid Reality

Many in-memory stores offer optional persistence features (like Redis snapshots or append-only logs) to provide some durability, while persistent stores use memory caching to improve speed. However, their core design priorities remain distinct.

Choosing Between Them

Use in-memory when:

  • Speed is critical (sub-millisecond response times)
  • Data can be regenerated if lost
  • Working set fits in available RAM

Use persistent when:

  • Data must survive crashes
  • Dataset exceeds memory capacity
  • Durability matters more than raw speed

Key Takeaway: In-memory key-value stores like Redis trade durability for extreme speed, while persistent stores like RocksDB sacrifice some performance to guarantee data survives failures—choose based on whether you can afford to lose the data.