The Publisher Subscriber Model
The publisher subscriber model (often shortened to pub/sub) is a way for parts of a system to talk to each other without knowing about each other. It's one of the first patterns I reach for when a system starts to get tangled, and I think more teams should use it.
How it works
There are three pieces. A publisher sends out a message when something happens, like "order placed". A subscriber tells the system which kinds of messages it wants to hear about. In the middle is the hub, which keeps track of who wants what and delivers each message to everyone who asked for it.
The publisher never calls the subscribers directly. It doesn't know who they are or how many there are. It just tells the hub that something happened.
An example
Say a customer places an order. Without pub/sub, the order code calls the warehouse, then the email service, then the reporting system, one after another. If the email service is slow, the customer waits. If you add a fourth system later, you change the order code again.
With pub/sub, the order code publishes "order placed" and moves on. The warehouse, the email service and the reporting system each subscribe to that message and do their own part. Adding a fourth system means adding one more subscriber, and the order code stays the same.
Why it's worth it
The parts of the system are loosely connected, so each one can change on its own schedule. Slow work moves to the background, where the user never waits on it. And the system is easier to grow, because new features listen in instead of being wired in.
What to watch for
It's harder to follow what happens after a message goes out, because the flow isn't written in one place. Subscribers need to handle messages that arrive late or more than once. And the hub is one more thing to run and keep an eye on. For a small app with one or two steps, a direct call is often the simpler choice.
Thanks for the read! Have any questions? Feel free to email me or send me a message on LinkedIn.