Implement Pub/Sub System
NetflixTechnicalDifficulty: Medium
Share on
Ready to answer it out loud?
Run a mock interview on this exact question and get instant AI feedback.
Question Explain
Design and implement a Publisher/Subscriber (Pub/Sub) messaging system.
Requirements:
- Support multiple topics
- Allow multiple subscribers per topic
- Ensure message delivery
- Handle subscriber failure
Example: publisher.publish("topic1", "message"); subscriber1.subscribe("topic1"); subscriber2.subscribe("topic1");
Answer Example
Publisher/Subscriber System Design:
-
Core Components:
- Message Broker
- Topic Manager
- Subscriber Manager
- Message Queue
-
Implementation:
interface Publisher {
publish(topic: string, message: Message): void;
}
interface Subscriber {
subscribe(topic: string): void;
unsubscribe(topic: string): void;
receive(message: Message): void;
}
class MessageBroker {
private topics: Map<string, Set<Subscriber>>;
private messageQueue: Queue<Message>;
async publish(topic: string, message: Message): Promise<void> {
const subscribers = this.topics.get(topic);
for (const subscriber of subscribers) {
await this.deliverMessage(subscriber, message);
}
}
private async deliverMessage(subscriber: Subscriber, message: Message): Promise<void> {
try {
await subscriber.receive(message);
} catch (error) {
await this.handleDeliveryFailure(subscriber, message);
}
}
}
-
Features:
- Message persistence
- At-least-once delivery
- Dead letter queue
- Message ordering
- Back-pressure handling
-
Scalability:
- Partitioned topics
- Consumer groups
- Load balancing
- Fault tolerance
Company Context (Netflix):
- High availability
- Eventual consistency
- Failure recovery
- Performance monitoring
Related Interview Questions
Knowledge of storage architectures and types
AmazonMedium
Merge Two Sorted ArraysGoogleEasy
Basic String ManipulationAdobe
Investment Portfolio OptimizationGoldman SachsMedium
How would you implement a custom authentication mechanism for a new AEM feature while adhering to AEM's security best practices?PwCHard
Can you share an example of using innovative problem-solving skills to overcome a major work challenge?GoogleHard
What strategies help you solve complex technical problems under pressure?eBayHard
Can you describe a situation where you used your problem-solving skills to overcome a technical challenge?TwitterMedium