FOUNDING PRICING · 40% OFFfor the first 1,000 members · from ₹95/month
ArchitectureAmazon11 min read

Amazon Dynamo Paper Explained: The Always-Writable Store

The 2007 Amazon Dynamo paper for students: consistent hashing, N, R and W quorums, vector clocks, hinted handoff, Merkle trees and gossip, step by step.

ShareXLinkedInWhatsApp
A tall brick Art Deco tower, the Pacific Medical Center building in Seattle, rising above trees beside a road bridge under a clear blue sky in August 2007.
A tall brick Art Deco tower, the Pacific Medical Center building in Seattle, rising above trees beside a road bridge under a clear blue sky in August 2007. Photo: Joe Mabel, CC BY-SA 3.0, via Wikimedia Commons.

In October 2007, nine Amazon engineers published a paper describing Dynamo, an internal key-value store built so that a customer could always add an item to a shopping cart, even while disks were failing and data centres were unreachable. To get that, Dynamo gave up something most databases treat as sacred: the promise that every read sees the latest write. The paper is worth studying because almost every technique in it now appears in system design interviews.

One warning before we start. This post is about the 2007 internal system. DynamoDB, the AWS service launched in 2012, borrows the name and some principles but is a different product, and we come back to the difference near the end.

A diagram of a consistent hashing ring with seven nodes, A to G, where a key hashes to a point between A and B, node B coordinates the write and copies it to C and D, beside the four steps of a put with N equal to 3 and W equal to 2.

The problem Amazon wanted to solve

By 2007 Amazon's site was built from hundreds of services running on tens of thousands of servers in many data centres. At that size something is always broken. The paper's starting point is that failure is the normal case and the storage layer has to be designed around it.

The authors observed that many of these services, such as shopping carts, session state and best seller lists, only ever read and wrote data by primary key. A relational database gave them joins and transactions they did not use, and its replication options tended to choose consistency over availability: if the replicas could not agree, the write was refused. For a shopping cart, a refused write is a lost sale.

So the requirements were unusual:

  • Simple interface. get(key) and put(key, context, object) on small binary objects, usually under 1 MB. No query spanning several keys.
  • Tail latency, not average latency. Amazon's services promised each other response times at the 99.9th percentile. The paper's example is a service that must answer within 300 ms for 99.9% of requests.
  • Always writable. The paper calls the goal an "always writeable" data store. A write must succeed during server failures and network partitions.
  • No special nodes. Every node does the same job, so there is no master whose failure stops the system, and machines can be added one at a time.
A long row of nested supermarket shopping trolleys with dark plastic handles, stretching away from the camera.
Supermarket trolleys, as an illustration: Dynamo's best known user was Amazon's shopping cart service. Photo: Shixart1985, CC BY 2.0, via Wikimedia Commons.

Partitioning: a ring, and many tokens per machine

Dynamo has to decide which machines hold which keys, and it must keep working as machines come and go. Hashing the key and taking it modulo the number of servers fails badly here, because changing the server count moves nearly every key.

Dynamo uses consistent hashing. The output range of a hash function is treated as a circle. Each node is given a position on the circle. A key is hashed (the paper uses MD5, giving a 128-bit value) to a point on the same circle, and it belongs to the first node found by walking clockwise from that point. When a node joins or leaves, only its neighbours on the ring are affected.

The plain version has two weaknesses the paper names. Random positions give uneven arcs, so some nodes get more data than others, and it ignores the fact that some machines are more powerful. Dynamo's answer is virtual nodes: each physical machine is assigned many positions on the ring, called tokens. This has three effects:

  • When a machine fails, its load is spread over many other machines, not dumped on one neighbour.
  • A new machine takes a roughly equal slice from each existing machine.
  • A bigger machine can simply be given more tokens.

The paper admits that the first scheme, random tokens with ranges defined by them, caused operational pain: handing key ranges to a new node could take almost a day in the busy shopping season. Amazon later divided the ring into a fixed number of equal partitions and assigned whole partitions to nodes.

Replication: N, R and W

Each key is stored on N nodes. The node that owns the key's position, the coordinator, stores it and replicates it to the next N-1 distinct physical machines clockwise. The ordered list of nodes responsible for a key is its preference list, and it is built by skipping ring positions that belong to a machine already in the list.

