A key value store is the simplest database there is. You give it a name (the key, like
cart:7) and some data (the value, like the items in that cart), and later you ask for
the data back by name. That is the whole interface: put and get.
Building one on a single machine is a weekend project. Building one that spreads billions of
keys across hundreds of machines, keeps working while some of them are down, and never
quietly loses a write is this question. Every HLD building block shows up in it: consistent
hashing decides where a key lives, quorums decide what a read returns, replication decides
what survives a crash. Watch one write go through.
Ask any node. The client sends put(cart:7) to whichever node it likes. There is no leader to find. That node becomes the coordinator for this one request.
Three words come up all through this page, so here they are in plain terms:
A node is one machine in the cluster.
A replica is one copy of a key. With three replicas, three different nodes hold the same
key, so losing one machine loses nothing.
Quorum means "enough of them". Instead of waiting for every replica, a write waits for a
set number of them to say yes.
If you can do this one, most of the other design questions become assembly.
Half of this question is deciding what you are not going to support. Every "no" below buys
you something in the design, so say them out loud and early.
You ask
They say
What it settles
What operations does the store have to support?
get and put by key. No range scans, no secondary indexes.
Everything. Because we only ever look up one exact key, we are free to scatter keys across nodes by hashing them. Range scans ("all keys from cart:1 to cart:9") need keys kept in order, which is a very different design.
Do we need transactions across several keys?
No.
No node ever has to lock keys on other nodes, so any node can serve any request. A store built to stay available cannot offer multi key transactions cheaply, so say this early.
When a node is down, do writes fail or succeed?
Succeed. The store must stay writable.
Availability over consistency, so no leader and no pause to elect a new one. It also means two writes to the same key can conflict, and something has to sort them out later.
How big can a value be?
Up to about 1MB.
Values are copied around whole, so replication traffic is predictable. Anything bigger goes to object storage, with only its key kept here.
How many nodes, and how often do they change?
Hundreds, and something is always restarting.
Consistent hashing, not modulo, so adding a node moves a fraction of a percent of the data. At this size a missing node is normal operation, not an incident.
One region or many?
One region for now.
Round trips between nodes stay under a millisecond, so waiting for two of three replicas is cheap. Across regions the same wait costs 150ms and the latency argument changes completely.
Three inputs. The last two rows are the ones that shape the design. (New to this kind of
estimate? The back of the envelope chapter shows the method.)
Cluster sizing
20 billion
1000 bytes
3
Data, one copy20B x 1060B = 21 TB
With 3 copies64 TB
Nodes at 8TB each64 / 8 = 8
Keys moved when one node joins1 / 8 = 12.50%
8 nodes
and one of them is always doing something unusual
At 8 nodes, a node being down is not an incident, it is Tuesday. That is the whole argument for a design with no leader and no special node: something is always restarting, so the system has to treat that as normal, not as a failure to recover from.
The 60 bytes added to each value is a rough allowance for the key and the bookkeeping stored
next to it.