To design object storage like Amazon S3 is to build a service that keeps files (objects) under names in buckets, never loses them even though disks and racks fail every week, and still answers uploads, downloads and listings quickly. A good answer separates the metadata (which names exist and where their bytes are) from the data (the bytes themselves), explains how pieces of every object are spread over failure domains, and shows what happens when an upload is cut off, two people write the same name, or one key prefix gets all the traffic.

This lesson designs CampusLocker, a document locker where students keep marksheets, certificates, ID scans and recorded lectures. Six runnable programs in Java, with Python and C++ versions, test the decisions interviewers probe most: copies against erasure coding, multipart uploads over a bad link, spreading metadata over shards, consistency of overwrites and listings, cleaning up abandoned uploads, and deduplication. All failure rates, sizes and timings are assumptions chosen to make the comparisons clear, not measurements of any product.

Key takeaways

  • Erasure coding 6+3 stores 1.50x the data, against 3.00x for three copies. When each piece is lost with chance 1%, it lost 1,210 objects per billion against 1,000 for three copies. At 10%, it lost 8,331,094 against 1,000,000, so many simultaneous failures hurt it more.

  • Placement mattered more than the code. With all pieces in one rack, about 500 of 50,000 objects were unreadable when a rack went offline. With pieces in different racks, 1 to 9 were.

  • Uploading a 1000 MB file in one piece over a link that drops once per 1000 MB sent moved 1697 MB and took 344.7 s on average (1488.0 s for the slowest). 25 MB parts moved 1012 MB and took 213.5 s. 1 MB parts took 403.1 s because of 1000 requests.

  • When all 200,000 results-day uploads had a key that began with the date, one metadata shard took all of them (2000 a second against a limit of 500). Hashing the whole key gave each shard 6.3% but a listing then had to read all 16 shards.

  • Replacing a file by overwriting bytes in place mixed old and new chunks in 12.3% of running downloads. Writing a new version and then switching the name mixed none.

  • Abandoned uploads left 1,954 GB of hidden parts after a month. A rule that aborts unfinished uploads after 7 days left 439 GB and cut no genuine upload. Deduplication with content-defined chunks saved 49.8% of the space against 22.9% for whole-file matching.

1. Clarifying Requirements

Functional requirements

  • Create buckets and store, read, replace and delete objects by key.

  • Upload large files in parts and resume after a failure.

  • List the objects under a prefix.

  • Keep old versions of an object when asked, and expire data by rule.

Non-functional requirements

  • Very high durability: an acknowledged object must survive disk, server and rack failures.

  • High availability for reads and writes.

  • Scale to billions of objects and to bursts such as a results day.

  • Low cost per stored GB, since most data is written once and read rarely.

Say what you leave out: encryption key management, billing, public static website hosting and event notifications.

2. Back-of-the-Envelope Estimation

  • Objects: 2 million students with 50 files each is 100 million objects of about 2 MB, which is 200 TB.

  • Raw space: three copies need 600 TB. A 6+3 erasure code needs 300 TB. At this size, the code saves 300 TB of disks.

  • Metadata: about 500 bytes per object is 50 GB, around 3 GB on each of 16 shards. It is small, but it takes every request.

  • Peak: on results day we assume 2,000 uploads a second. At 2 MB each, that is 4 GB a second, or 32 Gbit a second.

3. Core APIs

  • PUT /v1/buckets/{bucket}/objects/{key} uploads a small object. The reply carries a version id and an ETag (a content hash).

  • POST /v1/buckets/{bucket}/uploads?key={key} starts a multipart upload and returns an upload id.

  • PUT /v1/uploads/{uploadId}/parts/{n} sends part number n. Sending the same part again replaces it.

  • POST /v1/uploads/{uploadId}/complete with the list of part numbers and their hashes makes the object visible. DELETE /v1/uploads/{uploadId} aborts.

  • GET /v1/buckets/{bucket}/objects/{key} downloads, with an optional Range header.

  • GET /v1/buckets/{bucket}/objects?prefix=...&limit=1000&cursor=... lists keys in order, one page at a time.

  • DELETE /v1/buckets/{bucket}/objects/{key} deletes (or adds a delete marker when versioning is on).