Two more numbers control each operation:

  • W: how many replicas must confirm a write before the client is told it succeeded.
  • R: how many replicas must answer a read before the client gets a result.

If R + W > N, the set of nodes that answered the read must overlap the set that accepted the latest write, so at least one of the replies carries the newest version. The configuration the paper says several Dynamo instances commonly used was N = 3, R = 2, W = 2.

The three numbers are a dial, and the paper stresses this as the main benefit to service owners. Lower W and writes get faster and survive more failures, but fewer machines hold the data at the moment of acknowledgement. A read-heavy service could choose R = 1 and W = N. Since an operation waits for the slowest of the R or W replicas it needs, keeping them below N also protects that 99.9th percentile.

Versions, vector clocks and the shopping cart

Because a write is acknowledged before all replicas have it, and because writes are accepted during partitions, two replicas can hold different versions of the same key. Dynamo does not hide this. It treats every write as a new immutable version and attaches a vector clock to it: a list of (node, counter) pairs recording which coordinators have handled the object and how many times.

Comparing two clocks tells Dynamo how the versions are related. If every counter in clock X is less than or equal to the matching counter in clock Y, then Y descends from X and X can be discarded. The paper calls this syntactic reconciliation, and the system does it by itself. If neither clock dominates, the versions were written concurrently and neither is safe to throw away.

A diagram of five versions of one shopping cart: D1 and D2 written through node Sx, then two concurrent versions D3 through Sy and D4 through Sz whose vector clocks cannot be ordered, and a merged version D5 written through Sx with a clock that covers both.
Vector clocks spotting a conflict, following the paper's example; the cart contents are our illustration

In that case a get returns all the conflicting versions together with a context that summarises their clocks. The application merges them and writes the result back with that context, which tells Dynamo the new version supersedes both. This is semantic reconciliation, and it is why put takes a context argument.

The shopping cart is the paper's running example. Adding to the cart and deleting from it are both put calls. If two versions of a cart diverge, the cart service merges them by taking the union, so an added item is never lost. The cost is stated plainly in the paper: a deleted item can come back. Amazon judged a reappearing item to be a much smaller problem than a vanished one.

Services that do not want this work can ask Dynamo to resolve conflicts by timestamp, keeping the version with the latest wall-clock time. The paper says the session service did this.

How often do conflicts really happen? The team measured the shopping cart service for 24 hours and found that 99.94% of requests saw exactly one version.

Handling failures: hinted handoff, Merkle trees and gossip

A strict quorum would insist on the same N nodes every time, and would refuse writes if too few of them were reachable. Dynamo loosens this. Reads and writes go to the first N healthy nodes in the preference list, which is why the list is longer than N. The paper calls this a sloppy quorum.

Suppose N = 3, the preference list for a key begins A, B, C, D, and A is down. The copy meant for A goes to D with a hint in its metadata naming A as the intended owner. D keeps hinted copies in a separate local store and checks periodically whether A is back. When it is, D delivers the copy and can delete its own. This is hinted handoff.

A three-panel diagram: in normal operation a write for a key lands on nodes A, B and C; while A is down the third copy goes to node D with a hint naming A; when A recovers, D hands the copy to A and deletes its own.
A sloppy quorum and hinted handoff with N = 3

Preference lists are also arranged to span data centres, so that losing an entire site does not make any key unavailable.

Hinted handoff covers short outages. If the node holding the hint itself dies, or a replica misses updates for a long time, something has to compare replicas directly. Comparing every key would be far too slow, so Dynamo uses Merkle trees. Each leaf is the hash of one key's value, and each parent is the hash of its children. Two nodes that share a key range first compare root hashes. If the roots match, the whole range is identical and nothing more is sent. If not, they compare children and descend only into the branches that differ, until they reach the specific keys that are out of sync. Each node keeps one tree per key range it hosts. The paper calls this background process anti-entropy.

Dynamo also repairs on the read path. After answering a get, the coordinator waits briefly for the remaining replies, and if any replica returned a stale version it sends that replica the newer one. This is read repair.

