DEV Community

Nishchya Verma
Nishchya Verma

Posted on

System Design - Twitter X

Ever wondered how social platforms like Twitter manage millions of tweets, likes, and comments in real time? I did too—so I decided to design one from scratch. In this post, I’ll take you through the architecture, APIs, and decisions involved in creating a scalable microservices-based Twitter clone.

Functional Requirements:

  1. Create an account and login,
  2. Create edit delete tweets
  3. Follow other users
  4. View a timeline of tweets for following
  5. Like or reply to tweets
  6. Search for tweets

Non functional Requirements:

  1. availability - system should be available
  2. scalability - should be able to handle users as users, tweets created, etc increases
  3. durability - Tweets created should not be lost

Core Entities:

  1. User : {user_id. user_name, user_metadata, password}
  2. Tweet: {tweet_id, user_id. content, created_at, edited_at, edited, orignal_content}
  3. Comment: {tweet_id, comment_content, user_id, timestamp}
  4. Followers: {user_id, following_user_id}
  5. Likes: {tweet_id, user_id}

APIs:

SignIn/SignUP

- [GET] /auth/username-availability?username=<value>    -check if user already exist
- [POST] /auth/user/signup {user_name, user_metatData, user_credetials, email}
- [POST] /auth/login {user_name, email, credentails} 
- [POST] /auth/verify-otp {user_name, otp}
Enter fullscreen mode Exit fullscreen mode

Tweets

- [GET] /tweets?sort=latest ----Return top recent tweets
- [GET] /tweets/following?sort=latest&page=1&limit=10 ----Return top recent tweets by following
- [GET] /users/{user_id}/tweets?page=1&limit=10 ---- Return all recent tweets by user [support pagination] if some users userid
 passed it return for that user otherwise for the user himself. 
- [POST] /tweets/search {keywords []}
- [GET] /tweets/{tweet_id} – fetch tweet by ID
- [DELETE] /tweets/{tweet_id} – for user delete 
Enter fullscreen mode Exit fullscreen mode

Comments/Likes

-[POST] /tweets/{tweet_id}/comments {user_id, content, tweet_id}
-[GET] /tweets/{tweet_id}/comments
-[GET] /tweets/{tweet_id}/likes
-[POST] /tweets/{tweet_id}/likes
-[DELETE] /tweets/{tweet_id}/likes
Enter fullscreen mode Exit fullscreen mode

Search

- [POST] /search/tweets { keywords: [], tags: [] }
- [GET] /tags/trending – trending topics
Enter fullscreen mode Exit fullscreen mode

Follow

[POST] /users/{user_id}/follow
[DELETE] /users/{user_id}/follow
[GET] /users/{user_id}/followers
[GET] /users/{user_id}/following
Enter fullscreen mode Exit fullscreen mode

DIAGRAM:

High Level Design Twitter

Suggestions for Further Enhancement

1. Timeline Service: Push vs Pull Strategy

  • Push Model (Fan-out-on-write): Pre-generate user timelines when someone posts a tweet.
  • Pull Model: Fetch tweets in real-time from users a person follows. You can start with Pull (easier), and switch to Push for high-scale active users (e.g., celebrities).

2. Likes & Comments as Microservices
Not required now, but eventually split LikesService, CommentService to handle independent scaling.
Add rate limiting on likes/comments to avoid spam or abuse.

3. Notifications Service (Future)
For follows, likes, replies.
Async (Kafka-based) + push (WebSocket or polling)

4. Moderation / Flagging
Add a moderation flag or queue to enable content reporting.
Optional ML service for spam/inappropriate content.

Top comments (0)