Posting tweets
Open a user page and see their posts.
Possibility to react and answer to a post
Subscription to an account.
Home feed.
Low latency
Availability
Scalability
20 million daily active users = 100 million tweets/day
200 million daily passive users = read-only
1 billion total users
A globally read-heavy structure mainly.
The user id is always passed implicitly as an argument.
GET /users/id/posts ?limit=10&cursor=...
-> in reverse chronological order
GET /feed ?limit=10&cursor=...
POST /tweets {"content":"..."; "parent_id":...}
DELETE /tweets/id
POST /tweets/id/like
DELETE /tweets/id/like
POST /users/user_id/follow
DELETE /users/user_id/follow
GET /subscriptions
cf diagram.
Client connects to server, which fetches information from the database with the API described above.
When a new tweet is posted, the user calls the API endpoint POST /tweets.
The table posts is updated with a new line including author id and, if relevant, the parent post. Some metadata (e.g, time of posting) is added.
When a user subscribes to someone, the user calls the API endpoint POST /users/user_id/follow. A row is added to the table subscription, including the id of the user the person is now following, and the id of the person himself.
When someone opens the app, the feed is automatically queried through the endpoint GET /feed
To process that, the server first fetches the subscriptions of someone by querying subscription table (being indexed on both columns for fast read).
Once the subscriptions are queried, the last post of each person is fetched and returned to the client
SQL
User table
user_id; username; creation_date; nb_followers; nb_posts
Posts table
post_id; author (~user_id); content; nb_likes; parent_post; enclosed
Subscription
account; follower
(there can be several times the same value in each column, but no duplicate rows)
Storing post parent-child relationship as tree in NoSQL
If files enclosed to the post, a link in posts.enclosed column refers to the actual file stored in an AWS S3 bucket.
I don't really see how to store likes per post in a way that's efficient to retrieve
Let's use a multi-service architecture
User service
Tweet service
User service:
Tweet service