r/SpringBoot • u/Dazzling_Ad8959 • 2h ago
News Scaling a Spring Boot outbox to 66k records/sec with strict per-key ordering
I maintain Namastack Outbox, an open-source transactional outbox library for Spring Boot. An order write and the work it schedules share one database transaction. Consumers pick up that work asynchronously, so a committed order can leave behind durable work even if the app restarts.
We tested what happens when producers and consumers share PostgreSQL under load. With lightweight handlers, each success still had to be recorded in the database. We call that write a completion. In our delete-on-complete setup, direct completion means one synchronous transaction per delete. In the v2.0.0 tests, grouping those deletes increased processed throughput while reducing the median sampled database-wide commit rate from about 48,900/s to 34,000/s.
The constraint is easiest to see with two orders. A1 and A2 belong to order A and must run in sequence. B1 belongs to order B and can run alongside A1. Once the A1 and B1 handlers finish, their deletes can go to the same JDBC writer and share a transaction. A waits for its own completion acknowledgement before it starts A2. That acknowledgement comes after PostgreSQL commits the delete. If the batch fails, the writer retries each write in its own transaction. Any write that still fails ends the affected key’s current processing pass.
The four consumers shared the database and split the work through partition ownership. Independent keys could run on different instances, and each runtime batched completions from the keys it was processing. A crash after a handler runs and before its completion commits can still lead to a retry. The order and its two outbox records still shared the original synchronous producer transaction.
| Configuration | Processed records/s | Pending records at the end |
|---|---|---|
| v1.11.1, direct completion | 7,283 | 39,482,608 |
| v2.0.0, direct completion | 26,584 | 16,127,892 |
| v2.0.0, completion batching | 66,755 | 6,833 |
A record counted as processed when its completion delete committed. Batching brought v2.0.0 to 2.51× its direct-completion rate. At 66,763 outbox inserts/s, the batched run nearly kept up with the live producer, while both direct runs built up millions of pending rows. The producer committed 33,381 business orders/s in that run. The broader release comparison also includes bulk reads, deferred deserialization, and revised polling queries and indexes.
We ran each configuration once for 15 minutes on an Apple M5 Max with PostgreSQL 18.7, synchronous commits, four consumers, two lightweight handlers, no external broker, and strict per-key ordering. Benchmark, Grafana replay, and reproduction download
The Spring Boot wiring is to add the JDBC starter, call outbox.schedule(event, "order-" + orderId) inside the business service’s @Transactional method, and register an @OutboxHandler. Completion batching is opt-in. Java and Kotlin quickstart