Case study demo
Object storage (S3)
Store objects as metadata plus replicated data chunks.
An interactive demo of how this system behaves. It runs in your browser.
Design walkthrough: Object storage (S3)
Object storage keeps files of any size under a key, such as photos/cat.jpg, and almost never loses one. You cannot edit part of a file. You replace the whole object.
Requirements
What it must do
- Put an object under a bucket and a key.
- Get it back by the same key.
- List the keys in a bucket that start with a prefix.
How well it must do it
- Durability first: a saved object must survive the loss of a disk, a server or a data centre.
- Scale to billions of objects.
- A new object can be read as soon as its upload succeeds.
Rough numbers
- 1 billion objects at 1 MB on average is 1 PB of data.
- Three full copies need 3 PB of disk.
- Erasure coding with 6 data parts and 3 extra parts needs 1.5 PB, and survives the loss of any 3 parts.
- The metadata is tiny next to that: 1 billion rows at 200 bytes is 200 GB.
How a request flows
Write pathUpload an object
- Client
- API servers
- Data node A
- Data nodes B and C
- Metadata DB
Client to API servers
The client sends PUT photos/cat.jpg. The API checks that the caller may write to this bucket.
API servers to Data node A
The API picks data nodes with free space and streams the bytes to the first one.
Data node A to Data nodes B and C
That node copies the bytes to nodes in two other zones. Each node checks the data against a checksum.
API servers to Metadata DB
Only after the copies are safe does the API save a row: bucket, key, size, checksum and where the bytes are.
API servers to Client
It answers success. The object exists from the moment its metadata row is saved.
Read pathDownload an object
- Client
- API servers
- Metadata DB
- Data node A
Client to API servers
The client sends GET photos/cat.jpg.
API servers to Metadata DB
The API looks up where the bytes are.
API servers to Data node A
It reads from any healthy copy and checks the checksum.
API servers to Client
It streams the bytes back. If a copy is damaged, it reads another copy and repairs the bad one.
Scaling it
- Partition the metadata by bucket and key. Listing a prefix then reads one sorted range.
- Pack many small objects into one large file on disk, so a billion tiny files do not exhaust the file system.
- Move objects that are rarely read to cheaper, slower disks, with no manual step.
What breaks, and what happens then
A disk or a data node dies
- What users see
- One copy of many objects is gone.
- What the design does
- A repair job notices. It copies each object from a healthy node to a new one until three copies exist again.
A disk returns wrong bytes without an error
- What users see
- A read could return a damaged file.
- What the design does
- Every read checks the checksum. A background scan also reads old data again to catch damage early.
An upload fails half way
- What users see
- Bytes are on data nodes, but no metadata row exists.
- What the design does
- The object is not visible, because the row is written last. A cleaner deletes the leftover bytes later.
Trade-offs
- Three full copies, or erasure coding
- A good default
- Erasure coding for large, rarely read data: half the disk for the same safety.
- What it costs
- Reads and repairs need more CPU and more network calls.
- Write the metadata before the bytes, or after
- A good default
- After. A failed upload leaves no visible, broken object.
- What it costs
- Leftover bytes must be cleaned up.
Common mistakes
- Treating object storage like a file system and editing files in place.
- Putting the object bytes in the metadata database.
Questions an interviewer might ask
How does a 5 GB upload survive a bad network?
Use a multipart upload. Each part is sent and confirmed on its own, and only failed parts are sent again.
How do you list a billion keys?
Keep the keys sorted in the metadata store. Return pages of 1,000, each with a marker for the next page.
What does eleven nines of durability mean?
Store 10 million objects, and you would expect to lose one about every 10,000 years.