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.

Grow the design

Start with the smallest system that works. Change it only when something breaks.

Version by version

From one box to the full system

Seven versions

Version 1: One service, one database

Forced by: Upload and view.

Our systemUploadersViewersImage serviceLocal diskPostgreSQLimage records
What changes
The service saves the file on its disk, makes the sizes in the request, stores a record in PostgreSQL and serves the files.
What it costs
Simple. One machine to back up.
What breaks next
The disk. 14 TB a day fills it in hours, and a lost disk loses pictures. For a club's photos, stop here.

Say: Let me get upload and view working, then see which target it misses.

Architecture

High-level design

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.

Five layers

  1. 1

    Edge

    CDN on img.example: no cookies, long shared caching, sizes and formats from a fixed list.

  2. 2

    Gateway

    api.example: TLS, sign-in, limits, firewall, routing.

  3. 3

    Services

    Upload, image, album, user and a thin social and search part. Start as one program with clear modules; split upload first if its load differs.

  4. 4

    Processing

    Queue, sandboxed workers, checks, review.

  5. 5

    Data

    Object storage, PostgreSQL, Redis, search index, column store.

Flows

The flows that matter

Flow 1: upload

The link is safe to give: the original is stored on several devices.

Flow 2: process

If the worker dies, the job shows up again and writes the same names. The record becomes ready only at the end.

Flow 3: view

Flow 4: make an album

One batch instead of 50 calls. Work still grows with the album: 500 rows cost more than 5, but not a hundred times more trips.

Life of an image