Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

A Flux of 10,000 items stops processing at item 37 because one item throws an exception in map. The team wants processing to continue for the other items. What options exist?

The Flux stops because errors are terminal in Reactive Streams: once map throws on item 37, the whole sequence ends with onError and items 38 to 10,000 are never processed. To keep going, we handle the error per item inside flatMap or concatMap, wrapping each item in its own Mono with onErrorResume, so a failure ends only that item's inner sequence. onErrorContinue exists but only works with supporting operators and has surprising semantics, so per-item handling is preferred, with failed items sent to a dead-letter path for later analysis.

Background to this question

Batch-style reactive pipelines are common: importing a file, reprocessing events, syncing records from another system. Sooner or later one record is malformed, and the team finds that a single bad record stopped the whole job. This question tests whether a candidate understands why that happens, a rule of the Reactive Streams contract rather than a bug, and knows the reliable fix as well as the tempting one that misbehaves. In this answer, we will reproduce the problem, explain why the error ends the Flux, fix it with per-item handling, look at onErrorContinue and its traps, and add a dead-letter path.

1Reproducing the Problem

Here is a small version of the pipeline. process fails for one item, and we count how many items succeed.

Java
Flux.range(1, 10_000)
.map(i -> process(i)) // throws IllegalArgumentException for i == 37
.count()
.subscribe(
n -> System.out.println("processed " + n),
e -> System.out.println("failed: " + e.getMessage()));
// failed: bad record 37 (items 1 to 36 were processed; 38 to 10,000 never were)

Adding onErrorReturn or onErrorResume after map does not help. They would replace the error with a fallback and complete, but the original Flux has already stopped; the remaining 9,963 items are lost either way.

2Why One Error Ends the Whole Flux

The Reactive Streams specification defines onError and onComplete as terminal signals. After a publisher sends onError, it must not send anything else, and its subscriber treats the sequence as finished. When map's lambda throws, map cancels its upstream (Flux.range stops producing) and sends onError downstream. This rule is what makes reactive pipelines predictable: every operator knows a sequence ends exactly once.

So the fix cannot be to resume the outer sequence. The fix is to make sure the error never reaches the outer sequence. We do that by giving each item its own small sequence, where an error can be handled without ending anything else.

Step throughWhere the error goes
  1. Item 37 reaches mapprocess(37) throws
  2. Is the error handled inside a per-item Mono?
    YesInner Mono completes empty, outer Flux continues with 38Nomap sends onError downstream
  3. Outer Flux cancels upstreamrange stops at 37
  4. Subscriber receives onErrorjob ends

4 steps in order. Play them, or step through them one at a time.

3The Fix: Handle Errors Per Item Inside flatMap

We move the work into flatMap and wrap each item in Mono.fromCallable. Each item now has its own inner Mono, and onErrorResume on that inner Mono handles that item's failure.

Java
Flux.range(1, 10_000)
.flatMap(i -> Mono.fromCallable(() -> process(i))
.onErrorResume(e -> logAndSkip(i, e))) // returns Mono.empty() after recording the failure
.count()
.subscribe(n -> System.out.println("processed " + n));
// processed 9999
Mono<Result> logAndSkip(int item, Throwable e) {
log.warn("skipping item {}: {}", item, e.toString());
return Mono.empty();
}

Notice what changed. The exception is thrown inside fromCallable, so it becomes an error of the inner Mono only. onErrorResume turns it into an empty Mono, which simply contributes no item. flatMap merges the inner results into the outer Flux and never sees an error, so it keeps requesting items 38, 39 and onward.

flatMap runs inner publishers concurrently, up to 256 by default, and does not preserve order. If order matters, use concatMap, which processes one item at a time, or flatMapSequential, which runs concurrently but emits in order. If process is blocking, add subscribeOn(Schedulers.boundedElastic()) to the inner Mono. The handle operator is another option for purely synchronous work: we catch the exception inside it and simply do not emit for that item.

4What About onErrorContinue?

Reactor offers onErrorContinue, which looks like exactly what we want: it lets the sequence drop the failing item and continue.

Java
Flux.range(1, 10_000)
.map(i -> process(i))
.onErrorContinue((e, item) -> log.warn("skipping {}", item)) // works here, because map supports it
.count(); // 9999

