Bird
Raised Fist0
HLDsystem_design~7 mins

One-to-one messaging in HLD - System Design Guide

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Problem Statement
When two users want to exchange messages directly, a naive system can cause message loss, delays, or inconsistent message order. Without a dedicated mechanism, messages may not be delivered reliably or in the correct sequence, leading to poor user experience and confusion.
Solution
One-to-one messaging systems ensure that messages between two users are delivered reliably and in order by maintaining a dedicated communication channel or session. The system stores messages persistently, manages delivery acknowledgments, and orders messages before presenting them to users, ensuring a seamless and consistent conversation flow.
Architecture
User A
(Sender)
Messaging
User B
(Sender)

This diagram shows two users exchanging messages through a messaging server that stores and forwards messages to ensure reliable and ordered delivery.

Trade-offs
✓ Pros
Ensures reliable delivery of messages even if the receiver is offline.
Maintains message order for a coherent conversation experience.
Supports message persistence for history and synchronization across devices.
Enables real-time or near-real-time communication.
✗ Cons
Requires server-side storage and management, increasing infrastructure complexity.
Needs mechanisms for handling offline users and message retries.
Scaling to millions of users requires careful design to avoid bottlenecks.
Use when building chat applications or systems where direct, reliable, and ordered communication between two users is required, especially at scales above thousands of concurrent users.
Avoid if the system only requires simple notifications or one-way messaging without strict ordering or reliability guarantees, or if the user base is very small (under 100 concurrent users) where simpler methods suffice.
Real World Examples
WhatsApp
Uses one-to-one messaging with end-to-end encryption to ensure reliable, ordered delivery of messages between users even when offline.
Facebook Messenger
Implements one-to-one messaging with message storage and delivery receipts to provide seamless chat experiences across devices.
Slack
Supports direct messages between users with message persistence and ordering to maintain conversation context.
Alternatives
Broadcast Messaging
Sends messages to multiple recipients simultaneously rather than a single user.
Use when: Use when messages need to be sent to groups or all users, not just one-to-one.
One-way Notification
Delivers messages without expecting replies or maintaining conversation state.
Use when: Use for alerts or notifications where message ordering and persistence are less critical.
Summary
One-to-one messaging systems prevent message loss and disorder by managing delivery through a server.
They store messages persistently and ensure ordered delivery even if users are offline.
This pattern is essential for reliable, real-time chat applications between two users.

Practice

(1/5)
1. What is the primary role of the server in a one-to-one messaging system?
easy
A. To allow users to send messages directly without any intermediary
B. To store all messages permanently without delivering them
C. To receive messages from the sender and deliver them to the receiver
D. To broadcast messages to all users in the system

Solution

  1. Step 1: Understand message flow in one-to-one messaging

    Messages are sent from one user to another through a server acting as an intermediary.
  2. Step 2: Identify server's role

    The server receives messages from the sender and forwards them to the intended receiver, ensuring delivery and possibly storing temporarily.
  3. Final Answer:

    To receive messages from the sender and deliver them to the receiver -> Option C
  4. Quick Check:

    Server acts as message relay = B [OK]
Hint: Server relays messages between users, not direct connection [OK]
Common Mistakes:
  • Thinking users connect directly without server
  • Assuming server only stores without forwarding
  • Confusing one-to-one with broadcast messaging
2. Which component is essential to ensure message privacy in a one-to-one messaging system?
easy
A. End-to-end encryption
B. Message queue
C. Load balancer
D. Caching layer

Solution

  1. Step 1: Identify privacy needs in messaging

    Privacy means only sender and receiver can read the message content.
  2. Step 2: Match privacy solution

    End-to-end encryption encrypts messages so only sender and receiver can decrypt them, ensuring privacy.
  3. Final Answer:

    End-to-end encryption -> Option A
  4. Quick Check:

    Privacy needs encryption = C [OK]
Hint: Privacy means encrypt messages end-to-end [OK]
Common Mistakes:
  • Confusing load balancer as privacy tool
  • Thinking caching secures messages
  • Assuming message queue provides privacy
3. Consider this simplified message flow code snippet for one-to-one messaging:
def send_message(sender, receiver, message):
    server.store_message(sender, receiver, message)
    server.deliver_message(receiver)

send_message('Alice', 'Bob', 'Hello')
What is the expected output or result of this code?
medium
A. Syntax error due to missing parameters
B. Message is delivered to Alice instead of Bob
C. Message is stored but never delivered
D. Message 'Hello' is stored and delivered to Bob

Solution

  1. Step 1: Analyze function calls

    The function stores the message from Alice to Bob, then delivers it to Bob.
  2. Step 2: Check message flow correctness

    Message is stored correctly and delivery is triggered for Bob, the intended receiver.
  3. Final Answer:

    Message 'Hello' is stored and delivered to Bob -> Option D
  4. Quick Check:

    Store then deliver to receiver = D [OK]
Hint: Store then deliver to receiver means message sent correctly [OK]
Common Mistakes:
  • Confusing sender and receiver in delivery
  • Assuming message is not delivered after storing
  • Thinking code has syntax errors
4. In a one-to-one messaging system, this code snippet causes messages to never reach the receiver:
def deliver_message(receiver, message):
    if receiver.is_online == False:
        return
    send_to_client(receiver, message)
What is the main issue causing message delivery failure?
medium
A. The function sends messages to the wrong user
B. Messages are dropped if receiver is offline without queuing
C. There is a syntax error in the if condition
D. The message variable is undefined

Solution

  1. Step 1: Understand delivery condition

    The function returns early if the receiver is offline, skipping delivery.
  2. Step 2: Identify missing handling

    Messages are not queued or stored for later delivery, so offline users never get messages.
  3. Final Answer:

    Messages are dropped if receiver is offline without queuing -> Option B
  4. Quick Check:

    Offline receiver drops messages = A [OK]
Hint: Offline users need message queue to avoid drops [OK]
Common Mistakes:
  • Thinking syntax error causes failure
  • Assuming wrong user receives message
  • Assuming offline messages are automatically queued
5. You are designing a scalable one-to-one messaging system for millions of users. Which approach best ensures message delivery even if the receiver is offline?
hard
A. Use a message queue to store undelivered messages and retry delivery when receiver is online
B. Drop messages if receiver is offline to save storage and bandwidth
C. Broadcast messages to all users and let receiver filter them
D. Send messages only when both users are online simultaneously

Solution

  1. Step 1: Understand offline delivery challenge

    Receivers may be offline, so messages must be stored to avoid loss.
  2. Step 2: Choose scalable solution

    Message queues store undelivered messages and retry delivery when receiver reconnects, ensuring reliability and scalability.
  3. Final Answer:

    Use a message queue to store undelivered messages and retry delivery when receiver is online -> Option A
  4. Quick Check:

    Queue undelivered messages for offline users = A [OK]
Hint: Queue messages for offline users to ensure delivery [OK]
Common Mistakes:
  • Dropping messages loses data
  • Broadcast wastes resources and breaks privacy
  • Requiring both online limits usability