Skip to main content

FAANG Interview Questions: Designing a Scalable Social Media Platform: A Twitter-like System

Introduction:

Designing a social media platform like Twitter presents unique challenges due to the massive amounts of data and the need to scale for millions or billions of users. In this blog post, we will walk you through the process of designing a Twitter-like system, focusing on key aspects such as data modeling, data storage, feed generation, and search functionality. We will start with a high-level architecture and gradually delve into more low-level details, discussing potential complexities and trade-offs along the way.


High-Level Architecture


High Level Architecture

  1. Client: Web and mobile applications for users to interact with the platform.

  2. API Gateway: A single entry point to the system, which routes requests to appropriate backend services.

  3. Backend Services: Microservices responsible for various functionalities such as user management, tweets, and timeline generation.

  4. Data Storage: A combination of databases and storage systems to manage user data, tweets, and other content.

  5. Cache: A caching layer to reduce latency and improve performance.

  6. Search: A search engine to allow users to search for content efficiently.


Data Modeling





Entity

Fields

User

user ID, username, email, ...

Tweet

tweet ID, user ID, content, ...

Follower

source user ID, target user ID


Data Storage


  1. Relational Database (e.g., MySQL): Stores user and follower data, with tables for users and follower relationships. This choice allows for efficient querying of user data and follower relationships.

  2. Distributed Database (e.g., Cassandra): Stores tweet data, with a partition key based on user ID and clustering key based on tweet creation timestamp. This choice ensures efficient writes and reads for high write and read throughput requirements.   



Feed Generation




  1. Fan-out on Write: Whenever a user posts a tweet, the tweet is immediately pushed to the timelines of all their followers. This approach provides low latency in reading tweets but may lead to high write costs for popular users with many followers.

  2. Fan-out on Read: When a user requests their timeline, the system fetches tweets from the people they follow, merges them, and sorts them by creation timestamp. This approach reduces write costs but increases read latency.

 

A hybrid approach can be used to balance the trade-offs between these two strategies, combining fan-out on write for active users and fan-out on read for less active users.


Caching

       

  1. In-memory Cache (e.g., Redis or Memcached): Cache frequently accessed data like user profiles, tweets, and timelines to reduce latency and improve performance. Implement an eviction policy like LRU (Least Recently Used) to remove the least recently accessed items from the cache when it reaches capacity.


  1. Content Delivery Network (CDN): Cache static content like images and media files on edge servers closer to the users to reduce latency and network load.



Search Functionality


  1. Inverted Index: Use an inverted index to map terms to tweets containing those terms. This allows for efficient full-text search.


  1. Search Engine (e.g., Elasticsearch or Solr): Deploy a search engine to index and search tweets efficiently. These engines support features like relevance ranking, faceting, and filtering.


Feature

Elasticsearch

Solr

Relevance Ranking

Yes

Yes

Faceting

Yes

Yes

Filtering

Yes

Yes

Distributed

Yes

Yes

Real-time Indexing

Yes

Yes



Scaling and Performance Optimization


  1. Horizontal Scaling: Design the system to scale horizontally by adding more instances of services and databases as needed. This helps manage the growth of users, data, and traffic.


  1. Sharding: Partition of the data in distributed databases (e.g., Cassandra) to spread the load across multiple nodes, improving performance and fault tolerance.


  1. Load Balancing: Use load balancers to distribute incoming traffic evenly across backend service instances, preventing any single instance from becoming a bottleneck.


  1. Caching: Implement multiple layers of caching, including in-memory caches and CDNs, to reduce latency and improve performance.



Number of Users (in millions)

Latency (ms)

Throughput (req/s)

1

20

2000

10

22

18000

100

25

170000

1000

30

1600000


Conclusion:

Designing a scalable social media platform like Twitter requires careful consideration of various aspects, including data modeling, storage, feed generation, caching, and search functionality. By starting with a high-level architecture and gradually breaking it down into more detailed components, we can address the complexities and trade-offs involved in creating a system that can handle massive amounts of data and scale to accommodate millions or billions of users.



Comments

Some of My Bests

ডাটা স্ট্রাকচার- স্ট্যাক (Stack)

