Systemsfor Aravinda
0 / 78 core
Week 12 · Production stories

Facebook TAO

1 hours · core path

Read first: Caching

In one sitting

TAO stores the social graph as objects and associations and serves get, range, and count from a graph-aware cache instead of joining in MySQL. Followers sit in front of a leader cache, writes hit the owning region's database and invalidate caches, and a read in another region can be stale because cross-region replication is asynchronous. Inverse edges are stored so "who liked this" is a range, not a scan.

The read API of the social graph is not a join. A product asks for an object, for a range of its edges, and for a count of those edges. TAO is Facebook’s cache for that shape, sitting in front of MySQL, and it stores the edge in both directions so the second question is not a scan.

The problem

The social graph is objects and associations. An object is the node: the thing the product can point at. An association is a directed edge. If those reads hit MySQL on the request path, “show this post” and “who liked it” become queries the database answers by joining or by scanning. The product does those reads constantly. TAO exists so the common read is a graph operation against a cache that already understands objects and edges.

This is not the lookaside cache from the memcache paper. That system stores whatever keys the application fetched from a database. TAO’s API is graph-shaped on purpose. Mixing the two papers produces a story about a generic cache with a graph sketched on top. The published system is the other way around: the cache is the graph API.

The design

Objects and associations

An association is the tuple (id1, type, id2), plus a time, plus a small payload. The id pair says which two objects the edge connects. The type says what the edge means. The time and the payload ride along so the product is not forced back to MySQL for a detail that fits on the edge.

The API is three reads. Get an object. Range over associations. Count them. Those are the queries that would have been joins on the MySQL path. TAO serves them from a graph-aware cache. The application asks the graph question. It does not assemble the answer from a pile of unrelated keys.

Take a like, which is the association the paper’s “who liked this” question is about. User 17 likes post 9. Stored forward, that is (17, LIKES, 9), with a time and a small payload. The inverse is stored as its own association, (9, LIKED_BY, 17), again with a time and a payload. “Posts that user 17 liked” is a range over associations whose first id is 17 and whose type is LIKES. “Who liked post 9” is a range over associations whose first id is 9 and whose type is LIKED_BY. The count API answers “how many” without the product pulling the whole range and counting it in the web server.

The inverse is the entire reason the second range is cheap. If only the forward edge existed, “who liked post 9” would mean “find every association whose second id is 9.” That is a scan, unless some other index happens to exist. TAO stores the inverse explicitly so the question has the same shape as the forward question: an id and a type, then a range. You pay a second stored edge so you never pay the scan on the read path.

The type names LIKES and LIKED_BY in this walkthrough are labels for the example, so the two directions are easy to see. The published identity is (id1, type, id2) plus time plus payload. Any product edge with two read directions has the same shape.

Follower, leader, database

Followers sit in front of a leader cache. The database behind the cache is MySQL. A read hits a follower. A miss at the follower is the leader’s problem. A miss at the leader is MySQL’s problem. The web server is not issuing the join.

Writes go to the database in the owning region, and they invalidate caches. Invalidation is the correctness path for a cache: the write changes MySQL, and caches that hold the old object or the old edge list have to drop it. Invalidation is not instantaneous. A follower that still holds the previous range will serve it until the invalidation arrives. That is the general rule of invalidation, not a measured TAO delay. The notes we have do not publish that delay. Do not invent one.

flowchart LR
  web[Web server]
  follower[Follower cache]
  leader[Leader cache]
  db[MySQL in the owning region]
  other[Cache in another region]
  web --> follower
  follower --> leader
  leader --> db
  db -->|async replication| other

Replication between regions is asynchronous. A write that has committed in the owning region’s database has not necessarily arrived anywhere else. A read served in another region can be stale. Eventual consistency, if you even want the word, only says that replicas converge after updates stop. It says nothing about this read, and nothing about when. The honest sentence is the published one: a read in another region can be stale.

Read-your-writes is the guarantee that a client sees the write it just made. The replication lesson’s ways to get it are the primary, a session token, or a version check. TAO’s published cross-region behavior is the stale read. Do not add a session-token story the notes do not contain. If you need the user to see their own like immediately, you have to say where that read is served. The owning region has the database that took the write. Another region may not have the row yet.

What a page actually asks

Assume, as a sketch and not as a Facebook measurement, that rendering a post does three graph calls: get the post object, count the likes, and range the first page of people who liked it. Those are the three API calls above, and they match the product. Three MySQL queries, issued one after another, would wait on the sum. Issued together, the page would wait on the slowest, and the slowest might be a scan.

Dean’s teaching number for a round trip inside one datacenter is 0.5 ms. Three serial round trips have a network floor of 1.5 ms before MySQL does any work.

assumption: three serial reads, same datacenter, network floor only
3 * 0.5 ms = 1.5 ms

You do not build a graph cache to save 1.5 ms. The floor is the point of the arithmetic: the network is the cheap part. The expensive part is the join and the scan of edges, repeated on every view of a popular object. TAO’s follower answers the get, the count, and the range from a cache that stored the inverse association. MySQL is on the miss path and on the write path, not on every view.

A write of that like goes to MySQL in the owning region and invalidates the caches that hold the object’s associations and the count. The forward edge and the inverse edge both have to disappear from caches, because both are stored. Forget the inverse and “who liked this” keeps showing the old range after “posts I liked” has already moved on. Two stored directions means two invalidations. That is the bill for the cheap range.

