Reactive streams and Project Reactor
22 questions in 6 subtopics.
Reactive Streams specification
Describe the four interfaces of the Reactive Streams specification and the order of signals between a Publisher and a Subscriber.Why does the Reactive Streams specification forbid callingonNext concurrently and require request(n) to be positive? What goes wrong in a hand-written Publisher that ignores these rules?Reactive Streams became a standard interface (and part of the JDK as Flow) so that libraries could interoperate. What does its adoption history suggest about betting on low-level concurrency standards for an organisation?Mono and Flux
What is the difference betweenMono.just(loadUser()) and Mono.fromCallable(() -> loadUser())?In this code, sendEmail is called even when the user exists: findUser(id).switchIfEmpty(createUser(id)).flatMap(...) where createUser sends a welcome email when the method is called. Why, and how do you fix it?Review this method:public Mono<Order> place(Order o) {
repository.save(o).subscribe();
return Mono.just(o);
}
Operators
Explain the difference between map,flatMap, concatMap and flatMapSequential in Reactor.You have Flux<Long> userIds and a method Mono<User> fetch(Long id) that calls a remote service. Write a pipeline that fetches at most 10 users concurrently and collects them into a List.A Flux pipeline uses .flatMap(x -> repo.save(x)) to save 1 million records and exhausts the database connection pool, failing with pool acquire timeouts. Why, and how do you fix it?You are writing coding guidelines for reactive code in your organisation. Which operator-level rules would you include to prevent the most common production bugs?Backpressure
What is backpressure in Reactor and what happens when a producer likeFlux.interval emits faster than the subscriber can process?A WebFlux endpoint streams database rows to slow clients over HTTP. How does backpressure flow end to end, and where can it break?Design how a Kafka-consuming reactive service applies backpressure so that a slow downstream database does not cause unbounded memory growth or lag spikes.Your company's data flows (APIs, event streams, batch feeds) have no consistent way of handling overload: some drop data, some buffer until memory runs out. Describe a company-wide approach to flow control and overload that will hold up as traffic grows over years.Hot vs cold publishers
What is the difference between a cold and a hot publisher in Reactor? Give an example of each.How would you use Reactor Sinks to broadcast events from a non-reactive listener to multiple SSE subscribers, and what thread-safety rule must you follow?A service calls a pricing API twice per request. The code buildsMono<Price> price = client.getPrice(id); and then uses price in two places in Mono.zip(price.map(...), price.map(...)). Why are there two calls and how do you fix it?A reactive service caches the result of a configuration call with a Mono stored in a field and cache(). Months later, stale configuration causes an incident. What went wrong, and what pattern would you standardise?Schedulers and threading
What is the difference betweenpublishOn and subscribeOn, and which scheduler should you use for wrapping a blocking call?How is Schedulers.boundedElastic() bounded, what happens when the bound is reached, and how does it relate to virtual threads on Java 21?A team built a reactive service but wraps almost every call (JPA, a legacy SOAP client, file I/O) in subscribeOn(boundedElastic). Throughput is worse than the old MVC service. What would you advise?Java concurrency models for servers have shifted from thread pools to asynchronous callbacks, to reactive streams, and now to virtual threads. How should a long-lived organisation make concurrency-model choices so that each shift does not require rewriting its services?No question matches these choices.