AlgoViz

Design Pinterest

hard 65 min

People save pictures on boards and find more like them.

Video lesson · Chapter 1 of 5

Publish the image once and use it on many boards

0:00 / 3:34

Theory

Read or listen first. About 2 min.

What it is

A service like Pinterest lets people save images, called pins, on themed boards, and shows each person a home feed of pictures they may want next. It also finds pictures that look like a given one.

One idea makes the rest simple: an image is a file, and a pin is one person's note that points at it, on one board. Millions of pins may point at one image.

Why it exists

People often know what they like when they see it and cannot say it in words. Collecting on boards says it for them, and two boards that share a picture tell us which other pictures belong together.

In an interview it brings together large files, read-heavy traffic, background work, a personal feed and nearest-neighbour search. The skill is to size each separately: image traffic is not API traffic, and images are not pins.

A short history

  1. March 2010

    Pinterest launches as a closed beta.

  2. 2014 to 2017

    Its engineers describe Related Pins (2014), Similar Looks (2015), Flashlight (2016) and Lens (2017) in the paper Visual Discovery at Pinterest.

  3. 2016

    Malkov and Yashunin publish HNSW, a graph for approximate nearest-neighbour search.

  4. 2018

    The PinSage paper describes learning from a graph of 3 billion pins and boards joined by 18 billion edges.

  5. 2021

    The CLIP paper describes a model trained on 400 million image and text pairs.

Words to know

Image
The picture file and its facts: size in pixels, main colour, state.
Pin
One save of an image on one board, with a title and a link.
Board
A named collection of pins. It can be private.
Embedding
A list of numbers for a picture, made so that similar pictures get similar numbers.
ANN
Approximate nearest neighbours: find most of the closest vectors without comparing all.
Candidate
A pin that may go into a feed, before ranking.
Masonry grid
Columns of pictures with one width and different heights.

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

Save. One picture file, many small pins that point at it

Understand it

Leaves on cork boards
AUTUMN

Scope

What are we building?

A place to save pictures on boards, a personal home feed, search by words and search by picture. This is an exercise inspired by well-known products; it does not describe a company's real system except where its own papers are linked.

Similar applications

ServiceKnown for
InstagramPhoto sharing and a feed
TumblrReblogging, close to saving
FlickrOrganising photographs
DribbbleDesign inspiration
BehanceCreative portfolios
We Heart ItCollections of images

The hard part

Three loads that must not be mixed

LoadSizeWhere it belongs
Picture bytes29,000 views a secondEdge and object store
Feed and board calls1,200 feed pages a secondServices, cache, database
Picture understanding116 new images a secondQueue, workers, vector index
Averages under the assumptions in the Numbers tab. Multiply by three for the peak.

Image and pin are two things

Processing, safety and embeddings are done once per image, however often it is saved.

The finished system

Where the design ends up

This is the last version. The Design tab begins with one service and one database and shows which number forces each extra box.

The whole system

People load images from a CDN backed by S3 and upload directly to S3, which puts a job on a queue for image workers. Other requests pass a gateway to the pin, feed and finder services. The pin service uses MySQL and publishes to Kafka. The feed service uses Redis. The finder service uses Elasticsearch for words and a vector store for pictures, both filled from Kafka.
Blue is reading, pink is writing, orange is background work.

Four key ideas

  • Store once, save often

    The content's hash names the file. A save is a 200-byte row.

  • Bytes go around the service

    Upload straight to the object store, download from the edge.

  • Prepare, then rank

    Candidates ahead of time; the order when the person arrives.

  • Index suggests, row decides

    Search and vectors may lag. Check state and privacy before showing.

Where to go

The tabs and the concepts

If you wantOpen
What the interviewer looks forApproach, By level
Targets and sumsScope, Numbers
Endpoints and tablesAPI and data
Images, look-alikes, feed, gridDeep dives
Practice against the clockMock
Direct uploadsFile upload APIs, object storage
Edge cachingCDN, cache invalidation
Splitting dataSharding, sharded counters
WordsFull-text search
Feeds and rankingNews feed, recommendation system

Summary

The whole topic in one minute.

Store each picture once under its content, and store each save as a small row. Pictures travel by object store and CDN; the API carries ids and addresses.

Uploads finish in the background, feeds are candidates prepared ahead and ranked when a person arrives, and look-alikes come from a compressed vector index that is checked against the truth before anything is shown.

  • Image views are not API calls: 29,000 a second against 1,200 feed pages.
  • An image is stored and processed once; a pin only points at it.
  • Uploads answer at once, and pictures become visible when ready and safe.
  • Prepare candidates daily and rank per visit. Hourly feeds for all active people cost more than the visits.
  • Similarity search is approximate and may lag, so the database has the last word.

Sources