Publish / Subscribe (Pub/Sub): An event pattern where publishers emit events without knowing who's listening, and subscribers react without knowing who published. Decouples components. Publish and subscribe, or pub/sub, is a messaging pattern where publishers send events without knowing who will receive them, and subscribers listen for events without knowing who sent them. An OrderPlaced event can be picked up by the email service, the warehouse integration and the analytics system, each independently. Adding a new subscriber does not require any change to the code that publishes the event.
Frequently asked questions about Publish / Subscribe (Pub/Sub)
What should you keep in mind with Publish / Subscribe (Pub/Sub)?
This decoupling makes systems easier to extend. It also makes them harder to follow, because no single piece of code shows the whole flow. Use clear event names, document which subscribers exist, and decide how failures are handled, since one subscriber failing should usually not affect the others. Redis, Google Pub/Sub, SNS and Laravel's event system all support this pattern at different scales.
Implement proven backend architecture patterns including Clean Architecture, Hexagonal Architecture, and Domain-Driven Design. Use this skill when designing clean architecture for a new microservice, when refactoring a monolith to use bounded contexts, when implementing hexagonal or onion architecture patterns, or when debugging dependency cycles between application layers.
Implement Command Query Responsibility Segregation for scalable architectures. Use when separating read and write models, optimizing query performance, or building event-sourced systems.