How to design a pub-sub system where there can be multiple publisher for same entity?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When designing a publish-subscribe (pub-sub) system where multiple publishers exist for the same entity, several key aspects must be considered to ensure robustness, scalability, and efficiency. Such a system allows various parts of an application to remain decoupled while still communicating effectively through the exchange of messages.
Understanding the Basics of Pub-Sub
In a pub-sub model, publishers send messages without specifying a specific receiver. Instead, these messages are categorized into channels or topics. Subscribers listen to these topics and receive messages accordingly. The primary advantage of this model is that it allows for high levels of decoupling between message producers and consumers.
Design Considerations for Multiple Publishers
1. Topic or Channel Design
It is essential to define topics in a way that they are intuitive and effectively partition the domain of your application. For instance, if the entity is a "User," topics might include UserCreated, UserUpdated, and UserDeleted. This granularity allows subscribers to choose only the events they are interested in.
2. Message Queuing and Delivery Guarantees
With multiple publishers, the system needs to handle higher volumes of messages. Employing message queues can help manage this by buffering messages until subscribers are ready to process them. Delivery guarantees (like at-least-once, at-most-once, exactly-once) also need to be defined based on the criticality of the message content.
3. Load Balancing and Scalability
As the number of publishers increases, the load on the pub-sub system can become significant. Techniques such as sharding (partitioning topics among different servers) or adding more replicas of the message brokers can help distribute this load evenly.
4. Duplicate Message Handling
Since multiple publishers might produce the same event, the system should be capable of identifying and handling duplicate messages. This could be implemented via message deduplication strategies using unique message IDs or timestamps.
5. Event Ordering
Maintaining a consistent order of messages can be challenging but is crucial for many applications. Approaches like using vector clocks or sequence numbers per topic can help preserve the order at the cost of increased complexity.
Example Scenario
Consider a real-time stock price update system where multiple publishers (e.g., different stock exchanges) publish price updates about the same stocks. Subscribers might be trading systems or analytics tools that process these updates.
- Topics: Organized by stock symbol, e.g.,
AAPL,MSFT. - Message Content: Includes the publisher ID, timestamp, stock symbol, and new price.
- Delivery Guarantee: At-least-once, since missing a price update might affect trading decisions.
- Ordering: Important to know the sequence of price changes, thus sequence numbers might be used.
Subtopic: Ensuring Robust Security
In a system with multiple publishers, controlling who can publish to what topics is essential for security. Implementing proper authentication and authorization mechanisms ensures that only valid publishers can send messages, thereby preventing unauthorized message publication.
Summary Table
| Feature | Description | Considerations |
| Topic Design | Clear and intuitive partitioning of message domains. | Should align with the business or the logical entities. |
| Message Queuing | Temporary message storage to handle delivery at scale. | Choice of queuing system can impact performance. |
| Multiple Publishers | Handling messages from various sources without conflicts. | Requires mechanisms like deduplication and load balancing. |
| Security | Ensuring that only authorized publishers can send messages. | Involves authentication and potentially encryption. |
| Scalability & Redundancy | System ability to handle growth in publishers and subscribers, and to recover from failures. | May require sharding or additional replicas. |
Conclusion
Designing a pub-sub system with multiple publishers for the same entity involves careful consideration of topic structure, message handling mechanisms, scalability options, and robust security measures. By adequately addressing these areas, you can build a system that is both efficient and reliable, supporting a dynamic number of publishers without sacrificing performance or data integrity.

