News Feed (Twitter, X)
This is a basic outline of some of the core tables that could be included in a newsfeed data model.
Given the interrelated nature of the data, a GraphQL API could be a good solution for this system. More specifically a GraphQL API would allow the client to determine which fields to fetch and prevent the over fetching of data which could lead to slower response times and an overall worse UX.
Here is an example GraphQL query for a feed:
Similarly we could also use a classic RESTful API to interact with the data. RESTful APIs are simple, widely used, stateless, and support caching which make it a good candidate for our system.
Our REST API will comprise of two main endpoints:
In our system we are going to use the fanout method to construct the newsfeeds. Fanout is the process of distributing a message or content update to all the subscribers of a particular feed.
Two main fanout strategies include:
In this approach when a new piece of content is published (e.g. tweet), that content is then pushed to all the user's followers' newsfeeds cache, so that a user's newsfeed is precomputed. This makes reads very fast as a user's newsfeed has been precomputed before they make a request.
In this approach, instead of pushing content to a user's newsfeed cache on write, the system waits until a user requests (pulls) for their newsfeed and then computes the newsfeed on read.