Quick revision
The short answer
We compose functions with the pipe syntax in spring.cloud.function.definition, for example validate|enrich, so the output of one becomes the input of the next under one pair of bindings. A Function that returns null sends no output message for that input. A Function<Flux<In>, Flux<Out>> is invoked once at startup and processes the whole stream with Reactor operators. The trade-off is that per-message retries and the binder's usual error handling no longer apply in the same way, so we handle errors inside the reactive pipeline ourselves, which is why imperative functions are the simpler default for most services.
What to remember
In this answer, we composed functions with validate|enrich in spring.cloud.function.definition and saw that the composed bindings are named after the joined function names. Returning null from a Function sends no output message and still acknowledges the input. A Function<Flux<In>, Flux<Out>> is called once and processes the whole stream, but the binder's per-message retry and error handling no longer apply the same way, so an unhandled error can end the stream. That is why imperative functions are the simpler default.
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 compose functions with the pipe syntax in spring.cloud.function.definition, for example validate|enrich, so the output of one becomes the input of the next under one pair of bindings. A Function that returns null sends no output message for that input. A Function<Flux<In>, Flux<Out>> is invoked once at startup and processes the whole stream with Reactor operators. The trade-off is that per-message retries and the binder's usual error handling no longer apply in the same way, so we handle errors inside the reactive pipeline ourselves, which is why imperative functions are the simpler default for most services.
Background to this question
Once a service grows past a single mapping, three questions come up quickly: how to split the logic into small reusable steps, how to drop a message without throwing, and whether to go reactive for throughput. Each has a short answer in Spring Cloud Stream, and the reactive one has a catch that causes real incidents. In this answer, we will recap imperative functions, compose two of them, filter by returning null, write the same logic as a reactive function, and then look carefully at what changes with errors and why imperative is usually the better choice.
1A Quick Recap of Imperative Functions
In Spring Cloud Stream's functional model, a Function bean is called once for each message. The framework receives a message, converts its payload, calls apply, converts the result and publishes it. If apply throws, the binder retries the call according to the binding's max-attempts and back-off, and then dead-letters or logs the message. Each call is independent, which makes this model easy to reason about: one message in, one call, one result.
2Composing Functions With the Pipe Syntax
Suppose we have one bean that validates orders and another that enriches them with customer data. Instead of writing a third bean that calls both, we compose them in the function definition.
- Message from ordersJSON converted to
OrderPlaced - validate: total positive?YesPass the order onNoThrow: binder retries, then DLQ
- enrichlook up the customer
- Publish EnrichedOrderto orders-enriched
4 steps in order. Play them, or step through them one at a time.
The framework builds one composed function, so validate's output is passed straight to enrich in memory, without an intermediate topic. Notice the binding names: a composed function gets a name made by joining the parts, validateenrich, so its bindings are validateenrich-in-0 and validateenrich-out-0. Because that name is easy to get wrong, many teams give it an alias with spring.cloud.stream.function.bindings.validateenrich-in-0=orderInput; check your version's naming rules.
3Producing No Output: Returning null
Sometimes a function should not produce anything for a given input, for example a test order that should be ignored. Throwing would trigger retries and dead-lettering for something that is not an error. Instead, the function returns null, and the framework sends no output message for that input.
The input is still treated as successfully processed, so it is acknowledged and not redelivered. If a function would never produce output, a Consumer bean states that intent more clearly than a Function that always returns null.
4Processing a Flux Instead of Single Messages
A reactive function has a different signature: it takes a Flux, Reactor's type for a stream of zero or more elements, and returns one. The framework calls it only once, at startup, and connects the incoming messages to the input Flux. From then on, our Reactor operators process the stream.
This gives access to operators that need more than one message at a time, such as windowing, buffering into batches, or joining streams. It is not automatically faster: for one-in-one-out mapping, the imperative function is just as quick and much simpler.
5The Trade-Off: Errors in the Reactive Style
Here is the catch. In a reactive function, the framework is not calling our code per message, so it cannot wrap each call in its retry logic. The binding's max-attempts and back-off settings do not apply in the same way. Worse, an exception thrown inside map is an error signal, and by Reactor's rules an error ends the Flux. The subscription is cancelled and the function stops processing messages, while the application keeps running and looks healthy.
So in the reactive style, retries, dead-lettering and error isolation are our code, built from Reactor operators. Acknowledgement can also behave differently, because a message may be considered delivered when it enters the Flux rather than when our processing finishes; check how your binder version handles this before relying on it. That is a lot to get right. For most services, which handle one message at a time and want the binder's retries and DLQ, imperative functions are simpler to reason about, test and operate. We choose reactive only when we need stream operators.
6Follow-up questions
Can I compose an imperative function with a reactive one?
Composition expects compatible shapes. Composing imperative functions with each other is the common case; mixing styles is supported in some combinations but makes error behaviour harder to follow. If one step needs reactive operators, it is usually clearer to make the whole composed function reactive and handle errors in it.
Why does a reactive function only get called once?
Because its job is to build the pipeline, not to process a message. The returned Flux describes what happens to every future element. That is also why exceptions behave differently: there is no per-message call for the framework to retry.
Is there a way to filter without returning null?
In the imperative model, null is the intended way. A Consumer can also choose to do nothing. In the reactive model, the filter operator removes elements from the stream, which is the natural equivalent.
7Conclusion
In this answer, we composed functions with validate|enrich in spring.cloud.function.definition and saw that the composed bindings are named after the joined function names. Returning null from a Function sends no output message and still acknowledges the input. A Function<Flux<In>, Flux<Out>> is called once and processes the whole stream, but the binder's per-message retry and error handling no longer apply the same way, so an unhandled error can end the stream. That is why imperative functions are the simpler default.
Open with
To compose, I list the beans with a pipe in spring.cloud.function.definition, like validate pipe enrich. The framework builds one function in memory, validate's output goes straight into enrich, and the bindings are named after the joined names, validateenrich-in-0 and -out-0, which I'd usually alias to something readable.
Then, if they want more
If a Function shouldn't produce output for some input, it returns null: no message is published and the input is still acknowledged, so it's a clean way to filter without throwing. For reactive processing, the signature becomes Function of Flux to Flux.
It's invoked once at startup to build the pipeline, and every message then flows through the Reactor operators, which is useful for windowing, batching or joining streams. The big trade-off is error handling.
The framework isn't calling my code per message, so max-attempts and back-off don't apply the same way, and an exception inside map is an error signal that terminates the Flux, so the function silently stops consuming. I'd have to handle errors per element myself, for example concatMap with a Mono, retryWhen and onErrorResume to record and skip.
Acknowledgement timing can also differ. So for most services I'd stick with imperative functions, which keep the binder's retries and dead-letter handling and are easier to test, and use reactive only when I need stream operators.
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 Cloud Stream reference
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 "In Spring Cloud Stream's functional model, how do you compose functions, handle a...": https://scrivio-interview-library.netlify.app/spring-boot/spring-cloud-stream/049ddaf4f6/