The whole course ▾
System Design · Week 6 revision
Week 6 revision: Patterns
Patterns week in ten minutes: feeds, search, nearby, contention and big jobs, then six questions to test yourself.
New System Design videos go up at 6 pm. Subscribe on YouTube so you don't miss it.
Got a doubt? Ask in the comments under the video.Subscribe
Lesson 25Feeds and fan-out
This week was five patterns, each one hiding inside famous interview questions. It began with a feed of restaurants, and a pizza chain with five million followers.
Lesson twenty-five. Pull builds a feed when it's opened: cheap writes, expensive reads. Push builds it in advance: the restaurant app posts, the posts service drops a job on a fan-out queue, and fan-out workers add the post to each follower's list in a feed cache, so the customer app reads one list. But one celebrity post means millions of writes. So the hybrid: push for most accounts, and for huge ones, merge their recent posts in when the feed is read.
The interview line: hybrid fan-out, push into followers' feed caches through a queue, pull for accounts above a follower threshold, merged at read time, with cursor pagination. And say the threshold as a number. Inactive users get pull too, which saves a lot of memory.
Lesson 26Search
Then a customer typed paneer, and waited four seconds.
Lesson twenty-six, in three lines. LIKE percent paneer scans every row, so search engines use an inverted index: each word mapped to the dishes that contain it, and a query intersects the lists. They normalise every word: lowercase, stems, synonyms, and fuzzy matches for typos. And the index lives in Elasticsearch, synced from the database by change data capture, while the database stays the source of truth.
The interview line: search runs on an inverted index in Elasticsearch, sharded and replicated, fed by change data capture, ranked by text relevance plus business boosts like distance and popularity. And typeahead is a separate job: precomputed suggestions per prefix.
Lesson 27Nearby
Then an order was ready, and we needed the free riders within two kilometres.
Lesson twenty-seven, in three lines. An index helps with one range, not latitude and longitude together, so turn the map into cells: geohashes, quadtrees for uneven cities, or Uber's hexagonal H3. Search the target's cell and its eight neighbours, then filter by real distance. And riders who move every four seconds, seventy-five thousand updates a second, live in memory, in a Redis geo index.
The interview line: geohash or H3 cells in an in-memory index, search the cell and its neighbours, then filter and sort by real distance. And never search just one cell. Redis even has geo commands built in.
Lessons 28 and 29Contention, and long jobs
And to finish: two riders accepting one order, and two-gigabyte menu videos.
Lesson twenty-eight. Reading a value, deciding, then writing is a race: two riders both saw the order free. The fix is a conditional write, set the rider where the rider is still empty, or a version check, so exactly one request wins. Contention that lasts minutes, like a festival thali during checkout, uses holds that expire on their own. And if you truly need a distributed lock, give it a lease and a fencing token.
Lesson twenty-nine. Big uploads go straight to storage as multipart, resumable uploads, so a dropped connection costs one ten-megabyte part. Long work runs as jobs on a queue, each a record with a state and progress. And reliable jobs use heartbeats, checkpoints, time limits and retry limits. And the queue's timeout must outlast the job, or two workers do it at once.
What's nextNext week: case studies
Next week, every lesson is a famous system, designed from scratch with everything you know.
Design YouTube: upload, transcode, stream. Design Dropbox: files in sync on every device. Design a web crawler, politely, across billions of pages. Design search autocomplete, in under a hundred milliseconds. And design a notification system for push, text and email. Then Sunday's mock: Ticketmaster.
Test yourself
A customer follows 300 normal restaurants and 2 huge chains. How is their feed built?
Show the answer
Read their pushed list, then merge in the two chains' recent posts.
Why feed the search index with change data capture?
Show the answer
Every database change reaches the index, without the app writing twice.
Cells are 5 km wide, but you search 500 metres around a restaurant. What's the problem?
Show the answer
Too many riders per cell to filter: use smaller cells.
Give the one-line fix for two riders accepting the same order.
Show the answer
UPDATE orders SET rider = ? WHERE id = 421 AND rider IS NULL.
A customer holds a festival thali, then closes the app. What happens to it?
Show the answer
Nothing at first; when the hold expires, the thali goes back on offer.
A 2 GB upload drops at part 150 of 200. What does a resumable upload do?
Show the answer
Continue from part 150: the first 149 parts are already stored.
Lessons on weekdays, the week's revision on Saturday, a mock interview on Sunday.