Implement a Thread-Safe Pub-Sub Service
Problem: Implement a Thread-Safe Pub-Sub Service
Design and implement a simple server-side publish-subscribe system using Python's standard threading library. The system must support concurrent operations from multiple publishers and subscribers while ensuring thread safety.
Requirements:
- Concurrent Publishing: Multiple publisher threads must be able to publish messages concurrently without interference.
- Concurrent Subscribing: Multiple subscriber threads must be able to subscribe to and unsubscribe from topics concurrently.
- Subscription Management: A subscriber must be able to subscribe to a topic and later unsubscribe from it.
- Message Delivery: When a message is published to a specific topic, all currently subscribed subscribers for that topic must receive the message.
- Thread Safety: The implementation must rigorously avoid race conditions and deadlocks.
- Primitive Usage: Utilize Python threading primitives such as
Lock,RLock,Condition,Event, and thread-safe queues (queue.Queue) where appropriate to achieve thread safety and synchronization. - Discussion Points: Be prepared to discuss the chosen delivery semantics (e.g., at-most-once, at-least-once), the system's shutdown behavior, and strategies to prevent a single slow subscriber from blocking message delivery for all other subscribers or publishers.
AI interview prep
Core dilemma
Balancing robust thread safety and correct synchronization logic with system performance and responsiveness under high concurrency, especially concerning slow consumers.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement a Thread-Safe Pub-Sub Service
Problem: Implement a Thread-Safe Pub-Sub Service
Design and implement a simple server-side publish-subscribe system using Python's standard threading library. The system must support concurrent operations from multiple publishers and subscribers while ensuring thread safety.
Requirements:
- Concurrent Publishing: Multiple publisher threads must be able to publish messages concurrently without interference.
- Concurrent Subscribing: Multiple subscriber threads must be able to subscribe to and unsubscribe from topics concurrently.
- Subscription Management: A subscriber must be able to subscribe to a topic and later unsubscribe from it.
- Message Delivery: When a message is published to a specific topic, all currently subscribed subscribers for that topic must receive the message.
- Thread Safety: The implementation must rigorously avoid race conditions and deadlocks.
- Primitive Usage: Utilize Python threading primitives such as
Lock,RLock,Condition,Event, and thread-safe queues (queue.Queue) where appropriate to achieve thread safety and synchronization. - Discussion Points: Be prepared to discuss the chosen delivery semantics (e.g., at-most-once, at-least-once), the system's shutdown behavior, and strategies to prevent a single slow subscriber from blocking message delivery for all other subscribers or publishers.