Search DistillSys

Find a concept

Type at least two characters to search lessons, designs, papers, and interview prep.

End-to-end walkthrough

Distributed Database

Provide a coherent data model across partitions, replicas, transactions, and failures.

01
Frame before solving

Requirements & boundaries

Functional

  • Read and write keys or rows through a stable logical schema.
  • Support atomic multi-row transactions and snapshot reads.
  • Repartition and repair online.

Quality attributes

  • Committed data survives tolerated node/zone failure.
  • Serializable or clearly documented isolation.
  • Predictable behavior during leader movement and overload.

Explicitly out of scope

  • Every SQL feature and workload shape.
  • Cross-cloud active-active writes in the first version.
02
Size the important constraints

Back-of-the-envelope estimates

Data1 PB across 10k shards

Shard metadata and balancing are control-plane systems.

Traffic5M reads/s, 1M writes/s

Gateways cache routing; shards scale independently.

Transaction95% single-shard

Co-locate common access paths and optimize the fast path.

Replication3 or 5 replicas/shard

Write latency includes a quorum round trip.

These are reference assumptions, not universal facts. In an interview or architecture review, change them when the product context changes.

03
Define the contract

API & data model

Core operations

POST/v1/transactionsBegin transaction and return transaction identity/snapshot.
POST/v1/transactions/{id}/commitConditionally commit writes; retry-safe by transaction ID.
GET/v1/rows/{key}?at=Read latest or a specified consistent snapshot.

Authoritative records

MVCCVersionkey+timestamp, value/tombstone, transaction_idVersions support snapshots and conflict checks.
RangeDescriptorstart_key, end_key, epoch, replica_set, leaderEpoch fences stale routing and ownership.
04
Trace the critical path

Architecture & request flow

  1. 1Route key or range
  2. 2Coordinate transaction
  3. 3Replicate mutations
  4. 4Commit authoritative state
  5. 5Serve consistent reads

SQL or KV gateway

Parse requests and route work

Transaction coordinator

Track timestamps and commit state

Shard replicas

Store and agree on ordered mutations

Metadata plane

Own placement and rebalancing

05
Reason about the hard parts

Critical design deep dives

Single-shard commit

A gateway routes to the shard leader. The leader validates conflicts, appends the mutation to its consensus log, waits for quorum, applies to MVCC state, then acknowledges. Followers serve only reads allowed by the consistency contract.

Multi-shard transactions

Use a transaction record plus prepare/commit across participant shards. Resolve an uncertain coordinator by consulting durable transaction status. Timestamps and locks/intents require cleanup and deadlock policy.

Online repartitioning

Snapshot a key range to new replicas, stream the log tail, then transfer authority with a higher range epoch. Gateways encountering stale descriptors refresh and retry safely.

06
Make trade-offs explicit

Architecture decisions

ChoiceWhyCost
Consensus per shardProvides one authoritative orderWrites pay quorum latency
MVCCAllows nonblocking snapshotsOld versions require cleanup
07
Failure-first review

What happens if…?

Coordinator response is lost

Expose an idempotent transaction identity and resolve outcome from durable state.

Shard is rebalancing

Transfer authority explicitly and fence stale replicas.

08
Avoid premature complexity

How the design evolves

1
Replicated shard

One consensus group + MVCC

Move here when: Prove durability and transaction semantics.

2
Distributed ranges

Routing metadata, splits, balancing, transaction records

Move here when: Data or write throughput exceeds one group.

3
Operational maturity

Admission control, backups, follower reads, geo placement

Move here when: Predictability and disaster recovery become requirements.

09
Test the reasoning

Interview follow-ups

What happens after commit succeeds but the response is lost?

Strong answer signal: Retry with transaction ID and return the durable prior outcome.

How do follower reads stay correct?

Strong answer signal: Read index/lease, bounded staleness, or explicit snapshot timestamps.

How does a split avoid writing to both old and new owners?

Strong answer signal: Epoch fencing and an atomic authority transition.

10
Build from primitives

Concepts used