DEV Community

Karan Kumar
Karan Kumar

Posted on • Originally published at Medium

How Pinterest Serves 500 Billion Pins to 500 Million Users

In this guide, we explore architecture. Your feed is frozen. You've scrolled three times, and the same stale content stares back. Meanwhile, somewhere in a data center, a distributed system just failed to find the one image that would have kept you engaged for another hour.

Table of Contents

This is the invisible war Pinterest fights every day. Their challenge isn't just storing 500 billion Pins; it's surfacing the right ones to 500 million users in under 100 milliseconds.

Most system design interviews ask you to build a simple image board. The real Pinterest is something far more interesting: a massive-scale recommendation engine disguised as a social platform. Let's dissect how they actually do it.

The Problem Nobody Talks About

Storing images is easy—S3 handles that. The brutal part is the retrieval problem.

Every time a user opens Pinterest, the system must:

  • Evaluate their taste profile across thousands of dimensions.
  • Query billions of Pins through multiple ranking stages.
  • Blend organic content with targeted ads.
  • Personalize the mix based on real-time signals.
  • Return results faster than a human blink.

Do this wrong, and you burn millions in compute. Do it right, and you build one of the most sophisticated ML platforms on the internet.

Pinterest's architecture evolved through three distinct eras. Understanding this evolution explains every technical decision they've made.

diagram

The Storage Layer: Beyond "Just Use S3"

While Pinterest's object storage runs on S3, the real magic happens in how they organize and access metadata.

They built Zen, a sharding framework that sits above MySQL. Think of it as a distributed hash table with MySQL as the backing store. When a Pin is created, Zen computes which shard owns it based on the Pin ID. Each shard lives on a MySQL instance with a master and multiple replicas.

The sharding logic is simple but critical: shard_id = pin_id % num_shards. This ensures writes are spread evenly and range queries remain predictable.

However, Pinterest doesn't just shard by Pin ID; they maintain entity groups—clusters of related data that must reside on the same shard. A Pin, its comments, its repin counts, and its creator profile form one entity group. This eliminates expensive cross-shard joins for the most common read patterns.

diagram

For the "hot path"—Pin metadata lookups—they employ a multi-tier cache. Requests hit McRouter (Memcached with consistent hashing) first. If that misses, the system checks the local in-memory cache. A final miss triggers a query to MySQL via Zen. This architecture keeps the p99 for Pin metadata under 5 milliseconds.

The Real Beast: Learned Retrieval at Scale

This is where Pinterest diverges from the standard system design interview answer.

Traditional search relies on inverted indices: you index documents by terms and intersect posting lists. This works great for exact matches, but it's terrible for "show me stuff I might like but can't articulate."

Pinterest developed Learned Retrieval—a neural network that embeds both users and Pins into the same vector space. The distance between a user vector and a Pin vector predicts the probability of engagement.

The architecture operates in two phases: retrieval and ranking.

Phase 1: Candidate Generation

The system maintains an approximate nearest neighbor (ANN) index of 500 billion Pin embeddings. When a user loads their feed, their taste vector queries this index. Pinterest uses HNSW (Hierarchical Navigable Small World) graphs for this—the same algorithm powering vector databases like Pinecone and Weaviate.

Because 500 billion vectors cannot fit in a single machine's memory, Pinterest shards the index across hundreds of machines. A coordinator broadcasts the query to all shards, collects the top-K results, and merges them.

architecture diagram

The retrieval phase returns roughly 10,000 candidates, representing the widest point of the funnel.

Phase 2: Ranking Cascade

Pinterest then runs these candidates through a cascade of increasingly expensive models:

  • Light Ranker: A small neural network (a few million parameters) scores all 10,000 candidates and filters them down to ~500.
  • Heavy Ranker: A transformer-based model with billions of parameters. It considers Pin image features, text embeddings, user history, and real-time context to filter the list to ~100.
  • Blending Layer: Mixes organic Pins with ads, applies diversification rules, and enforces business constraints.

Each stage is a latency-quality tradeoff. The light ranker runs in 5ms, while the heavy ranker takes 50ms but delivers 3x better engagement predictions.

Real-Time Signals: The Kappa Architecture Pivot

Here is a mistake Pinterest made so you don't have to.

