Scaling Data: Replication, Partitioning and Consistency
Stateless app servers scale by adding machines. Data does not, and that is where the hard part of most system design interviews lives. This lesson is for engineers who are comfortable with the core building blocks and now need to answer the deeper questions mid-level and senior interviews ask: how data is copied, how it is split, and what users see while copies disagree. It ends with a worked example that partitions a chat app's messages.
What the interviewer is probing
The deep-dive part of the interview, roughly its middle third, usually lands on data. The interviewer scores your depth and your trade-offs: can you pick a partition key and defend it, predict where it breaks, and say which consistency guarantee each feature needs? "We'll use a distributed database" is not an answer. "We'll partition messages by channel id, which keeps a channel's history on one node, and here is what we do about very large channels" is.
Replication: keeping copies
Replication keeps the same data on several nodes, for availability (a node can die) and read capacity (reads spread across copies). There are three main shapes.
Leader-follower
One node, the leader, accepts all writes and sends a log of changes to followers, which serve reads. It is the default in most relational databases and the easiest to reason about, because there is a single order of writes.
Replication is usually asynchronous: the leader confirms a write before followers have it. That keeps writes fast, but a follower can lag behind, and if the leader dies before its last writes reach a follower, those writes can be lost when the follower is promoted. Synchronous replication to at least one follower prevents that loss at the cost of slower writes, and a common compromise is one synchronous follower and the rest asynchronous.
Multi-leader
Several leaders, often one per region, each accept writes and replicate to the others. Users write to a nearby leader, so writes stay fast across continents, and a region can keep working if it loses contact with the others. The price is write conflicts: two users edit the same record in two regions at once. You need a rule to resolve them, such as last write wins (simple, but silently drops one edit), merging, or data types designed to merge. Reach for multi-leader only when multi-region writes are a real requirement.
Leaderless with quorums
Any node accepts reads and writes. The client, or a coordinator, writes to N replicas and waits for W acknowledgements, and reads from R replicas and takes the newest value. If R + W > N, every read set overlaps every write set in at least one node, so a read sees the latest acknowledged write. With N = 3, a common setting is W = 2 and R = 2: the system tolerates one node down for both reads and writes. Lowering R or W buys speed and availability at the cost of possibly stale reads.
Replication lag and what users notice
Asynchronous followers are eventually consistent: given no new writes, they catch up. In between, users notice specific anomalies, and an interviewer wants you to name the guarantee that fixes each:
Read-your-writes. A user posts a comment, refreshes, and it is gone, because the read went to a lagging follower. Fix: route a user's reads to the leader for a short time after they write, or read only from followers that have caught up to that user's last write position.
Monotonic reads. A user refreshes twice and sees the comment appear, then disappear, because the second read hit a more stale follower. Fix: pin each user to one follower.
Consistent prefix. A reply appears before the message it answers. Fix: keep causally related writes on the same partition, so they are applied in order.
Most features only need one or two of these, not strong consistency everywhere. Saying which feature needs which is a sign of seniority.
Partitioning: splitting data
When one node cannot hold all the data or absorb all the writes, you partition (shard): each record belongs to exactly one partition, chosen from a partition key. Each partition is then replicated as above.
Range vs hash partitioning
Range partitioning assigns contiguous key ranges to partitions (user ids A to F on partition 1, and so on). Range scans are efficient, which suits time series and sorted queries. The risk is skew: if the key is a timestamp, all of today's writes land on the last partition.
Hash partitioning applies a hash function to the key and assigns hash ranges to partitions. Load spreads evenly, but range queries across keys must ask every partition.
Consistent hashing
With naive hash(key) mod number_of_partitions, adding a partition changes the result for almost every key, so nearly all data moves. Consistent hashing places nodes and keys on the same hash ring; each key belongs to the next node clockwise. Adding a node only takes keys from its neighbor, so only about 1/n of the data moves. Real systems give each physical node many virtual positions on the ring so load stays even and a departing node's keys spread over many others.
Choosing a partition key
Pick the key from your most important queries. The goal is that the hottest query reads one partition, and that writes spread evenly. Ask two questions about a candidate key: does the main query include it, and can a single value of it grow too large or too hot?
Hot keys
Even a good hash cannot help when one key value is itself hot: a celebrity's profile, a viral post, a huge channel. Options, from simplest:
Cache the hot reads so they never reach the partition.
Split the hot key by adding a suffix (
channel_42#0tochannel_42#7), writing to a random suffix and reading from all of them. Writes spread eight ways; reads cost eight lookups, so apply it only to keys known to be hot.Give the largest tenants their own dedicated partition.
CAP and PACELC, stated precisely
CAP is often quoted as "pick two of three". The precise version: during a network partition, when nodes cannot reach each other, a system must choose between consistency (refuse some requests rather than return stale or conflicting data) and availability (answer every request, accepting that some answers may be stale). Partitions are not optional in a distributed system, so the real choice is what happens during one. A bank balance usually chooses consistency; a like counter or a shopping cart can choose availability.
PACELC adds the everyday case: if there is a Partition, choose Availability or Consistency; Else, in normal operation, choose Latency or Consistency. Synchronous replication across regions gives consistency but adds a cross-region round trip to every write. That second half is the trade-off you face on every request, and naming it shows you understand where latency comes from.
Idempotency keys
Networks fail after the server has done the work but before the client hears back, so clients retry, and a retried "send payment" must not pay twice. The client generates a unique idempotency key per logical operation and sends it with every attempt. The server records each key with its result; a repeat key returns the stored result instead of doing the work again. Store the key in the same transaction as the effect, or a crash between the two reintroduces duplicates. The same idea makes queue consumers safe under at-least-once delivery.
Worked example: partitioning a chat app's messages
Requirements: users post messages in channels; opening a channel shows its latest 50 messages and scrolls back in time. Assume 1 billion messages a day, and some channels with hundreds of thousands of members.
Choosing the key. The main query is "latest messages in channel C, before time T". Partitioning by channel_id puts a channel's whole history on one partition, so that query reads one partition. Within the partition, sort by a time-ordered message id, so "the 50 before T" is one range scan.
partition key: channel_id
sort key: message_id (time-ordered, unique)
query: channel_id = C AND message_id < T ORDER BY message_id DESC LIMIT 50Partitioning by message_id instead would spread writes perfectly but make every channel load a scatter across all partitions, which is the wrong trade for the read path.
The hot channel problem. A very large channel is now one hot partition for writes, and one partition that grows without limit. Address both: add a time bucket to the key (channel_id + week), so a channel's history spreads across partitions over time and each bucket stays bounded. Reads of the latest messages still hit one bucket, and scrolling back moves to the previous bucket. For the few channels that are hot even within a week, cache the latest page of messages and fan reads out from the cache.
Consistency per feature. Message order within a channel matters, so writes for a channel go through one partition leader and get time-ordered ids. Read-your-writes matters for the sender (their own message must appear instantly), which the client handles by showing the sent message locally and the server by reading from the leader right after a write. Read receipts and member counts can be eventually consistent.
Mistakes that cost points
Choosing a partition key without the query. Always state the main query first, then the key that serves it.
Quoting CAP as "pick two". State the partition-time choice, and add the latency trade-off from PACELC.
Strong consistency everywhere. It is expensive; match the guarantee to the feature and say why.
Ignoring hot keys. Every celebrity, viral post or giant channel is a predictable follow-up. Have a plan before you are asked.
Practice it
Drill: for a social feed, pick a partition key for posts and one for likes, name the hot key in each, and say which reads can be eventually consistent. Then run Design a Real-Time Chat System as a full interview and use this lesson's partitioning when the interviewer asks about scale. For more on choosing a store, read the databases deep dive.
Practise this in a mock interview
Try what this lesson covers out loud: the AI interviewer asks one of these questions, follows up, and scores your answer.
- Design a URL ShortenerSystem Design · Medium
- Design a Rate Limiter ServiceSystem Design · Medium
- Design Live Score Alert FanoutSystem Design · Medium
- Design a Video Upload and Transcoding PipelineSystem Design · Medium
Read next
- API Design in a System Design Interview
Write a small, defensible API in five minutes, with resources, cursor pagination, idempotency keys, errors, versioning and a reasoned choice of REST, RPC, GraphQL or streaming, and answer the follow-ups it triggers.
8 min read
- Data Modeling for System Design Interviews
Turn requirements into entities and access patterns, choose keys and indexes for the queries that matter, and denormalize for the read path with a stated update path.
8 min read
- System Design Interviews: The Quick Start
Know which system design interview you face and how depth expectations change by level, then deliver any design with a six-step, 45-minute framework whose estimates drive real decisions.
7 min read
- ML System Design Interviews: The Quick Start
Run an ML system design interview with an eight-step, 45-minute framework, from framing and labels to metrics, serving, drift and feedback loops, worked through on a harmful-comment classifier.
9 min read
