How to Fix MongoDB Error 216 (ElectionInProgress)

Published
Length
3 min read

Luis Felipe Dias

All posts

You see mongodb error 216 when a replica set runs a primary node election. To fix it, wait for the election to finish and retry your database operation. In the official database rules, error code 216 maps to the ElectionInProgress state. During this brief window, the cluster has no active primary node to take writes. Once the voting nodes pick a new leader, normal operations resume right away. If the error continues, your nodes might have network drops or resource limits. What causes mongodb

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 see mongodb error 216 when a replica set runs a primary node election. To fix it, wait for the election to finish and retry your database operation.

In the official database rules, error code 216 maps to the ElectionInProgress state. During this brief window, the cluster has no active primary node to take writes. Once the voting nodes pick a new leader, normal operations resume right away. If the error continues, your nodes might have network drops or resource limits.

What causes mongodb error 216 in a replica set

A replica set relies on one primary node to handle all write requests. Secondary nodes copy data from the primary and watch its health with heartbeats. When the primary stops answering heartbeats, secondary nodes start a new leader election. Any command that needs a stable leader during this vote gets rejected.

Planned maintenance often triggers an election when an admin steps down the current primary. Unplanned outages also cause elections when a server crashes or loses network reach. High CPU usage or disk pauses can delay heartbeat replies across the cluster. When heartbeats arrive late, peer nodes assume the leader is dead and vote.

Split network paths can create an election loop if nodes cannot reach a majority. Without a clear majority vote, the replica set stays stuck in the election state. Client apps will keep seeing error 216 until you restore stable network paths.

How to diagnose and fix the failure safely in production

Start by checking the replica set status and server logs to find the trigger. Look for heartbeat timeouts, slow disk warnings, or node restarts around the failure time. You do not need full admin rights to run basic health checks. MongoDB grants access through role-based rules that apply down to a collection level.

Give on-call responders the minimal built-in roles needed to inspect database state safely. For example, the read role lets engineers run collection stats and database stats commands. It does not allow insert, update, remove, or drop actions on production data. Keeping human access read-only stops accidental writes while the cluster is unstable.

MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

When engineers inspect sample records during an incident, protect sensitive user fields too. Data masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs. This lets the team verify data state without exposing private details in query results.

How to harden client apps against election pauses

Even healthy clusters run elections during routine upgrades and cloud host reboots. Your client apps must handle brief leader changes without crashing or losing user data. Set up your database driver to use retryable writes and a replica set URI.

When the driver hits an ElectionInProgress response, it waits a moment and tries again. By the time the retry runs, the new primary node is usually ready. Also check your read preference settings if your app only needs read queries. Routing read queries to secondary nodes keeps read traffic moving during a vote.

Finally, fix any underlying server strain that causes false heartbeat timeouts in production. Add memory or faster storage if background tasks block the main database network thread. Keep an odd number of voting nodes so every election reaches a fast majority.

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

What does the ElectionInProgress error message mean?

ElectionInProgress is the official name for error code 216 in the database engine. It means the replica set is voting for a new primary node right now.

Do I need write permissions to check replica set health?

No, you should use read-only roles when checking cluster health in production. Built-in read roles allow stats commands while blocking changes to your collections.

Why does error 216 keep happening every few minutes?

Repeated elections point to unstable network links or overloaded servers dropping heartbeat checks. Check your node logs for timeout alerts and verify that nodes have enough CPU.

The query runs. The emails stay hidden.