Design Pinterest
hard 65 minPeople 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
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
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
criticalSeconds of work for each upload, and the evening peak takes the API down.
Wrongresize_all(file); embed(file)return 201Rightqueue.send(upload_id)return 202Counting pictures as API calls
majorA page of 25 is one call. The edge serves the pictures.
Wrongservers = 87_000 / 500 # 174Rightservers = 3_500 / 500 # 7, so run about 12A cursor that hides an offset
majorThe feed changes, so pins repeat or vanish between pages.
Wrongcursor = base64('{"offset":25}')Rightcursor = (session_list_id, last_position)Trusting the index
criticalIt lags, and may return private or blocked pictures.
Wrongreturn vector_store.search(vec)Rightids = vector_store.search(vec)return [r for r in db.images(ids) if visible(r, me)]Height is ratio times width
minorWith ratio as width over height, tall pictures would become flat.
Wrongheight = ratio * widthRightheight = 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.