articles / mvp-spring-webflux-mongodb

A reactive MVP with Spring WebFlux and MongoDB

Mounir Guizani · 29 November 2026 · 3 min read

  • spring
  • webflux
  • mongodb
  • java

An MVP has to ship fast without mortgaging the future. Spring WebFlux and MongoDB make an effective pair for an API that must handle many concurrent requests with few resources. Here is a complete MVP in four files, and the questions to ask before choosing reactive.

Why WebFlux + MongoDB

  • WebFlux handles requests in a non-blocking way, on a small number of threads (Netty). A thread never waits for a network response: it moves on to the next request.
  • MongoDB has a native reactive driver: the database follows the same model end to end, with no blocked thread.
  • Spring Data derives queries from method names: the MVP needs very little code.

The example: an idea box where people submit and vote for ideas.

1. The model

@Document("ideas")
public record Idea(@Id String id, String title, int votes, Instant createdAt) {}

public record NewIdea(@NotBlank @Size(max = 120) String title) {}

A Java record is enough: immutable, readable, no boilerplate.

2. The reactive repository

public interface IdeaRepository extends ReactiveMongoRepository<Idea, String> {
    Flux<Idea> findByVotesGreaterThanOrderByVotesDesc(int votes);
}

Mono<T> represents zero or one result, Flux<T> zero to N results, emitted as they come.

3. The controller

@RestController
@RequestMapping("/ideas")
class IdeaController {

    private final IdeaRepository repository;
    private final ReactiveMongoTemplate template;

    IdeaController(IdeaRepository repository, ReactiveMongoTemplate template) {
        this.repository = repository;
        this.template = template;
    }

    @GetMapping
    Flux<Idea> all() {
        return repository.findAll(Sort.by(Sort.Direction.DESC, "votes"));
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    Mono<Idea> create(@Valid @RequestBody NewIdea body) {
        return repository.save(new Idea(null, body.title(), 0, Instant.now()));
    }

    @PostMapping("/{id}/vote")
    Mono<Idea> vote(@PathVariable String id) {
        // Atomic increment in the database: no concurrent read-modify-write
        return template.findAndModify(
                        Query.query(Criteria.where("id").is(id)),
                        new Update().inc("votes", 1),
                        FindAndModifyOptions.options().returnNew(true),
                        Idea.class)
                .switchIfEmpty(Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND)));
    }
}

The classic trap is the vote: reading the idea, adding 1, then saving loses votes when two requests arrive at the same time. MongoDB's $inc operator makes the increment atomic.

4. An integration test against a real database

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Testcontainers
class IdeaApiTest {

    @Container
    @ServiceConnection
    static MongoDBContainer mongo = new MongoDBContainer("mongo:7");

    @Autowired
    WebTestClient client;

    @Test
    void creates_then_lists_an_idea() {
        client.post().uri("/ideas").bodyValue(new NewIdea("Serverless portfolio"))
                .exchange().expectStatus().isCreated();

        client.get().uri("/ideas").exchange()
                .expectStatus().isOk()
                .expectBodyList(Idea.class).hasSize(1);
    }
}

Testcontainers starts a real MongoDB in Docker and @ServiceConnection wires Spring to it automatically: no configuration, no database mocks.

Reactive, yes, but when?

Situation My choice
Many concurrent connections, slow network calls (third-party APIs, streaming) WebFlux
Fully non-blocking chain available (reactive driver, reactive HTTP client) WebFlux
Team new to Reactor, complex business logic Spring MVC
Blocking JDBC access, classic SQL transactions Spring MVC, possibly with Java 21 virtual threads

Since Java 21, virtual threads deliver much of the concurrency gain with plain imperative code. Reactive keeps the edge for streaming, backpressure and composing asynchronous calls.

Golden rules

  1. Never block in a reactive chain (no block(), no JDBC, no synchronous call).
  2. Stay reactive end to end: a single blocking link cancels the benefit.
  3. Test with StepVerifier and WebTestClient, and a real database through Testcontainers.
  4. Index the filtered and sorted fields in MongoDB from the MVP onwards.

In a day you get a tested, concurrent and resource-efficient API. The MVP can then grow without a rewrite.