Product question Against MySQL on the read path What TAO serves
Get this object Point read of the row Get object, from the cache when it hits
List this user’s likes Range or join on the forward edge Range associations for (id, type)
Who liked this post Scan on the far end of the edge, unless you built another index Range on the inverse association, stored for this
How many likes A count query The count API
The same read in another region Whatever replication lag the other region’s database has The same staleness, because cross-region replication is asynchronous

What they traded

Choice What they gained What they spent
A graph API in front of MySQL Get, range, and count match the product, and the read path does not join A cache that has to understand objects and edges, not just opaque keys
Followers in front of a leader cache Repeated reads stay off MySQL, and a follower miss hits the leader first Two cache tiers to invalidate, and a miss path that is longer than one hop
Invalidate caches on write The correctness path, rather than waiting out a timer A window where a cache still holds the previous object or the previous range
Store the inverse association “Who liked this” is a range on (id, type), the same shape as the forward read A second stored edge per relationship, and a second invalidation
Asynchronous replication across regions The owning region does not wait for the other region to apply the write A read in another region can be stale

The fourth row is the one to defend in a design review. Doubling association storage is easy to resent until you price the alternative. The alternative is a scan, or a second index you will have to maintain anyway. They chose the explicit inverse so the read API stays graph-shaped in both directions. The fifth row is the one to defend in an interview. You say the other region can be stale before anyone asks you to pretend it cannot.

The hierarchy and the invalidation window are ordinary cache costs from the caching lesson. A burst of misses wants something in front of the database, and invalidation is how you get correctness when a write has happened. The notes do not publish a hit rate, a follower count, or an invalidation latency. Leave those out.

What to steal

Steal the read API. Write down the three questions the product actually asks, and store the bytes in that shape. If a relationship is read from both ends, store both ends. A like that is only stored as “user to post” makes “who liked this post” somebody else’s scan. The inverse row is the design.

Steal the honesty about regions. A write that commits in the owning region and replicates asynchronously is a write another region may not see yet. Say that when you draw a second region. If the product cannot tolerate it, the read has to go where the write went, or you do not have a second region yet.

Steal invalidation as the correctness path, and treat a time-to-live as the bound for when invalidation fails, which is the caching lesson’s split. Do not cite a TAO time-to-live. The notes here do not give you one.

Leave the system. A five-person product with one MySQL database copies the tables, not the cache hierarchy. One table of objects. One table of associations whose columns are the two ids, the type, the time, and the payload. When you record a like, write the forward row and the inverse row in the same local transaction, so one direction cannot commit without the other. That sentence is advice for a single database. It is not a claim about TAO’s internal write protocol, which these notes do not specify.

You add a cache when those reads are actually hot, cache-aside, and you invalidate on the write. You add a follower tier and a leader tier when one cache in front of MySQL is the thing falling over. You add a second region when you have a second region, and you say the read can be stale. Building TAO before you have the graph traffic is how a five-person team ends up operating a distributed cache for a join MySQL would have answered.

In five minutes: objects and associations, the association is (id1, type, id2) plus time plus a small payload, followers in front of a leader cache in front of MySQL, writes to the owning region and cache invalidation, async cross-region so another region can be stale, and the inverse edge stored so “who liked this” is a range. Then say you would copy the inverse row and not the fleet.

Check yourself

Write the forward and inverse associations for user 17 liking post 9, and say which range answers who liked the post.

The forward association is (17, LIKES, 9) plus a time and a small payload. The inverse is stored explicitly as (9, LIKED_BY, 17) plus a time and a small payload. Who liked post 9 is a range over associations with id 9 and that inverse type. The type names are example labels. The published shape is (id1, type, id2) plus time plus payload. Without the inverse, the question is a scan for every edge that points at 9.

A like commits in the owning region. A read in another region runs immediately afterward. What can that read return, and why?

It can return the world from before the like. Replication between regions is asynchronous, so the other region may not have the write yet. A cache filled from that region’s data serves the stale graph. Eventual convergence, once updates stop, says nothing about this read and nothing about the wait. The owning region is where the write went. The other region is allowed to be behind.

Why put a follower cache in front of a leader cache instead of sending every graph read to MySQL?

The product reads are get object, range over associations, and count. Those would be joins or scans on the MySQL path, and they happen on the view path. The follower answers the repeated reads. A miss at the follower goes to the leader. A miss at the leader goes to MySQL. The hierarchy keeps the database off the hit path. You do not build it to shave the datacenter round trip. Three serial round trips at Dean’s 0.5 ms floor are 1.5 ms of network, and the scan is the cost that matters.

What would you be mixing up if you described TAO as the 2013 memcache paper with graph keys?

The memcache paper is a demand-filled lookaside cache of keys in front of databases. TAO is a graph-shaped API: objects, associations, ranges, and counts, with inverse associations stored so a question like who liked this is a range. Both sit in front of MySQL, and both care about invalidation. They are different systems. A generic key that happens to contain an id is not an association the cache knows how to range.

You have five people and one MySQL. Which TAO idea do you copy this quarter, and which machines do you refuse to run?

Copy the data model. Store objects, and store each association in the direction you will read, including the inverse, writing both rows in one local transaction so the two directions cannot diverge. Serve get, range, and count from those tables. Refuse the follower tier, the leader tier, and a second region until the read path is actually a scan problem or a capacity problem. When you do add a cache, invalidate it on the write. When you do add a region, say that a read there can be stale.

Based on: TAO, ATC 2013, Bronson and others.