How to Fix MongoDB Error 515 (ConnectionClosedByPeer)
You get mongodb error 515 when a remote server or firewall drops an active socket. You fix it by matching idle timeouts and turning on driver retry rules. In the database engine, code 515 has the name ConnectionClosedByPeer. It belongs to the NetworkError and RetriableError groups. This means the link broke outside normal app flow, but the query can run again. What causes mongodb error 515 in your cluster A database node, client app, or network box can close a socket without warning. When th
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 515 when a remote server or firewall drops an active socket. You fix it by matching idle timeouts and turning on driver retry rules.
In the database engine, code 515 has the name ConnectionClosedByPeer. It belongs to the NetworkError and RetriableError groups. This means the link broke outside normal app flow, but the query can run again.
What causes mongodb error 515 in your cluster
A database node, client app, or network box can close a socket without warning. When the other side tries to read or write, it hits code 515. Several common events trigger this drop in live systems.
First, firewalls and load balancers often cut idle links. An app pool might hold a socket open for ten minutes without sending traffic. If a middle box drops idle links after five minutes, the next query fails.
Second, cluster state changes can drop links. When a primary node steps down, it closes open sockets to force a new election check. Planned restarts, upgrades, or sudden crashes also cut the peer link right away.
Third, resource limits on the database host can force a drop. If a node runs out of memory or file handles, the host OS kills connections. Mismatched TLS settings between the client and server also end handshakes early.
How to check and fix connection drops safely
Start by checking the server logs around the exact time of the drop. Look for stepdown events, memory warnings, or slow queries that timed out. Compare these timestamps with your app error logs to find the root cause.
When engineers inspect a live database, do not hand out broad admin rights. MongoDB grants access through roles that define allowed actions on specific resources. A role applies to its home database and can control access down to one collection.
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 on-call team the built-in read role for safe checks. This role allows find, listCollections, listIndexes, dbStats, and collStats actions on non-system collections. It blocks insert, update, remove, and dropCollection actions so nobody changes live data by mistake.
If you must protect private fields during checks, add data masking to your workflow. Masking rewrites matched fields like emails, phone numbers, card numbers, or national IDs in query results. This keeps sensitive records safe while engineers fix the network path.
How to stop future peer disconnects
Once you find the weak link, update your client and network settings. Because code 515 is a RetriableError, modern drivers can handle transient drops for you. Make sure retryable reads and retryable writes are active in your client setup.
Next, fix the timeout gap between your app pool and your network path. Set your client pool max idle time lower than your firewall idle timeout. This makes the app close and refresh old sockets before the firewall cuts them.
Turn on TCP keepalive probes on both the client host and the database server. Keepalive packets tell middle boxes that the socket is still in use. Also check that your connection pool size matches the limits of your database tier.
Finally, test your app during a planned primary stepdown in a staging cluster. Watch the logs to confirm that the driver catches code 515 and retries the query. Good pool rules turn sudden socket drops into harmless background retries.
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
Is mongodb error 515 safe to retry in my app?
Yes, MongoDB marks code 515 as both a NetworkError and a RetriableError. If your write operation is idempotent, the client driver can safely run it again.
Why does ConnectionClosedByPeer happen after my app sits idle?
A network firewall or load balancer between your app and database likely dropped the quiet socket. Lower your client pool idle timeout so the app refreshes links before the firewall cuts them.
What database role should engineers use to check error 515?
Assign the built-in read role on the affected database. It lets engineers run dbStats and collStats to check health without allowing data changes.
Related guides
In this post