But there is a catch. onErrorContinue does not handle errors itself. It places a flag in the subscriber context, and operators above it that support the mode, such as map, filter, handle and flatMap, check the flag and drop the failing element instead of failing. Operators that do not support it ignore the flag and fail as usual. The semantics are surprising in other ways too: it affects upstream operators, not downstream ones; an onErrorResume placed inside a flatMap above it may never be reached, because onErrorContinue intercepts first; and its behaviour depends on implementation details of each operator. The Reactor documentation itself recommends per-item handling instead. So we prefer explicit onErrorResume inside flatMap, where anyone reading the code can see exactly which errors are handled and how.

5Sending Failed Items to a Dead-Letter Path

Skipping with a log line loses information. In production we want every failed item recorded somewhere we can inspect and replay: a dead-letter table, a Kafka dead-letter topic, or a file. The fallback publisher becomes a write to that store.

Java
Flux.fromIterable(records)
.concatMap(r -> Mono.fromCallable(() -> process(r))
.onErrorResume(e -> deadLetters.save(new FailedRecord(r.id(), e.toString()))
.then(Mono.empty()))) // record it, then emit nothing for this item
.count(); // successes only; failures are in dead_letters

Two details matter. If the dead-letter write itself fails, we must decide whether that should stop the job, since silently losing the failure record is worse than stopping. And we should distinguish bad data from infrastructure failures: a malformed record will fail forever and belongs in dead letters, but a database outage would send every record there, so for transient errors a retry with retryWhen(Retry.backoff(3, Duration.ofMillis(200))) on the inner Mono, or stopping the job, is the better response.

6Follow-up questions

Why not just wrap process() in a try and catch inside map?

That works for synchronous code: catch the exception and return a marker such as an Optional or a Result type, then filter. It is the same idea as per-item handling. The flatMap version extends better when the per-item work is itself asynchronous, like a WebClient call or a database write, because those failures arrive as error signals rather than exceptions.

Does per-item handling change backpressure?

flatMap requests items from upstream as inner publishers complete, limited by its concurrency argument, so backpressure still works. With concatMap, only one item is in progress at a time. Choosing the concurrency value is how we control load on whatever process calls.

How do we know how many items failed?

Count them as we go, for example with a counter incremented in the fallback or a metric through Micrometer, and include successes and failures in the job's final log line or summary. A job that reports 9,999 processed and 1 failed, with the failure in the dead-letter store, is something an operator can act on.

7Conclusion

In this answer, we saw that one exception in map ends a 10,000-item Flux because onError is terminal in Reactive Streams, so no fallback after map can bring back the remaining items. The reliable fix is to handle errors per item, wrapping each item in its own Mono inside flatMap or concatMap with onErrorResume, so a failure ends only that inner sequence. onErrorContinue can work with supporting operators but has surprising semantics, so we prefer explicit per-item handling and send failed items to a dead-letter path, retrying transient failures rather than dead-lettering them.

What to say in the interviewAbout 90 seconds aloud

Open with

The Flux stops because in Reactive Streams onError is a terminal signal. When map throws on item 37, it cancels the source and sends onError downstream, and after that the sequence is finished, so items 38 onward are never processed.

Then, if they want more

Putting onErrorResume after map does not help, because it can only replace the end of a sequence that has already stopped. The fix is to give each item its own sequence: flatMap each item into Mono.fromCallable of process, with onErrorResume on that inner Mono.

A failure then ends only that item's Mono, the fallback returns empty, and flatMap keeps pulling the next items. If order matters I use concatMap or flatMapSequential, and if process blocks I add subscribeOn boundedElastic.

There is onErrorContinue, which can drop failing items, but it only works with operators that support it, it acts on operators upstream of it, and it can interfere with error handling inside flatMap, so I avoid it and prefer explicit per-item handling.

Rather than only logging, I would send failed items to a dead-letter store, like a table or a Kafka dead-letter topic, with the error, so they can be inspected and replayed, and I would retry transient errors like timeouts instead of dead-lettering them, and report counts of successes and failures at the end.

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.
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 "A Flux of 10,000 items stops processing at item 37 because one item throws an exception...": https://scrivio-interview-library.netlify.app/spring-boot/webflux/f4b9c4c52f/