What is Arbiter in MongoDB: Role and Usage Explained
arbiter is a special member of a replica set that participates in elections to help choose the primary but does not store data. It helps maintain high availability by providing an additional vote without increasing resource usage.How It Works
Imagine a group of friends deciding who will be the leader for a game. Each friend votes, and the one with the most votes becomes the leader. In MongoDB, a replica set is like this group, and the leader is called the primary node.
An arbiter is like a friend who only votes but doesn't play the game or keep score. It doesn't store any data but helps break ties during elections to pick the primary. This way, the replica set can have an odd number of votes, which is important to avoid ties and keep the system running smoothly.
Because arbiters don't hold data, they use very little resources. They only exist to help with voting, making them useful when you want to maintain high availability without adding a full data-bearing member.
Example
This example shows how to add an arbiter to a MongoDB replica set using the rs.addArb() command.
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "mongo1:27017" },
{ _id: 1, host: "mongo2:27017" }
]
})
// Add an arbiter member
rs.addArb("mongo-arbiter:27017")When to Use
Use an arbiter when you want to maintain an odd number of voting members in a replica set but cannot add another full data-bearing node due to resource limits or cost. This helps avoid election ties and keeps your database highly available.
For example, if you have two data nodes and want to ensure automatic failover, adding an arbiter gives you a third vote without extra storage or memory needs. However, do not use arbiters in production environments where data redundancy is critical, as they do not store data.
Key Points
- An
arbitervotes in elections but does not store data. - It helps maintain an odd number of votes to avoid election ties.
- Arbiters use minimal resources compared to full members.
- They improve availability but do not provide data redundancy.
- Best used when adding a full member is not feasible.