Databases and distributed dataOrganize the records. Keep distributed data correct.
Databases and distributed data. Learn the concepts, explore a model and defend your design.
15SYSTEM DESIGN
DEFINITION
Data design combines record organization, authoritative update rules and the placement of copies or shards. An efficient read is useful only if the system also preserves ownership and concurrency rules.
An index is a maintained shortcut
STEP 1Lookup keystudent_id = 42
STEP 2Index entriesLocate matching row references
STEP 3Table rowsRead the requested records
An index reduces some read work but uses space and must be maintained during writes.
CONCEPT 1
Store records and change them safely
Data models and indexes
Relational tables connect structured records and can enforce constraints. Document, key-value and graph models suit other access patterns. Choose the actual product behavior rather than assuming NoSQL means more scalable. Large photo bytes can live in object storage with ownership metadata in a database. An index is a maintained shortcut for selected queries; it speeds some reads while adding write and space cost.
The final-seat problem
Two buyers can both read stock=1 before either changes it. An unprotected read-then-write can accept both. Use an atomic conditional update or an appropriate transaction/locking strategy, check which request won and record related changes consistently. Atomic means the chosen change cannot be split into conflicting intermediate decisions. The database must enforce the rule, not only the browser.
Copies are not the same as splits
Replication copies data and can produce stale reads when propagation lags. Sharding divides data ownership across instances. A popular key can overload one shard even if record counts are balanced. Consistency states what users may observe across updates. During a partition, an uncertain seat sale may need rejection while an approximate counter can tolerate temporary differences. A replica is not a recoverable historical backup.
Replication copies; sharding divides
CONCEPTReplicationA B C -> A B C
CONCEPTShardingA B on one; C D on another
CONCEPTCombined designEach shard can have replicas
These solve different problems and can be used together.
Key learning points
Match indexes to real query patterns.
Use atomic authoritative decisions for scarce stock.
Distinguish copied data from split ownership.
Compare the important ideas
Databases and distributed data: concepts and trade-offs
Term
Means
Check
Index
Maintained read shortcut
Query plan and write cost
Transaction
Coordinated selected changes
Boundary and concurrent anomalies
Replica
Another copy
Lag, failover and recovery
Shard
A divided ownership group
Key skew and movement
WORKED EXAMPLE
Separate browsing and purchase correctness
A cached seat map can briefly show an old available seat. The purchase path must still atomically decide whether a buyer can reserve it. A replica that has not seen the latest reservation is not an independent authority allowed to sell the same seat.
Further reading
Original teaching explanations by Buildfest Learn. Check these primary references for the underlying concepts and implementation details.
Two buyers both saw one available seat. Compare a separate read-then-write with an authoritative conditional update.
1A and B both saw one seat
2Both write after stale reads
32 bookings for 1 seat
This interleaving oversells the seat.
Make the eligibility check and accepted state transition atomic at the authoritative store. The loser must get an unavailable result; a stale browse response does not grant a reservation.
Chapter review: two questions
Attempt both questions before opening the model answers.
Question 1
Explain how an index differs from a replica and a shard, using a student-order example.
Reveal model answer
An index helps find selected orders in a store. A replica copies those records to another node. A shard divides ownership, for example by a chosen user key. Each adds different write, consistency or routing consequences.
Question 2
Two buyers see one available seat. State where the final decision belongs and what happens to a loser.
Reveal model answer
The decision belongs to an authoritative atomic update or correct database transaction, with the unique sale/reservation invariant enforced. One request succeeds; the other receives an unavailable result. A stale browse response does not justify accepting both.
Answers stay in this tab. Download before leaving. This practice is not graded or saved to your account.