Bird
0
0
LLDsystem_design~25 mins

Mediator pattern in LLD - System Design Exercise

Choose your learning style9 modes available
Design: Mediator Pattern Implementation
Design the mediator pattern structure and communication flow between components. Out of scope: persistence, distributed system scaling, UI design.
Functional Requirements
FR1: Allow multiple components (colleagues) to communicate without knowing each other directly
FR2: Centralize communication logic in a mediator component
FR3: Support adding new components without changing existing ones
FR4: Ensure components can send and receive messages via the mediator
FR5: Handle message routing and coordination between components
Non-Functional Requirements
NFR1: System should support up to 100 concurrent components
NFR2: Message delivery latency should be under 50ms
NFR3: Mediator should be a single point of communication but avoid becoming a bottleneck
NFR4: System should be easy to extend with new component types
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
Mediator interface and concrete mediator implementation
Colleague components that communicate via the mediator
Message or event objects passed between components
Registration mechanism for components
Communication protocol (method calls, events, callbacks)
Design Patterns
Observer pattern for event notification
Command pattern for encapsulating requests
Singleton pattern if mediator should be unique
Dependency injection for mediator and colleagues
Reference Architecture
          +-------------------+
          |    Mediator       |
          |  (central hub)    |
          +---------+---------+
                    |
    +---------------+---------------+
    |               |               |
+---+---+       +---+---+       +---+---+
|Comp A |       |Comp B |       |Comp C |
+-------+       +-------+       +-------+
Components
Mediator Interface
Abstract class or interface
Defines methods for communication between components
Concrete Mediator
Class implementing Mediator Interface
Coordinates communication and message routing between components
Colleague Components
Classes implementing a common interface
Send and receive messages only via the mediator
Message/Event Objects
Simple data structures or classes
Encapsulate information passed between components
Registration Mechanism
Methods in mediator
Allow components to register and deregister themselves
Request Flow
1. 1. Component A sends a message to the mediator.
2. 2. Mediator receives the message and determines target components.
3. 3. Mediator forwards the message to Component B and/or Component C.
4. 4. Components B and C process the message and optionally respond via mediator.
5. 5. Mediator routes any responses back to the original sender or other components.
Database Schema
Not applicable as this is an in-memory communication pattern without persistent storage.
Scaling Discussion
Bottlenecks
Mediator becomes a single point of failure and performance bottleneck as number of components grows
High message volume can cause latency increase
Complex routing logic can slow down message delivery
Solutions
Implement mediator as a distributed or replicated service to avoid single point of failure
Use asynchronous message queues inside mediator to handle high volume
Optimize routing logic with efficient data structures or indexing
Partition components into groups with separate mediators to reduce load
Interview Tips
Time: Spend 10 minutes understanding requirements and clarifying communication needs, 20 minutes designing components and data flow, 10 minutes discussing scaling and trade-offs, 5 minutes summarizing.
Explain how mediator decouples components to reduce dependencies
Describe component registration and message routing clearly
Discuss pros and cons of centralized communication
Mention how to handle scaling and fault tolerance
Use simple examples to illustrate message flow