Patterns

A cache in front of the graph: Meta

Meta put a write-through memcached tier in front of sharded MySQL, and kept one primary region for each shard.

Facebook built TAO in 2013 to answer the social graph's read-heavy queries without sending every one to MySQL. TAO is a write-through cache: a read is served from memcached, and a write goes through the cache to the shard that owns the object. The data set is split into hundreds of thousands of shards, and each shard has one primary region.

The system#

The API is small and fixed: get an object, get its outgoing edges, count them, and iterate a range. TAO servers keep the cache consistent across regions, and MySQL replication carries each shard's updates from its primary region to the secondary regions. A secondary region forwards writes to the primary, and its write-through cache keeps read-after-write consistency for the member who made the change. By 2022 TAO served more than 10 billion requests a second.

The board#

Drag to pan. Hold Ctrl or Cmd and scroll to zoom.

At 4.00k Users the Simulation solves this Design to Apdex 1.00, p99 157 ms, 0.0% errors and $64/s Burn. The board is the read path at game scale. TAO’s per-shard primary regions are not on it. The caption is Quorum's result, not the company's numbers.
WhereSettingValueWhy
App ServerInstance sizelargeLarge, because the app tier answers the graph reads.
App ServerCopies3Three copies stand for the app tier over the graph.
CacheEnginememcachedMemcached, the cache TAO puts in front of MySQL.
CacheCache strategywrite-throughA write goes through the cache and on to the database, so the cache never holds an old object. TAO is a write-through cache over MySQL.
CacheTTL (seconds)60A minute bounds staleness for an object nobody writes.
CacheInstance sizexlargeExtra large, because the hot graph is the point of the cache.
DatabaseShards8TAO splits the graph into many shards. Eight is the widest the board offers, and it stands for hundreds of thousands.
DatabaseShard keyhashA hash spreads objects and associations evenly, so no shard owns the hot end of the graph.
DatabaseInstance sizelargeLarge for each shard, because each shard is a real MySQL database.
DatabaseEnginemysqlMySQL, the engine TAO keeps the graph in.

Research this Design needs: Caching, Caching, Cache strategies, Sharding.

The Cache is TAO's memcached tier, on write-through, so a write updates the cache and the database. The Database has eight shards on a hash key, standing for the hundreds of thousands of TAO shards. The board shows one region. A second region reads through its own cache and forwards writes to the primary, which the board does not draw.

Why it works#

A read-heavy graph spends most of its queries on the same popular objects. A write-through cache keeps those objects in memory and keeps the cache correct on a write, so the database sees the writes and the misses. Write-through and write-behind and shard the database are the two halves.

Where the game stops#

The game has one primary region per Database, not one per shard, and the board draws one region. It does not show TAO's API, its per-shard primary map, or the memcached fleet. The caption is Quorum's result, not Meta's 10 billion requests a second.

Sources#