Skip to content
System Design Explorer
Company architectureadvanced

Facebook System Design

Serving the social graph: TAO and memcache at a billion reads a second

By Ankur NayakReviewed

How Facebook served its social graph in 2013: a graph-aware cache called TAO, with follower and leader tiers in front of sharded MySQL, handled a billion reads a second, while a look-aside memcache fleet with leases, a Gutter pool and commit-log invalidations served everything else. This is the architecture from the two 2013 papers.

Open in Studio· edit a copy
Users
Over a billion users (2013)
Throughput
1B+ TAO reads/s (2013)
Storage
Many petabytes; trillions of cached items
TAO reads / writes
1B/s · millions/s
Read share
99.8%
TAO hit rate
96.4%
Memcache
Billions req/s
Facebook system design architecture diagramComponents: Browser / App, Web Servers (PHP), TAO, mcrouter, Memcache Pools (memcached), Gutter Pool, MySQL (Master Region), MySQL Replicas, McSqueal. Connections: Browser / App to Web Servers (load page); Web Servers to TAO (objects + associations); Web Servers to mcrouter (get / set / delete); mcrouter to Memcache Pools; mcrouter to Gutter Pool (on failure); Web Servers to MySQL (Master Region) (on cache miss); TAO to MySQL (Master Region) (misses + writes); MySQL (Master Region) to MySQL Replicas (async); MySQL (Master Region) to McSqueal (commit log); McSqueal to Memcache Pools (invalidations).Browser / AppClientWeb ServersPHPTAOCachemcrouterLoad balancerMemcache PoolsmemcachedGutter PoolCacheMySQL (Master Region)SQL databaseMySQL ReplicasSQL databaseMcSquealWorker

Requirements

Functional

  • Read a user's profile, friends, posts, likes and comments to build each page
  • Write new posts, comments, likes and friendships
  • Keep both directions of a relationship (A friends B, B friends A) in step
  • Show users their own writes immediately
  • Serve users from data centres in several regions

Non-functional

  • A billion reads per second, overwhelmingly reads
  • Low latency for pages that fetch hundreds of items
  • Survive server, cache and database failures without overloading MySQL
  • Favour availability; accept brief staleness between regions

In depth

A read problem at enormous scale

A Facebook page is built from hundreds of small pieces of data: a profile, a list of friends, the newest posts, the likes and comments on each. In 2013 Facebook published two papers describing how it served that data to over a billion users: TAO, its data store for the social graph (USENIX ATC), and Scaling Memcache at Facebook (NSDI). Together they describe one of the largest caching systems ever documented: TAO handled a billion reads and millions of writes per second, and memcache billions of requests per second over trillions of items.

The defining characteristic is the read ratio. In the TAO paper's sample, 99.8% of requests were reads. So the architecture is about serving reads from cache, cheaply and in every region, while keeping writes correct and caches reasonably fresh. Meta's stack has changed since 2013; these papers remain the clearest public description of the design.

The social graph as objects and associations

TAO's data model is small. Objects are typed nodes: users, posts, check-ins, comments. Associations are typed, directed edges: (id1, atype, id2) with a time and some data, such as "Alice FRIEND Bob" or "Bob COMMENTED on post 42".

The key query is the association list: all edges of one type from one object, in descending time order. "The 50 newest comments on this post" is assoc_range(post, COMMENT, 0, 50). Because pages almost always want the newest items, TAO caches contiguous prefixes of these lists.

Many edges come with an inverse: if Alice friends Bob, Bob friends Alice. TAO can configure association types with an inverse type and writes both together. The forward edge lives on the shard for id1 and the inverse on the shard for id2, so one logical write can touch two shards. If one half fails, the "hanging" association is repaired later by an asynchronous job.

Followers, leaders and MySQL

Each object ID contains an embedded shard ID, and objects and associations are stored in MySQL, sharded. In front of MySQL, TAO's caches are split into two levels:

  • Follower tiers (at least two per region) take requests from web servers. Each tier can answer any request; each client has a primary and a backup tier. Very hot shards are cloned across several followers.
  • A leader tier (one per region) receives misses and writes from followers, talks to MySQL, serialises concurrent writes and sends invalidation and refill messages back to the followers.

This split lets follower tiers scale out for reads without multiplying the load on MySQL. The overall read hit rate was 96.4%.

Regions: master and replicas

