ZeroMQ
PUB/SUB topology
Networking
Message Queue
Localhost Communication

ZeroMQ PUB/SUB topology on the same machine

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

ZeroMQ, often abbreviated as zmq or 0MQ, is a high-performance asynchronous messaging library aimed at use in distributed or concurrent applications. It provides a message queue but unlike other messaging queues, ZeroMQ can run with no dedicated message broker in between.

Understanding ZeroMQ PUB/SUB Model

The Publish-Subscribe (PUB/SUB) model in ZeroMQ is one of its several communication patterns. This model is particularly useful for distributing messages to multiple receivers (subscribers). In a PUB/SUB scenario, the publisher sends messages (broadcasts) without the knowledge of any subscribers – thus the system is loosely coupled. Subscribers can filter messages based on their interests.

Basic Functioning

  • Publisher (PUB): Sends messages to an arbitrary number of subscribers. It can publish messages on different topics.
  • Subscriber (SUB): Receives messages from a publisher. In ZeroMQ, subscribers can filter messages based on a topic, receiving only messages of interest.

When used on the same machine, the PUB and SUB sockets can efficiently communicate via the ipc (Inter-Process Communication) or inproc (in-process) transport mechanisms, providing a lightweight and fast method for inter-process communication.

Implementation Example

Below is a basic example of how to set up a PUB/SUB system using ZeroMQ in Python. This example assumes that both the publisher and the subscriber are running on the same machine.

Setting up the Publisher

python
1import zmq
2import time
3
4# Prepare the context and publisher socket
5context = zmq.Context()
6publisher = context.socket(zmq.PUB)
7publisher.bind("tcp://*:5556")
8
9while True:
10    # Send a message with a topic
11    publisher.send_string("temperature 20")
12    time.sleep(1)

Setting up the Subscriber

python
1import zmq
2
3# Prepare the context and subscriber socket
4context = zmq.Context()
5subscriber = context.socket(zmq.SUB)
6subscriber.connect("tcp://localhost:5556")
7
8# Subscribe to the 'temperature' topic
9subscriber.setsockopt_string(zmq.SUBSCRIBE, 'temperature')
10
11while True:
12    message = subscriber.recv_string()
13    print(f"Received: {message}")

Benefits of Using ZeroMQ PUB/SUB on the Same Machine

Using ZeroMQ PUB/SUB topology on the same machine can have several benefits:

  • Low Latency: Communication between processes is faster as it avoids network latency.
  • High Throughput: ZeroMQ optimizes the transfer of messages, handling high volumes efficiently.
  • Flexibility: Easily scales to multiple publishers and subscribers; also capable of transitioning to distributed systems if needed.
  • Decoupling of Components: Publishers and subscribers are loosely coupled, promoting simpler, maintainable code.

Key Comparison with Other Models

Below is a table that summarizes how PUB/SUB compares with other ZeroMQ patterns like Request/Reply (REQ/REP) and Pipeline (PUSH/PULL):

Feature / PatternPUB/SUBREQ/REPPUSH/PULL
CommunicationOne-to-manyOne-to-oneOne-to-many
CouplingLooseTightModerate
Message FilteringSupportedNot supportedNot supported
Load BalancingNot inherentInherentInherent

Potential Use Cases

  • Logging System: Where multiple parts of an application might send log messages to a central subscriber that processes or stores these logs.
  • Real-time Analytics: In scenarios where analytics need to be updated in real-time, such as monitoring temperature or CPU usage across different processes.
  • Event Notification Systems: Systems where events (e.g., changes in a database) are broadcasted to interested components (subscribers).

Conclusion

ZeroMQ provides a robust framework for decoupled communications in distributed applications, and its PUB/SUB model is particularly effective for scenarios requiring scalable, efficient one-to-many communication. Whether used locally on a single machine or across distributed systems, ZeroMQ’s flexibility and performance make it an excellent choice for modern, complex software architectures.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.