Design an S3-like object store - clients put, get, list and delete objects by bucket and key, identical content is stored once, and eleven nines of durability must hold on cheap disks that fail every day. A small metadata layer has to stay exactly right while petabytes of bytes move past it. The staff-level signal is keeping object bytes off the control path, making an upload visible in one commit, and freeing shared bytes only when it is provably safe.
Finished this lesson?
Sign in to save your progress across devices.
Tip: use ← and → to move between lessons.
Was this lesson helpful?
Rehearse this with a Staff+ engineer
45-minute mock with a Staff+ engineer who interviews at your target company. Premium members save $20.
What to Design?
Could you answer these today? Tick what you already know.
Video Walkthrough
The happy path is quick to draw: an API, a database for metadata, a fleet of disks. The interview is decided by four questions about what happens around that path. Each one is a deep dive later on this page, with three designs side by side: the one that sounds right, a better one, and the one that holds.
Swipe to see all columns
| The four questions, and the answer that sounds right | |||
|---|---|---|---|
| # | The interviewer asks | The answer that sounds right | What it misses |
| DD1 | A client uploads a 6 GB file. Which machines do the bytes pass through? | The API gateway streams them to the storage nodes | Every byte still crosses the gateway. Its network becomes the limit of the whole system |
| DD2 | The upload dies after two of three chunks. What does a reader see? | A row with status UPLOADING, cleaned up later | |
The same three habits answer all four:
Do not spend your questions on API details. Spend them on what the service promises.
Swipe to see all columns
| Scope Decisions | ||
|---|---|---|
| Question | Agreed answer | Why it matters |
| What are we building? | The storage engine and the data path: put, get, list, delete. Login and permissions already exist | The time goes to metadata, replication and cleanup |
| How durable? | Eleven nines. Losing a whole rack must lose nothing | Copies sit in different racks, and a lost copy is replaced fast (DD4) |
| Is identical content stored once? | Yes, also across users | Chunks are named by their hash and shared. Delete becomes reference counting (DD3) |
Out of scope (say this out loud): file sync and desktop clients, replication across regions, object versions and lifecycle rules, a permission policy engine, the public multipart-upload API, encryption key management, and event notifications.
FR1 — Upload. PUT /{bucket}/{key} stores an object. A key is unique in its bucket. An object is never edited: writing the same key again replaces it.
FR2 — Download. GET /{bucket}/{key} returns exactly the bytes that were stored.
FR3 — Delete. DELETE /{bucket}/{key} makes the object disappear at once. Its disk space is freed later.
FR4 — Store identical content once. The same bytes are kept once on disk, however many objects contain them. This is called deduplication, or dedup.
FR5 — List. GET /{bucket}?prefix=... returns the keys of a bucket, filtered by prefix, one page at a time.
The key principle behind all five
Swipe to see all columns
| Five facts and the conclusions they do not supportRead each row as: the fact on the left does not, by itself, imply the claim in the middle. | ||
|---|---|---|
| Fact | Does not imply | Why the gap exists |
| The chunks are on disk | The object exists | An object exists when its row is committed. Chunks without a row are leftovers |
NFR1 — Durability: eleven nines. At 10 billion objects that is about one lost object in ten years. Every chunk has three copies in three racks.
NFR2 — Read availability: 99.99%. A read works as long as one copy of each chunk can be reached.
NFR3 — Scale. 10 billion objects, 10 PB, hundreds of data nodes. Metadata and data grow separately.
NFR4 — Cost. Identical content is stored once. With 30% duplicates that saves 9 PB of disk.
NFR5 — Speed. First byte in under 200 ms for objects up to 1 MB. Large objects are limited only by the network between the client and the data nodes.
| Capacity Thought Experiment | |
|---|---|
| Parameter | Value |
| Objects stored | 10 billion, 1 MB on average: 10 PB |
| After dedup, with 30% duplicates | 7 PB of unique chunks |
| On disk, with three copies | 21 PB. Without dedup it would be 30 PB, so 9 PB are saved |
| Data nodes | About 300 nodes of 100 TB, in 25 racks of 12 |
| Uploads | 100 million a day, about 1,200 a second |
The number that identifies the real system is the gap between two sizes:
metadata ~10 TB must be exactly right: every change is a transaction
bytes ~21 PB 2,000 times more, and only needs "store" and "fetch" by hash
incoming 1.2 GB/s must never pass through the metadata path
A small, strict database and a huge, simple disk farm. The design is the line between the two. Every number above is an illustrative assumption.
Interviewer signal: candidates who draw one "storage service" that receives the upload, stores the bytes and records the metadata have missed the number. Candidates who say "metadata is tiny and must be transactional, bytes are huge and simple, so I split them and keep bytes off the metadata path" have found the system.
Three ideas shape everything:
Metadata is 10 TB and decides everything: what exists, where it lives, what may be deleted. Data is 21 PB and decides nothing. If a data node loses a chunk, the metadata knows where the other copies are. If the metadata is lost, the bytes on disk are noise. So metadata gets a transactional database, and data nodes get cheap disks and no logic.
A file is cut into chunks, and each chunk is named by the SHA-256 hash of its bytes. Identical bytes get the same name, so storing content once is a lookup. And any node can check a chunk by hashing it: a copy is right when its hash is its name.
Once two objects can share a chunk, a delete cannot free bytes. It lowers a counter. The bytes are freed later by a background job, and only when the counter has been 0 for long enough.
The core recognition
This is a small transactional metadata store in front of a large content-addressed chunk store. Content-addressed means: a chunk is found by the hash of its bytes, not by a name someone chose. One transaction makes an object exist or disappear. The bytes follow slower rules: written before the commit, deleted long after.
| Design Decisions | |
|---|---|
| Design choice | Reason |
| Metadata in PostgreSQL, bytes on data nodes | Small data that must be exact and huge data that must be cheap grow separately |
| 64 MB chunks, named by their SHA-256 hash | Identical content gets the same name and is stored once |
Three tables carry the design:
objects,object_chunksandchunks. The field to explain isreference_count: a chunk may be deleted only when it is 0.
One row per bucket and key. An object exists exactly when this row exists.
Swipe to see all columns
| objects | ||
|---|---|---|
| Field | Type | Comment |
object_id | STRING | Primary key, for example O-1 |
bucket_id, key | STRING | Unique together. A key such as videos/demo.mp4 is one string. The slash is just a character |
The ordered list of chunks that make up one object.
Swipe to see all columns
| object_chunks | ||
|---|---|---|
| Field | Type | Comment |
object_id, chunk_index | STRING, INT | Primary key together. The index 0, 1, 2 gives the order of the bytes |
chunk_hash | STRING | |
One row per unique piece of content, shared by every object that contains it.
Swipe to see all columns
| chunks | ||
|---|---|---|
| Field | Type | Comment |
chunk_hash | STRING | Primary key. SHA-256 of the bytes. Shown here by its first four characters, for example a1f3 |
size | INT | Bytes, at most 64 MB |
Swipe to see all columns
| Supporting records | ||
|---|---|---|
| Record | What it holds | Why it exists |
| Bucket | bucket_id, a unique name, the owner | The top-level container of keys |
| Upload | upload_id, bucket, key, the chunk list, expires_at, status OPEN / COMMITTED | |
Swipe to see all columns
| Storage | ||
|---|---|---|
| Store | Data | Consistency |
| PostgreSQL | objects, object_chunks, chunks, uploads, nodes | Strong, transactional. The source of truth |
| Redis | The chunk list of hot objects, for 60 s | Disposable. Removed when the object is deleted |
| Data nodes, local disk | Chunk files, named by their hash | No logic. A chunk is correct when its hash equals its name |
Swipe to see all columns
| What the user calls | ||
|---|---|---|
| Endpoint | Request | Response |
PUT /{bucket}/{key} | The object | 201. Inside the client library this is the three calls below |
GET /{bucket}/{key} | No body | 200 with the chunk list and presigned URLs. 404 if there is no such object |
DELETE /{bucket}/{key} | No body | |
Swipe to see all columns
| One PUT is three calls | ||
|---|---|---|
| Call | Request | Response |
1. POST /uploads | Bucket, key, size, and for each chunk its index, hash and size | Upload U-1, valid 6 hours. Per chunk: stored, or a presigned PUT URL valid 15 minutes |
2. PUT {presigned URL} | The bytes of one chunk, sent straight to a data node | 200 when three nodes have stored it |
A presigned URL is a link that carries its own permission: which chunk, which method, until when, and a signature. The gateway signs it with a secret key that the data nodes also hold. A data node checks it on the spot.
Present the design as four short stories, each in its own colour on one whiteboard: Phase 1 upload (black), Phase 2 download (blue), Phase 3 delete (green), Phase 4 repair (purple). Every story has six numbered steps. The number on an arrow is the same number in the animation and in the step table.
Swipe to see all columns
| The four phases | |||
|---|---|---|---|
| Phase | Colour | Story | What it leaves behind |
| Phase 1: Upload (FR1, FR4) | Black | Alice uploads a 150 MB video: three chunks, none stored yet | Object O-1. Three chunks with count 1, each in three racks |
| Phase 2: Download (FR2) | Blue | Alice reads the video back | A cache entry for 60 seconds |
How to read the picture
videos/demo.mp4: 150 MB, chunks a1f3, 7c2e and e9b0, on nodes A4, B2 and C7.Each phase below is an interactive walkthrough of the same picture. It plays on its own. You can pause it, step with the arrow keys, and click any component to read its role.
Goal: get the bytes onto three racks without passing the gateway, and make the object appear in one step.
/uploadsClient → API gateway → Metadata serviceNothing is written in this step.
Swipe to see all columns
| Phase 1 step by step (black arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | The client cuts the file into 64 MB chunks and hashes each one. It sends POST /uploads with bucket, key, size and the three hashes. | Nothing yet |
| 2 | The metadata service looks the hashes up in chunks. None is stored. It picks three nodes in three racks: A4, B2, C7. | Upload U-1, OPEN |
Why it is built this way
A 6 GB object as one file has to go to one node in one request, and a failure at 90% starts it again. Chunks can go to different nodes in parallel, and a failed chunk is sent again alone. Two files that share most of their content also share most of their chunks. Most objects are small, and for them nothing changes: an object below 64 MB is one chunk.
Bob uploads the same video to bob-backup/demo.mp4.
Swipe to see all columns
| Bob's upload | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | POST /uploads with the same three hashes. | Upload U-2 |
| 2 | The metadata service finds all three in chunks. Each one comes back as stored. There is no URL and no byte to send. | Nothing |
150 MB arrived as three hashes. Two objects now share three chunks, and the count of each chunk says so.
Only as long as nobody can get a hash without having the file. Someone who learns the hash of a private file could claim it and then download it. There are two usual answers. Share chunks only inside one account, so a hash from outside is worth nothing. Or always make the client send the bytes, and let the data node drop them when the chunk is already there: that saves disk but not upload time. Name the risk and pick one.
Goal: find the chunks quickly, and read the bytes straight from the data nodes.
/alice-backup/videos/demo.mp4Client → API gateway → Metadata serviceNothing is written in this step.
Swipe to see all columns
| Phase 2 step by step (blue arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | The client sends GET /alice-backup/videos/demo.mp4. | Nothing |
| 2 | The metadata service asks the manifest cache. It is a miss. | Nothing |
| 3 | The metadata service reads the object, its three chunks in order and their replicas from PostgreSQL. | |
Why it is built this way
Listing a bucket (FR5) is one range scan on the index over bucket and key. The prefix is the start of the range, and the marker is where the previous page stopped. There are no folders: videos/ is just the beginning of some keys.
Goal: make the object disappear at once, and free its bytes only when that is safe.
/alice-backup/videos/demo.mp4Client → API gateway → Metadata serviceNothing is written in this step.
Swipe to see all columns
| Phase 3 step by step (green arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | The client sends DELETE /alice-backup/videos/demo.mp4. | Nothing |
| 2 | One transaction removes the object and lowers the counts. The cache entry is removed. The object is gone for readers. The bytes are still on disk. | Object O-1 and its chunk list deleted. Counts 1 to 0, zero_since set |
Why it is built this way
A scan has to compare 7 billion chunks with 10 billion manifest rows, again and again. A counter is one small update inside a transaction that is running anyway. The scan is still useful as a slow audit: it checks that the counters are right. A counter that is too high wastes disk. A counter that is too low loses data.
Goal: notice a dead node quickly, and get every chunk back to three copies.
heartbeatA4, B2, C7 → Metadata serviceNothing is written in this step.
Swipe to see all columns
| Phase 4 step by step (purple arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | Every data node sends a heartbeat to the metadata service every 10 seconds. | Node: last heartbeat |
| 2 | C7 is silent for 30 seconds. No reads and no writes are sent to it any more. | Node C7: DEAD |
| 3 | The repair worker finds the chunks that list C7. They have two copies now. It queues them, fewest copies first. | |
Why it is built this way
The deep dives are the four questions from the top of the page. Each one compares three designs for the same scenario: a Bad one, which is usually the answer that sounds right; a Good one, which still leaves a gap; and a Great one, which holds. The answers start covered. Read the scenario, decide what you would build, then click each card.
Swipe to see all columns
| Deep Dive Map | |||
|---|---|---|---|
| Deep Dive | Topic | Scenario | NFR |
Scenario: Uploads arrive at 1.2 GB every second, all day. One client uploads a 6.4 GB file, which is 100 chunks. Which machines do those bytes pass through?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. The client sends the whole file to the gateway. The gateway saves it and then writes it to the data nodes.
Where it breaks. The gateway has to hold 6.4 GB for one upload. If it restarts at 90%, the upload starts again from byte 0. And all 1.2 GB per second pass through the gateway.
The idea. The gateway holds nothing. Each piece that arrives goes on to a data node at once.
What it fixes. Memory. A 6.4 GB upload no longer needs 6.4 GB on the gateway.
What it still misses. Every byte still crosses the gateway, in and out. Its network is the limit of the whole system. Adding data nodes adds storage, but no speed.
The key idea. The gateway hands out permission, not bandwidth. A presigned URL lets a client talk to a data node directly, for one chunk and a short time.
How it works
The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
/{bucket}/{key}Client → API gatewayNothing is written in this step.
Swipe to see all columns
| DD1 side by side | |||
|---|---|---|---|
| Bad: hold and store | Good: stream through | Great: presigned URLs | |
| Object bytes through the gateway | All, and held there | All, passing through | None |
| Gateway traffic at 1.2 GB/s of uploads | 1.2 GB/s in and out | 1.2 GB/s in and out | About 1 MB/s of hashes and URLs |
Check it against the scenario
| One upload of 6.4 GB | |
|---|---|
| Situation | Outcome with the Great option |
| Chunk 57 fails on the way | Only chunk 57 is sent again |
| A URL expires before its chunk is sent | The data node refuses it. The client asks for a new URL |
| The client sends other bytes than the hash says | The data node's own hash does not match. The chunk is refused |
| Someone finds a leaked URL | It allows one chunk and one method, for at most 15 minutes |
| The gateway restarts in the middle | The chunks keep flowing. Only the commit has to wait |
The trade-off to name. The client gets smarter: it cuts, hashes, retries and talks to many nodes. And data nodes are reachable by clients, so each of them must check signatures. In exchange, upload speed grows with the number of data nodes.
Because the hash is the chunk's name, and chunks are shared. If a client could store wrong bytes under the name a1f3, every later upload of the real a1f3 would be told "stored", and every object that uses the chunk would return the wrong bytes. The data node is the only place that sees the real bytes, so it is the one that checks.
"The gateway only coordinates. The client cuts the file into 64 MB chunks, hashes them and sends the hashes. For every chunk that is not stored yet it gets a presigned URL: one chunk, one method, 15 minutes, signed with a key the data nodes share. The bytes go straight from the client to a data node, which checks the signature, re-hashes the bytes and replicates the chunk to two more racks. A failed chunk is retried alone. So coordination and data throughput scale separately: the gateway sees about a megabyte a second while the data nodes take 1.2 gigabytes a second."
Follow-up: "What stops a client from uploading garbage under someone else's hash?" — The data node hashes what it receives and refuses a mismatch. The name of a chunk is always the hash of its bytes.
Scenario: Alice's upload can die at any moment: after two of three chunks, or in the middle of saving the metadata. What does a reader of videos/demo.mp4 see? And is anything lost later?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. Save the object row at the start with status UPLOADING. Send the chunks. Set the status to DONE at the end.
Where it breaks. The row is visible before the bytes are. A reader or a list finds an object with two of three chunks. Every query has to remember to filter on the status. And when the client dies, the half object stays until some extra job cleans it up.
The idea. Store all chunks first. Then write the metadata: the object row, then the chunk list, then the counts.
What it fixes. A dead upload leaves no row. Readers see nothing.
What it still misses. The three writes are not one step. A crash after the first or the second leaves an object that readers can see, with chunks that still have count 0. A day later GC deletes those chunks, and the object points at nothing. The damage shows up long after the bug.
The key idea. One transaction is the only moment at which an object starts to exist. Before it there are only chunks that nobody uses. After it there is a complete object.
How it works
ACTIVE chunk. If one is missing, the whole commit fails and names the chunk to send again.The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
INSERT INTO objectsClient → Metadata service → Metadata DBO-1new rowSwipe to see all columns
| DD2 side by side | |||
|---|---|---|---|
| Bad: row first | Good: three writes | Great: one transaction | |
| When the object becomes visible | Before its bytes exist | After the first of three writes | At the commit, complete |
| The upload dies after 2 of 3 chunks | Readers find half an object | Nothing is visible | Nothing is visible |
Check it against the scenario
| Alice's upload dies | |
|---|---|
| Situation | Outcome with the Great option |
| The client dies after 2 of 3 chunks | There is no object row. A read returns 404. Two chunks wait at count 0 |
| The client tries again an hour later | a1f3 and 7c2e come back as stored. Only e9b0 is sent |
| The server crashes inside the commit | PostgreSQL rolls back. Nothing changed. The client repeats the commit |
| The commit worked and the response was lost | The client repeats it and gets the same 201 |
| Nobody ever tries again | The two chunks stay at count 0 and are deleted after 24 hours |
The trade-off to name. Everything a commit changes must be within reach of one transaction. That is easy in one PostgreSQL. It is the hard part once the tables are spread over many machines (see the follow-ups below).
Because of this transaction. A commit changes an object row, a chunk list and several counters together, and a delete does the reverse. A store without transactions over several rows would force you to rebuild them by hand. The metadata is only about 10 TB, so being exactly right is worth more here than raw speed.
"I write the bytes first and the metadata last. Chunks go to the data nodes and get a row with reference count 0. Then one transaction inserts the object row and the chunk list and increments the counts. That transaction is the commit point: before it nothing is visible, after it the whole object is. A crash at any earlier moment leaves only unreferenced chunks, and the normal garbage collector removes them. The commit is idempotent per upload id, so a lost response is just a retry."
Follow-up: "What if the same key is uploaded twice at the same time?" — Both uploads store their chunks. The commits run one after the other on the unique index over bucket and key. The second one replaces the first object and lowers the counts of its chunks in the same transaction. The last commit wins.
Scenario: Alice and Bob store the same video, so each of its chunks has count 2. At 10:00 Carol starts uploading the same video. The dedup check says all three chunks are stored, so she sends no bytes. At 10:05 Alice and Bob both delete their copy, and the counts drop to 0. At 10:20 Carol commits. What must still be on disk at 10:20?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. A count of 0 means no object uses the chunk. Delete it right away and get the space back.
Where it breaks. At 10:05 the bytes are deleted. At 10:20 Carol's commit creates object O-3 with three chunks that no longer exist. She never sent the bytes, because she was told they were stored. Every download of O-3 fails. A count of 0 says nothing about uploads in flight.
The idea. Keep a chunk for 24 hours after its count reached 0. An upload may last 6 hours at most, so every upload that was told "stored" commits long before that.
What it fixes. Carol. She commits at 10:20 and the count goes from 0 to 1. When GC looks the next day, it finds 1 and leaves the chunk alone.
What it still misses. The delete itself. GC reads "count 0" and then deletes. Between those two steps a new upload can be told "stored" for the same chunk. And if GC removes the row before the bytes, a new upload can store the chunk again while the nodes are still deleting it.
The key idea. Before any byte is deleted, the chunk is taken out of use in the database. From that moment on, no upload can be told "stored" for it.
How it works
zero_since is set when the count drops to 0, when a chunk is first written, and when a dedup check finds a chunk at count 0. GC may act 24 hours after that.PENDING_DELETION.PENDING_DELETION chunk answers "not stored". A commit that uses it fails.The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
/uploadsCarol → Metadata service200: upload U-3a1f3, 7c2e, e9b0read onlySwipe to see all columns
| DD3 side by side | |||
|---|---|---|---|
| Bad: delete at zero | Good: wait 24 hours | Great: wait, then two phases | |
| The bytes are deleted | At 10:05, when the count is 0 | The next day at 10:05 | The next day, after the chunk is marked |
| Carol's object after her commit | Points at missing bytes | Complete | Complete |
Check it against the scenario
| Two deletes and one upload | |
|---|---|
| Situation | Outcome with the Great option |
| 10:00 Carol starts. The chunks have count 2 | She is told: stored. She sends no bytes |
| 10:05 Alice and Bob delete | Count 0. The 24-hour clock starts. The bytes stay |
| 10:20 Carol commits | Count 1. GC will leave these chunks alone |
| Carol never commits | Her upload expires at 16:00. The next day GC marks the chunks, the nodes delete them, and the rows go |
| Dave uploads the same video one second before GC acts | His dedup check restarts the clock. GC checks again, sees that, and skips the chunk |
The trade-off to name. Deleted space comes back a day late, and the chunks of a failed upload sit on disk for a day. That is cheap. Deleting bytes that an object needs cannot be undone.
The wait must be longer than the longest upload, because an upload is told "stored" at its start and commits at its end. Uploads are capped at 6 hours here. The rest of the 24 hours is room for retries, clocks that disagree and a GC run that starts late. Size the wait by the worst upload, not by the average one.
"A delete only removes the object row and decrements the reference counts, in one transaction. Bytes are freed by a deferred garbage collector. A chunk must sit at count zero for a grace period that is longer than the longest possible upload, because an upload learns that a chunk exists at its start and references it only at its commit. I cap uploads at 6 hours and wait 24. The deletion itself is two-phase: GC re-checks the count and marks the chunk pending deletion in one transaction, then the data nodes delete the replicas, and only then is the chunk row removed. While a chunk is pending, the dedup check treats it as not stored."
Follow-up: "Why not remove the row first and clean the disks later?" — Then a new upload sees no row, stores the chunk again on the same nodes, and the late delete command removes the fresh bytes. The row stays as a marker until the disks are clean.
Scenario: It is 02:00. Rack C loses power. Twelve data nodes go dark at the same moment, with about 840 TB of chunks. How many objects become unreadable? And how do you get back to three copies of everything?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. Keep three copies of every chunk. For each chunk, pick the three emptiest nodes.
Where it breaks. Nodes in one rack share a power feed and a network switch, so they fail together. With random placement about 1 chunk in 800 has all three copies in one rack. Rack C holds about 350,000 of them. At 02:00 they are all unreadable, and if the disks are damaged they are lost. Three copies that fail together are one copy.
The idea. Put the three copies in three different racks. When nodes die, copy their chunks somewhere else immediately.
What it fixes. Rack C takes at most one copy of a chunk. Every chunk still has two, and all reads keep working.
What it still misses. The repair. Copying 840 TB at full speed fills the network, and user reads and writes slow down. And the queue is in arrival order: if a disk in rack A dies during the repair, a chunk with one copy left waits behind millions that still have two.
The key idea. After a failure, the chunks closest to being lost go first, and the repair never takes more than its share of the network.
How it works
DEAD: no reads, no new writes.The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
pick 3 nodes for chunk a1f3Metadata servicea1f3new rowSwipe to see all columns
| DD4 side by side | |||
|---|---|---|---|
| Bad: any three nodes | Good: three racks, repair at once | Great: three racks, ordered repair | |
| Chunks unreadable when rack C goes dark | About 350,000 | None | None |
| User traffic during the repair | Slows down | Slows down | Keeps its bandwidth |
Check it against the scenario
| Rack C loses power at 02:00 | |
|---|---|
| Situation | Outcome with the Great option |
| 02:00 rack C goes dark | After 30 seconds its 12 nodes are DEAD. Reads use racks A and B. Nothing is lost |
| A reader wants chunk a1f3 | A4 or B2 answers |
| Chunk a1f3 is repaired | B2 copies it to D5 in rack D. D5 hashes it. Replicas: A4, B2, D5 |
| A disk in A4 dies at 03:00 | Its chunks that also lived in rack C have one copy left. They move to the front of the queue |
| Rack C is back at 02:20 | Copies that were not replaced yet count again. Copies that were replaced are deleted |
The trade-off to name. Every write waits for three racks, and three copies cost three times the disk. Erasure coding stores cold data in about half of that, but rebuilding a lost piece then means reading several others. Start with three copies, and offer erasure coding for cold data as the next step.
Partly. That is why many systems wait a few minutes before the big copy starts. 30 seconds of silence is enough to stop reading from a node, but most outages end with a restart. A common rule: chunks with one copy left are copied at once, chunks with two copies wait about 10 minutes. What was copied in the meantime is not harmful. The metadata simply deletes the extra copies.
"Every chunk has three replicas in three different racks, because nodes in a rack share power and a switch and fail together. Nodes send heartbeats, and after 30 seconds of silence a node is marked dead and gets no traffic. Its chunks are now under-replicated and go into a repair queue ordered by copies left, so a chunk with one copy is always repaired before a chunk with two. Each repair copies from a surviving replica to a node in a rack that does not hold the chunk, and the new node verifies the hash. Repair runs under a bandwidth budget so that user traffic keeps flowing. A node that returns is treated as out of date until the metadata service has told it what to keep."
Follow-up: "Why not erasure coding from the start?" — Three copies are simple, and a repair is one copy from one node. Erasure coding cuts the overhead from 3 times to about 1.5 times, but a repair has to read several pieces and compute. Use it later for cold data.
| If the interviewer keeps going | |
|---|---|
| Question | Short answer |
| 10 billion objects in one PostgreSQL? | No. Split objects and object_chunks by bucket_id: every request names a bucket, so it stays on one shard. chunks is shared by all buckets, so it gets its own cluster, split by hash. A commit then touches two shards and needs a two-phase commit, or a database with distributed transactions |
| An edit at the start of a file shifts every chunk. Is dedup gone? | With fixed 64 MB chunks, yes. Content-defined chunking cuts where a rolling hash of the bytes hits a pattern, so an insert changes only the chunks near it. The price is chunks of different sizes |
| Three copies of data that nobody reads? | |
Streaming fixes the memory, not the bandwidth. Hand out presigned URLs and let clients talk to the data nodes.
Readers find half objects. Store the chunks first, then make the object exist in one transaction.
An object whose chunks still count 0 loses its bytes at the next GC run. The object row, the chunk list and the counts change together.
The hash is the name of bytes that others share. The data node hashes what it received and refuses a mismatch.
An upload in flight may have been told that the chunk is stored. Wait longer than the longest upload.
A new upload can store the chunk again while the nodes are still deleting it. Mark the chunk, delete the bytes, then remove the row.
Nodes in one rack fail together. Three copies belong in three racks.
Copying at full speed starves the users, and arrival order ignores which chunk is closest to loss. Fewest copies first, under a budget.
The shape of a strong answer: split the system into a small metadata store that must be exact and a large chunk store that must be cheap. Name chunks by their hash. Move bytes straight between clients and data nodes. Let one transaction make an object exist, and one transaction make it disappear. Free bytes a day later, in two phases. Keep three copies in three racks, and repair in order.
Keep the bytes away from the control path
The gateway hands out signed URLs. The metadata service decides placement. Neither one carries a single object byte, so throughput grows with the number of data nodes.
Let one transaction decide what exists
Chunks are written before the commit and belong to nobody until it happens. The commit and the delete each change the object row, the chunk list and the counts together.
Be slow to destroy
A chunk waits 24 hours at count 0, is marked, and only then deleted. A dead node is replaced copy by copy, starting with the chunks that have the fewest copies left.
| What the interviewer expects at each level | |
|---|---|
| Level | A strong answer shows |
| Junior | Bucket and key, a metadata layer that maps objects to chunks, and data nodes that hold the bytes. PostgreSQL, because the counts need transactions |
| Mid-level | Splits metadata from data without being asked. Explains chunking, dedup by hash and presigned URLs. Designs the commit, the 24-hour wait and the three racks, each with the failure it prevents |
| Senior | Goes to the hard parts first: the full delete protocol, races on the counters, the order of repair. Names the limits, such as sharding the metadata, and ties every choice to cost |
These aren't toy problems — they reflect real infrastructure challenges at companies that store data at petabyte scale. If you want to walk through this design in a mock interview or get feedback on your own approach, book a session with us. We're here to help you sharpen your thinking, build your confidence, and land your next offer.
Pick an answer for each question, and you see at once whether it is right. Show explanation opens the answer, why it is right, and why each of the other options fails.
Question 1 of 10
Uploads arrive at 1.2 GB every second. What passes through the API gateway?
Question 2 of 10
The client already sent the hash of a chunk. Why does the data node hash the bytes again?
Question 3 of 10
An upload stops after two of its three chunks reached the data nodes. What does a reader of that key see?
Question 4 of 10
What makes an object exist?
Question 5 of 10
Bob uploads a 150 MB video that Alice has already stored. How many bytes of video does Bob send?
Question 6 of 10
The count of a chunk drops to 0. Why does GC wait 24 hours before it deletes the bytes?
Question 7 of 10
The 24 hours are over and the count is still 0. In which order does GC work?
Question 8 of 10
Every chunk has three copies on three randomly chosen nodes. Rack C loses power. What happens?
Question 9 of 10
A data node is silent for 30 seconds. What does the system do?
Question 10 of 10
10 PB of objects, 30% of it duplicates, three copies of every chunk. How much disk does dedup save?
| Readers can find half an object. Nothing says at which moment an object starts to exist |
| DD3 | Two users delete the same video while a third is uploading it. When are the bytes freed? | When the reference count reaches zero | The third upload was told the bytes are stored. Its object now points at nothing |
| DD4 | A whole rack loses power. What is lost? | Nothing, we keep three copies | Three copies in one rack fail together. And copying everything back at once starves the users |
| How big is an object? | From a few KB to many GB, on one upload path | Large objects are cut into 64 MB chunks |
| May object bytes pass through the metadata service? | No. Metadata only coordinates | Clients move bytes straight to the data nodes (DD1) |
| When is deleted space freed? | Later, in the background. Never at the cost of an upload in flight | Deferred garbage collection (DD3) |
| The reference count is 0 | Nobody needs the bytes | An upload in flight may be about to use them |
| There are three copies | The data survives a failure | Three copies in one rack fail together |
| The client sent a hash | The bytes match it | Only the node that receives the bytes can check |
| A node is silent | Its data is gone | It may be back in a minute. Stop reading from it now, replace its copies in order |
| Bytes coming in |
| 100 TB a day, 1.2 GB every second |
| Reads at peak | 50,000 a second |
| Chunk size | 64 MB. An object below 64 MB is one chunk |
| Metadata | About 1 KB per object: 10 TB in total |
| The client cuts and hashes. The data node hashes again | Bytes never pass the gateway, and wrong bytes are caught where they arrive |
| A presigned URL for every chunk | The data node checks the permission itself, without calling anyone |
| Bytes first, then one commit transaction | A reader sees the whole object or nothing |
| A reference count per chunk, changed only in that transaction | The count always equals the number of objects that use the chunk |
| Deferred GC: 24 hours at count 0, then delete in two phases | An upload in flight never loses bytes it was told are stored |
| Three copies in three racks | A rack failure leaves two copies of everything |
| Repair in order, under a budget | The chunks closest to loss are copied first, and users keep their bandwidth |
size | INT | Bytes |
created_at | TIMESTAMP | Set at the commit |
| Which chunk sits at this position |
reference_countINT |
| How many manifest rows point at this chunk. Changed only inside a commit or a delete |
state | ENUM | ACTIVE / PENDING_DELETION (DD3) |
zero_since | TIMESTAMP | When the 24-hour wait before deletion last started (DD3) |
replicas | LIST | The data nodes that hold a copy, for example A4, B2, C7 |
EXPIRED| An upload in progress. It expires after 6 hours, which puts a limit on how long any upload can be in flight (DD3) |
| Node | node_id, rack, last heartbeat, LIVE / DEAD | Where a chunk can be placed and read (DD4) |
| 204. The object is gone at once |
GET /{bucket}?prefix=&marker=&maxKeys= | A prefix, the last key of the previous page, a page size up to 1000 | Keys in order, and the marker for the next page |
3. POST /uploads/U-1/commit| No body |
| 201: the object exists. Repeating the call returns the same answer |
| Phase 3: Delete (FR3) |
| Green |
| Alice deletes the video. A day later the bytes are freed |
| Nothing |
| Phase 4: Repair (NFR1) | Purple | Data node C7 dies while the video is stored | Three copies in three racks again |
| 3 | The client gets U-1 and one presigned PUT URL per chunk, valid 15 minutes. | Nothing |
| 4 | The client sends each chunk straight to A4. A4 checks the signature and hashes the bytes. Then A4 passes the chunk to B2, and B2 to C7. 200 when all three have it. | Chunk files on A4, B2, C7 |
| 5 | A4 reports the stored chunks to the metadata service. | chunks rows a1f3, 7c2e, e9b0: count 0, ACTIVE, replicas A4, B2, C7 |
| 6 | The client sends POST /uploads/U-1/commit. One transaction does the rest. Only now does the object exist. | Object O-1, three object_chunks rows, counts 0 to 1, upload COMMITTED |
| 3 |
POST /uploads/U-2/commit. |
Object O-2, three object_chunks rows, counts 1 to 2 |
| Manifest cache: the chunk list, for 60 s |
| 4 | The client gets the size, the three chunks in order and presigned GET URLs for A4, B2 and C7. | Nothing |
| 5 | The client fetches the three chunks in parallel from different nodes, checks each hash and joins them by index. | Nothing |
| 6 | C7 does not answer. The client asks A4 for the same chunk. | Nothing |
| 3 |
| 24 hours later the GC worker selects the chunks that have been at count 0 since then. |
| Nothing |
| 4 | In one transaction per chunk, GC checks that the count is still 0 and marks the chunk. | chunks: state PENDING_DELETION |
| 5 | GC tells A4, B2 and C7 to delete the chunk files. Each node confirms. | Chunk files removed |
| 6 | GC removes the chunk rows. | chunks rows deleted |
| Nothing |
| 4 | The repair worker tells C3, a spare node in rack C, to copy chunk a1f3 from B2. | Chunk file on C3 |
| 5 | C3 hashes what it received and reports. | chunks a1f3: replicas A4, B2, C3 |
| 6 | C7 comes back later. It is told to delete the chunks it no longer owns. | Chunk files on C7 removed |
| DD1 |
| Keep the bytes off the gateway |
| 1.2 GB every second, and one 6.4 GB upload |
| NFR3, NFR5 — Scale, speed |
| DD2 | An upload is all or nothing | The upload dies after two of three chunks | NFR1 — Durability |
| DD3 | Delete a chunk that others share | Two deletes and one upload of the same video | NFR1, NFR4 — Durability, cost |
| DD4 | Lose a rack, lose nothing | Rack C loses power: 12 nodes at once | NFR1, NFR2 — Durability, availability |
| What limits the upload speed |
| The gateway |
| The gateway |
| The data nodes, and there are 300 |
| A failure at 90% of the upload | Start again from byte 0 | Start again from byte 0 | Send the missing chunks again |
| A crash while saving the metadata |
| A row stuck in UPLOADING |
| A visible object whose chunks count 0 |
| The transaction rolls back |
| Risk of losing data later | No, but readers see broken objects | Yes: GC frees chunks that are in use | None |
| A new upload while GC is deleting |
| Its bytes are already gone |
| Can be told "stored" for bytes that are being deleted |
| Is told "not stored" and sends the bytes |
| The space comes back | At once | A day later | A day later |
| Dave arrives while the chunk is PENDING_DELETION | He is told: not stored. He sends the bytes, and they become a new chunk once the old row is gone |
| A second failure during the repair |
| Some chunks are already gone |
| A chunk with one copy waits in line |
| A chunk with one copy goes first |
| Time to three copies again | Never, for the lost chunks | Fast, at the users' expense | About a day, at 10 GB/s |
| Tiering. A background job moves chunks that were not read for months to cheaper disks with erasure coding |
| A disk returns wrong bytes without an error? | Scrubbing. Each node re-reads its chunks in the background and hashes them. A chunk whose hash is not its name is replaced from another copy |
| Billions of 1 MB files on the disks? | Pack small chunks into large volume files with an index of offsets, so a node holds thousands of files and not hundreds of millions |
| A bucket with a billion keys? | Listing stays a range scan, 1000 keys a page. If one bucket outgrows a shard, split it by key range |