AlgoViz

Design Pinterest

hard 65 min

People save pictures on boards and find more like them.

See it run

Create a pin, open a feed, search by picture and lay out the grid, one hop at a time.

Operations

Create a pin

The picture goes straight to the store. The work happens later.

Step 1 of 13

Ask for an upload

Our systemPhone appPin svcS3filesSQSimage jobsImage workerModelMySQLSearch indexes

limit

12 of 100

The service checks the person and the limit of 100 pins an hour, and returns a signed address for ten minutes.

Step 1: Ask for an upload

def process(job):                       # may run twice    img = decode_safely(s3.get(job.key))    if known := images.by_hash(sha256(img)):        return attach(job.pin, known)    for width in (150, 236, 474, 736):        s3.put(name(job.image, width), resize(img, width))    safe = model.safety(img)    vec = model.embed(img)    images.set(job.image, state=decide(safe), color=main_color(img))    events.publish("image_ready", job.image, vec)
Latency
202 at once, visible in seconds
Load
116 images/s, 350 at peak
per image
about 1.5 MB

Fixed names make the job safe to run twice.

In the real world

The same saving and discovering pictures doing real jobs. Pick one and step through it.

Ten million saves in an hour are 2,800 a second.

Step 1 of 4

Bytes are fine

Our systemMany peopleCDN edgePin svcMySQL shardssavers' rowsKafkaCounts16 parts

The edge has the picture. Many misses at once become one fetch.

Step 1: Bytes are fine

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Processing in the request

    critical

    Seconds of work for each upload, and the evening peak takes the API down.

    Wrong
    resize_all(file); embed(file)return 201
    Right
    queue.send(upload_id)return 202
  • Counting pictures as API calls

    major

    A page of 25 is one call. The edge serves the pictures.

    Wrong
    servers = 87_000 / 500      # 174
    Right
    servers = 3_500 / 500       # 7, so run about 12
  • A cursor that hides an offset

    major

    The feed changes, so pins repeat or vanish between pages.

    Wrong
    cursor = base64('{"offset":25}')
    Right
    cursor = (session_list_id, last_position)
  • Trusting the index

    critical

    It lags, and may return private or blocked pictures.

    Wrong
    return vector_store.search(vec)
    Right
    ids = vector_store.search(vec)return [r for r in db.images(ids) if visible(r, me)]
  • Height is ratio times width

    minor

    With ratio as width over height, tall pictures would become flat.

    Wrong
    height = ratio * width
    Right
    height = width / ratio

Interview tips

  • Lead with the model

    • Image once, pin many.
    • Board and pins on the owner's shard.
    • Content hash names the file.
  • Numbers that decide

    • 29,000 views, 1,160 pages a second.
    • 15 TB of pictures, 20 GB of rows a day.
    • 7.5 TB of vectors become 234 GB.
  • Anticipate failures

    • Ranking down: last list, then topics.
    • Slow shard: answer without it.
    • Takedown: row first.
  • Show judgement

    • pgvector before a new system.
    • Rank per visit, not per hour.
    • Measure recall on a sample.