Design a thread-safe event dispatcher
Problem: Thread-Safe Event Dispatcher
Design an in-memory event system that supports registering listeners and firing events.
Core Requirements
Implement an EventDispatcher (or EventBus) supporting the following operations:
subscribe(eventType, listener): Registers alistenercallback/function for a giveneventType.unsubscribe(eventType, listener): Deregisters a specificlistenerfrom aneventType.fire(eventType, eventPayload): Invokes all listeners currently subscribed to the specifiedeventType.
Follow-up: Multithreading Considerations
Assume subscribe, unsubscribe, and fire can be called concurrently from multiple threads. Discuss and answer the following:
- What thread-safety guarantees will your design provide? (e.g., "listeners registered before
firestarts must be called exactly once", handling concurrentunsubscribe, etc.) - How will you prevent race conditions and deadlocks?
- How will you manage performance trade-offs under high read (
fire) versus high write (subscribe/unsubscribe) workloads? - Will
firebe synchronous (executing on the caller's thread) or asynchronous (utilizing a thread pool or queue)? How does this choice impact your design?
You may state reasonable assumptions (e.g., whether per-event-type ordering is required; at-least-once vs. exactly-once delivery within a single process).
AI interview prep
Core dilemma
Balancing strict thread-safety and correctness for listener management and event delivery with high performance and low latency under concurrent `subscribe`, `unsubscribe`, and `fire` operations, especially when `fire` is a read-heavy operation.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Design a thread-safe event dispatcher
Problem: Thread-Safe Event Dispatcher
Design an in-memory event system that supports registering listeners and firing events.
Core Requirements
Implement an EventDispatcher (or EventBus) supporting the following operations:
subscribe(eventType, listener): Registers alistenercallback/function for a giveneventType.unsubscribe(eventType, listener): Deregisters a specificlistenerfrom aneventType.fire(eventType, eventPayload): Invokes all listeners currently subscribed to the specifiedeventType.
Follow-up: Multithreading Considerations
Assume subscribe, unsubscribe, and fire can be called concurrently from multiple threads. Discuss and answer the following:
- What thread-safety guarantees will your design provide? (e.g., "listeners registered before
firestarts must be called exactly once", handling concurrentunsubscribe, etc.) - How will you prevent race conditions and deadlocks?
- How will you manage performance trade-offs under high read (
fire) versus high write (subscribe/unsubscribe) workloads? - Will
firebe synchronous (executing on the caller's thread) or asynchronous (utilizing a thread pool or queue)? How does this choice impact your design?
You may state reasonable assumptions (e.g., whether per-event-type ordering is required; at-least-once vs. exactly-once delivery within a single process).