Library
34 min read

Design a distributed key value store

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.

ClientCoordinatorRing + vnodesReplica 1Replica 2Replica 3Commit log

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.

Step 1: Understand the problem#

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 askThey sayWhat 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.

What you are building#

Functional requirements
What the system does.
  • get(key) and put(key, value). That is the entire API, and keeping it that small is what makes the rest possible.
  • Data spread across many nodes, with a configurable number of copies. No node holds everything and no node is special.
  • Stay writable while nodes are down. This one goal forces almost every other decision.
  • Repair copies that fell behind. Quickly when a key is read, and slowly in the background for keys nobody reads.
Non-functional requirements
How well it has to do it. The numbers you commit to.
  • Values up to about 1MB. Larger belongs in object storage with the key here.
  • Single digit millisecond reads at p99 inside one region.
  • No single point of failure and no leader to elect.
  • Adding a node must not require moving most of the data.
Out of scope
What you cut, and say so out loud.
  • Range scans and secondary indexes. Get and put only, and saying so early keeps the design honest.
  • Multi key transactions. A store built for availability cannot offer them cheaply, and pretending otherwise is where these designs go wrong.
  • Cross region replication. Same ideas, much longer round trips, and enough material for its own page.

"p99" means the 99th percentile: 99 out of every 100 reads must be at least this fast.

Back of the envelope#

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.