MongoDB Config Servers
What Is Config Servers?
Config servers hold the metadata a sharded cluster needs to function: which shards exist, which collections are sharded, their shard keys, and which chunk ranges live on which shard. In production they run as a config server replica set (CSRS), and mongos routers read from it to route operations. The data is stored in the config database.
Why It Matters
| Reason | Benefit |
|---|---|
| Single source of truth | All routers and shards agree on where data lives. |
| Supports rebalancing | Chunk migrations are recorded in the config metadata. |
| Highly available | A replica set means metadata survives member failures. |
| Separates concerns | Metadata is isolated from your application data on the shards. |
How To Use It
- Create a three-member replica set where each mongod runs with --configsvr and a replica set name such as csrs.
- Initiate it with rs.initiate() including the configsvr option set to true.
- Start the shard replica sets with --shardsvr.
- Start mongos with --configdb pointing at the config server replica set.
- Add shards and shard collections through mongos using sh.* helpers.
- Inspect metadata read-only from mongos: db.getSiblingDB("config").shards.find().
- Back up the config servers regularly, as part of cluster backup.
mongosh Examples
# Start each config server member
mongod --configsvr --replSet csrs --dbpath /data/cfg1 --port 27019 --bind_ip localhost,cfg1
// Initiate the CSRS
rs.initiate({
_id: "csrs",
configsvr: true,
members: [
{ _id: 0, host: "cfg1:27019" },
{ _id: 1, host: "cfg2:27019" },
{ _id: 2, host: "cfg3:27019" }
]
})
// From mongos: read the metadata (read-only exploration)
const cfg = db.getSiblingDB("config")
cfg.shards.find()
cfg.collections.find()
cfg.chunks.countDocuments({})
// Use helpers instead of editing config directly
sh.status() Edge Cases & Pitfalls
Never edit config by hand
Changing the config database manually can corrupt the cluster. Use sh.* helpers and admin commands.
Config outage limits operations
Without a primary on the CSRS, metadata changes such as chunk migrations and new sharded collections stop.
Must be a replica set
Modern versions require config servers as a replica set. Mirrored config servers are not supported.
Back up the metadata
Restoring shards without matching config server metadata is difficult. Include config servers in your backup plan.
Cheat Sheet
| Topic | Detail |
|---|---|
| CSRS | Config Server Replica Set; required for production sharded clusters |
| --configsvr | mongod option (or sharding.clusterRole: configsvr) to run as a config server |
| Default port | 27019 for config servers started with the configsvr role |
| config.shards | Collection listing the shards in the cluster |
| config.databases | Which databases are sharded and their primary shards |
| config.collections | Sharded collections and their shard keys |
| config.chunks | Chunk ranges and the shard that owns each (chunks collection in the config database) |
| Read by mongos | Routers cache this metadata to route queries |
| Do not edit | Never modify the config database by hand; use sh.* commands |
Key Takeaways
Treat the CSRS as critical: run it as a replica set (up to 7 members, commonly 3), back it up, and keep it healthy. If the config servers are unavailable, the cluster metadata cannot change and routers cannot refresh it.
Frequently Asked Questions
What do config servers store?
Cluster metadata: the list of shards, sharded collections with their shard keys, and chunk ranges mapped to shards.
What is CSRS?
A config server replica set, the required deployment form for config servers in production sharded clusters.
How many config servers do I need?
A three-member replica set is the common production setup. A config server replica set can have up to seven members.
Does application data live on config servers?
No. Application data lives on the shards. Config servers store only metadata.
Do clients connect to config servers?
No. Clients connect to mongos, and mongos reads the config servers.
Can I query the config database?
You can read it from mongos for inspection, but avoid writing to it. Use sh.* commands instead.
Next: MongoDB Sharding
Continue the MongoDB learning path.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
8 people found this page helpful
