Skip to content

mintc2/mongodb-tx-bench

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

MongoDB Transaction Performance Benchmark

Background

While discussing database performance, an LLM told me that MongoDB with transactions runs 5-15x slower than without transactions. This seemed like a substantial claim worth investigating, so I decided to test it.

I vibe coded this benchmark to compare MongoDB's performance with and without transactions on a single machine. All replica set members run on localhost, which means essentially zero network latency between them. The tests revealed a ~2x slowdown for typical workloads, but this overhead increases dramatically with larger transaction sizes, reaching ~7x at 40,000 operations per transaction. It's important to note that production setups with replica set members on different physical machines would experience additional overhead from network latency.

After running my tests and, I found this EnterpriseDB report which appears to be the source of the 5-15x figure. The report actually compares MongoDB transactions versus PostgreSQL transactions, not MongoDB with versus without transactions. This explained the discrepancy.

So TL;DR is that mongo is indeed kinda slow, but the LLM misrepresented the number.

Results

Testing on a machine with the total of 12 cores, MongoDB 8.0:

Test 1: 1M prepopulated, 500K inserts (10 workers, baseline)

Mode Throughput Avg Latency Duration
Without TX 457,961 ops/s 20.6 ms 1.09s
With TX 228,646 ops/s 43.1 ms 2.19s

Slowdown: ~2.0x

Test 2: 10M prepopulated, 10M inserts (10 workers, 2 indexes, 10 batches/tx)

Mode Throughput Avg Latency/TX Duration
Without TX 239,573 ops/s 416.4 ms 41.74s
With TX 92,579 ops/s 1077.8 ms 1m48.02s

Slowdown: ~2.6x

Test 3: 5M prepopulated, 5M mixed ops (10 workers, 3 indexes, 30 batches/tx, cross-collection)

Mode Throughput Avg Latency/TX Duration
Without TX 197,708 ops/s 1495.1 ms 25.29s
With TX 73,016 ops/s 4061.7 ms 1m8.48s

Slowdown: ~2.7x

Test 3b: 5M prepopulated, 5M mixed ops (10 workers, 3 indexes, 40 batches/tx, cross-collection)

Mode Throughput Avg Latency/TX Duration
Without TX 196,001 ops/s 1992.5 ms 25.51s
With TX 27,562 ops/s 14401.1 ms 3m1.41s

Slowdown: ~7.1x - Transaction overhead increases dramatically with larger transaction sizes (40K ops/tx vs 30K ops/tx).

Usage

Prerequisites: Go 1.23+, Docker or Podman engine running

# Run both tests (default: fair comparison)
go test -v -run TestBatchInsert

# Run specific test
go test -v -run TestBatchInsert_NoTx
go test -v -run TestBatchInsert_Tx

Configuration

Basic Parameters

Variable Default Description
PREPOPULATE_DOCS 1000000 Documents to prepopulate
INSERT_BATCHES 500 Number of batch operations
BATCH_SIZE 1000 Documents per batch
INSERT_WORKERS 10 Concurrent workers

Workload Parameters

Variable Default Description
MIXED_WORKLOAD 0 Enable mixed read+update+insert workload (set to 1)
When enabled: 25% reads, 25% updates, 50% inserts

Stress Parameters (to increase TX overhead)

Variable Default Description
SECONDARY_INDEXES 0 Number of secondary indexes (try 2-4)
BATCHES_PER_TX 1 Batches per transaction (try 10-100)
CROSS_COLLECTION 0 Enable cross-collection writes (set to 1)

Progress

Variable Default Description
PREPOP_PROGRESS_SECONDS 2 Prepopulation progress interval
TEST_PROGRESS_SECONDS 1 Insert progress interval

Example Scenarios

Baseline (insert-only, minimal overhead)

go test -v -run TestBatchInsert

Heavy Load

PREPOPULATE_DOCS=10000000 INSERT_BATCHES=10000 \
SECONDARY_INDEXES=2 BATCHES_PER_TX=10 \
go test -v -run TestBatchInsert

Stress Test (mixed workload + indexes + 30K ops/tx)

PREPOPULATE_DOCS=5000000 INSERT_BATCHES=5000 MIXED_WORKLOAD=1 SECONDARY_INDEXES=3 BATCHES_PER_TX=30 CROSS_COLLECTION=1 \
go test -v -run TestBatchInsert

Extreme Stress Test (40K ops/tx - shows non-linear overhead)

PREPOPULATE_DOCS=5000000 INSERT_BATCHES=5000 MIXED_WORKLOAD=1 SECONDARY_INDEXES=3 BATCHES_PER_TX=40 CROSS_COLLECTION=1 \
go test -v -run TestBatchInsert

About

Tests how much using TX slows down mongo

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages