Bird
Raised Fist0
HLDsystem_design~25 mins

Fan-out on write vs fan-out on read in HLD - Design Approaches Compared

Choose your learning style9 modes available
Design: Fan-out Messaging System
Design the message delivery mechanism focusing on fan-out strategies. Out of scope: message content creation, user authentication, and UI design.
Functional Requirements
FR1: Deliver messages from one sender to many receivers efficiently
FR2: Support real-time or near real-time message delivery
FR3: Handle up to 1 million users with 100,000 concurrent active users
FR4: Ensure message delivery latency under 200ms for 99th percentile
FR5: Allow users to read their message feeds on demand
Non-Functional Requirements
NFR1: System availability of 99.9% uptime
NFR2: Support horizontal scaling as user base grows
NFR3: Minimize storage and compute costs
NFR4: Handle spikes in message volume gracefully
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
Message queue or broker
Datastore for messages and user feeds
Cache layer for fast reads
Worker services for processing fan-out
API servers for client interaction
Design Patterns
Fan-out on write (push updates to receivers on message arrival)
Fan-out on read (store messages once, fan-out when receivers read)
Caching strategies
Batch processing for fan-out
Load balancing and rate limiting
Reference Architecture
Client
  |
  v
API Servers
  |
  v
Message Queue / Broker
  |
  +-------------------+
  |                   |
Fan-out Workers    Datastore (Messages, User Feeds)
  |                   |
  +-------------------+
          |
          v
       Cache
          |
          v
        Clients (Receivers)
Components
API Servers
REST/HTTP servers
Receive messages from senders and serve message feeds to receivers
Message Queue / Broker
Kafka or RabbitMQ
Buffer incoming messages and decouple producers from consumers
Fan-out Workers
Microservices
Process messages and distribute them to receivers depending on fan-out strategy
Datastore
NoSQL database (e.g., Cassandra, DynamoDB)
Store messages and user feeds for persistence
Cache
Redis or Memcached
Serve frequently accessed message feeds with low latency
Request Flow
1. Sender sends message to API Server
2. API Server publishes message to Message Queue
3. Fan-out Workers consume message from queue
4. For fan-out on write: Workers push message copies to each receiver's feed in Datastore
5. For fan-out on read: Workers store message once; feeds are generated on demand
6. Receivers request their feeds from API Server
7. API Server checks Cache; if miss, reads from Datastore
8. API Server returns messages to receivers
Database Schema
Entities: - User: user_id (PK), name - Message: message_id (PK), sender_id (FK User), content, timestamp - UserFeed (for fan-out on write): user_id (FK User), message_id (FK Message), read_status Relationships: - One User can send many Messages (1:N) - UserFeed stores many messages per user (1:N) For fan-out on read, UserFeed may be virtual or generated on demand by querying Messages from followed users.
Scaling Discussion
Bottlenecks
Fan-out on write: High write amplification when sender has many receivers
Fan-out on read: High read latency and compute when many receivers read simultaneously
Message Queue throughput limits
Datastore write/read capacity
Cache size and eviction policies
Solutions
Use batch writes and asynchronous processing to reduce write load in fan-out on write
Implement caching and pre-computed feeds to reduce read latency in fan-out on read
Partition message queue topics by sender or user groups to increase throughput
Use scalable NoSQL databases with horizontal partitioning
Employ multi-level caching and TTL strategies to optimize cache usage
Interview Tips
Time: Spend 10 minutes clarifying requirements and constraints, 15 minutes designing architecture and data flow, 10 minutes discussing scaling and trade-offs, 10 minutes answering questions.
Explain difference between fan-out on write and fan-out on read with examples
Discuss trade-offs in latency, storage, and compute costs
Describe components and their roles clearly
Address scaling challenges and mitigation strategies
Show understanding of real-world constraints and user behavior