Quick revision
The short answer
We create one Sinks.Many<Event>, for example Sinks.many().multicast().onBackpressureBuffer(), call tryEmitNext(event) from the listener, and return sink.asFlux() from an SSE endpoint so every connected client receives the events. The thread-safety rule is that emissions must be serialized: if two threads call tryEmitNext at the same moment, one gets EmitResult.FAIL_NON_SERIALIZED and its event is not delivered. So we emit from a single thread, guard emission with a lock, or use emitNext with an EmitFailureHandler that retries, and we use replay().limit(n) if late subscribers need recent events.
What to remember
In this answer, we bridged a callback listener to many SSE clients with a single Sinks.Many: the listener calls tryEmitNext, the controller returns sink.asFlux() mapped to ServerSentEvent, and we disabled autoCancel so the hub survives when every client leaves. The thread-safety rule is that emissions must be serialized; concurrent calls get FAIL_NON_SERIALIZED and lose the event, so we emit from one thread, hold a lock, or use emitNext with a retrying handler, and we always check the EmitResult. We then chose the flavour by how slow and late subscribers should be treated, with replay().limit(n) for recent history.
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.
We create one Sinks.Many<Event>, for example Sinks.many().multicast().onBackpressureBuffer(), call tryEmitNext(event) from the listener, and return sink.asFlux() from an SSE endpoint so every connected client receives the events. The thread-safety rule is that emissions must be serialized: if two threads call tryEmitNext at the same moment, one gets EmitResult.FAIL_NON_SERIALIZED and its event is not delivered. So we emit from a single thread, guard emission with a lock, or use emitNext with an EmitFailureHandler that retries, and we use replay().limit(n) if late subscribers need recent events.
Why this question is asked
Plenty of systems produce events through old-fashioned callbacks: a JMS or Kafka listener, an application event, a webhook handler. Showing those events live in a browser is a natural job for Server-Sent Events, a simple HTTP format in which the server keeps the response open and writes one event at a time. Reactor's Sinks are the bridge between the two worlds. Interviewers ask about this because the code looks trivial and has one rule that, if broken, loses events silently under load. In this answer, we will recap what a sink is, build the broadcast, look at the serialization rule and its fixes, choose the right sink flavour, and handle the details that bite in production.
1A Quick Recap of Sinks
A sink is an object we push signals into from ordinary imperative code, which Reactor then exposes as a Flux or a Mono. Sinks.many() gives sinks that accept many items. The unicast() flavour allows only one subscriber, multicast() sends each item to all current subscribers, and replay() also keeps history for subscribers that arrive later.
Each emission method comes in two forms. tryEmitNext(value) returns an EmitResult that tells us whether the item was accepted (OK) or why not, such as FAIL_ZERO_SUBSCRIBER, FAIL_OVERFLOW, FAIL_CANCELLED, FAIL_TERMINATED or FAIL_NON_SERIALIZED. emitNext(value, handler) does the same but lets a handler decide whether to retry a failure, and throws if the failure is not retried.
2Building the Broadcast
We create the sink once, as a singleton bean, feed it from the listener and return its Flux from a controller that produces text/event-stream.
6 messages in order. Play them, or step through them one at a time.
Notice the second argument to onBackpressureBuffer. By default a multicast buffered sink has autoCancel enabled, so when the last subscriber cancels, the sink itself is cancelled and every later emission fails with FAIL_CANCELLED. With SSE clients that come and go, that would kill the hub the first time everyone closed their browser, so we pass false (check your Reactor version for this overload).
We also check the EmitResult. Ignoring it is the most common way events disappear without anyone noticing.
3The Thread-Safety Rule and FAIL_NON_SERIALIZED
The Reactive Streams specification requires that onNext calls to a subscriber are serialized: never two at once. A sink enforces this for us. If thread 1 is in the middle of emitting and thread 2 calls tryEmitNext at the same moment, the sink does not block thread 2 and does not interleave the calls. It returns FAIL_NON_SERIALIZED to thread 2, and that event is simply not delivered.
This matters because a JMS or Kafka listener container usually runs several consumer threads. In a quiet test with one message at a time, it never happens. Under real load, two messages arrive within microseconds, and a few events per thousand quietly vanish.
busyLooping retries FAIL_NON_SERIALIZED for up to the given duration, which is safe because the competing emission finishes within microseconds (check that your Reactor version has it; older versions require a small custom handler that returns true for that result). A lock is equally correct and easy to read. A single emitting thread is the cleanest when we already have one, for example a dedicated dispatcher.
4Choosing the Sink for Slow and Late Subscribers
The flavour decides what happens when one browser is slow or arrives late. multicast().onBackpressureBuffer() keeps a bounded queue shared by all subscribers and delivers at the pace of the slowest one, so a single slow client can hold back everyone until the buffer fills and emissions start failing with FAIL_OVERFLOW. multicast().directBestEffort() never waits: a subscriber with no demand simply misses that item while the others receive it, which isolates a slow client at the cost of gaps for it. multicast().directAllOrNothing() delivers an item only if every subscriber can take it.
If late subscribers need recent events, for example the last 50 so a dashboard is not empty when it opens, we use Sinks.many().replay().limit(50). Each new subscriber first receives the stored 50 events and then the live ones. For SSE we can also honour the Last-Event-ID header that browsers send on reconnect, by replaying from that id.
| Sink | Slow subscriber | Late subscriber |
|---|---|---|
multicast().onBackpressureBuffer() | everyone slows; FAIL_OVERFLOW when full | gets only new events |
multicast().directBestEffort() | that subscriber misses items | gets only new events |
multicast().directAllOrNothing() | item dropped for all | gets only new events |
replay().limit(n) | has its own position in history | gets the last n, then live |
5Production Details That Matter
SSE connections stay open, so intermediaries may close idle ones. Sending a comment or heartbeat every 15 or 30 seconds keeps them alive, for example by merging a Flux.interval of ServerSentEvent comments into the stream. We count connected subscribers, which sink.currentSubscriberCount() reports, and the number of failed emissions per result, and we put a per-subscriber timeout or limit on how far a client may lag. Finally, the sink lives in one JVM, so with several instances behind a load balancer each instance only broadcasts the events its own listener received; if every client must see every event, each instance needs to consume the full stream, for example with its own consumer group or a broadcast topic.
6Follow-up questions
Why not use Flux.create instead of a sink?
Flux.create gives a FluxSink per subscriber, so it suits a source we start for each subscription, like registering a callback when a client subscribes. For one shared listener feeding many clients, Sinks.many().multicast() is the natural fit: it exists once and fans out. FluxSink from Flux.create is also serialized automatically for next calls, which is convenient, but it does not give us the shared hub.
What happens if tryEmitNext is called when no client is connected?
It depends on the flavour. A multicast buffered sink keeps the items in its buffer until the first subscriber arrives, then delivers them, and once its buffer is full it fails with FAIL_OVERFLOW. A directBestEffort sink returns FAIL_ZERO_SUBSCRIBER and drops the item. A replay sink stores it for future subscribers within its limit.
Is synchronized acceptable around emission with virtual threads?
Yes, especially on Java 24 and later, where a virtual thread blocked in synchronized no longer pins its carrier thread. The critical section is a few microseconds long either way. A ReentrantLock works the same and is the usual choice on older versions.
7Conclusion
In this answer, we bridged a callback listener to many SSE clients with a single Sinks.Many: the listener calls tryEmitNext, the controller returns sink.asFlux() mapped to ServerSentEvent, and we disabled autoCancel so the hub survives when every client leaves. The thread-safety rule is that emissions must be serialized; concurrent calls get FAIL_NON_SERIALIZED and lose the event, so we emit from one thread, hold a lock, or use emitNext with a retrying handler, and we always check the EmitResult. We then chose the flavour by how slow and late subscribers should be treated, with replay().limit(n) for recent history.
Open with
I'd create one Sinks.Many as a singleton, for example Sinks.many().multicast().onBackpressureBuffer, and pass false for autoCancel so the sink doesn't shut down when the last browser disconnects. The listener, say a JMS or Kafka listener, calls sink.tryEmitNext(event), and the controller returns sink.asFlux() mapped to ServerSentEvent with produces text/event-stream, so every connected client gets each event.
Then, if they want more
The rule I have to follow is that emissions must be serialized, because Reactive Streams forbids concurrent onNext calls. The sink doesn't block or queue a concurrent caller: if two listener threads emit at the same moment, one of them gets EmitResult.FAIL_NON_SERIALIZED and that event is just not delivered.
With a listener container running four threads that happens under load and you lose a few events silently. So I either emit from a single thread, wrap the emit in a short lock, or use emitNext with an EmitFailureHandler that busy-loops for a short time on that failure. And I always check the EmitResult and count failures.
Then I choose the flavour deliberately. A buffered multicast sink goes at the pace of the slowest client, directBestEffort lets a slow client miss items without hurting others, and if new clients should see recent history I'd use Sinks.many().replay().limit(50), possibly with Last-Event-ID on reconnect.
I'd also add heartbeats so proxies don't close idle connections, and remember that a sink is per instance, so each instance must receive all events if all clients need them.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 24
- 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 WebFlux, Project Reactor reference guide
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 "How would you use Reactor Sinks to broadcast events from a non-reactive listener to...": https://scrivio-interview-library.netlify.app/spring-boot/reactive-fundamentals/316257f3cc/