In-Memory Databases

📖 3 min read

What They Are

In-memory databases store all data in RAM rather than on disk, eliminating disk I/O for dramatically lower latency. Operations complete in microseconds rather than milliseconds.

Disk access has always been the bottleneck for database performance. Even with SSDs, reading from disk takes thousands of times longer than reading from RAM. In-memory databases accept the cost of RAM (more expensive per gigabyte) in exchange for eliminating that bottleneck entirely.


Data Structure

┌─────────────────────────────────────────────────────────────────────────┐
│  IN-MEMORY DATABASE (Redis example)                                     │
├─────────────────────────────────────────────────────────────────────────┤
│                                                                         │
│  STRINGS                                                                │
│  ┌─────────────────────┬────────────────────────────────────┐          │
│  │  user:42:name       │  "Alice"                           │          │
│  │  counter:pageviews  │  158472                            │          │
│  └─────────────────────┴────────────────────────────────────┘          │
│                                                                         │
│  LISTS (ordered, duplicates allowed)                                    │
│  ┌─────────────────────┬────────────────────────────────────┐          │
│  │  queue:emails       │  ["msg1", "msg2", "msg3"] ←→ FIFO  │          │
│  └─────────────────────┴────────────────────────────────────┘          │
│                                                                         │
│  SETS (unordered, unique)                                               │
│  ┌─────────────────────┬────────────────────────────────────┐          │
│  │  user:42:tags       │  {"premium", "early-adopter"}      │          │
│  └─────────────────────┴────────────────────────────────────┘          │
│                                                                         │
│  SORTED SETS (ordered by score)                                         │
│  ┌─────────────────────┬────────────────────────────────────┐          │
│  │  leaderboard:game1  │  {alice: 9500, bob: 8200,          │          │
│  │                     │   carol: 7800}  ← sorted by score  │          │
│  └─────────────────────┴────────────────────────────────────┘          │
│                                                                         │
│  HASHES (field-value pairs)                                             │
│  ┌─────────────────────┬────────────────────────────────────┐          │
│  │  session:abc123     │  {user_id: 42, ip: "10.0.0.1",     │          │
│  │                     │   expires: 1705352400}             │          │
│  └─────────────────────┴────────────────────────────────────┘          │
│                                                                         │
└─────────────────────────────────────────────────────────────────────────┘

Operations: O(1) for most operations, executed atomically
  INCR counter:pageviews        → 158473 (atomic increment)
  LPUSH queue:emails "msg4"     → adds to list head
  ZADD leaderboard:game1 9600 alice  → updates score

Data lives entirely in RAM with optional disk persistence. Rich data structures like sorted sets, lists, and hashes are native operations, not application-layer abstractions.


How They Work

Data in RAM

The entire dataset lives in memory. There’s no disk seek, no page cache miss, no buffer pool management. Data is always immediately accessible.

Persistence Options

Despite being memory-first, most in-memory databases provide durability:

RDB snapshots: Periodic full dumps to disk

AOF (Append-Only File): Log every operation for replay on restart

On restart, the database reconstructs state from the persistence file.

Data Structures

In-memory databases often expose efficient data structures directly. Redis provides:

  • Lists: For queues
  • Sets: For unique collections
  • Sorted sets: For leaderboards
  • Hashes: For objects with fields
  • Streams: For event logs

Operations on these structures are atomic.

Single-Threaded Execution

Redis uses a single thread for command execution, avoiding lock contention. This simplifies programming because there are no race conditions between operations on the same key.


Why They Excel

Latency

Sub-millisecond response times. Network latency becomes the bottleneck, not database operations.

Throughput

Without disk I/O, a single server handles hundreds of thousands of operations per second.

Atomic Operations

Operations like incrementing a counter, adding to a set, and popping from a queue are all atomic with no race conditions.


Why They Struggle

Dataset Size

Data must fit in RAM. While servers with terabytes of RAM exist, they’re expensive.

Cost

RAM costs significantly more per gigabyte than disk storage.

Durability Trade-offs

Persistence options trade performance for durability. The fastest configuration risks data loss on crash.


When to Use Them

In-memory databases excel at:

  • Caching: Store computed results, database queries, API responses
  • Session storage: User sessions with fast access and atomic operations
  • Rate limiting: Atomic counters with expiration
  • Real-time leaderboards: Sorted sets maintain rankings
  • Pub/sub messaging: Low-latency message distribution
  • Distributed locking: Atomic operations for coordination

When to Look Elsewhere

If your dataset exceeds available RAM, if durability is more important than latency, or if cost is a primary concern, disk-based databases are more appropriate.


Examples

Redis is the most popular in-memory database, with rich data structures, clustering, persistence, and a massive ecosystem.

Memcached is simpler, offering pure caching without persistence or data structures beyond strings. It’s multi-threaded, scaling better on multi-core machines for simple workloads.

SAP HANA is an enterprise in-memory database supporting both OLTP and OLAP workloads with columnar storage.

VoltDB provides in-memory ACID transactions with SQL support for high-performance transactional workloads.


Found this guide helpful? Share it with your team:

Share on LinkedIn