Quick revision
The short answer
Each instance holds only its own SSE connections, so an event produced on instance A has to be fanned out: A publishes it to a broker such as Redis pub/sub, Kafka or RabbitMQ, every instance subscribes, and each one delivers it to the matching users connected locally. Sticky sessions are only an optimisation, never the delivery mechanism. We also store recent events per user so a reconnecting client can replay what it missed using Last-Event-ID, and we send heartbeat comments and turn off proxy buffering so connections stay open and events arrive immediately.
What to remember
In this answer, we saw that SSE connections are local to the instance that accepted them, so an event produced on A must be fanned out. Producers publish to a broker such as Redis pub/sub, Kafka or RabbitMQ, every instance subscribes and delivers to its local connections, and sticky sessions remain only an optimisation. Recent events stored per user allow replay with Last-Event-ID after reconnects, and heartbeat comments plus disabled proxy buffering keep the streams alive and timely.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Each instance holds only its own SSE connections, so an event produced on instance A has to be fanned out: A publishes it to a broker such as Redis pub/sub, Kafka or RabbitMQ, every instance subscribes, and each one delivers it to the matching users connected locally. Sticky sessions are only an optimisation, never the delivery mechanism. We also store recent events per user so a reconnecting client can replay what it missed using Last-Event-ID, and we send heartbeat comments and turn off proxy buffering so connections stay open and events arrive immediately.
Why this question is asked
SSE is easy on one instance: a map from user to emitter, and a send() call. The moment there are six instances behind a load balancer, that map is split into six pieces and nobody holds the whole picture. Interviewers ask this scenario because it tests whether we understand that long-lived connections are state, and how to move events to where that state lives. In this answer, we will recap why a single instance cannot deliver the event, design the fan-out through a broker, compare broker choices, explain why sticky sessions are not the answer, add replay after reconnects, and handle proxies and heartbeats.
1A Quick Recap: Connections Are Local State
An SSE connection is an open HTTP response on one specific server process. In Spring MVC, it is an SseEmitter object in that instance's memory; in WebFlux, a subscribed Flux. Only that instance can write to it. When user 42 opens the notifications stream, the load balancer picks an instance, say D, and from then on only D can reach user 42.
Events, on the other hand, are produced wherever the business logic happens to run. An order is shipped and the request handling it lands on instance A. A has the event but not the connection; D has the connection but not the event. With six instances, the chance that they are the same is one in six, which is exactly the "sometimes it works" behaviour teams report.
2Fan-Out Through a Broker
The fix is to separate producing an event from delivering it. Producers publish to a broker, and every instance subscribes. Each instance receives every event, checks its local registry, and sends the event to any local connection of that user. Instances without that user simply drop it.
7 messages in order. Play them, or step through them one at a time.
3The Delivery Code on Each Instance
Here is the subscriber side with Redis pub/sub through Spring Data Redis. The same shape works with a Kafka listener or a RabbitMQ listener.
Notice that the registry maps a user to a set of emitters, because one user may have several tabs or devices, and that the onCompletion, onTimeout and onError callbacks remove emitters from it, so the set only contains live connections.
4Choosing the Broker
Redis pub/sub is the simplest: very low latency, and every subscriber gets every message. But it is fire-and-forget; an instance that is restarting misses messages, and nothing is stored. Kafka stores events durably; to give every instance all events, each instance uses its own consumer group (or its own assignment of all partitions), and on restart it continues from the latest offset. RabbitMQ can do it with a fanout exchange and an exclusive, auto-deleted queue per instance.
At six instances, broadcasting every event to every instance is perfectly fine. At 200 instances and 50,000 events per second it becomes wasteful, because each event is processed 200 times. Then we route instead: each instance subscribes only to channels for the users it holds, or a small presence store records which instance holds which user, and producers publish to that instance's channel. That is more moving parts, so we start with broadcast and change only when the numbers demand it.
| Broker | Delivery to all instances | Durability |
|---|---|---|
| Redis pub/sub | every subscriber gets every message | none, missed while disconnected |
| Redis Streams | each instance reads the stream | kept until trimmed |
| Kafka | one consumer group per instance | durable, replay by offset |
| RabbitMQ | fanout exchange, queue per instance | queued while connected |
5Why Sticky Sessions Are Not the Answer
Sticky sessions make the load balancer send a user's requests to the same instance, usually by cookie. It is tempting to think this solves the problem: if all of user 42's requests go to D, the event will be produced on D. But it does not hold. Events are often produced by background jobs, Kafka consumers or other users' actions, which have nothing to do with user 42's requests. And when D is restarted during a deploy, user 42 reconnects to another instance anyway.
So stickiness can be an optimisation, for example to keep a user's tabs on one instance and reduce duplicate work, but the broker remains the delivery mechanism. A design that is correct only with stickiness fails silently the first time an instance goes away.
6Replay After Reconnect, Heartbeats and Proxies
Connections drop during deploys, network changes and sleep. EventSource reconnects automatically and sends Last-Event-ID with the id of the last event it got. To use it, we store recent events per user in shared storage, for example a capped Redis list or stream per user holding the last 100 events for an hour, with increasing ids. On reconnect, the instance first registers the new connection for live events, then sends stored events with ids greater than Last-Event-ID, and skips duplicates by id. Registering first avoids a gap in which an event could arrive between the replay and the subscription.
Finally, the network path must cooperate. Reverse proxies and load balancers close connections that are idle for a while, often 60 seconds, so each instance sends a heartbeat comment line every 15 to 30 seconds. Proxies that buffer responses would hold events back until the buffer fills; for nginx we send X-Accel-Buffering: no on the stream or disable proxy buffering for that route. And we size each instance for its share of connections: with 60,000 connected users over 6 instances, each holds about 10,000 open streams.
7Follow-up questions
What happens to events published while an instance is restarting?
With Redis pub/sub they are lost for that instance, but its users have disconnected too. When they reconnect to any instance, the per-user history in shared storage lets that instance replay what they missed, using Last-Event-ID. So durability comes from the history store, not from the pub/sub channel.
How do you avoid every instance doing work for every event at large scale?
Route events instead of broadcasting. Each instance can subscribe to a channel per connected user, or register in a presence store which users it holds, and the producer publishes to that specific channel. This costs subscription management and a presence store, so we only switch when broadcast traffic is really a problem.
Do you need SSE if the client could just poll?
For notifications, polling every few seconds from tens of thousands of clients costs far more requests and still adds latency. SSE keeps one quiet connection per client and delivers events within milliseconds, so it is usually cheaper once fan-out is solved.
8Conclusion
In this answer, we saw that SSE connections are local to the instance that accepted them, so an event produced on A must be fanned out. Producers publish to a broker such as Redis pub/sub, Kafka or RabbitMQ, every instance subscribes and delivers to its local connections, and sticky sessions remain only an optimisation. Recent events stored per user allow replay with Last-Event-ID after reconnects, and heartbeat comments plus disabled proxy buffering keep the streams alive and timely.
Open with
The root of the problem is that each SSE connection lives on one instance. User 42 is connected to D, so only D can write to that stream, but the event may be produced on A by a request, a job or a consumer.
Then, if they want more
So I separate producing from delivering. Every producer publishes the event to a broker, and every instance subscribes. Each instance looks up the user in its local registry, which maps users to their open emitters, and sends the event if it holds a connection, otherwise it ignores it.
At six instances, broadcasting to all of them is cheap; Redis pub/sub is the simplest, Kafka with a consumer group per instance gives durability, and RabbitMQ works with a fanout exchange. If we grew to hundreds of instances, I would route by user instead of broadcasting.
I would not rely on sticky sessions, because events often do not come from the user's own requests and instances get replaced; stickiness can be an optimisation only.
For reconnects, I keep the last events per user in shared storage with increasing ids, and when the browser reconnects with Last-Event-ID, I register the new connection first, then replay the newer events and skip duplicates. And I send a heartbeat comment every 15 to 30 seconds and disable proxy buffering, so idle timeouts and buffering do not break delivery.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You run 6 instances of a service that pushes notifications over SSE. An event produced...": https://scrivio-interview-library.netlify.app/spring-boot/web-other/347c51b209/