Transaction Performance Optimization Guide
Overview
This page explains the transaction behavior that matters when you tune throughput and cache consistency:
- Read-only transaction accounting - transactions that do not run monSQLize write helpers are counted separately in
getTransactionStats(). - Process-local cache invalidation barrier - transaction writes with explicit invalidation record intents and flush them only after a successful commit.
Applicable scenarios
Optimization 1: Read-only optimization
Working principle
How to use
No code changes are required for read-only transactions. A transaction is counted as read-only when it does not run monSQLize write helpers.
Optimization 2: cache invalidation barrier
Working principle
When a transaction performs writes through monSQLize helpers with explicit cache invalidation configured, the runtime records read-cache invalidation intents. After the MongoDB commit succeeds, it flushes the recorded invalidations and releases the process-local cache lock; abort does not flush.
Usage
When you want transaction writes to clear cached reads, pass session: tx.session together with cache.invalidate or autoInvalidate: true.
Boundary
Usage suggestions
1. Keep transactional writes narrow
Recommended:
- Keep transactions short.
- Pass
session: tx.sessiononly to the operations that must be part of the transaction. - Prefer targeted filters and indexed queries so MongoDB can resolve conflicts efficiently.
Avoid:
- Long-running transactions with network calls inside the callback.
- Large batch updates inside one transaction unless you have measured the lock and oplog impact.
- Relying on monSQLize cache locks as cross-process business locks.
2. Make full use of read-only optimization
✅ Recommended Scenario:
- Report query
- Data analysis
- read-only copy
Best Practice:
3. Monitor transaction statistics
4. Configuration tuning
Monitoring indicators
Key indicators
Monitoring script example
FAQ
Q1: Will cache barriers increase memory usage?
A: The barrier and cache-lock metadata are process-local and short-lived. Watch activeTransactions and your process RSS if you run many concurrent long transactions.
Q2: How do I inspect transaction behavior?
A: Use aggregate transaction stats and per-transaction stats:
Q3: Will read-only optimization affect data consistency?
A: No.
- Read-only transactions are still executed under transaction isolation level
- It just does not invalidate the cache and does not affect data accuracy.
- Data read within a transaction is still a consistent snapshot
Q4: Can cache locking be disabled for one transaction?
A: Yes. Pass enableCacheLock: false to transaction options when you want driver transaction semantics without the process-local cache lock. Cache invalidation still follows the documented best-effort boundary.
Summary
What to measure
Suggestions
- Keep transaction callbacks small and idempotent.
- Monitor
getTransactionStats()regularly. - Tune timeout, retry, and cache-lock settings from measured behavior.