Dynamo DB
Amazon runs hundreds of services on thousands of commodity machines, with millions of customers. They need availability, scalability, failure tolerance and manageability.
We assume a simple query model on binary objects (no multi-row ops), ACID properties but with weaker consistency and efficiency (optimized for the 99.9% percentile):
put(key, context, obj): store an object with a primary key and context (vector clock & history).get(key)
CAP Theorem states you can only have 2 of the 3 properties: Consistency, Availability, Partition (failure) Tolerance. Dynamo DB is AP.
Dynamo does conflict resolution on reads - so its always writeable. Its designed for a SLA of 99.9% requests served in under <300ms. Conflict resolution can be application aware, but falls back to last-write-wins.
Data is partitioned by key, and replicated across multiplpe node. The key is hashed to index a distributed has table (DHT) with a consistent hashing scheme. Each node is responsible for a range of keys, and the hash ring is used to determine which node is responsible for a given key.
To avoid uneven data, use virtual nodes - each physical node is assigned multiple virtual nodes in the hash ring. Virtual nodes are distributed across the ring.
1. Data Versioning
Not all updates may arrive at all replicas. Vector clocks are used for versioning - they associated each data update with a vector of (node, counter) pairs. This is enough to detect conflicts (and resolve those that are not). Resolution is application-based.
2. Quorum Reads & Writes
A quorum is a minimum number of nodes that must be available to perform a read or write operation. This ensures that the system can tolerate failures while maintaining consistency.
I.e. if
A sloppy quorum allows some nodes to be temporarily unavailable, and the system will still function as long as a quorum of nodes is available. This can improve availability and performance, but may lead to temporary inconsistencies.
3. Storage Nodes
Each node is responsible for:
- Request Coordination: execute read/writes, have a state machine for identifying which nodes are responsible for which keys, and coordinate with other nodes to ensure consistency.
- Membership & Failure Detection: use a gossip protocol to detect node failures and update the membership list.
- Local Persistent Storage: may use different engines, but BDB is most popular.
4. Handling Failures
Temporary Failures use hinted handoff - offload data to node that follows the last node in preference list, hinting that its temporary. This means the data will be sent back when the node recovers.
Permanent Failures are handled by synchronizing data with another node using Merkle trees (tree consisteing of hashes, where each node is the hash of its children). This allows for efficient comparison of data between nodes, and only the differences need to be transferred.