Design Patterns interview question · Most asked
What is the difference between Observer and Pub/Sub?
Summary
In Observer, the subject holds direct references to its observers and usually notifies them synchronously in the same process. In Pub/Sub, publishers and subscribers only know a broker and topic names; delivery is typically asynchronous, can cross processes and services, and failures are isolated with retries and dead-letter queues.
Longer Answer
Observer is simpler and immediate (UI events, in-process domain events). Pub/Sub decouples further and scales across services (SNS/SQS, Kafka, RabbitMQ, Google Pub/Sub), at the cost of eventual consistency, duplicate deliveries (subscribers must be idempotent) and harder debugging.
Key Points
- Observer: direct references, synchronous, in-process.
- Pub/Sub: broker and topics, asynchronous.
- Pub/Sub isolates failures but brings eventual consistency.
- Make Pub/Sub subscribers idempotent.
Deep dive: Design Patterns, Explained Simply: Pub/Sub and the Event Bus
Explanation
What interviewers look for
Coupling, synchronicity and failure-isolation differences.
More most-asked Design Patterns questions
- What problem does the Builder pattern solve? Show an example.
- Explain the SOLID principles.
- What is a design pattern, and why are they useful?
- What is the difference between dependency inversion, dependency injection and inversion of control?
- Why is composition often preferred over inheritance?
- What is dependency injection, and what forms does it take?
- Explain the Factory Method pattern and how it differs from a simple factory.
- How do you implement a Singleton, and why is it often considered an anti-pattern?