Video in progress

Design a notification platform for 5 million concurrent connections. What are the main layers?

A thin gateway layer that holds the connections is separated from business logic, with a message bus and a routing table ("which gateway is this user on") between them. State lives not in the gateway but in the storage layer.

📖 Answer

I split the system into four parts.

1. Connection gateway. It only holds connections: TLS, handshake, auth check, heart-beat, subscriptions, write buffers. No business logic, no trips to the DB. A Netty-style event-loop server. If one node handles 100 to 200 thousand connections, 5 million needs 30 to 50 nodes plus spare. I deliberately make the nodes "simple and replaceable".

2. Routing / presence. A Redis cluster: userId -> {gatewayId, connId}. The gateway writes on connect, deletes on disconnect, and refreshes with TTL plus heart-beat, so if a gateway dies unexpectedly the entries expire on their own.

  1. Delivery service. Business services write "event for user X" to Kafka. The delivery service picks the event up, stores it (inbox), finds the gateway in the routing table, and passes it to that gateway's private channel. If the user is offline, it sends a push notification or simply leaves it in the inbox.

  2. Inbox storage. The last N events for each user, with a seq. On reconnect the client sends its seq and gets what it missed. Cassandra or sharded Postgres: lots of writes, and reads mostly from the recent part.

Why this split? When the gateways restart on a deploy, millions of reconnects happen. If you put business logic inside them, every logic change turns into a connection storm. A separated gateway is rarely deployed.

Trade-off: the extra hop and extra systems (Redis, Kafka) add operational load. At 50,000 users this architecture is overkill; there, Spring STOMP plus Redis pub/sub is enough. I don't build the 5-million design in one step either: first I separate the gateway, and I add the routing table only once the waste of broadcasting becomes noticeable.

The list of questions that still have to be settled: multiple devices per user, a separate path for global broadcast (one message to everyone), and regions.