Part 7 · Blueprint 42
In-Memory Distributed Key-Value Store (Redis Cluster / Memcached)
15 min reading · Sources · Editorial review pending
Fast in-memory execution does not remove persistence, placement or failover choices.
Page details
Published date · Last edited:
Draft edition. Editorial review and supplementary reading recommendations are still being completed.
Format: Architecture walkthrough. Prerequisites: the beginner bootcamp. Chapter prerequisites: Distributed Caching & Stampede Prevention, Database Sharding & Consistent Hashing, Replication, Consensus & Automated Failover.
Sixty-second mental model
Problem: Fast in-memory execution does not remove persistence, placement or failover choices.
Use: State the acknowledgment durability and migration behavior.
Design boundary: Operate a partitioned in-memory key-value service with eviction and failover semantics. Memory capacity, replication lag and persistence are separate limits.
Layer 1 · Intuition
Hypothetical scenario: Northwind is designing in-memory key-value stores. Maya represents product requirements, Dev implementation and Priya reliability. The team must agree what one key operation means and which component owns the key ownership and persistence policy.
Think of a fast shared desk with explicit recovery copies. Separate the requester from key ownership and persistence policy, then identify the retained evidence needed after failure.
- Authority
- The entitled state owner.
- Invariant
- A rule preserved across concurrency and failures.
Architecture and worked reasoning
Design boundary: Operate a partitioned in-memory key-value service with eviction and failover semantics. Memory capacity, replication lag and persistence are separate limits.
Responsibilities on the request path
Workload and limiting resource
Hypothetical 10,000,000 entries at 200 bytes payload/key-value pair use 2 GB raw. A chosen measured representation factor of 2 would make 4 GB before replica copies and fragmentation; that factor must be validated for an actual allocator/workload.
Keep the units attached to each intermediate result. The assumptions above describe a hypothetical exercise, not observed vendor capacity. A real capacity decision also needs a measured service curve, skew, recovery traffic and an explicit admission policy for overload.
Mutation or publication path
- Map a key to its current slot owner.
- Apply the selected mutation atomically.
- Replicate/persist under the declared acknowledgement rule.
Observation and recovery path
- Route reads to an eligible owner/copy.
- Handle redirects during slot migration.
- Distinguish a true miss from unavailability or eviction.
Failure that changes the decision
Hypothetical scenario: A large hot key overloads one slot owner while cluster memory remains free. Add a domain-specific split or admission strategy rather than assuming more slots split one key.
Attempt this decision before reading the explanation
Is cache eviction equivalent to deleting authoritative data?
Only if the service is explicitly a cache. If it is authoritative, eviction and durability must be designed as data-loss decisions rather than routine memory management.
Supporting mechanism analysis
Hypothetical scenario: Northwind. Maya represents product, Dev implementation and Priya reliability. The following workflow develops the underlying mechanism.
Hypothetical scenario. Dev combines Northwind’s partition map and replicas into a distributed key-value drawing. Priya asks which answer wins when copies disagree. Maya asks who may still accept a change after the caller times out. The boxes locate data, but do not yet settle either operation.
Compose the mechanisms under a small per-key contract. Follow routing, replica choice and version handling together, then inspect what the quorum count establishes and what it leaves unresolved. Category selection remains a separate responsibility from retrieving one representation.
Counts do not specify membership, concurrent versions or which value the read returns. The overlap must belong to a declared protocol with compatible operation rules.
This exposes the assumptions that make overlap useful. Keep authority and uncertain outcomes visible instead of hiding them behind the arithmetic.
Choose a deliberately restricted update model: changes to one key are serialized by its currently authorized owner. Versions increase within that owner’s valid history. If the system cannot establish who may write, it refuses to claim a successful new update. This avoids assuming that arbitrary concurrent values can always be merged by selecting a larger timestamp.
The exercise does not yet supply a complete leader-election or membership-change protocol. Those mechanisms appear later. Record that limitation instead of hiding it behind the word “distributed.” A design can explain the contract a protocol must preserve before teaching the protocol that establishes it.
A client presents the full key to a routing layer. The partition map identifies the owner group, and a coordinator manages communication with the selected replicas. The owner group is not a random sample of all machines: the replicas must belong to the key’s declared placement and membership configuration.
Inside each node, a local storage engine handles records and its persistence rules. Across nodes, the replication protocol determines which versions are retained and which replies satisfy the operation. A fast in-memory lookup at one replica is therefore only one part of the end-to-end operation.
Hypothetical scenario. For one fixed replica set A, B and C, let writes wait for two replicas and reads contact two. The arithmetic 2 + 2 > 3 guarantees set intersection within that membership. It does not specify which returned version wins, how concurrent writers are ordered or whether an incomplete write becomes visible.
Store a value together with its version evidence and deletion state. If A and B accepted version 12 while C retains 11, a read from B and C must apply the declared version-selection rule. Concurrent versions may require reconciliation; choosing the physically newest timestamp is a policy with clock assumptions, not a proof of causal order.
A sloppy quorum can acknowledge substitutes outside the normal owner set, so the ordinary intersection argument no longer applies unchanged. Hinted handoff later forwards an obligation; anti-entropy compares replicas beyond requests that happen to be read. A tombstone must survive long enough to prevent an older value returning from an offline replica, under the chosen repair and retention assumptions.
Hypothetical worked example. Choose three replicas, A, B and C. A serialized update to version 2 is durably stored at A and B before the coordinator acknowledges it. C still holds version 1. A later read obtains replies from B and C and chooses version 2 under the selected valid-history comparison rule.
The write set has two members and the read set has two. Within one fixed three-member configuration, minimum set intersection is 2 + 2 − 3 = 1 member. That explains why any such read set intersects the completed write set. It does not prove that a real implementation returns the desired value merely because it waits for two replies.
The reader still needs the relevant stored state in those replies, a correct version comparison and a defined treatment of incomplete or concurrent writes. Membership changes can invalidate the fixed-set assumption. A read observing a version from an unfinished write also requires a protocol decision; simply choosing the largest observed label is not a universal consistency theorem.
This is why the familiar condition R + W > N is an intersection argument rather than a complete linearizability proof. Linearizability will receive its own treatment using histories. For now, preserve the narrower conclusion that these particular sets overlap under these particular assumptions.
Hypothetical scenario. Refine the serialized-owner exercise with a put request carrying operation K, key Q7, expected version 11 and replacement version 12. The current owner checks the expectation and retains K’s outcome with the mutation under its declared serialization rule. A repeated K returns that outcome; it does not create version 13. This is a teaching contract, not Cassandra’s native conditional-write protocol.
Inside each chosen LSM-style replica, a write enters a recovery log and a sorted in-memory table. For this exercise, require the mutation’s log record to be synchronized to stable storage before that replica sends a durable acknowledgement. A later flush creates an immutable sorted table; reads reconcile the relevant in-memory and on-disk versions, including deletion markers. Background compaction combines tables but must retain versions and tombstones still required by recovery and repair.
If A and B have synchronized version 12 while C has not, their two replies satisfy the selected durable-write threshold. A crash before a memory-table flush can recover from the surviving log records. A lost coordinator reply still leaves K uncertain to the caller, so retry through the same owner and operation identity rather than inferring failure.
Cassandra’s storage-engine documentation explains commit logs, memtables and immutable SSTables, including synchronization settings that change crash-loss exposure. This exercise chooses a stronger acknowledgement rule explicitly; the mere presence of a log does not establish it. Local replay restores one replica’s state, while anti-entropy repairs disagreement between replicas. Neither mechanism supplies the missing owner-election protocol.
Keep Page 15’s selected component workload of fifteen single-key reads per second. This is distinct from the catalogue HTTP request rate and its multi-record listings. If each illustrative key read contacts two replicas once, it generates 15 × 2 = 30 replica-read requests per second. At the selected two changes per second, sending each change to three replicas produces 2 × 3 = 6 replica-update requests per second before retries or repair.
Layer 3 · Internals
Redis Cluster maps keys to 16,384 hash slots using the documented key hashing rule; it does not create 16,384 independent copies of each value. Clients use slot ownership to locate a primary, and redirection responses can guide them during topology changes. A multikey operation may require keys to share a slot; hash tags can deliberately colocate related keys, at the cost of concentrating their load.
Persistence is a separate choice from in-memory execution. Snapshotting and append-only logging offer different recovery and write-amplification behavior, and a successful response must be interpreted under the actual configured persistence and replication policy. Fork-based snapshots can create copy-on-write memory pressure while the parent changes pages. During slot migration, clients must handle the documented transitional routing rather than assume that one stale map remains authoritative forever. A hot key remains one ownership problem even when many other keys distribute evenly; replicating reads, changing the operation or explicitly splitting the key solves different parts of that problem.
Trade-offs and limits
| Approach | Provides | Requires |
|---|---|---|
| Snapshot recovery | Produces a bounded point-in-time image. | Recent acknowledged changes may require another recovery source. |
| Append-only recovery log | Can preserve a more recent sequence of accepted changes. | Rewrite, fsync policy and replay time remain capacity inputs. |
Recovery decision
Hypothetical scenario: A large hot key overloads one slot owner while cluster memory remains free. Add a domain-specific split or admission strategy rather than assuming more slots split one key.
Only if the service is explicitly a cache. If it is authoritative, eviction and durability must be designed as data-loss decisions rather than routine memory management.
Write your design before revealing the review
Is cache eviction equivalent to deleting authoritative data?
Drafts and self-review are stored only in this browser. They are separate from reading completion.
Compare your decision
Only if the service is explicitly a cache. If it is authoritative, eviction and durability must be designed as data-loss decisions rather than routine memory management.
A large hot key overloads one slot owner while cluster memory remains free. Add a domain-specific split or admission strategy rather than assuming more slots split one key.
Check your understanding
Is cache eviction equivalent to deleting authoritative data?
Only if the service is explicitly a cache. If it is authoritative, eviction and durability must be designed as data-loss decisions rather than routine memory management.
How would you recover from this failure: A large hot key overloads one slot owner while cluster memory remains free. Add a domain-specific split or admission strategy rather than assuming more slots split one key.
Start with the exact boundary that failed. Map a key to its current slot owner. Apply the selected mutation atomically. Replicate/persist under the declared acknowledgement rule. Then recover evidence through the observation path: Route reads to an eligible owner/copy. Handle redirects during slot migration. Distinguish a true miss from unavailability or eviction. Preserve the distinction between a confirmed state change and an unavailable response.
Which assumption in the worked calculation would a burst or concentrated tenant invalidate first?
Hypothetical 10,000,000 entries at 200 bytes payload/key-value pair use 2 GB raw. A chosen measured representation factor of 2 would make 4 GB before replica copies and fragmentation; that factor must be validated for an actual allocator/workload. Recompute the affected resource rather than multiplying unrelated storage and network budgets by the same factor. Operate a partitioned in-memory key-value service with eviction and failover semantics. Memory capacity, replication lag and persistence are separate limits.
Which component in this topology could be removed from a smaller first deployment?
Slot router, Memory primary, Replica, Persistence log name responsibilities, not a requirement for separate processes. Co-locate responsibilities when their transaction, trust and capacity boundaries permit it. Map a key to its current slot owner.
What evidence must a caller have before relying on the observation path?
Route reads to an eligible owner/copy. Handle redirects during slot migration. Distinguish a true miss from unavailability or eviction. Only if the service is explicitly a cache. If it is authoritative, eviction and durability must be designed as data-loss decisions rather than routine memory management.
45-minute interview rubric
- 00–05: scope key operation and key ownership and persistence policy.
- 05–12: identify the limiting resource and derive the domain workload above, retaining units and assumptions.
- 12–28: draw key ownership and persistence policy, numbered writes and reads.
- 28–40: challenge recovery: State the acknowledgment durability and migration behavior.
- 40–45: defend key placement, observations and remaining assumptions.
Go deeper
- Redis cluster specification | Docs · Primary document · redis.io. State the acknowledgment durability and migration behavior. Verified 2026-10-26.
- Dynamo | Apache Cassandra Documentation · Primary document · cassandra.apache.org. State the acknowledgment durability and migration behavior. Verified 2026-10-26.
- Storage Engine | Apache Cassandra Documentation · Primary document · cassandra.apache.org. State the acknowledgment durability and migration behavior. Verified 2026-10-26.
Decision log
State the acknowledgment durability and migration behavior.
Preserve key operation identity and key ownership and persistence policy ownership.
Revisit changed workload, observations or recovery objectives.