4. High-Level Design

The central idea is to keep two kinds of data apart. The metadata layer says what exists: the key, the version, the size, the hash, and where the pieces are. The data layer holds only bytes, split into pieces that sit on different disks, servers and racks. A request first reaches an API layer that checks the signature and applies limits.

An upload writes the pieces first and only then records the object in the metadata layer. If the writer dies in between, the pieces are garbage that a cleanup job removes later; no reader ever sees a half-written object. A large upload does this part by part and ends with one step that lists the parts.

A read looks the key up in the metadata, gets the places of the pieces, and asks for them in parallel. With an erasure code, any k of the pieces are enough, so a slow or dead disk is simply skipped.

5. Durability: Copies or Erasure Coding

Disks fail, so every object needs redundancy. The simple way is to keep copies: three copies tolerate the loss of two. An erasure code splits an object into k data pieces and adds m parity pieces. Any k of the k+m pieces rebuild the object, so 6+3 tolerates the loss of any three of nine pieces while storing 1.5 times the data.

The first program asks two questions. Part 1 gives every piece the same chance p of being lost during one repair window and calculates the chance that the object is lost (more than m pieces gone). Part 2 simulates a rack outage: 1 in 100 objects has its rack offline, and each disk also fails 3 times in 100. It compares keeping all pieces of an object in one rack with putting each piece in a different rack.

Output

Part 1: every piece (copy or fragment) is lost on its own with chance p during one repair window.
Objects lost per billion objects, and the cost of that safety:

setup          pieces   p = 1%       p = 3%       p = 10%      storage    TB read to rebuild 1 TB disk
2 copies       2        100000       900000       10000000     2.00x      1
3 copies       3        1000         27000        1000000      3.00x      1
erasure 4+2    6        19554        504418       15850000     1.50x      4
erasure 6+3    9        1210         90410        8331094      1.50x      6
erasure 10+4   14       186          38762        9230212      1.40x      10

Part 2: 50,000 objects per row. 1 in 100 objects has its rack offline in the window; each disk also fails 3 in 100.
Objects that cannot be read:

setup          all pieces in one rack pieces in different racks
3 copies       503                    3
erasure 6+3    524                    9
erasure 10+4   489                    1
  • At p = 1%, three copies lost 1,000 objects per billion and 6+3 lost 1,210, for half the space (1.50x against 3.00x). 10+4 lost 186 at 1.40x. Two copies lost 100,000.

  • At p = 10%, the order reversed: three copies lost 1,000,000 per billion, 6+3 lost 8,331,094 and 10+4 lost 9,230,212. A wide code is only good while failures are rare and repaired quickly.

  • Rebuilding a 1 TB disk reads 1 TB with copies but 6 TB with 6+3 and 10 TB with 10+4, because each lost piece needs k others. Repair traffic is the price of the space saving.

  • With all pieces in one rack, 503 (three copies), 524 (6+3) and 489 (10+4) of 50,000 objects could not be read after the rack outage. With one piece per rack, it was 3, 9 and 1.

The calculation assumes pieces fail independently, which is exactly what a rack, a switch or a power feed breaks. That is why placement across racks (and zones) is part of the design, not a detail. A common real choice is copies for small and hot objects and erasure codes for large and cold ones.

6. Uploading Large Files: Multipart

If a 1000 MB upload is one request, one dropped connection restarts it. Multipart upload cuts the file into parts that are sent as separate requests. A failed part is sent again, and the other parts are not affected. Parts can also be sent in parallel. The cost is one request per part and the work of listing the parts at the end.

The second program uploads a 1000 MB file 300 times at 5 MB per second. After each MB sent, the connection drops with chance 1 in 1000; a drop costs 3 s to reconnect and the part in flight starts again. Each request costs 200 ms.

Output

1000 MB file, 300 uploads per row, 5 MB/s link, 1 drop in 1000 MB sent, 3 s to reconnect, 200 ms per request.
Perfect upload: 200 s plus one request.

