Skip to main content
Definition Website Computer and Technology

Publish-Subscribe Pattern

Subject: Computer and Technology noun /ˈpʌb.lɪʃ səbˈskraɪb ˈpæt.ərn/ 1 definition

  1. Definition (noun)

    Entry #247 · Updated

    The Publish-Subscribe Pattern (pub-sub) is an asynchronous messaging architecture in which senders (publishers) emit events without knowing who will receive them, and receivers (subscribers) register interest in specific event categories to receive matching messages. Rather than communicating directly, publishers and subscribers interact through an intermediary—such as a message broker or event bus—that filters and distributes each event. This indirection enforces space, time, and synchronization decoupling, distinguishing pub-sub from point-to-point queues (where one consumer claims each message) and the classic Observer pattern (where subjects hold direct references to observers). As a result, distributed systems can add, remove, or scale producers and consumers independently without introducing tight coupling.

    Common Types

    • Topic-Based: Publishers categorize messages under named logical channels (often supporting hierarchical paths and wildcards), and subscribers receive every event emitted to the topics they follow; widely used in MQTT and Apache Kafka.

    • Content-Based: Subscribers register predicate filters against message headers or payload fields (such as severity = "critical"), requiring the broker to inspect each message and route only matching events.

    • Type-Based: Subscriptions bind directly to explicit event classes or data schemas rather than string topic names, providing compile-time type safety in in-process event buses and typed messaging frameworks.

    Example

    When a customer completes checkout in an e-commerce platform, the order service publishes an order.placed event containing the order ID, total amount, and item SKUs to a message broker without knowing which downstream services exist. The inventory, billing, and email notification services each subscribe independently to the order.placed topic, enabling them to reserve stock, process payment, and send a confirmation receipt asynchronously in parallel.