For years, they utilized a Lambda architecture—separate batch and streaming pipelines for user signals. Batch processed historical data overnight, while streaming handled clicks and impressions in real-time. These were merged at query time.

While functional, this approach was expensive, complex, and occasionally inconsistent. In 2019, they pivoted to Kappa architecture. Now, everything flows through a single streaming pipeline built on Apache Flink. User actions—clicks, closeups, saves, and hides—immediately update feature stores.

architecture diagram

The feature store tiers data by freshness. Real-time features (last 5 minutes) live in Redis for millisecond latency. Short-term features (last hour) reside in a local RocksDB cache. Long-term user embeddings are updated via periodic batch processes to the ANN index.

This is critical because user intent shifts rapidly. Someone searching for "minimalist bedroom" at 9 AM wants inspiration; by 9 PM, they might be looking to buy bedding. The Kappa pipeline captures this shift and feeds it to ranking models within seconds.

The Ads Mixer: When Business Logic Meets ML

Pinterest generates revenue through Promoted Pins, but jamming ads into every feed destroys the user experience. Conversely, too few ads leave revenue on the table.

They solved this with AdMixer, a dedicated service that orchestrates ad insertion. It receives organic candidates from the retrieval system, queries the ad platform for relevant Promoted Pins, and runs an auction.

The auction isn't a simple "highest bidder wins" scenario. It optimizes for expected value: bid amount × predicted engagement rate × user quality score. A 0.50bidwith100.50 bid with 10% predicted engagement beats a 1.00 bid with only 4% engagement.

AdMixer also enforces pacing constraints. Since advertisers set daily budgets, the system must spread these throughout the day to avoid exhausting budgets too early. This is essentially a control theory problem disguised as ad serving.

sequence diagram

A 2023 rewrite of AdMixer eliminated a major latency bottleneck. By reducing the number of microservices in the critical path from 50+ to just 12, they dropped p99 latency from 180ms to 80ms.

What Actually Breaks at Scale

Pinterest's postmortems reveal recurring failure modes common to all large-scale systems.

The Thundering Herd on Cold Starts
When a new model deploys, prediction servers start with empty caches. Every request becomes a cache miss, triggering a surge of database queries that can overwhelm MySQL shards.
The Fix: Cache warming. Before switching traffic to new instances, Pinterest replays recent requests to populate caches. The switch only occurs once hit rates stabilize above 95%.

Embedding Index Drift
User and Pin embeddings are trained separately. Over time, these vector spaces can drift. If a user's preference shifts from rustic decor to modern minimalism but their embedding doesn't update, recommendations feel stale.
The Fix: Pinterest retrains user embeddings weekly and Pin embeddings daily. They also utilize joint training experiments where both models learn together to keep the vector spaces aligned.

The Attribution Problem
Did a user save a Pin because the ranking model was effective, or would they have found it anyway? This is vital for training data. Training on saves that would have happened regardless teaches the model to recommend popular content rather than personalized content.
The Fix: Counterfactual evaluation. Pinterest holds out a small percentage of traffic for random recommendations. Only saves that exceed this random baseline are counted as true positives for training.

Key Takeaways

  • Entity groups eliminate cross-shard joins. Design your data model so related data lives together. The first query pattern you optimize for will dominate your system's performance.
  • Learned retrieval beats inverted indices for discovery. When users can't articulate exactly what they want, vector similarity search is the superior interface. Invest in ANN infrastructure early.
  • Kappa over Lambda for real-time ML. Maintaining two separate pipelines (batch and streaming) guarantees bugs at the merge point. Embrace the complexity of stream processing once to avoid reconciliation forever.
  • Ranking is a latency cascade. Use "cheap" models for many candidates and "expensive" models for a few. Your p99 latency is always determined by the slowest stage in the pipeline.
  • Counterfactuals provide the only honest evaluation. Engagement metrics can be misleading. Random holdouts reveal the actual value your models add.

Pinterest's architecture isn't magic; it's a series of pragmatic decisions made under strict constraints: drive engagement, keep infrastructure costs under 20% of revenue, and never let a feed load slower than a heartbeat.

The next time you're asked to design Pinterest in an interview, skip the basic CRUD diagram. Talk about vector spaces, auction theory, and Kappa architecture. That's where the real engineering happens.

Top comments (0)