AlgoViz

Introduction to pub-sub

easy 25 min

In publish-subscribe, a program publishes each message once to a named topic, and every subscription on that topic gets its own copy.

Where it shows up

The same pattern appears across everyday software.

  • New account

    The user service publishes USER_CREATED. Email sends a welcome, analytics counts it and the audit log stores it, each through its own subscription.

  • Cloud notifications

    Amazon SNS sends a message published to a topic to its subscribers, and filter policies let each one take only a part.

  • Sensors and small devices

    Devices publish readings with MQTT to topics such as a building and a room. The client picks a quality of service for each message.

  • Live updates between servers

    Chat servers use Redis Pub/Sub to tell each other about a new message. It is fast, and a server that is disconnected misses it, so the message is also stored.

  • Cache invalidation

    A product service publishes PRODUCT_CHANGED. Every server with a local cache subscribes and drops that product.

  • Order processing

    One order event feeds inventory, billing and analytics. Analytics can be down for an hour and catch up afterwards.