Bird
0
0
LLDsystem_design~10 mins

Mediator pattern in LLD - Scalability & System Analysis

Choose your learning style9 modes available
Scalability Analysis - Mediator pattern
Growth Table: Mediator Pattern
Users / Components10010,0001,000,000100,000,000
Number of components communicating10-20100-2001,000-2,00010,000+
Messages per second through mediator100-5005,000-10,000100,000-200,00010M+
Mediator complexitySimple centralized logicModerate, some async handlingHigh, needs concurrency controlVery high, requires distributed mediator
Latency impactLowModerateHighVery high without partitioning
Single point of failure riskHighHighCriticalCritical
First Bottleneck

The mediator component becomes the first bottleneck as the number of components and messages grow. It must handle all communication, so CPU and memory usage spike. At medium scale (10,000+ components), the mediator's processing and message routing slows down, increasing latency. At large scale, the single mediator cannot handle the load, causing delays and failures.

Scaling Solutions
  • Horizontal scaling: Split the mediator into multiple instances, each handling a subset of components (partitioning). Use a directory or registry service to route messages to the correct mediator.
  • Asynchronous messaging: Use message queues or event buses to decouple senders and receivers, reducing blocking and improving throughput.
  • Caching: Cache frequent communication patterns or results to reduce mediator processing.
  • Load balancing: Distribute incoming messages evenly across mediator instances.
  • Sharding: Partition components by domain or function to reduce mediator load.
  • Failover and redundancy: Use multiple mediator instances with health checks to avoid single points of failure.
Back-of-Envelope Cost Analysis
  • At 10,000 components sending 10 messages/sec each, mediator handles ~100,000 messages/sec.
  • Assuming 1 KB per message, bandwidth needed is ~100 MB/s (800 Mbps).
  • CPU load depends on message processing complexity; expect multiple cores or distributed mediators.
  • Memory needed depends on message queue sizes and state; estimate several GBs for buffering.
  • Storage for logs or message history grows with message volume; plan for scalable storage solutions.
Interview Tip

Start by explaining the mediator pattern's role in centralizing communication. Discuss how it simplifies component interactions but can become a bottleneck as scale grows. Then, outline how you would identify bottlenecks (CPU, memory, latency) and propose scaling strategies like partitioning the mediator, asynchronous messaging, and load balancing. Always connect your solutions to the specific bottleneck you identified.

Self Check Question

Your mediator handles 1,000 messages per second. Traffic grows 10x. What do you do first and why?

Answer: The mediator is the bottleneck. First, partition the mediator into multiple instances to distribute the load. This reduces CPU and memory pressure on a single mediator and improves throughput.

Key Result
The mediator pattern centralizes communication but becomes a bottleneck as message volume grows; partitioning and asynchronous messaging are key to scaling.