Programming is all about data manipulation. Data structure is way of storing data for further manipulation. ডাটা স্ট্রাকচার আমাদেরকে বিভিন্ন ডাটা সাজিয়ে রাখার ব্যবস্থা করে দেয়। ডাটা সাজিয়ে রাখার অনেক গুলো "তরিকা" আছে। কোনকিছু আমরা কেন সাজিয়ে রাখি? যেন পরে নির্দিষ্ট একটা ডাটা সহজে খুঁজে পেতে পারি। "তরিকা" গুলোর নাম Array, Stack, Queue, Linked List, Tree, Graph. এগুলা শ খানেক ডাটা স্ট্রাকচারের মধ্যে কিছুর নাম, যেগুলো অনেক বেশি ব্যবহার হয়। এই পোস্টটা স্ট্যাক বুঝানোর জন্য। স্ট্যাক খুবই ইন্টেরেস্টিং একটা ডাটা স্ট্রাকচার। অনেক কারনেই এটা ব্যাবহার হয়। আগে স্ট্যাক কনসেপ্টটা নিয়ে আলোচনা করা যাক। স্ট্যাক বলতে বোঝায় একটার উপর একটা সাজায়ে রাখা। বিয়ের বাড়িতে আগে মেলামাইনের গ্লাস দেখা যেত একটার ভেতর আরেকটা ঢুকায়ে লম্বা একটা পাইল তৈরি করে একসাথে ক্যারি করা হচ্ছে। এটা একটা স্ট্যাক। প্রোগ্রামিং এর ভাষায় স্ট্যাক এক্সাক্টলি সেইম জিনিসই। তবে একটু ঘষামাজা আছে। আরেকটা উদাহরণ দেয়া যেতে পারে। বয়ামের ভেতর একটার পর আরেকটা বিস্কিট ঢুকিয়ে রাখা হয়। হ্যা, এইটা পারফেক্ট উদাহরণ হয়েছে। এই

ডাটা স্ট্রাকচার- কিউ (Queue)

Programming is all about data manipulation. Data structure is way of storing data for further manipulation. ডাটা স্ট্রাকচার আমাদেরকে বিভিন্ন ডাটা সাজিয়ে রাখার ব্যবস্থা করে দেয়। ডাটা সাজিয়ে রাখার অনেক গুলো "তরিকা" আছে। কোনকিছু আমরা কেন সাজিয়ে রাখি? যেন পরে নির্দিষ্ট একটা ডাটা সহজে খুঁজে পেতে পারি। "তরিকা" গুলোর নাম Array, Stack, Queue, Linked List, Tree, Graph. এগুলা শ খানেক ডাটা স্ট্রাকচারের মধ্যে কিছুর নাম, যেগুলো অনেক বেশি ব্যবহার হয়। এই পোস্টটা কিউ বুঝানোর জন্য। কিউ জিনিসটার সাথে আমরা সবাই পরিচিত। জীবনে আমরা সবাই কখনো না কখনো লাইনে দাঁড়ায়ছি। কিউ এর বেসিক ক্যারেক্টারিস্টিকসের সাথে মিলিয়েই প্রোগ্রামিং এ কিউ এর কনসেপ্ট। বাস্তব জীবনে একটা কিউ তে কি হয়? সবাই লাইন ধরে দাঁড়ায় কিছু একটা কারনে। যে সবার আগে দাঁড়ায় সেই সবার আগে কার্জসিদ্ধি করে। সবার পরের জন সবার পরে। স্ট্যাকের ক্ষেত্রে আমরা পড়েছিলাম Last In First Out (LIFO) or First In Last Out (FILO)। কেমন আনফেয়ার শোনায় না? সবার পরে আসবে, আবার সবার আগে চলে যাবে। অ্যাটলিস্ট আমার আনফেয়ার লেগেছিলো যখন স্ট্যাক শিখছিল

প্রোগ্রামিং এর সংগে ঠাট্টা

প্রথমে কিছু কাটখোট্টা সেশন দিয়ে শুরু করা যাক। কিছু কমন প্রশ্ন এবং এই গোবেচারার কিছু উত্তর। ১ . প্রোগ্রামিং কি? খায় না মাথায় দেয়?        সোজা বাংলায় বলতে গেলে প্রোগ্রামিং হলো কম্পিউটারকে আদেশ দিয়ে কিছু কাজ করানো। মানে কি?     মানে হলো কম্পিউটার হলো একটা বোকা বাক্স। ক্ষেত্রবিশেষে এদের ক্ষমতা মানুষের চেয়েও বেশী থাকলেও এর অনেক বড় সীমাবধ্যতা হলো এরা নিজে থেকে কিছু করতে পারেনা। এই দাস তাই করবে যা তার মনিব তাকে আদেশ দিবে। সমস্যা হলো কম্পিউটার মানুষের ভাষা বুঝেনা। এই সমস্যার সমাধান হলো প্রোগ্রামিং। মানুষ তার নিজের ভাষায় কম্পিউটারের জন্য কিছু আদেশ লিখে দিবে, কম্পিউটার সেটাকে কোন এক ভাবে নিজের ভাষায় ট্রান্সলেট করে নিবে। ব্যাস...কাজ শেষ। কম্পিউটার সেই আদশ পুরন করে ফেলবে। ২. প্রোগ্রামিং করে লাভ কি? কম্পিউটারকে কতটুকু নিয়ন্ত্রণ করা যায়?     যেমনটা আগেই বলা হয়েছে। কম্পিউটারকে আদেশ না দিলে কিছুই করতে পারবেনা। তাই এতটুকু বুঝতেই পারছেন যে একটা প্রোগ্রাম বানিয়ে আপনি পুরো কম্পিউটারটাকেই নিয়ন্ত্রণ করতে পারবেন। আসলে আপনি কম্পিউটারে যা কিছু দেখছেন( গেমস, ওয়েব ব্রাউজার, মিডিয়া প্লেয়ার ইত্যাদি ),