Mongos balancer process
Problem Statement:
Whenever mongos balancer is running, the cluster gets insanely slow..
Latencies are back to normalcy immediately if the balancer stopped.
Mongos balancing process for reference:
- Balancer takes a lock
- Identify chunk to migrate. This is based on several criteria
- Send command to "Source" shard.
- Source does sanity check.
- Source sends a command to Destination shard.
- Transfer starts. Essentially, Destination does read queries from Source and writes to itself.
- Once the transfer is in sync, catch up on subsequent ops happen. inserts/deletes/updates happening during the above step doesn't stop clients from doing any op on those chunks. So far, source does all those ops. Now, destination catches up on those tasks.
- Once the catchup is finished, transfer is marked complete.
- Now the shard goes from Steady state to Critical section. Source does a commit to Config servers saying the chunk move is complete and config must be updated.
- Clean up on Source. Now that shard is aware of chunk that moved away, it needs to clean up those records on itself. So, it initiates a cleanup.
- Few subsequent requests to Source give exception to Mongos router, saying Stale config exception. This forces mongos to refresh the state by querying mongos. Client is unaware of this Stale Config exception.