FjellSystem Design Lab
Lesson 13.3• 8 min read

Graph partitioning and the TAO cache

Hash versus locality-aware placement, why cross-shard lookups never disappear, and TAO’s leader and follower cache tiers.

What you will learn

  • Compare hash placement with placement that keeps friends together.
  • Explain TAO's leader and follower cache tiers.
  • Say how TAO stays consistent and handles hot objects and other regions.

Where to put each person

With hash partitioning, hash(user_id) mod N\text{hash}(\text{user\_id}) \bmod N, people spread evenly over NN shards. But a person's friends land on shards at random. Fetching all ff friends of someone touches up to min⁡(f,N)\min(f, N) shards, and friends-of-friends touch more.

The primer suggests sharding Person Servers by location, "as friends generally live closer to each other", so more friend lookups stay on one server. More generally, a graph partitioner can place tightly connected groups together. METIS, for example, is a multilevel tool for partitioning graphs. Facebook used its own method, Balanced Label Propagation (Ugander and Backstrom, WSDM 2013), to maximize the share of edges kept on the same shard.

There is a limit, in TAO's words: "The social graph is tightly interconnected; it is not possible to group users so that cross-partition requests are rare." Good placement reduces cross-shard traffic but does not remove it, so the system must still handle many remote lookups cheaply, which is what the cache tier does.

The visual shows both on a tiny graph: hash placement splits most friendships across shards, while grouping keeps them together.


TAO's two-level cache

TAO places a cache between the application and MySQL:

  • Sharding: each object belongs to a shard, and an association is stored on the shard of its id1. Shards are mapped onto cache servers "using consistent hashing".
  • Leaders and followers: "Each shard is hosted by one leader, and all writes to the shard go through that leader." Follower tiers serve reads and forward read misses and writes to the leader. Splitting the cache this way keeps each tier small, because large tiers "are more prone to hot spots".
  • The leader protects the database. Because one leader mediates all requests for an id1, it can avoid concurrent overlapping queries ("thundering herds") and limit pending queries per shard.
  • Eventual consistency: the leader asynchronously sends invalidation or refill messages to followers, and TAO "guarantees the eventual delivery of an invalidation or refill to all tiers". Replication lag is "usually less than one second".
  • Across regions: followers forward writes to the leader in the region with the master database, and serve reads locally, at the risk of slightly stale data.
  • Hot objects: a shard that is read heavily is cloned across several followers, and very hot items are cached in the client.

Read misses at followers were "25 times as frequent as writes" in TAO's workload. That is why the design puts so much effort into serving reads locally.

Where friends land on shards

Eight people in two tight groups. Compare hash(user_id) mod 4 with putting each group on its own shard, and count the friend lookups that leave the user's shard.

Shard 0
David, Hanna
Shard 1
Carin, Gunnar
Shard 2
Birger, Frida
Shard 3
Astrid, Erik
Friendships split across shards
12 of 13
Friends and friends-of-friends
4 + 3
Lookups on another shard
6
Shards touched
4

Real social graphs are not this tidy. Facebook's TAO paper says that "it is not possible to group users so that cross-partition requests are rare", so grouping reduces cross-shard lookups but cannot remove them.

Figure 3: TAO two-tier distributed caching hierarchy separating regional follower read caches from leader write-through caches and sharded MySQL edge tables.
  • Social Application Clients: 1 × Clients & Users
  • Regional Follower Caches: 6 × Redis In-Memory Cache
  • Leader Cache Tier (all writes): 4 × Redis In-Memory Cache
  • Community-Sharded Primary DB: 2 × Primary Relational DB
  • Cross-Region Read Replicas: 4 × Database Read Replica
  • Social Application Clients sends requests to Regional Follower Caches
  • Regional Follower Caches sends requests to Leader Cache Tier (all writes)
  • Leader Cache Tier (all writes) sends requests to Community-Sharded Primary DB
  • Community-Sharded Primary DB sends requests to Cross-Region Read Replicas
  • Regional Follower Caches sends requests to Cross-Region Read Replicas

Key takeaways

  • Hashing spreads people evenly but scatters friends, so one friend list can touch many shards; grouping by location or community keeps more lookups local, but cross-shard lookups stay common.
  • In TAO every write to a shard goes through its leader cache, which also stops thundering herds reaching MySQL; followers serve the reads.
  • Leaders invalidate or refill followers asynchronously, so reads are eventually consistent; other regions read locally and send writes to the master region.
  • Hot shards are cloned across followers, and very hot items are cached in the client.

Sources

Adapted from The System Design Primer by Donne Martin, licensed under CC BY 4.0. Changes were made.