Facebook ran in several geographic regions, each with a complete copy of the databases. For each shard, one region holds the master database; others hold asynchronous replicas. Writes are forwarded to the master region; reads are served locally. In the TAO paper's measurements replicas lagged by under one second 85% of the time, under three seconds 99% of the time.

That lag is a deliberate trade: availability and local reads over strong consistency. To still show users their own writes, the result of a write is sent back to the follower tier that issued it, and the memcache paper describes "remote markers" that send reads of known-stale keys to the master region.

Memcache: a look-aside cache, made robust

Not everything is graph data. For the rest, Facebook used memcache as a demand-filled look-aside cache: a web server reads a key, and on a miss queries the database and sets the value. Writes go to the database, and the cache is invalidated. See caching.

Several techniques make this work at scale:

  • mcrouter runs alongside web servers and routes requests to memcached servers; gets can go over UDP directly, while sets and deletes go over TCP through mcrouter, which also coalesces connections.
  • Leases solve two problems. On a miss, memcached gives the client a 64-bit token to set that key; if the key is deleted meanwhile, the token is invalidated, which prevents stale sets. Handing out only one lease per key in a short window prevents thundering herds: in one measurement, peak database queries fell from 17,000 per second to 1,300.
  • Gutter: about 1% of a cluster's servers are held in reserve. When a memcached server stops responding, clients retry against Gutter, which absorbs the dead server's load with short-lived entries. Within four minutes of a total failure, Gutter hit rates typically exceeded 35%.
  • McSqueal: invalidation daemons on every database read the MySQL commit log and broadcast deletes, so caches learn about every committed change.

Failures

The design assumes failures are constant. A dead memcached server is absorbed by Gutter rather than turning into a database stampede. A hot key's invalidation is tamed by leases. If a TAO leader is unavailable, followers read directly from the database and delegate writes to another leader. If a region's replica database goes down, TAO sends that region's misses to the master region and replays invalidations when it recovers.

The trade-offs

Facebook's 2013 architecture favours availability over strong consistency: asynchronous cross-region replication means another region may briefly see old data. It favours a specialised cache (TAO's graph API) over a generic one, which cost a large engineering investment but gave efficient association lists and automatic inverse edges. And it keeps cache servers simple by putting logic in clients and mcrouter, at the price of coordinating thousands of clients through leases and invalidation pipelines.

Compare the read-heavy, cache-first approach here with the ranking pipeline in Twitter (X), and with the news feed interview design.

What to take into an interview

  • Model social data as objects and typed edges; cache the newest-first edge lists
  • Put a cache tier in front of sharded databases, and split it into followers and leaders so reads scale without multiplying database load
  • Replicate asynchronously across regions if you can tolerate brief staleness, and handle read-your-writes explicitly
  • Protect the database from your cache: leases against thundering herds, a spare pool for failed cache servers
  • Drive invalidation from the database's commit log

Components

Browser / App

Client. Loads a page that needs hundreds of objects and edges from the social graph.

Web Servers

PHP

Service. Build each page by fetching graph data from TAO and other cached data from memcache.

TAO

Cache. A geographically distributed, graph-aware cache and data store for objects (nodes) and associations (edges). Caches are split into follower tiers that serve clients and a leader tier that talks to MySQL.

Why: A graph API with its own caching replaced hand-written memcache + MySQL code for social data, and keeps edges and their inverses in sync.

Building block: Cache, Replication

Follower Tiers

Cache. At least two per region. Each tier can answer any request; clients have a primary and a backup tier. Hot shards are cloned across several followers.

Leader Tier

Cache. One per region. Followers forward misses and writes to it; it reads and writes MySQL, serialises concurrent writes and sends invalidations back to followers.

MySQL Shards

SQL database. Objects and associations are stored in many logical shards; each object's ID identifies its shard.

Building block: Database Sharding

Inverse Edge Repair

Worker. An edge and its inverse may live on different shards. If one write fails, the hanging association is repaired by an asynchronous job.

mcrouter

Load balancer. Memcache client logic as a proxy on each web server: routes requests to the right memcached server and coalesces connections. Gets can use UDP directly; sets and deletes go over TCP via mcrouter.

Memcache Pools

memcached

Cache. A demand-filled look-aside cache for non-graph data: read from memcache, on a miss read the database and set the value. Leases stop stale sets and thundering herds.

Building block: Cache, Consistent Hashing

Gutter Pool

Cache. About 1% of a cluster's memcached servers, held in reserve. When a server stops responding, clients retry against Gutter, which absorbs its load with short-lived entries.