part size    avg MB sent  extra data   avg time       slowest        requests
whole file   1697         69%          344.7 s        1488.0 s       2
100 MB       1046         4%           214.5 s        258.4 s        10
25 MB        1012         1%           213.5 s        231.6 s        40
5 MB         1002         0%           243.5 s        256.0 s        200
1 MB         1000         0%           403.1 s        413.6 s        1000
  • As one piece the upload sent 1697 MB on average (69% extra), took 344.7 s, and the slowest took 1488.0 s. The perfect time is 200 s plus one request.

  • 100 MB parts sent 1046 MB and took 214.5 s. 25 MB parts sent 1012 MB and took 213.5 s with 40 requests.

  • 5 MB parts took 243.5 s and 1 MB parts 403.1 s, because 200 and 1000 requests add up even though almost nothing is resent.

Choose the part size from the failure rate and the request cost, and set a lower limit (real services require a minimum part size, and a maximum number of parts). The program sends parts one after another. Sending several in parallel shortens the time further and does not change the lesson about retries.

7. Metadata and Key Names

The metadata layer is a sorted store of keys, split into shards by key range so that a listing under a prefix reads neighbouring keys. That is also its weakness: the key decides which shard takes the write.

The third program has 16 shards, each able to take 500 writes a second. 200,000 uploads arrive in 100 s on results day (2,000 a second), and 1 in 5 comes from a bulk uploader. It compares four ways to start the key, and counts how many shards a listing must read.

Output

200000 uploads in 100 s on results day, 16 metadata shards, 1 upload in 5 from one bulk uploader. One shard can take 500 writes a second.

key starts with              busiest shard  busiest shard    shards     shards to list shards to list
                             share          peak             used       a normal user  the bulk uploader
date first                   100.0%         2000 a second    1          1              1
user first                   24.9%          568 a second     16         1              1
hash of whole key            6.3%           169 a second     16         16             16
user first, hot user split   6.3%           172 a second     16         1              16
  • Date first: all 200,000 writes went to one shard, 2,000 a second against its limit of 500. (The 1 shard to list is only because all the data was in one place.)

  • User first: the busiest shard took 24.9% (568 a second) because of the bulk uploader. Listing one user's files read 1 shard.

  • Hash of the whole key: 6.3% on the busiest shard (169 a second), but any listing had to read all 16 shards.

  • User first with the hot user split into 16 sub-prefixes: 6.3% (172 a second), normal users listed from 1 shard, and only the bulk uploader needed all 16.

The general rule: spread writes without destroying the listings you need. Split a hot prefix automatically when a shard is over its limit, and split the key range, not the application. Cloud services do the same, so a key prefix that is fine today can become a limit tomorrow.

8. Overwrites, Listings and Consistency

Three situations expose the consistency model. A student replaces a certificate and reads it back. A student uploads a file and lists the folder. A download is running while someone replaces the same file. The fourth program tests each.

Output

Part 1: replace a file, then read it 0 to 999 ms after the write was acknowledged. 100000 trials per row.

setup                              stale reads    write waits    reads that use primary
read any 1 copy                    20.6%          0 ms           33.0%
read the primary only              0.0%           0 ms           100.0%
write waits for 2, read asks 2     0.0%           211 ms         66.3%

Part 2: upload a file, then list the folder 0 to 1499 ms later. 100000 trials per row.

setup                              new file missing from the list
index updated later, in its own store 62.3%
index updated in the same step     0.0%

Part 3: a 200-chunk download is running while the file is replaced. 20000 trials per row.

setup                              downloads with old and new chunks mixed
overwrite the bytes in place       12.3%
write a new version, then switch   0.0%
  • Read after replace. Reading any one of 3 copies returned the old file 20.6% of the time within a second of the write. Reading only the primary returned 0.0% stale but sent every read to the primary. Making the write wait for one follower as well (2 of 3) and reading 2 copies gave 0.0% stale, at a write wait of 211 ms and with 66.3% of reads touching the primary.

  • List after upload. With the list index in a separate store, updated up to 2 s later, 62.3% of listings within 1.5 s missed the new file. Updating the index in the same step gave 0.0%.

  • Replace during a download. Overwriting bytes in place mixed old and new chunks in 12.3% of running downloads. Writing the new version elsewhere and then switching the name gave 0.0%.

