How to Fix MongoDB Error 160 (MaxStalenessOutOfRange)
You get mongodb error 160 when a client sets an invalid staleness limit for reads. You can fix this MaxStalenessOutOfRange failure by setting a valid staleness window or reading from the primary node. Database clusters copy data from a primary node to secondary nodes. This copy process takes a small amount of time. Some apps read from secondary nodes to spread out the query load. When they do this, they can set a rule for how old that secondary data can be. If the app picks a time limit outside
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.
You get mongodb error 160 when a client sets an invalid staleness limit for reads. You can fix this MaxStalenessOutOfRange failure by setting a valid staleness window or reading from the primary node.
Database clusters copy data from a primary node to secondary nodes. This copy process takes a small amount of time. Some apps read from secondary nodes to spread out the query load. When they do this, they can set a rule for how old that secondary data can be. If the app picks a time limit outside the allowed range, the server stops the query. The database then returns code 160 to protect data quality.
What causes mongodb error 160 in replica sets
In the official database rules, code 160 carries the name MaxStalenessOutOfRange. It happens when the client driver sends a read rule that conflicts with cluster timing. Every node in a replica set sends regular status pulses to other nodes. The client driver compares the staleness limit against these pulse times. If the limit is too short, the cluster cannot track lag with enough speed.
This problem often starts after an app change or a driver update. A developer might type a small number into the connection string by mistake. They might confuse seconds with milliseconds when setting up the app. It can also happen if an app combines a staleness limit with primary read rules. Primary nodes always hold fresh data, so a staleness limit does not make sense there.
Network delays and slow nodes do not trigger this exact code directly. Instead, this error points to a bad setting in the client request itself. The server or driver rejects the math before the read even runs on a secondary node.
How to diagnose and fix MaxStalenessOutOfRange in production
To find the broken setting, check the connection strings across your services. Look for any read rules that route queries to secondary nodes. Check the time unit your client driver expects for staleness limits. Most drivers measure this window in seconds rather than milliseconds. Raise the value so it sits well above the cluster pulse interval, or remove the custom limit.
When you check the cluster during an outage, keep your access tight. You do not need write permissions to inspect cluster state or test read rules. MongoDB grants access through roles that apply to a specific database down to the collection level. The built-in read role grants read access to all non-system collections in its database. It allows actions like find, listCollections, listIndexes, dbStats, and collStats. It leaves out actions that change data, such as insert, update, remove, or dropCollection.
MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Giving your team the read role lets them test secondary reads without risk. Never hand out the readWrite role just to debug a read error. The readWrite role adds actions like insert, update, remove, createIndex, and dropCollection. Stick to read-only roles while you test your new connection settings in production.
Preventing read rule failures across teams
Once you fix the broken connection string, standardize how apps talk to the cluster. Store shared connection templates in one place so teams do not invent custom read timers. Review any query path that reads from secondary nodes during code checks. Make sure the app actually needs secondary reads before adding staleness rules.
You should also watch how engineers run ad-hoc checks against production data. When people connect with local tools, they might paste broken connection options or view sensitive fields. Putting a central control point in front of your databases helps prevent both mistakes. It can enforce clean read settings and mask fields like emails, phone numbers, and card numbers in query results.
Inspect MongoDB wire traffic in flight
Built-in MongoDB roles govern collection access at setup time. They cannot score command risk per call or mask sensitive document values on the fly. By design, hoop.dev sits inline in the MongoDB protocol next to your server as a sidecar for your MongoDB. On every production command from mongosh, drivers, or AI agents, hoop.dev decides whether to pass, mask, block, or hold it for human review.
First, hoop.dev guardrails block bad deletes, drops, and write pipelines or hold them for approval. Next, hoop.dev data masking rewrites sensitive values in returned documents in memory and logs each decision. Explore the repo on GitHub.
Frequently asked questions
What does error code 160 mean in MongoDB?
Code 160 is named MaxStalenessOutOfRange in the official error list. It means a client set a secondary read staleness limit that is too small or invalid.
Does fixing error 160 require restarting the database cluster?
No, you do not need to restart any database nodes. You fix this error by updating the read settings in your client app or connection string.
What database role should engineers use when testing read settings?
Engineers should use the built-in read role on the target database. This role allows find, dbStats, and collStats actions without permitting data changes like insert or dropCollection.
Related guides
In this post