MySQL (Master Region)

SQL database. The persistent store for graph and other data, sharded. Each shard has a master in one region; all writes for that shard go there.

Building block: Database Sharding

MySQL Replicas

SQL database. Asynchronous replicas in other regions serve local reads. In the TAO paper's measurements they lagged by under 1 second 85% of the time.

Building block: Replication

McSqueal

Worker. Invalidation daemons that read each database's commit log and broadcast deletes to memcache, so caches are invalidated after data changes.

Request flows

Load a profile page

  1. A user opens a friend's profile.
  2. The web server asks TAO for the profile object and the newest items of its association lists (posts, friends).
  3. Most reads hit a follower's cache (96.4% in 2013); misses go to the leader tier and then MySQL.
  4. Non-graph data comes from memcache through mcrouter, many keys in parallel.
  5. The page is rendered from hundreds of cached items.

Write a comment and invalidate caches

  1. The user comments; TAO receives an object write and an association add.
  2. The write is forwarded to the shard's master region and committed to MySQL.
  3. MySQL replicates the change asynchronously to replicas in other regions.
  4. McSqueal reads the commit log and extracts the invalidations.
  5. Deletes are broadcast so no cache serves the old value for long.

Failure scenarios

What if: a memcached server dies?

Its keys would all miss and send a burst of queries to MySQL. Instead, clients retry against the Gutter pool, which fills from the database once and then serves hits. In the paper, Gutter hit rates exceeded 35% within four minutes of a total failure, often approaching 50%.

User impact: Slightly stale data for a short time instead of an overloaded database.

  • Gutter pool (~1% of servers)
  • Short expiry on Gutter entries

What if: a hot key is invalidated?

Thousands of web servers miss at once and all query the database. With leases, memcached gives only one client the right to refill the key within a short window; the others wait briefly or accept a slightly stale value. In one measurement this cut peak database queries from 17K/s to 1.3K/s.

User impact: Negligible; a few milliseconds of waiting.

  • Leases (64-bit tokens bound to a key)
  • Serving marked-stale values when acceptable

What if: a region's replica database is down?

TAO redirects that region's cache misses to the leaders in the region holding the master, and an extra binlog tailer delivers invalidations and refills across regions until the replica recovers.

User impact: Higher latency for misses in that region.

  • Redirect misses to the master region
  • Replay invalidations when the replica returns

Trade-offs

Availability over strong consistency

Replicate MySQL asynchronously across regions and let TAO's caches be eventually consistent.

Pros
  • Reads served locally in every region
  • A billion reads per second
Cons
  • Readers in other regions can briefly see old data
  • Read-after-write needs special handling

A graph-aware cache instead of a generic one

Build TAO with an object and association API rather than caching raw SQL results.

Pros
  • Caches association lists efficiently, newest first
  • Keeps inverse edges in sync
  • One cache layer for every product
Cons
  • A large, specialised system to build and operate

Leader and follower tiers

Split each region's cache into follower tiers for reads and a leader tier for writes and misses.

Pros
  • Followers scale out without overloading MySQL
  • Leaders serialise writes per shard
Cons
  • Invalidations must flow from leaders to every follower tier

Look-aside cache with leases

Let clients manage memcache (read, miss, query, set) and use leases to arbitrate refills.

Pros
  • Simple, stateless cache servers
  • Prevents stale sets and thundering herds
Cons
  • Cache logic lives in every client (via mcrouter)

Frequently asked questions

What is Facebook TAO?

TAO (The Associations and Objects) is Facebook's distributed data store for the social graph, described in a 2013 paper. It exposes objects and typed associations between them, caches them in follower and leader tiers, stores them in sharded MySQL, and handled a billion reads per second.

How does Facebook use memcache?

As a demand-filled look-aside cache: a web server reads a key from memcache and, on a miss, queries the database and sets the value. Writes go to the database, and invalidation daemons reading the MySQL commit log delete the affected keys.

What are memcache leases?

A lease is a 64-bit token memcached hands to a client on a cache miss, giving it the right to set that key. It prevents stale sets, since a lease is invalidated if the key is deleted, and limits thundering herds by handing out one lease per key at a time.

How does Facebook handle replication across regions?

Each MySQL shard has a master in one region and asynchronous replicas elsewhere. Writes are forwarded to the master region; reads are served locally from TAO and the replicas, which in 2013 lagged by under a second 85% of the time.

Building blocks used

Sources