The usual design for an object store is immutable data with a single, strongly consistent metadata step. Bytes are never changed after they are written, a new version is a new set of pieces, and the name points to a version. When write and read quorums overlap (W + R larger than N) a read after an acknowledged write sees it. Keep the list index in the same transaction as the key, or the listing will be stale.

9. Cleaning Up: Abandoned Uploads and Old Versions

Parts of an upload that never completes are not shown as a file, but they take space and cost money. Old versions and deleted objects do the same. A background lifecycle job removes them by rule.

The fifth program starts 50,000 multipart uploads in a 30-day month. 8 in 100 are abandoned, leaving 1 to 1000 MB. Most others finish within an hour, 9 in 100 take 2 to 12 hours and 1 in 100 takes 24 to 60 hours (a very slow link). A rule aborts any upload still unfinished after a limit.

Output

50000 uploads in 30 days, 8 in 100 abandoned. Numbers are at the end of day 30.

cleanup rule           hidden parts left  storage wasted over  finished uploads that
                       (GB)               the month (GB-days)  were cut, per 10,000
no rule                1954               29465                0
abort after 7 days     439                12136                0
abort after 1 day      66                 1925                 98
abort after 12 hours   21                 971                  101
abort after 1 hour     0                  81                   989
  • With no rule, 1,954 GB of hidden parts remained at the end of the month and 29,465 GB-days were wasted.

  • Abort after 7 days: 439 GB left, 12,136 GB-days wasted, and no finished upload was cut.

  • Abort after 1 day: 66 GB left, but 98 of every 10,000 genuine uploads were cut (the very slow ones).

  • Abort after 1 hour: 0 GB left, but 989 of every 10,000 genuine uploads were cut, which is not acceptable.

Pick the limit from the slowest legitimate upload, not the average. Better still, let an active upload keep itself alive: count the time since the last part arrived, not since the start.

10. Deduplication

Students upload the same marksheet more than once, and save a file again after fixing a typo. Deduplication stores identical pieces once, found by a hash of the content. The question is where to cut the file into pieces. Fixed-size chunks break whenever bytes are inserted, because every later chunk shifts. Content-defined chunks cut where the content itself says so (for example after a byte value with a certain property), so a cut point survives an insert and only the neighbouring chunks change.

The sixth program uploads 400 files of about 1000 KB: half are new, a quarter exact copies, a quarter an earlier file with 1 KB inserted at a random place. It compares whole-file matching, fixed chunks and content-defined chunks at two sizes.

Output

400 files of about 1000 KB. Half new, a quarter exact copies, a quarter an earlier file with 1 KB inserted at a random place.

how files are split            uploaded     kept on disk   saved      index entries
whole file only                400 MB       308.1 MB       22.9%      308
fixed chunks of 250 KB         400 MB       259.3 MB       35.1%      1113
fixed chunks of 25 KB          400 MB       242.0 MB       39.5%      9755
content-defined, about 250 KB  400 MB       241.7 MB       39.5%      1071
content-defined, about 25 KB   400 MB       200.5 MB       49.8%      8040
  • Whole-file matching kept 308.1 MB of the 400 MB uploaded (22.9% saved). The inserted byte made every edited file look new.

  • Fixed 250 KB chunks saved 35.1% and fixed 25 KB chunks 39.5%. Every chunk after the insert changed.

  • Content-defined chunks of about 250 KB saved 39.5%, and of about 25 KB saved 49.8%, which is nearly everything that can be saved in this data.

  • The price: 8,040 index entries for 25 KB chunks against 308 for whole files and 1,071 for 250 KB chunks. Each entry is metadata that must be stored and looked up.

Deduplication also has costs the program does not measure: hashing every chunk, reading more pieces for one file (a download touches shared chunks), and deleting safely (a chunk can go only when no file refers to it, so you need reference counts or a mark and sweep). Across customers it can also reveal that someone else holds the same file, so many services deduplicate only within one account.

