Skip to content
LESSON 19 / 50Advancedwiredtiger

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).

Interactive Engine Simulator

WiredTiger Storage Engine: Cache, Journal & Checkpoints

Simulate RAM page states, dirty page eviction thresholds, WAL journaling, and 60-second checkpoint disk flushes.

Dirty Page Ratio
50%
Eviction triggers at 80%
Journal WAL LSN
#1048
Flushed every 100ms
Last Checkpoint
18s ago
Default period: 60s
Node State
HEALTHY
WiredTiger Engine
1. RAM Cache (Volatile)4 Pages
p-1users: [u1..u20]
CLEAN
p-2orders: [o101..o150]
DIRTY
p-3products: [p1..p50]
CLEAN
p-4users: [u21..u40]
DIRTY
2. Journal (WAL Ring Buffer)fsync 100ms
Active Journal File:WiredTigerLog.0000000001
Current LSN:#1048
Persistence guarantee:Write Concern j:true
Sequential append-only log enables instant crash recovery by replaying changes between last checkpoint and current LSN.
3. Disk Storage (.wt B-Trees)Checkpointed
Data Tables:collection-*.wt
Index Tables:index-*.wt
Root Pointer:WiredTiger.turtle
Disk files store B-Tree pages with Snappy / zlib compression. Only updated during checkpoint flush or heavy eviction.
ENGINE_LOG:Engine initialized. WiredTiger cache allocated (50% RAM minus 1GB).

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.

Sample BSON Documents:
Collection: db.serverStatus.wiredTiger.concurrentTransactions
{
  "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

Step 1: Check available read and write tickets in mongosh
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);
Available tickets dropping to 0 indicates severe database queueing.

5. Critical Engineering Rules

Rule / ConstraintUnderlying Reason & Impact
Default Allocation128 concurrent read tickets and 128 concurrent write tickets.
Ticket ExhaustionWhen 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
});
Production Warning: Increasing tickets on CPU-saturated machines exacerbates CPU context-switching overhead.

7. Architectural Trade-offs

Advantages
  • •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
Disadvantages & Gotchas
  • •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

Q1: Why does a slow, unindexed query cause WiredTiger ticket exhaustion for unrelated fast queries?

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?