Dynamo: Amazon's highly available key-value store
medium 35 minAmazon's shopping cart had to accept every add, even while servers and links failed.
Theory
Read or listen first. About 4 min.
What it is
Dynamo is a key-value store that Amazon built for services that only read and write by primary key, such as shopping carts, session state, best seller lists and the product catalog (§1). It offers two calls: get(key) and put(key, context, object) (§4.1). It was described in the paper "Dynamo: Amazon's Highly Available Key-value Store" by DeCandia, Hastorun, Jampani, Kakulapati, Lakshman, Pilchin, Sivasubramanian, Vosshall and Vogels, presented at SOSP 2007, the ACM Symposium on Operating Systems Principles, held 14 to 17 October 2007 in Stevenson, Washington.
The paper's own words for its goal are an "always writeable" data store (§2.3). To get there it gives up strong consistency during some failures. When copies disagree, Dynamo keeps every version it cannot order and hands them to the application to merge.
The paper does not invent one big new algorithm. Its main contribution, in its own words, is showing how known techniques can be combined into one highly available system that works in production (§1). Table 1 of the paper lists them: consistent hashing, vector clocks, sloppy quorums with hinted handoff, Merkle trees and gossip. This page teaches each one and how they fit together.
Level: must-know (three stars) in our papers list. Read it: about 16 pages. Before it: consistent hashing and Lamport clocks help, and both are explained here as far as Dynamo needs them.
Why it exists
Amazon ran its shop on tens of thousands of servers in many data centres. At that size something is always broken: the paper calls failure handling "our standard mode of operation" (§1). A customer still had to be able to add an item to the cart while disks failed and network routes flapped.
Classic replicated databases keep copies strongly consistent by coordinating them during the write. When they cannot be sure, they refuse the write (§2.3). For a cart that means a lost sale. Dynamo flips the choice: never refuse a write, and do the hard work of resolving conflicts when the data is read.
The paper also changed how people talk about storage. Werner Vogels wrote in 2017 that it served as a catalyst for the category now called NoSQL. In interviews today its ideas, more than the paper itself, are what you are expected to use: quorums, vector clocks, consistent hashing and hinted handoff.
A short history
1978
Leslie Lamport publishes "Time, clocks, and the ordering of events in a distributed system" in Communications of the ACM 21(7). Dynamo's vector clocks cite it as their root (reference 12 of the paper).
1997
Karger and others present consistent hashing at STOC '97, 4 to 6 May, El Paso. Dynamo's partitioning is built on it (reference 10).
2004
Amazon runs Oracle's enterprise edition with clustering and replication and cannot meet its availability and scale needs, Vogels wrote in 2017. A 2012 post by Vogels traces several outages in the 2004 holiday season to commercial technology pushed beyond its limits.
2007
On 2 October Vogels posts the paper on All Things Distributed. It is presented at SOSP, 14 to 17 October, in Stevenson, Washington.
2012
Amazon launches DynamoDB on 18 January. Vogels' launch post says it is based on the principles of Dynamo and combines them with the best parts of SimpleDB.
2022
The DynamoDB paper at USENIX ATC, 11 to 13 July, says DynamoDB shared most of Dynamo's name but little of its architecture: each partition has a leader chosen with Multi-Paxos.
Words to know
- Consistent hashing
- Put machines and keys on one circle of hash values. A key belongs to the first machine clockwise from it. Adding a machine moves only nearby keys.
- Virtual node or token
- One position of a machine on the ring. Each machine has several, which spreads its load round the circle.
- Preference list
- The machines responsible for a key, in ring order, distinct machines only. The first N are the owners, the rest are spares (§4.3).
- N, R, W
- N copies of each key. A read waits for R answers, a write waits for W acknowledgements, the coordinator included (§4.5).
- Coordinator
- The node that handles one get or put, usually the first owner in the preference list (§4.5).
- Vector clock
- A list of (node, counter) pairs on each version. It tells whether one version has seen another, or whether they happened side by side (§4.4).
- Sibling
- One of several versions that no clock orders. A get returns all of them and the application merges them.
- Sloppy quorum
- Reads and writes go to the first N healthy nodes in the preference list, which may not be the N owners (§4.6).
- Hinted handoff
- A stand-in node keeps a copy with a note naming the real owner, and hands it back when the owner returns (§4.6).
- Anti-entropy and Merkle tree
- A background job that finds differences between replicas by comparing a tree of hashes from the top down (§4.7).
- Gossip
- Every second each node talks to one random peer and they merge what they know about membership (§4.8.1).
- Read repair
- After a read, the coordinator sends the newest version to replicas that answered with an old one (§5).
How it works
Step through the idea, or press Play.
Place. Hash the key onto the ring, walk to N distinct machines
Understand it
Start here
The paper on one card
| Paper | Dynamo: Amazon's Highly Available Key-value Store |
|---|---|
| Authors | G. DeCandia, D. Hastorun, M. Jampani, G. Kakulapati, A. Lakshman, A. Pilchin, S. Sivasubramanian, P. Vosshall, W. Vogels (Amazon.com) |
| Venue and year | SOSP 2007, 21st ACM Symposium on Operating Systems Principles, 14 to 17 October 2007, Stevenson, Washington |
| DOI | 10.1145/1294261.1294281 |
| Read it | [PDF on All Things Distributed](https://www.allthingsdistributed.com/files/amazon-dynamo-sosp2007.pdf), or the [Amazon Science page](https://www.amazon.science/publications/dynamo-amazons-highly-available-key-value-store) |
| Level | Must-know. About 35 minutes here, 16 pages in the original |
| Helps to know first | Consistent hashing and the idea of logical clocks |
The analogy
The street party list
Five neighbours, Asha, Lee, Noor, Sam and Max, live in cottages on a path round a pond. Together they keep the shopping list for a street party. There is no single master copy. Each line of the list is kept in three cottages, and anyone may add a line at any hour, even when a neighbour is away or the path is blocked.
Nobody ever refuses a new line. The cost is that pages can disagree for a while, and someone has to merge them.
Section 1 and 2
The problem before the paper
Before Dynamo, the usual answer for state was a relational database. The paper gives three reasons it did not fit these services (§2). Most of them only read and write by primary key, so the rich query features cost money for nothing. The replication technology of the day usually chose consistency over availability. And it was hard to scale out and spread load across many machines.
A strongly consistent design must answer one awkward question during a failure: what if I cannot be sure my copy is the latest? Its answer is to wait or refuse. The paper puts it plainly: the data is made unavailable until it is absolutely certain that it is correct (§2.3).
The old answer: refuse while unsure
What the services needed (§2.1, §2.3)
Simple queries
Read and write one item by key. No operation spans several items, and objects are usually smaller than 1 MB.
Always writeable
Rejecting a customer's update gives a poor experience, so writes must be accepted even during failures.
Tight tail latency
Service level agreements are measured at the 99.9th percentile, not the average.
Trusted, small clusters
Internal use only, no hostile nodes, and each service runs its own instance of up to hundreds of hosts.
The paper also states four principles (§2.3). Incremental scalability: add one machine at a time. Symmetry: every node has the same job. Decentralization: prefer peer-to-peer techniques, because central control had caused outages. Heterogeneity: a bigger machine should be able to take more work.
Why the 99.9th percentile matters
The key idea
The whole paper in one picture
One put: three copies, two acknowledgements
Table 1 of the paper, in our words
| Problem | Technique | What it buys |
|---|---|---|
| Partitioning | Consistent hashing | Add machines one at a time |
| Always writeable | Vector clocks, reconciled during reads | Accept writes, sort out conflicts later |
| Short failures | Sloppy quorum and hinted handoff | Keep writing when some replicas are away |
| Long failures | Anti-entropy with Merkle trees | Replicas catch up in the background |
| Membership and failure detection | Gossip | No central registry to fail |
Step 1
Spread the keys on a ring
Dynamo hashes each key with MD5 into a 128-bit number (§4.1). Picture the numbers bent into a circle, so the largest wraps round to the smallest. Each machine sits at some positions on the circle. A key belongs to the first machine you meet walking clockwise from the key (§4.2).
Plain consistent hashing gives each machine one random spot, which leaves some arcs much bigger than others and ignores that machines differ in size. Dynamo gives each machine several positions, called virtual nodes or tokens. When a machine leaves, its load spreads across many others. When one joins, it takes a little from many. A bigger machine can get more tokens (§4.2).
How much moves when a machine joins
Step 2
Copy each key to N machines
The first machine clockwise is the key's coordinator. It also copies the key to the next N - 1 machines clockwise (§4.3). In Figure 2 of the paper, B stores keys in the range from A to B and copies them to C and D. So each machine is responsible for the arc between itself and its Nth predecessor.
The list of machines responsible for a key is its preference list. Two details matter. First, with tokens the next three positions may belong to only two machines, so the walk skips positions of machines already in the list. Second, the list holds more than N machines, so there are spares when one fails (§4.3). Section 4.6 adds that the list is built so the machines span several data centres.
Building the list for cart:42 on our toy ring
| Walk | Token | Machine | Decision | List so far |
|---|---|---|---|---|
| 1 | 10 | A | new machine | A |
| 2 | 22 | B | new machine | A, B |
| 3 | 30 | A | skip: A is already in | A, B |
| 4 | 40 | C | new machine, N = 3 reached | A, B, C |
| 5 | 55 | D | spare | A, B, C, then D |
Step 3
Count to W, count to R
Any node can receive a request. If it is not among the key's top N, it forwards the request to the first of them (§4.5). The coordinator then does this. For a put, it gives the new version a vector clock, writes it locally, sends it to the N highest-ranked reachable nodes, and answers success once W - 1 of them reply, because its own copy counts as one. For a get, it asks the same nodes for every version they hold, waits for R answers, and returns every version that is not an ancestor of another (§4.5).
A put and a get with N = 3, R = 2, W = 2
When do a read and a write meet?
Settings the paper reports (§6)
| Use | N, R, W | What it means |
|---|---|---|
| Common setting | 3, 2, 2 | Reads and writes overlap without failures; one slow node does not slow anyone |
| High performance read engine | R = 1, W = N | Product catalog and promotions: many reads, few updates |
| Highest write availability | W = 1 | A write succeeds while any single node can store it, but it may live on one machine for a while |
| Typical N | 3 | N sets durability |
What W buys, in our own numbers
Step 4
Version every change with a vector clock
Dynamo treats each change as a new, immutable version (§4.4). Usually a new version simply replaces the old one and the system can tell by itself. That is syntactic reconciliation. During failures plus concurrent updates, the history can branch. Then the system cannot pick, so the client must merge the branches. That is semantic reconciliation.
To tell the two cases apart, every version carries a vector clock, a list of (node, counter) pairs. Version a is an ancestor of version b if every counter in a is less than or equal to the matching counter in b. Otherwise they conflict (§4.4). A client that updates must pass the context from its earlier read, which holds the clock of what it saw.
Figure 3 of the paper, step by step
| Version | Written by | Clock | Relation |
|---|---|---|---|
| D1 | Sx | [(Sx, 1)] | first version |
| D2 | Sx | [(Sx, 2)] | descends from D1, which can be dropped |
| D3 | Sy | [(Sx, 2), (Sy, 1)] | descends from D2 |
| D4 | Sz, after a client read D2 | [(Sx, 2), (Sz, 1)] | descends from D2, concurrent with D3 |
| D5 | Sx, after a client read D3 and D4 and merged them | [(Sx, 3), (Sy, 1), (Sz, 1)] | covers both branches |
Compare two clocks
def descends(a, b): """True if clock a has seen everything clock b has seen.""" return all(a.get(node, 0) >= n for node, n in b.items())def relation(a, b): if descends(a, b) and descends(b, a): return "same" if descends(b, a): return "a is older" if descends(a, b): return "b is older" return "concurrent: keep both"d2 = {"Sx": 2}d3 = {"Sx": 2, "Sy": 1}d4 = {"Sx": 2, "Sz": 1}d5 = {"Sx": 3, "Sy": 1, "Sz": 1}print("D2 vs D3:", relation(d2, d3))print("D3 vs D4:", relation(d3, d4))print("D5 vs D4:", relation(d5, d4))Clocks can grow if many nodes coordinate writes to one object. In practice writes are usually handled by one of the top N nodes, so clocks stay short. To be safe, Dynamo stores a timestamp with each pair, and when a clock reaches a threshold, the paper says "say 10" pairs, it drops the oldest pair. That can make an ancestor look concurrent, but the paper reports the problem had not shown up in production (§4.4). The general topic of versions is covered in data versioning.
Step 5
Keep writing when a node is down
A strict quorum would wait for the N owners. If one is down or cut off, the write fails. Dynamo uses a sloppy quorum instead: every read and write goes to the first N healthy nodes in the preference list, which may not be the first N on the ring (§4.6).
In the paper's example, with N = 3, if node A is down, a replica meant for A goes to D. It carries a hint naming A. D keeps hinted replicas in a separate local database and scans it now and then. When A is back, D delivers the replica and may delete its own copy, so the number of replicas does not drop (§4.6).
Hinted handoff
Whole data centres can fail too: power, cooling, network or natural disasters. Dynamo builds each preference list across several data centres connected by fast links, so a whole data centre can go without a data outage (§4.6). A different view on what to give up during a split: the CAP theorem.
Step 6
Repair what the hints missed
Hints work best when failures are short and membership barely changes. A stand-in can itself fail before it hands a replica back. For that and other threats, Dynamo runs anti-entropy: replicas compare their data in the background and copy what is missing (§4.7).
Comparing every key would be slow. So each node keeps a Merkle tree for each key range it holds. A leaf is the hash of one key's value, and each parent is the hash of its children. If two roots are equal, the whole range is equal. If not, the nodes compare the children and keep going down only where hashes differ, until they find the keys that are out of sync (§4.7).
Two replicas look for a difference
Cost of finding one bad key
Three ways copies find each other again
The drawback in the original scheme: when a node joins or leaves, many key ranges change and their trees must be rebuilt. The fixed partitions of §6.2 solve that, as the numbers part shows.
Step 7
Gossip about who is in the ring
Nodes are added and removed explicitly: an operator uses a command line tool or a browser to tell a node about the change, which is written to persistent storage (§4.8.1). The paper's reason is that an outage rarely means a node left for good, so it should not trigger rebalancing.
Then gossip spreads it. Every second, each node contacts one random peer and they reconcile their histories of membership changes. The same exchange spreads which tokens each node owns, so every node can send a request straight to the right nodes. That is why the paper calls Dynamo a zero-hop distributed hash table (§3.3, §4.8.1).
One gossip exchange
Two more pieces. Seeds are nodes everyone knows through configuration, so two halves of a ring that were started separately still find each other (§4.8.2). And failure detection is local: A treats B as failed if B does not answer A, even if B answers C. A then uses other nodes and retries B now and then (§4.8.3). Early designs kept a global view of failures, but explicit join and leave made it unnecessary. More on detectors in general: failure detection.
Step 8
One request, one state machine
Each node has three parts, all in Java: request coordination, membership and failure detection, and a pluggable local storage engine. Most instances used the Berkeley DB Transactional Data Store; others used BDB Java Edition, MySQL, or an in-memory buffer with a persistent store behind it (§5). Every client request gets its own state machine on the node that received it.
The read state machine of §5
One more trick targets the tail. Letting only the first node coordinate writes put too much load on some nodes, so any of the top N may coordinate. Because a write usually follows a read, the coordinator is the node that answered that read fastest, which the context records. That raises the chance of read-your-writes and makes latency steadier (§5).
Section 6
The numbers that matter
| Number | Value | Where | What it means for a design |
|---|---|---|---|
| Common (N, R, W) | (3, 2, 2) | §6 | The default to quote in an interview |
| Example SLA | 300 ms for 99.9% of requests at a peak of 500 requests per second | §2.2 | Set targets on the tail, not the mean |
| Tail versus average | 99.9th percentile around 200 ms, an order of magnitude above the average | §6.1, Figure 4 | Averages hide what slow users see |
| Write buffer | A buffer of 1,000 objects cut the 99.9th percentile by a factor of 5 at peak | §6.1, Figure 5 | Trades durability for speed; one replica still does a durable write |
| One version per read | 99.94% of cart requests in 24 hours | §6.3 | Conflicts are rare; most came from many concurrent writers, often robots |
| Successful responses | 99.9995% without timing out, no data loss event | §6.6 | Over the two years the paper covers |
| Client-driven routing | 99.9th percentile reads 30.4 ms versus 68.9 ms server-driven | §6.4, Table 2 | Skipping the load balancer hop helps the tail most |
| Clock size threshold | "say 10" pairs | §4.4 | Truncation keeps clocks small |
| Gossip and polling | Every 1 s between nodes; clients poll every 10 s | §4.8.1, §6.4 | Membership can be up to 10 s stale at a client |
| Busy day | Tens of millions of requests, well over 3 million checkouts in one day | §1 | The cart service on Dynamo, holiday season |
Load balancing efficiency (§6.2)
How partitioning changed in production (§6.2)
| Strategy | How | Problem or gain |
|---|---|---|
| 1: random tokens, partition by token | T random tokens per node; ranges between tokens | A join scanned other nodes' stores; bootstrapping took almost a day at busy times; Merkle trees rebuilt; archiving hard |
| 2: random tokens, equal partitions | Q equal ranges, tokens only decide placement | Worst balance; used briefly while migrating |
| 3: Q / S tokens, equal partitions | Q equal ranges; each of S nodes holds Q / S of them | Best balance; membership data three orders of magnitude smaller than strategy 1; partitions move as files |
Smaller lessons from §6
Imbalance falls with load
Out-of-balance nodes were about 20% at low load and close to 10% at high load, using a 15% threshold. More popular keys spread better.
Background work waits
Repair and handoff tasks get time slices from an admission controller that watches foreground latency, for example the 99th percentile of database reads over 60 seconds.
Three ways to merge
Business logic (the cart), last write wins by timestamp (session state), or a read engine with R = 1 and W = N (catalog).
Limits of full membership
Each node gossips the full routing table. Fine for a couple of hundred nodes, hard for tens of thousands (§6.6).
Since 2007
What aged badly, and what changed
How the field moved
- Apache Cassandra took the ring, tokens, gossip, hints and Merkle repair, but resolves conflicts with last write wins on per-column timestamps instead of vector clocks.
- Riak, another Dynamo-style store, returns siblings and recommends dotted version vectors over plain vector clocks to limit siblings.
- Amazon DynamoDB (2012) is a managed service with a leader per partition, chosen with Multi-Paxos, and offers strongly consistent reads.
- Data types with built-in merge rules, such as sets that track adds and removes, are a principled answer to the cart problem.
Did not age well
- Asking every application to merge versions is hard work. Even the paper says some developers pushed it back to the store, which then used last write wins (§2.3).
- A union merge brings back deleted items, as §4.4 admits. You need remove markers or a smarter data type.
- Each team ran its own instance. The DynamoDB paper says that operational load became a barrier to adoption.
- Sloppy quorums make R + W > N a weak promise during failures, which is often misquoted.
Dynamo is not DynamoDB
| Dynamo (2007 paper) | DynamoDB (2022 paper) | |
|---|---|---|
| Who runs it | Each service team ran its own instance | A managed AWS service |
| Replication | Leaderless: any of the top N coordinates | One leader per partition, elected with Multi-Paxos |
| Write done when | W replicas, sloppy quorum | A quorum of the replication group has the log record; two of three replicas in different zones |
| Reads | Eventually consistent; siblings possible | Strongly consistent reads from the leader, or eventually consistent from any replica |
| Conflicts | Vector clocks, application merges | One leader orders writes per partition |
Connections
Where Dynamo sits
Lineage
Do not mix these up
| System | Who decides order | Consistency during a split | Data model |
|---|---|---|---|
| Dynamo | Nobody: vector clocks and the app | Accepts writes, may return siblings | Key to opaque blob |
| Bigtable | A single master assigns tablets; one server per tablet | A row is served by one tablet server | Sorted rows, column families, timestamps |
| Cassandra | Timestamps per cell, last write wins | Tunable per request: ONE, QUORUM, ALL | Rows and columns in partitions |
| DynamoDB | A leader per partition (Multi-Paxos) | Strong reads need the leader | Items with a partition key, optional sort key |
Pages that own the ideas in depth: consistent hashing, data replication, data versioning, failure detection, CAP theorem, consistency models, designing a key-value store, DynamoDB and Cassandra.
Say it aloud
The 60-second recap
- 1
The need
Amazon needed a store whose writes are never refused, because a lost add to cart is a lost sale.
- 2
Placement
Keys sit on a consistent-hash ring with several tokens per machine. Each key has N copies on distinct machines.
- 3
Counting
A write succeeds after W acknowledgements, a read waits for R answers. Services tune them; 3, 2, 2 was common.
- 4
Versions
Concurrent writes create versions tracked by vector clocks. The application merges the ones the clocks cannot order.
- 5
Repair
Short failures use a sloppy quorum and hinted handoff. Merkle trees fix lasting drift. Gossip spreads membership.
- 6
The price
Eventual consistency: a read can be stale, merges are your code, and a removed item can come back.
The original
Read it in three passes
S. Keshav's "How to Read a Paper" suggests three passes: a quick look, a careful read of the figures, and a pass in which you could almost rebuild the work. Here is that plan for Dynamo.
- 1
Pass 1, about 10 minutes
Read the abstract, §1, §2.3 on design choices, the headings of §4, Table 1, and §7. Then fill in the five Cs below before you look at our answers.
- 2
Pass 2, about 45 minutes
Read §4.1 to §4.9 with the Visualize tab open. Study Figure 2 (the ring), Figure 3 (clocks), Figures 4 and 5 (latency), and Figures 7 and 8 (partitioning). Skim §3, related work.
- 3
Pass 3, about an hour
Run the Code tab. Set N = 3, R = 2, W = 2, take one node down, write from both sides of a split, and predict the siblings before you print them. Then list the paper's hidden assumptions.
- 4
Skip for interviews
The storage engine plug-ins of §5 and the details of the admission controller in §6.5. Know that they exist, not how they work.
The five Cs for this paper
| C | Our answer |
|---|---|
| Category | A description of a production system, with measurements |
| Context | Consistent hashing, Lamport's logical clocks, quorum systems, and systems that reconcile later such as Bayou and Coda |
| Correctness | Assumes trusted nodes, small objects, single-key operations, and applications willing to merge |
| Contributions | Showing that these techniques combine into one always-writeable store that meets tail-latency targets in production |
| Clarity | Clear and practical; Table 1 is the map, and some numbers are given only as aggregates to protect business details (§1) |
Summary
The whole topic in one minute.
Dynamo is Amazon's 2007 answer to one question: how do you keep a store writable while machines and links fail? Keys sit on a consistent-hash ring with several tokens per machine. Each key has N copies on the first N distinct machines clockwise. A write is done after W acknowledgements and a read after R answers, and each service picks its own numbers, usually 3, 2 and 2.
Concurrent writes create versions that vector clocks cannot order. Dynamo keeps them all and the application merges them, which never loses an add but can bring back a removed item. A down node's writes go to a stand-in with a hint, Merkle trees repair what hints miss, and gossip spreads membership with no central coordinator. The price is eventual consistency: a read can be stale and merging is your code.
- Dynamo, SOSP 2007: an always-writeable key-value store for Amazon's cart and similar services.
- Consistent hashing with several tokens per machine; a preference list of N distinct machines, plus spares.
- A put is done after W copies, a get after R answers; (3, 2, 2) was common, and R + W > N overlaps only when the same N answer.
- Vector clocks tell newer from concurrent; siblings go to the application, which merges them.
- A union merge never loses an add but can bring back a removed item.
- Sloppy quorum and hinted handoff for short failures, Merkle trees for long ones, gossip for membership.
- SLAs at the 99.9th percentile; 99.94% of cart reads saw one version.
- DynamoDB shares the name, not the design: it uses a leader per partition chosen with Multi-Paxos.
Sources
- DeCandia et al., Dynamo: Amazon's Highly Available Key-value Store, SOSP 2007 (PDF)
- Amazon Science: publication page for the Dynamo paper
- Werner Vogels: Amazon's Dynamo (2 October 2007)
- Werner Vogels: Amazon DynamoDB (18 January 2012)
- Werner Vogels: A decade of Dynamo (2 October 2017)
- Elhemali et al., Amazon DynamoDB, USENIX ATC 2022
- Lakshman and Malik, Cassandra: a decentralized structured storage system (LADIS 2009)
- Apache Cassandra documentation: Dynamo
- Riak KV documentation: causal context
- S. Keshav, How to Read a Paper (copy hosted by Stanford)