CSE Roadmap

Core tier · about 4 hours · 8 parts

Part 6 of 8 · 1 min read

Cache layers

What sits in memory.

The book divides the cache tier into five layers.

  • News feed: the ids of each user's feed.
  • Content: the data of every post. Popular posts live in a hot cache.
  • Social graph: who follows or befriends whom.
  • Action: whether a user liked, replied to or otherwise acted on a post.
  • Counters: likes, replies, followers and following.

Read path in detail. The feed service takes the id list from the news feed layer. It fetches each post from the content layer, each author from the user cache, and flags and counts from the action and counter layers. It returns one JSON answer. For a hybrid feed, it also pulls recent posts of the followed celebrities and merges them by time.

The merge rule is simple. Both lists are already newest first, so the service walks down both and keeps the newer id each time, until it has one page.

A worked example (made up). A page holds 20 posts. The reader follows 300 people, three of them celebrities. A read costs one lookup of the feed list and three lookups of celebrity post lists. That is 4 lookups plus hydration. Under pure pull it would be 300 lookups.

Read it as

The feed cache is a to-do list of ids. The content cache is the shelf where the actual posts sit. Hydration is walking along the list and picking each item from the shelf.

Check yourself: 1. Which layer holds like counts? 2. How are two newest-first lists merged? 3. Why does the hybrid read cost 4 lookups in the example?