AlgoMaster Logo

Design Multi-threaded Pub-Sub System

Last Updated: February 4, 2026

Ashish

Ashish Pratap Singh

hard

Real-world examples

SystemHow it uses Pub-Sub
Apache KafkaDistributed event streaming with topics, partitions, and consumer groups
Redis Pub/SubLightweight in-memory message broadcasting
RabbitMQMessage broker with exchanges, queues, and bindings
AWS SNSCloud-native fan-out messaging to multiple subscribers

In this chapter, we'll design an in-memory concurrent Pub-Sub system that handles the core concurrency challenges. Let's start by defining exactly what we need to build.

1. Problem Definition

At first glance, Pub-Sub seems simple: maintain a list of subscribers per topic, and when a message arrives, iterate through the list and deliver. But once multiple threads are publishing, subscribing, and unsubscribing simultaneously, the problem becomes a real concurrency challenge.

Consider what happens when a publisher is iterating through subscribers to deliver a message at the exact moment a subscriber decides to unsubscribe. Or when two threads try to create the same topic simultaneously. Or when a slow subscriber can't keep up with a fast publisher, causing unbounded queue growth.

With these requirements in mind, let's design the system architecture.

2. System Overview

Premium Content

This content is for premium members only.