Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

Explain the difference between map, flatMap, concatMap and flatMapSequential in Reactor.

map transforms each item synchronously into exactly one other item. flatMap maps each item to a publisher and subscribes to those inner publishers eagerly and concurrently, up to 256 at a time by default, so results interleave in completion order. concatMap subscribes to one inner publisher at a time and waits for it to complete, so it keeps source order but has no parallelism. flatMapSequential subscribes eagerly like flatMap but emits results in source order, buffering results that arrive early.

Why this question is asked

These four operators are the workhorses of Reactor, and choosing the wrong one causes real bugs: results in the wrong order, calls that run one by one when they could run in parallel, or hundreds of concurrent calls that overwhelm a downstream service. Interviewers ask about them because a clear answer shows we understand what an inner publisher is and how concurrency and ordering trade off. In this answer, we will start with map, see why flatMap is needed, compare the ordering and concurrency of the three flattening operators with one runnable example, and look at the concurrency argument of flatMap.

1Map: One Synchronous Transformation per Item

map takes a function from one value to another and applies it to each item as it passes. It is synchronous: the function must return the new value directly, not a Mono or Flux. Order is preserved and nothing runs concurrently.

Java
Flux.just("a", "bb", "ccc")
.map(String::length)
.subscribe(System.out::println); // 1, 2, 3
Flux.just(1L, 2L).map(id -> userClient.fetch(id)); // Flux<Mono<User>>: not what we want

Notice the second line. If the function returns a Mono, map gives us a Flux of Monos, and nobody subscribes to the inner Monos, so the remote calls never happen. Whenever the transformation is asynchronous, we need one of the flattening operators.

2FlatMap: Eager, Concurrent, Unordered

flatMap takes a function that returns a publisher for each item. As items arrive, it calls the function and subscribes to each inner publisher immediately, without waiting for earlier ones to finish, and merges all their results into one Flux. So the inner work runs concurrently, and results come out in the order they complete, which may differ from the input order.

It is not unlimited. flatMap has a concurrency argument, the maximum number of inner publishers subscribed at the same time, and its default is 256 (Reactor's Queues.SMALL_BUFFER_SIZE). When 256 are in flight, it stops requesting new items from the source until one finishes. We can pass a smaller value, for example flatMap(this::fetch, 10).

3ConcatMap and flatMapSequential: Keeping the Order

concatMap also maps to inner publishers, but it subscribes to only one at a time: it waits for the current inner publisher to complete before subscribing to the next. Results come out in source order, and there is no parallelism, so the total time is the sum of all inner durations.

flatMapSequential combines the two. It subscribes to inner publishers eagerly and concurrently, like flatMap, but it emits their results in source order. If item 3 finishes before item 1, its result waits in a buffer until items 1 and 2 have been emitted. It is the right choice when we want parallel calls and ordered output, at the cost of memory for the buffered results and of a slow first item delaying everything behind it.

4Comparing All Three With One Example

Here, item 1 takes 300 ms, item 2 takes 100 ms and item 3 takes 200 ms. We run the same calls through each operator.

Java
Mono<String> call(int i) {
long ms = switch (i) { case 1 -> 300; case 2 -> 100; default -> 200; };
return Mono.delay(Duration.ofMillis(ms)).thenReturn("r" + i);
}
Flux.range(1, 3).flatMap(this::call) // r2, r3, r1 total about 300 ms
Flux.range(1, 3).concatMap(this::call) // r1, r2, r3 total about 600 ms
Flux.range(1, 3).flatMapSequential(this::call) // r1, r2, r3 total about 300 ms
// (r2 and r3 wait in a buffer for r1)
The four operators side by side
OperatorFunction returnsInner subscriptionsOutput order
mapa valuenone, synchronoussource order
flatMapa publishereager, up to 256 by defaultcompletion order, interleaved
concatMapa publisherone at a timesource order
flatMapSequentiala publishereager, concurrentsource order, early results buffered

Notice the trade-off: flatMap and flatMapSequential take the time of the slowest call because they run in parallel, while concatMap takes the sum. Only flatMap emits out of order.

5Choosing Between Them

Use map when the transformation is synchronous and cheap, such as converting an entity to a DTO. Use flatMap when each item needs an asynchronous call and order does not matter, such as sending notifications, and set the concurrency to what the downstream system can handle. Use concatMap when each step must finish before the next starts, for example applying events to an account in order, or when calls must not overlap. Use flatMapSequential when we want parallel calls but the output must follow the input, such as enriching a sorted page of results.

One more detail: when an inner publisher returns a Flux of several items, flatMap interleaves items from different inner publishers, while concatMap and flatMapSequential keep each inner publisher's items together in source order.

6Follow-up questions

Does flatMap run the inner calls on different threads?

Not by itself. flatMap subscribes to inner publishers concurrently, but each runs on whatever thread its own publisher uses. Non-blocking calls such as WebClient requests overlap without extra threads. A blocking call inside flatMap still blocks, unless it is wrapped with subscribeOn on a scheduler such as boundedElastic.

What does the prefetch argument of concatMap mean?

It is how many source items concatMap requests ahead of time and keeps queued while the current inner publisher runs, so the next item is ready immediately. It does not add concurrency; only one inner publisher is active at a time.

Why is the default concurrency of flatMap 256 and is that safe?

It is a general default from Reactor's small buffer size, chosen for throughput with non-blocking sources. It is often far too high for calls to limited resources such as a database pool of 10 connections or a rate-limited API, so we should pass an explicit value in those cases.

7Conclusion

In this answer, we saw that map transforms each item synchronously one to one, while the other three map items to publishers. flatMap subscribes to them eagerly and concurrently, up to 256 by default, so results interleave out of order. concatMap subscribes one at a time, preserving order without parallelism. flatMapSequential subscribes eagerly but emits in source order by buffering early results. The choice depends on whether we need asynchronous work, ordering, parallelism and a bound on concurrency.

What to say in the interviewAbout 90 seconds aloud

Open with

map is a synchronous, one-to-one transformation: each item goes through a function that returns a plain value, like turning an entity into a DTO, and order is preserved.

Then, if they want more

The other three are for when the function returns a publisher, typically an asynchronous call. flatMap subscribes to each inner publisher as soon as the item arrives, without waiting for earlier ones, so the calls run concurrently and results come out in completion order, interleaved.

Its concurrency is bounded by an argument that defaults to 256, Queues.SMALL_BUFFER_SIZE, and I usually pass a lower number for calls to limited resources. concatMap subscribes to one inner publisher at a time and waits for it to complete before the next, so it preserves order but there's no parallelism; total time is the sum of the calls. flatMapSequential is the middle ground: it subscribes eagerly and concurrently like flatMap, but emits results in source order, buffering the ones that finish early.

So with three calls of 300, 100 and 200 milliseconds, flatMap gives r2, r3, r1 in about 300 milliseconds, concatMap gives r1, r2, r3 in about 600, and flatMapSequential gives r1, r2, r3 in about 300. I choose flatMap when order doesn't matter, concatMap when steps must not overlap, and flatMapSequential when I want parallel calls with ordered output.

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 "Explain the difference between map, flatMap, concatMap and flatMapSequential in Reactor.": https://scrivio-interview-library.netlify.app/spring-boot/reactive-fundamentals/b8b24600b5/