r/java • • 1d ago

Scaling a PostgreSQL-backed transactional outbox to 66k records/sec with strict per-key ordering

Post image

I maintain Namastack Outbox, an open-source transactional outbox library for Spring Boot. It stores pending work in the same database transaction as the business write, then runs the handlers asynchronously.

With lightweight handlers, persistence became a cost worth isolating. In direct mode, every successful record in this test caused a JDBC delete and a transaction commit. In the 15-minute v2.0.0 runs, the median sampled database-wide commit rate went from about 48,900/s with direct completion to 34,000/s with batching, even as the batched run committed more orders and deleted more records. Those counters include reads and monitoring.

The batch has to preserve a dependency. If A1 and A2 share a key, the A2 handler can start only after A1’s completion has committed. A handler returning successfully is one step. Its completion is a separate database write.

The optional JDBC batcher has one completion writer per runtime. The A1 and B1 handlers can run independently because their keys differ. If their deletes arrive in the same batch, the writer executes both in one PostgreSQL transaction. The key tasks wait on their own CompletableFuture. The writer resolves each future after the commit returns. A’s task can then start A2. If the batch transaction fails, the writer retries the completion writes in individual transactions. A write that still fails completes its future exceptionally and ends that key’s current processing pass.

The test collected up to 1,024 actions with a 1 ms collection window after the first submission. A lightly loaded writer can spend that window collecting a single completion. A hot key contributes at most one in-flight completion, so batching relies on concurrency across keys.

ConfigurationProcessed records/sPending records at the end
v1.11.1, direct completion7,28339,482,608
v2.0.0, direct completion26,58416,127,892
v2.0.0, completion batching66,7556,833

A record counted as processed when its completion delete committed. Within v2.0.0, batching brought throughput to 2.51× the direct rate. The backlog tells the other half of the story. The batched run nearly kept pace with 66,763 new outbox records/s, while both direct runs accumulated millions of rows. The release comparison also includes bulk reads, deferred deserialization, and revised polling queries and indexes.

Each order and its two outbox records were saved in one synchronous transaction. The batched run committed 33,381 orders/s. Producers and consumers ran concurrently on an Apple M5 Max with PostgreSQL 18.7, four consumers, two lightweight handlers, and no external broker. Every configuration used strict per-key ordering and ran once for 15 minutes.

For the minimal Spring Boot wiring, see the Java and Kotlin quickstart.

5 Upvotes

2 comments sorted by

-2

u/0xFatWhiteMan 1h ago

another message queue ? ok