The last piece is knowing who is in the ring. There is no central registry. An administrator explicitly adds or removes a node, and that change spreads by gossip: every second each node picks a random peer and the two reconcile their membership histories and token maps. A few nodes act as seeds that everyone knows, so that two halves of a new cluster cannot form separate rings. Temporary failures are not announced globally at all. A node simply notices that a peer is not answering and routes around it.

What Dynamo gave up, and what came after

The paper is clear about the costs. Applications must be written to cope with several versions of an object. There are no multi-key operations and no isolation guarantees. Every node keeps the full routing table, which the authors say works for hundreds of nodes but would need a different design for tens of thousands.

The results they report: Dynamo had been used by internal services for two years, applications received successful responses without timing out for 99.9995% of requests, and no data loss event had occurred. The shopping cart service handled tens of millions of requests in a single day, with more than 3 million checkouts.

Dynamo was never sold as a product. In 2012 Werner Vogels, Amazon's CTO and one of the paper's authors, wrote that it had met its reliability goals but was still software each team had to run and tune for itself, and that many engineers avoided it for that reason. DynamoDB, launched that year, was designed as a managed service based on Dynamo's principles combined with lessons from SimpleDB. Do not assume that a detail in the 2007 paper, such as client-side merging of vector clock conflicts, describes how DynamoDB behaves.

The computer science underneath

Consistent hashing. The idea is to make the mapping from keys to machines depend on positions in a fixed hash space, not on the number of machines. The lesson on consistent hashing in depth works through rings, virtual nodes and the alternatives.

CAP and eventual consistency. When the network splits, a replicated store must either refuse some requests or let replicas disagree for a while. Dynamo chooses to stay available and converge later. CAP and consistency models explains the choice and what eventual consistency does and does not promise.

Quorums. R + W > N is a pigeonhole argument: two sets of sizes R and W drawn from N items must share a member. Quorums, vector clocks and CRDTs covers leaderless replication, and the DBMS lesson on key-value stores and Dynamo-style design puts the pieces together.

Logical time. Wall clocks on different machines disagree, so they cannot say reliably which write came first. A vector clock records causality instead: who had seen what. Logical clocks: Lamport and vector timestamps builds this up from first principles.

What an interviewer could ask from this

Why not just use hash(key) mod number of servers? Changing the number of servers changes the result for almost every key, so nearly all data moves. With consistent hashing only the keys next to the changed node move.

With N = 3, R = 2, W = 2, can a read miss the latest write? With a strict quorum, no: any two readers overlap any two writers. With Dynamo's sloppy quorum a write may have landed on a stand-in node, so the guarantee weakens during failures. That is the price of availability.

How does a vector clock tell a conflict from an update? If one clock is greater than or equal to the other in every entry, it is a descendant. If each has an entry larger than the other's, the writes were concurrent.

Why can a deleted cart item reappear? Conflicting carts are merged by union. If one branch deleted the item and a concurrent branch still has it, the union contains it.

Why Merkle trees for replica repair? Two replicas can prove a whole key range is identical by exchanging one hash, and locate differences in a number of steps proportional to the tree's depth, not the number of keys.

Takeaways

  • Dynamo's design follows from one business rule: never refuse a write to the shopping cart. Everything else is a consequence.
  • Consistent hashing with virtual nodes decides where data lives. N, R and W decide how safe and how fast each operation is.
  • Vector clocks do not prevent conflicts. They detect them accurately, and the application decides how to merge.
  • Sloppy quorums and hinted handoff cover short failures, while Merkle trees and read repair fix whatever those miss.
  • The 2007 Dynamo and today's DynamoDB are different systems. Cite the paper for the ideas, not for the service's behaviour.

Sources

  1. DeCandia et al.: Dynamo, Amazon's Highly Available Key-value Store (SOSP 2007)
  2. Werner Vogels: Amazon DynamoDB, a Fast and Scalable NoSQL Database Service (18 January 2012)

Written by the Codevizag Academy team

An independent explainer built from the public sources listed above. Codevizag Academy is not affiliated with Amazon. Found a mistake? Write to support@codevizag.com.

More case studies

Understood it? Now prove it.

The lessons behind this case study are free, and 20 problems a month are judged on real test cases at no cost.

Start solving free