How to Fix MongoDB Error 112 (WriteConflict)
Two operations modifying the same document at the same time trigger mongodb error 112. You fix this WriteConflict error by adding client retry logic and shortening your transactions. Database teams often see this failure during high traffic spikes or big batch jobs. When two processes touch the same record at once, the storage engine steps in. It stops one operation to protect data accuracy across the cluster. Knowing why this happens helps your team keep apps fast and stable. Root causes of
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
Two operations modifying the same document at the same time trigger mongodb error 112. You fix this WriteConflict error by adding client retry logic and shortening your transactions.
Database teams often see this failure during high traffic spikes or big batch jobs. When two processes touch the same record at once, the storage engine steps in. It stops one operation to protect data accuracy across the cluster. Knowing why this happens helps your team keep apps fast and stable.
Root causes of mongodb error 112 in your database
In the official database rules, code 112 maps to the WriteConflict name. The engine uses optimistic concurrency control for most write tasks. Instead of locking a whole collection, it lets multiple tasks run at once. If two tasks change the exact same document together, a conflict occurs.
Multi-document transactions are the most common source of this specific error code. When a transaction starts, it takes a snapshot of the data at that moment. If an outside write changes a targeted document before commit, the transaction fails. The engine aborts the transaction and returns code 112 to the app.
Hot documents also trigger this error during normal traffic spikes. A hot document is a single record that many workers update every second. Counters, status records, and shared job queues often create this bottleneck. Because so many threads compete for one record, the engine cannot order them all.
Long-running background scripts can also collide with live user requests. If a cleanup script updates thousands of rows, it overlaps with normal app writes. The longer a transaction keeps the session active, the higher the chance of a conflict.
How to diagnose and fix write conflicts safely
Start by checking your database logs to find which collection has the conflict. You do not need full write rights to inspect the database state. MongoDB grants access through roles that define actions on specific resources. A role can grant access down to a collection level of granularity.
MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Give your debugging team the built-in read role on the affected database. The read role allows actions like find, listCollections, listIndexes, dbStats, and collStats. It does not permit insert, update, remove, or dropCollection actions. This keeps production data safe while engineers check collection stats and active queries.
Once you find the hot collection, look at how your app handles retries. Most modern drivers can retry whole transactions when a transient write conflict happens. Make sure your app code wraps multi-document work in a retry loop. Use a short delay with random jitter between tries so threads do not collide.
Next, make your transactions smaller and faster. Do not make external network calls while a database transaction is open. Fetch all needed data first, open the transaction, write the changes, and commit right away. Break big batch updates into small chunks so they finish in milliseconds.
How to prevent future write contention
If one document gets too many writes, change your schema design. Instead of updating one counter document, insert small delta records and sum them later. You can also split a single hot document into several bucket documents. Each worker updates a random bucket, which spreads the write load out.
Review who and what holds the readWrite role in your production cluster. The readWrite role includes everything in read plus insert, update, and remove actions. Limit this role to the core app services that truly need to change data. Schedule heavy admin scripts during quiet hours so they do not hit live traffic.
Check MongoDB commands and documents on the wire
MongoDB roles and SCRAM login decide who opens a session. They do not check single commands or redact sensitive fields inside returned documents. With hoop.dev, you run an open-source proxy, a sidecar for your MongoDB, inline in the MongoDB wire protocol. On every command an AI agent or engineer runs against production, hoop.dev decides to pass, mask, block, or hold it for human review.
Before a command runs, hoop.dev guardrails check its effect and refuse banned drops or writes with MongoDB error code 13, or hold risky commands for approval. On the way back, hoop.dev data masking rewrites sensitive fields in returned documents in memory, and every verdict is logged. See the open-source code on GitHub.
Frequently asked questions
Does mongodb error 112 mean my database lost data?
No, this error means the engine stopped a write to protect your data. The failed transaction rolls back clean, and your app just needs to retry it.
Can single-document updates trigger a WriteConflict error?
Yes, high contention on one document can exhaust internal engine retries and return code 112. Still, multi-document transactions trigger this error much more often in production.
What role does an engineer need to check collection stats during an incident?
Assign the built-in read role on the target database. It allows dbStats and collStats actions without granting permission to change or delete records.
Related guides
In this post