# Design a news feed

A news feed is your social network's "For You" page. It displays your friends' updates.

The *System Design Interview* book provides a straightforward solution without many details. Hello Interview's version provides more depth.

### Scope

A mobile/web interface that supports posting and retrieving media-rich posts from up to 5,000 friends in reverse chronological order.

### APIs

1.  POST /v1/me/feed
    
2.  GET /v1/me/feed
    

### Pull / Push

Updates will be in the form of fan-outs, where a single *action* triggers a waterfall of updates. The trigger can be either on read (GET) or on write (POST).

Pull (read) updates are resource-conservative but can potentially be slow. Push (write) updates will be resource-heavy, but reads will be fast and always up to date. Given there are always more reads than writes, push could be the way to go. It also avoids the sluggish refresh on pull waterfall of queries to get your friend's updates.

The book uses a complicated system: a graph DB with a multi-tiered caching scheme and a hybrid pull/push approach.

The Hello Interview version that follows uses an elegant DynamoDB design to solve the same problem. No graph DB, just two tables and two GSIs.

### DynamoDB

DynamoDB is Amazon's serverless NoSQL datastore that lets you store documents by a partition key (PK) and a sort key (SK). You can vary the sort key with local secondary indexes (LSIs). You can also vary the partition key of your data with global secondary indexes (GSIs).

[![Posts and Follows DynamoDB tables with GSIs](https://mermaid.ink/img/pako:eNqNU1tv2jAU_ivWeVolQIkDpPit29oJrRJVs6rqFAlZ-BSyEjuyHbUd8N9nJ4RmNKzzky_fzcfHG1gogcAA9deMLzXPU0ncuEsub8mmnvthrM7kkpQG9VSQm-_vTiTPsd7cpbKe3MySH0mHSKGMrURICgXXNrOZkuQJX1M4ZXjlsby0K6U7QAuN3KK4sA40TWbEZjkay_OiC6ukRWm7os4_P8z9veffkum_7u5cPOKj6O1UHm-UtiegdUWOMl3Nrq9n910FfFTrtXp-S_NRkj0eHT6F4xhHfpe3yYn7t1T-uwatpN1FOLhXDbfd9vtqs28cRp51ZtG8P28qw5xo7WDIJ25abmeN_iladkCLNhVb1DqF5263HQ3iVQqtfuHCPTLJpFUNsfHZU_-uaxcNerDUmQBmdYk9yFHn3C-heoQU7Ard5wJP1SjKl77g-qm_UOv6N-wcv-Dyp1J5I6FVuVwBe-Rr41ZlIVwn7v_3AYJSoP6iSmmBhVFYaQDbwAswSseDSRSPRsEwDMY0pj14BRaHg5hG0ZBGNAjOh3Sy68HvyjQYnNNJEE4ojUdxFI_Huz9RWFWf?type=png align="center")](https://mermaid.live/edit#pako:eNqNU1tP2zAU_ivWeWJSW4WEJk3exgaoAqmIbEKgSJVVH1pDY0e2Iy5t_zt20pSspAM_-fLdfHy8gplkCAmg-s3pXNE8E8SOv-nZDVnVcze0UVzMSalRjRm5vvx0ImiO9eYmE_XkepL-STtECqlNJUIyKKgy3HApyBO-ZnDI8NxhaWkWUnWAZgqpQfbTWNA4nRDDc9SG5kUXVgqDwnRFnZ7eTd29pxfp-H93ty4O8VX0diqH11KZA9C6InuZzidXV5PbrgI-yOVSPn-k-SrJFo8Wn8F-jD2_s5v0wP1bKt-uQStpdxF27lXDrdf9vlxtGychz4ob1J_Pm8okVrR20OSI6pbbj0b_EI3v0KxNxRa1TuG463VHgziVQslHnNlHJlwY2RAbny3137p20aAHc8UZJEaV2IMcVU7dEqpHyMAs0H4ucFSFrHzpM6qe-jO5rH_DxvILKu6lzBsJJcv5ApIHutR2VRbMduL2f-8gKBiqX7IUBpKTsJKAZAUvkBx7o0EcRMNg5IWxdzwKox682m0_HkR-EARxHHnD0AuGmx68Va7eYORbZOz70TAKojDcvAOu0VX5)

We have a Posts table with a PK of postId along with userId. We have a GSI on Posts with userId as the PK and createdAt as the sort key.

Similarly, we maintain user follow lists in a Follows table and the reverse (followers) in its GSI.

For a given user, we first request all of the users they follow from the Follows table. Then we request all the posts from those N users from the Posts table via its GSI. Then we sort all of those posts by timestamp and return the results to the user!

This is the gist, but the article goes into various deep dives using caches and precomputed feeds to deal with uneven reads and writes.

It's definitely a worthwhile read.

Happy Hackin'!

### References

*   Facebook news feed via [Hellointerview.com](https://www.hellointerview.com/learn/system-design/problem-breakdowns/fb-news-feed)
    
*   [AWS: DynamoDB global secondary indexes](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html)
    

* * *

> This article is part of the [system design series](https://blog.danwald.me/series/system-design) where I am summarizing chapters from The System Design Interview: [Volume 1](https://www.amazon.ae/Independently-Published-System-Design-Interview/dp/B08CMF2CQF) / [Volume 2](https://www.amazon.ae/System-Design-Interview-Insiders-Guide/dp/1736049119) amongst other related content
