Observer vs Pub/Sub
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
Classic Observer usually has a subject maintain or use subscriptions and invoke an observer interface when state changes. Publish/subscribe makes the publisher address a topic, channel, or event type instead of subscriber objects, and an intermediary routes the event. For example, a Swing button calls its registered action listeners directly, while an order service may publish to a Kafka topic whose consumers run in other processes. Kafka or RabbitMQ are familiar Pub/Sub systems, but a broker and separate processes are not required by the term; an in-process event bus can also be Pub/Sub. I compare timing, durability, ordering, retries, failure isolation, and lifecycle rather than saying Observer is always synchronous and Pub/Sub is always remote.
Observer vs Pub/Sub
Direct Answer
Observer usually describes an object that maintains a subscription relationship and notifies listeners through a known contract. Publish/subscribe describes communication through a topic or event channel so publishers address the channel rather than subscriber objects. A broker, separate processes, and asynchronous delivery are common Pub/Sub choices, not universal requirements; an in-process synchronous event bus can also be publish/subscribe.
Common Mistakes & Pitfalls
- Calling Spring events Pub/Sub.
ApplicationEventhas no broker and is single-JVM — it's Observer. True Pub/Sub needs Kafka/RabbitMQ. - Using a broker for same-process events. Adding Kafka to talk between two beans in one app buys network latency and ops burden for nothing.
- Expecting Pub/Sub to be synchronous. After
send(), consumers may process seconds later. Don't assume the work is done when the publish returns. - Forgetting durability differences. Observer notifications vanish if the process dies; broker messages can persist and be replayed.
Check your understanding
Answer these without looking back at the article.
- When would you choose Observer over Spring's ApplicationEventPublisher?
- How does Kafka's consumer group model relate to Pub/Sub?
- How does message durability differ between Observer and Pub/Sub?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.