AlgoViz

Design an image hosting service (like Imgur)

medium 50 min

Upload a picture, get a link at once, and let the whole world look at it.

Video lesson · Chapter 1 of 5

The share link arrives before every picture size is ready

0:00 / 4:44

Theory

Read or listen first. About 1 min.

What it is

An image hosting service stores a picture and gives back a short link. Anyone with the link can see the picture, in a page, a forum or a chat.

Inside it is three systems: a careful one that accepts and checks uploads, a patient one that prints each picture in several sizes, and a very fast one that hands out finished copies. They meet only in storage.

Why it exists

A picture from a phone is large, may contain the place where it was taken, and may be in a format some browsers cannot show. Sending it as it is wastes traffic and can hurt the person who took it.

A link also has to survive success. One picture in a million becomes famous within an hour. If each view cost the origin anything, the famous picture would take all the others down.

A short history

  1. 1987

    GIF appears, a small image format that later also carries animations.

  2. 1992

    The JPEG standard makes photographs small enough to share.

  3. 2006

    Amazon S3 opens on 14 March: storage for files as a service.

  4. 2009

    Imgur starts in February, announced in a post on Reddit.

  5. 2017

    RFC 8246 adds immutable to HTTP caching, for files that never change.

Words to know

Original
The file exactly as uploaded. Kept private.
Variant
A finished copy in one size and format.
Exif
Data inside a photo file: camera, time and often the place.
Edge
A cache close to the viewer.
Origin shield
One more cache between all edges and the store.
Content hash
A fingerprint of the bytes. Equal bytes give equal hashes.
Delete token
A long secret that lets an anonymous uploader remove a picture.
Purge
Telling caches to drop a copy now.

Solve it like an interview

New to it? Start with Approach. Revising? Go straight to the mock.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 3

Hand in. The original goes to a private store, the link comes back

Understand it

The photo booth
closedhome7Maxticketthe photo goes in

Scope

What are we building?

A service like Imgur: anonymous or signed-in uploads of images up to 20 MB, short links, several sizes, albums, deletion and fast delivery worldwide. Social feeds and video have pages of their own: Instagram stories, Reddit, YouTube.

  • Upload

    Straight to a private store, safe to retry, link in about a second.

  • Process

    A queue and workers: check, clean, resize.

  • Serve

    Edge, shield, store. 99 of 100 never reach the store.

  • Protect

    Nothing public before checks; takedown everywhere.

Similar services

KindExamplesMain idea
Quick image linksImgur, ImgBB, Postimages, CatboxUpload and share
PhotographersFlickr, Photobucket, UnsplashCollections, large files, licences
AnimationsGiphySearch and short loops
ScreenshotsGyazoCapture and link
Media APICloudinaryTransform by address

The finished system

Where the design ends up

The whole system

Uploaders call the API for permission and send the file straight to a private store. The API records the image and puts a job on a queue. Workers check the file, remove private data, make three sizes and write them to the public store. Viewers ask the CDN edge, then an origin shield, then the public store. Edge logs feed view counts. A review team decides doubtful images.
Blue is the view path, pink the upload path, orange work in the background. Dashed means later.

Summary

The whole topic in one minute.

An image service accepts the original into a private place, answers with a link, and publishes only finished copies after checking and cleaning them in the background. Users wait a second for the link, and the picture follows.

Reads are 500 times more common than writes, and finished copies never change. So the edge keeps them, a shield collapses misses, and storage sees about one request in a hundred. Views are counted from edge logs, old files move to cheaper storage, and takedowns purge every layer.

  • Answer the upload when the original is safe, not when every size is ready.
  • Publish nothing until it is checked, cleaned and re-encoded.
  • A finished copy never changes, so caches may keep it long.
  • 95% at the edge and 80% of the rest at the shield leaves 1% for the store.
  • Make the common sizes ahead and the rare ones on demand.
  • Deletion is part of the design: record, files, shield, edge.

Sources