A simple introduction to Pub/Sub Pattern

Just a guy who loves to write code and watch anime.
Search for a command to run...

Just a guy who loves to write code and watch anime.
No comments yet. Be the first to comment.
Qualities that make outstanding builders.

Bipedal vs quadrupedal: how many legs This is about the number of legs the creature walks on. Bipedal. Two legs. Humans, ostriches, T-rex, kangaroos, most fantasy humanoids. Quadrupedal. Four legs. Do

Intro You hear "lerp" and "smoothstep" everywhere in game dev. They sound like math jargon. They're not. Both are small tools that do the same job: smoothly move from one value to another. The problem

What is Kinematics Kinematics is the math field. It's the study of how things move without worrying about forces (which would be dynamics). FK and IK are the two branches: forward kinematics and inver

Intro Textures are usually the biggest cost in a 3D scene. Memory, bandwidth, and load time all get eaten by them. Resizing them is the obvious lever. There's more. This post is about the less obvious

The Publish/Subscribe pattern, often shortened to Pub/Sub, allows parts of a software system to communicate with each other.
Imagine you have an app that needs to send updates to users. With Pub/Sub, you have a system that's split into four main parts:
Publisher: This is the part that sends out messages. Think of it as the app sending updates.
Subscriber: This is the part that receives messages. It's like the app on your phone that gets the update.
Topic: This is a way to organize messages. For example, one topic could be for app updates, and another could be for new messages.
Message: This is the actual information being sent from the publisher to the subscriber.

The Pub/Sub model lets publishers send messages without waiting, so subscribers can get them when they can.
This asynchronous approach means senders don't have to stop and wait.
Adding more subscribers is easy without needing to change anything for the publishers, making everything work smoothly and quickly.
Pub/Sub systems can deliver messages in different ways:
At most once: The message is sent once but might not get delivered.
At least once: The message definitely gets delivered, but it might arrive more than once.
Exactly once: The message is guaranteed to be delivered once and only once.
When a message is sent more than once, it's important that it doesn't mess anything up. This is where idempotency comes in. An idempotent message can be received multiple times without causing any issues. No matter how often the message is received, it will yield the same result.
In Pub/Sub systems, messages are not guaranteed to arrive in the order they were sent. For instance, Google Cloud Pub/Sub supports ordered delivery by using ordering keys, which group messages into sequences that are delivered in order. This makes sure messages that are grouped together get delivered in the right order.
But, ensuring messages stay in order means you might have to deal with slower message handling and more complex setups, especially if a message doesn't get through correctly. For example, if one message gets stuck, others with the same key might also get held up, slowing things down.
Using different topics lets you organize messages based on their type. For instance, one topic might be for notifications, while another for user messages. This helps in making sure the right messages get to the right subscribers.
Many tools exist for setting up a Pub/Sub system.
Some of the well-known ones include:
Amazon SNS
Azure Web PubSub
Google Cloud Pub/Sub
The Pub/Sub pattern is a smart way to make parts of your software talk to each other without getting too complicated.
It's like having a postal system that knows exactly where to deliver messages without the sender and receiver ever needing to meet.
A Dead Letter Queue is an important component in Pub/Sub or Queue-based messaging system. You can read more about it here.