Concurrency Control: Read Tickets vs Write Tickets
MongoDB limits concurrent WiredTiger storage engine execution using Ticket Concurrency Control (128 read tickets and 128 write tickets by default).
WiredTiger Storage Engine: Cache, Journal & Checkpoints
Simulate RAM page states, dirty page eviction thresholds, WAL journaling, and 60-second checkpoint disk flushes.
1. Formal Definition & Architecture Intent
WiredTiger uses an admission-control ticketing system to prevent thread thrashing and excessive context switching at the CPU level. By default, MongoDB configures 128 concurrent Read Tickets and 128 concurrent Write Tickets. Before an operation touches the storage engine, it must acquire a ticket; if none are available, operations queue.
2. Explain Like I'm 5 (Mental Model)
Think of a nightclub with a strict capacity of 128 dancers and 128 staff. Even if 1,000 people are standing outside the door, the bouncer will not let person #129 in until someone leaves, preventing the dance floor from collapsing.
3. Real-World Architecture Example
Checking available storage tickets during a traffic spike.
{
"write": {
"out": 12,
"available": 116,
"totalTickets": 128
},
"read": {
"out": 4,
"available": 124,
"totalTickets": 128
}
}If write.available reaches 0, incoming write requests queue up at the network layer, spiking query latency.
4. MQL Implementation Steps
const t = db.serverStatus().wiredTiger.concurrentTransactions;
print("Read tickets available: " + t.read.available + " / " + t.read.totalTickets);
print("Write tickets available: " + t.write.available + " / " + t.write.totalTickets);5. Critical Engineering Rules
| Rule / Constraint | Underlying Reason & Impact |
|---|---|
| Default Allocation | 128 concurrent read tickets and 128 concurrent write tickets. |
| Ticket Exhaustion | When available tickets hit 0, new queries queue in the global lock queue, increasing connection counts. |
6. Deep Dive: Ticket Queuing vs Slow Queries
A common misconception is that ticket exhaustion is caused by high traffic volume. In 90% of production incidents, ticket exhaustion is caused by a few unindexed slow queries (COLLSCAN) holding read tickets for 5 seconds each. Because each query hogs a ticket for seconds rather than milliseconds, the 128 available tickets vanish in less than a second.
// Adjust ticket limits dynamically in mongod (use caution):
db.adminCommand({
setParameter: 1,
wiredTigerConcurrentReadTransactions: 256,
wiredTigerConcurrentWriteTransactions: 256
});7. Architectural Trade-offs
- •Prevents catastrophic CPU collapse caused by thousands of concurrent threads thrashing execution contexts
- •Protects storage engine internal data structures from mutex lock contention
- •Provides a deterministic ceiling for active storage engine workloads
- •A single unindexed query scanning millions of rows can hold a ticket for seconds, starving all clients
- •Ticket exhaustion cascades into client application connection pool exhaustion
8. Technical Interview Questions
Each storage engine operation must acquire a ticket before accessing data and holds it until the operation completes. A fast indexed query releases its ticket in 1ms, allowing thousands of queries per second. An unindexed query scanning a large table holds its ticket for seconds. If dozens of slow queries run concurrently, all 128 tickets are held, causing all incoming fast queries to queue indefinitely.
9. Staff-Level Ready Answer
"MongoDB limits concurrent WiredTiger operations using 128 read and 128 write tickets to prevent CPU context thrashing. If tickets are exhausted (available = 0), operations queue at the networking layer. Ticket exhaustion is almost always caused by slow unindexed queries holding tickets for extended durations."
10. Quick Knowledge Check
What is the default number of concurrent write tickets configured in MongoDB WiredTiger?