Sub topic 1
Requirements and the First Distributed Design
Prerequisites
Assume you know hash maps, sorted lists, basic RPC, and Python syntax. You should also understand that a node is one server in a cluster.
Requirements and scale targets
Start with the contract before choosing algorithms. A key-value store maps a unique key to a value and exposes operations such as:
PUT(key, value)creates or replaces a value.GET(key)returns the value or “not found.”DELETE(key)removes the key.
Define semantics for retries, missing keys, and concurrent writes. For example, a retry should not create an unexpected second record. Decide whether values are opaque bytes, how large they may be, and whether deletes need durable tombstones.
Scale targets turn vague goals into design constraints. Record expected requests per second, data size, growth rate, availability target, and latency target such as p99 latency. Also specify durability: a successful write might need to survive a process restart or a complete node failure.
class KeyValueStore:
def __init__(self):
self.data = {}
def put(self, key, value):
self.data[key] = value
def get(self, key):
return self.data.get(key)
def delete(self, key):
self.data.pop(key, None)
store = KeyValueStore()
store.put("user:42", b"Ada")
print(store.get("user:42"))
store.delete("user:42")The single-node storage engine
A durable node commonly uses a write-ahead log (WAL). The node appends a record to the WAL before acknowledging a write. After a crash, it replays the log.
Recent writes are held in a memtable, an in-memory sorted structure. Reads check the memtable first. When it fills, the node writes an immutable SSTable to disk. Compaction later merges SSTables, removes obsolete versions, and reduces read work.
Why and how to partition data
Partitioning, or sharding, divides keys among nodes. It prevents one machine from storing or serving the entire dataset. The partition function must be deterministic so every coordinator can identify the responsible nodes.
Consistent hashing and the hash ring
Consistent hashing places both node identifiers and key hashes on a circular hash space. A key belongs to the first node found clockwise from its hash. When membership changes, only keys near the changed position usually move, unlike ordinary modulo hashing.
Virtual nodes and balanced load
A physical node can own many virtual nodes, or vnodes, on the ring. Vnodes spread ownership across the hash space and make rebalancing smaller. Adding capacity means assigning some vnodes to the new server.
Exercise
Why does consistent hashing reduce movement when one node joins?
Show answer
Only keys in the new node's clockwise intervals move; most keys keep their previous owners.