Data at Scale
Question: how do you store more data, and serve more reads and writes, than one machine can handle, while keeping latency and correctness acceptable?
The microservices pattern language does not cover this question; it assumes each service can handle its own data. Most interview cases are about it, so it keeps its own hub, using the same format.
Forces
- Data size and throughput: what grows, and which resource runs out first. See scalability and capacity estimates.
- Latency: distance to the data and the number of round trips.
- Availability and durability: copies on several machines survive failures. They must be kept in agreement.
- Consistency: every copy, whether a replica, a cache, an index, or a view, can be stale. Decide what each operation may observe; see consistency.
- Skew: real workloads concentrate on hot keys, hot partitions, and peak hours.
- Coordination: every decision that several nodes must agree on costs round trips and fails when nodes cannot reach each other.
Options
Partitioning and replication answer different questions and are usually combined: partitioning spreads the load, and replication keeps each partition available.
For splitting read and write models, see CQRS. It applies within one service as well as across services.
Indexes
Storage engines:
Secondary indexes in a partitioned store:
- Local index, partitioned by document: each partition indexes only its own rows. A write stays on one partition, but a query by the indexed value must go to every partition, which is scatter-gather.
- Global index, partitioned by the indexed value: a query reads one index partition, but a write may touch several. The index is usually updated asynchronously, so reads can be stale. DynamoDB’s global secondary indexes behave this way; see the DynamoDB notes.
Choose indexes from the access patterns recorded with the requirements. Each one is paid for on every write. The database notes show how an index reduces disk reads.
Search index
An inverted index maps each term to a posting list: the documents that contain the term, often with positions. Treat it as a derived view, not as the system of record.
- Feeding it: publish changes from the owning service through the outbox or change data capture, and apply them idempotently. The index is then a read model in the sense of CQRS.
- Partitioning by document: each shard holds a complete index for a subset of the documents. A write touches one shard. Every query goes to all shards, and the top results from each are merged. Elasticsearch and most search engines work this way.
- Partitioning by term: each shard holds the posting lists for some terms. A one-term query reads one shard, but multi-term queries and document writes span shards, so it is rare in practice.
- Ranking: BM25 scores keyword matches. Dense retrieval over embeddings and hybrid search are in the RAG definitions.
- Costs: the index lags the source by the propagation delay. A change to the mapping needs a full reindex, usually into a new index followed by an alias switch. Replicas add read throughput, but the cluster is one more system to operate.
Scatter-gather and tail latency
When a request fans out to N partitions, its latency is the latency of the slowest one. If one call in a hundred to a single server is slow, a request that calls 100 servers is slow about 63 percent of the time (1 − 0.99¹⁰⁰). Dean and Barroso describe the mitigations:
- Hedged requests: if a partition has not answered within, for example, its 95th-percentile latency, send the same request to another replica and use whichever answers first.
- Partial results: answer at a deadline with the partitions that replied, when the use case tolerates it, as search usually does.
- Fewer partitions per query: choose the partition key so the common queries hit one partition.
Timeouts bound each partition’s call; hedging and partial results decide what to do when they fire.
Coordination
Coordination is slow and fails when nodes cannot reach each other, so the options come in order. The theory is in time and order, broadcast and consensus, and coordination avoidance.
- Avoid it. Give each key one writer by partitioning, so no two nodes need to agree.
- Use a coordination service. etcd and ZooKeeper provide leader election, membership, locks, and configuration on top of consensus. Kubernetes keeps its state in etcd; see the control plane.
- Do not implement consensus yourself.
Mechanisms:
- Leases and fencing tokens: a leader holds a lease that expires unless it is renewed. A process can pause, for garbage collection or a VM migration, past the end of its lease and still believe it is the leader. So each lease carries a fencing token, a number that increases with every grant, and the storage rejects writes that carry an older token.
- Consensus (Raft, Paxos): nodes agree on an ordered log; an entry is committed once a majority accepts it, so 2f + 1 nodes tolerate f failures. Every write waits for a majority, which is the latency cost in CAP and PACELC.
- Logical clocks: a Lamport clock orders events consistently with causality, but cannot tell whether two events were concurrent. Version vectors can. Leaderless and multi-leader stores such as Dynamo use them to detect concurrent writes, then keep both versions for the application, or a CRDT, to merge.
- Last-write-wins: resolving conflicts by wall-clock timestamp silently drops one of two concurrent writes, and clock skew decides which one.
Key-value stores and data grids
A key-value store is the storage choice for access by key. Its internals are a standard interview design, recorded in the key-value store case: consistent hashing, replication with quorums, hinted handoff, anti-entropy, gossip, and version vectors.
An in-memory data grid, such as Hazelcast, Apache Ignite, or Oracle Coherence, is a partitioned and replicated store held in memory that can also run computation next to the data. Unlike a cache, it may be the system of record. Losing a node then means rebalancing and possibly losing data, so replication and persistence settings matter. It fits low-latency state shared by many application instances, such as sessions or reference data, and costs memory and operational care.
Rate limiting across nodes
One limiter keeps an exact count. When many nodes enforce one limit, they must share the count:
Two further decisions:
- When the store is down: fail open and allow traffic, risking overload, or fail closed and reject it, causing an outage. Failing open with a local fallback limit is the common choice when the limiter protects a shared backend.
- Tiers and several limits: key each limit by tenant and tier, read the limits from configuration rather than code, and apply several at once, such as a per-second burst limit and a daily quota. Reject with a 429 response and a
Retry-Afterheader so clients can back off.
Leads to
- Every copy creates a consistency question: consistency.
- Propagating changes to caches, replicas, and search indexes reliably leads to the outbox and change data capture.
- Stateless instances behind a load balancer move state into these stores.
- Failover of a primary leads to reliability and availability.
- Scatter-gather leads to timeouts and degraded results.
- A coordination service is the state store of a control plane.
Seen in cases
- URL shortener: key generation, caching of redirects, replication, and failover with possible loss of recent writes.
- Key-value store: cache write policies and a Dynamo-style design.
- Dropbox: large objects, metadata, and synchronization.
- Ticketmaster: event search served from Elasticsearch, kept in sync with PostgreSQL through change data capture.
- Job scheduler: due jobs claimed with leases, and a single active scheduler.
- Rate limiter and Snowflake IDs: focused applications.
Gaps
- Object storage: chunking, metadata, and durability through replication or erasure coding (Xu and Lam 2022, chapter 9).
- Approximate structures beyond HyperLogLog: Bloom filters and count-min sketches.
- Vector indexes, such as HNSW, as a storage choice; the RAG notes use them without a design discussion.
- No case yet designs a search index end to end or uses a coordination service explicitly.
Sources
- Data partitioning and the sharding pattern – Microsoft.
- Designing Data-Intensive Applications, second edition: storage and indexes, replication, sharding, transactions, distributed-systems faults, consistency and consensus, and batch processing.
- Xu 2020: chapters on scaling from one server, consistent hashing, a key-value store, a unique ID generator, a rate limiter, and a URL shortener.
- Dean and Barroso, The Tail at Scale, Communications of the ACM, 2013 (doi:10.1145/2408776.2408794).
- How to do distributed locking – Martin Kleppmann: leases and fencing tokens.
- Gubernator: distributed rate limiting – Mailgun.
- Distributed-systems notes for coordination and theory, with a reading list.
Library sources
- Kleppmann and Riccomini 2026, Designing Data-Intensive Applications, Second Edition – chapter 4 on Indexes and Search index; chapter 7 on hot spots and secondary indexes; chapters 9 and 10 on Coordination; chapter 11 on batch processing
- Xu 2020, System Design Interview: An Insider’s Guide – chapters 1 and 4-8
- DeCandia et al. 2007, Dynamo: Amazon’s Highly Available Key-value Store
- Petrov 2019, Database Internals – part I (chapters 2-7) on B-trees and log-structured storage; chapters 10 and 14 on leader election and consensus
- Educative 2023: sharded counters, distributed search, and rate limiter, Grokking Modern System Design Interview
- Manning et al. 2008, Introduction to Information Retrieval – chapters 1 and 4 on the inverted index and its construction; chapter 20 on partitioning an index by term or by document
Further reading:
- Gorton 2022, Foundations of Scalable Systems: Designing Distributed Architectures