11. Versions, Storage Classes and Lifecycle

With versioning on, an overwrite or delete adds a new version or a delete marker and keeps the old one. That protects against mistakes and costs space, so a lifecycle rule moves older versions to cheaper storage (more erasure coding, slower disks) and deletes them after a set time. The metadata layer holds the age, the class and the rule, and the lifecycle job scans it in the background. A job that scans billions of keys must be paced and spread over shards, or it will compete with users for the metadata.

12. Choosing the Design

ConcernChoiceWatch for
Durability for big cold filesErasure code, pieces in different racksRepair traffic and wide failures
Small or hot filesCopies3x space
Big uploadsMultipart, 25 MB parts, resumableToo many tiny parts
Metadata loadRange shards, split a hot prefixA listing that must read all shards
Overwrites and listingsImmutable data, one metadata stepA separate list index
Wasted spaceAbort unfinished uploads, expire old versionsCutting a slow but genuine upload

13. Trade-offs to Mention

  • Erasure codes save space and cost repair traffic and read latency; copies are the reverse.

  • Spreading keys balances writes and makes prefix listings expensive.

  • Strong consistency adds a wait on the write; eventual consistency gives stale reads and lists.

  • Smaller dedup chunks save more space and need a larger index and more lookups.

  • Short cleanup limits save space and cut slow uploads.

  • Versioning protects data and keeps old bytes alive.

14. Common Mistakes

  • Putting all pieces of an object in one rack.

  • Writing data in place instead of writing a new immutable version.

  • Showing an object before all of its pieces are durable.

  • Keying objects by date or a counter, so one shard takes the whole burst.

  • Keeping the list index in a separate store with no ordering guarantee.

  • Forgetting to clean up abandoned multipart uploads.

  • Deleting a deduplicated chunk that another file still uses.

15. Interview Questions

  • Why separate metadata from data? They have different sizes, access patterns and failure handling: the metadata is small, hot and must be consistent; the data is huge and can be spread and repaired in the background.

  • Copies or erasure coding? Copies for small hot objects, coding for large cold ones, with pieces in different failure domains either way.

  • How do you upload a 50 GB file over a bad link? Multipart with resumable parts, parallel parts, a hash per part and one completion step.

  • What goes wrong with keys that start with a timestamp? All new writes land on the same key range and shard.

  • How do you make a read after a write see the new version? Immutable data, a single strongly consistent metadata commit, and reads that go through it (or overlapping quorums).

  • How do you stop abandoned uploads from filling the disks? A lifecycle rule that aborts unfinished uploads after a limit chosen from the slowest real upload.

Summary

  • Keep metadata and data in separate layers; publish an object only after its pieces are durable.

  • Use copies for small hot objects and erasure codes for large cold ones; place pieces in different racks.

  • Use multipart uploads with parts large enough to keep requests few and small enough to make a retry cheap.

  • Choose keys that spread writes, and split a hot prefix instead of giving up listings.

  • Write immutable versions and switch the name in one consistent metadata step.

  • Abort abandoned uploads and expire old versions by rule.

  • Deduplicate with content-defined chunks only where the saving justifies the index.

Conclusion

Object storage looks like a bucket of files, and most of its design is about the corners: failures that arrive together, uploads that stop half-way, keys that make one shard hot, overwrites that overlap with readers, and leftovers that nobody lists. The design above handles each with a mechanism and a measured result. In an interview, give the numbers (objects, size, peak rate), then show how each choice keeps data safe and metadata balanced.

Previous topic: Design a CDN. Next topic: Design Message Queue.

References and further reading

  • DeCandia et al., Dynamo: Amazon's Highly Available Key-value Store (2007), and Ghemawat, Gobioff and Leung, The Google File System (2003).

  • Reed and Solomon, Polynomial Codes over Certain Finite Fields (1960), for the idea behind erasure codes; Muralidhar et al., f4: Facebook's Warm BLOB Storage System (2014).

  • Amazon Web Services documentation on S3 multipart upload, consistency and lifecycle rules.

  • Design Key-Value Store, Design Distributed Cache and Design a CDN on this site.