Sub topic 1
Requirements, scale targets, and the get/put/delete API
Prerequisites
This chapter assumes you know:
- Basic data structures such as hash maps and sorted lists.
- How clients and servers talk over a network, including RPC (remote procedure call).
- What a hash function does.
- How to read short Python snippets.
What we are building
A key-value store maps a key (a string or bytes) to a value (an opaque blob). It does not interpret the value and has no joins or secondary indexes. This narrow scope is what lets it scale across many machines.
Before choosing any technique, write down what the system must do and how well it must do it. Every later decision, such as partitioning, replication and quorum sizes, follows from these requirements.
Functional requirements
The store exposes three operations:
put(key, value)stores a value under a key. If the key exists, the new value replaces it.get(key)returns the value for a key, or reports that the key is missing.delete(key)removes a key.
Values are small, typically under 1 MB. Keys are looked up by exact match only. Range scans are out of scope for this design.
Non-functional requirements and scale targets
Non-functional requirements describe quality rather than behavior. The numbers below are example targets for this tutorial. Real projects take them from product needs.
| Requirement | Example target | Design consequence |
|---|---|---|
| Data volume | 10 TB total | Data must be split across many nodes (partitioning) |
| Throughput | 100,000 requests per second | Load must spread evenly across nodes |
| Latency | p99 under 10 ms for get and put | Reads and writes cannot wait on every replica |
| Availability | Writes succeed during node failures | Data is replicated, with no single point of failure |
| Durability | Acknowledged writes survive a crash | Writes go to disk before the acknowledgement |
| Scalability | Add nodes without downtime | Adding a node moves only a small share of keys |
| Consistency | Tunable per request | Clients pick between stronger and faster behavior |
The p99 latency is the value that 99% of requests beat. Tail latency matters because one slow replica can slow a whole request.
A quick estimate shows why one machine is not enough. With 10 TB of data and 2 TB of usable disk per node, you need at least 5 nodes. With 3 copies of each key, you need at least 15. Throughput and failures push the number higher.
The API in code
The single-node interface below is the contract the distributed system must preserve. It is a plain in-memory version.
class KeyValueStore:
def __init__(self):
self._data = {}
def put(self, key: str, value: bytes) -> None:
self._data[key] = value
def get(self, key: str):
"""Return the value, or None if the key is missing."""
return self._data.get(key)
def delete(self, key: str) -> None:
self._data.pop(key, None)
store = KeyValueStore()
store.put("user:42", b"alice")
print(store.get("user:42")) # b'alice'
store.delete("user:42")
print(store.get("user:42")) # NoneWhere the simple API gets harder
On many machines, each of these operations needs a precise definition:
- Delete is usually a write of a marker called a tombstone. If nodes simply erased the key, a replica that missed the delete could bring the old value back.
- A get may return several conflicting values when two clients wrote at the same time. The API must be able to carry that information, for example as a list of versions plus a context token passed back on the next put.
- A put can time out without telling the client whether it was applied. Making puts idempotent, meaning repeating one has the same effect as doing it once, makes retries safe.
Exercise
Why is delete usually implemented as writing a tombstone instead of erasing the key right away?
Show answer
With replication, a replica that was down during the delete would still hold the old value. If the other replicas had erased the key, the old value could spread back as if it were new. A tombstone is a record that the key was deleted, so replicas can compare it with the old value and agree the key is gone.