MongoDB Config Servers

Intermediate
6 min read
Updated: Nov 2026
Getting Started

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

ReasonBenefit
Single source of truthAll routers and shards agree on where data lives.
Supports rebalancingChunk migrations are recorded in the config metadata.
Highly availableA replica set means metadata survives member failures.
Separates concernsMetadata is isolated from your application data on the shards.

How To Use It

  1. Create a three-member replica set where each mongod runs with --configsvr and a replica set name such as csrs.
  2. Initiate it with rs.initiate() including the configsvr option set to true.
  3. Start the shard replica sets with --shardsvr.
  4. Start mongos with --configdb pointing at the config server replica set.
  5. Add shards and shard collections through mongos using sh.* helpers.
  6. Inspect metadata read-only from mongos: db.getSiblingDB("config").shards.find().
  7. Back up the config servers regularly, as part of cluster backup.

mongosh Examples

shell / mongosh
# 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

TopicDetail
CSRSConfig Server Replica Set; required for production sharded clusters
--configsvrmongod option (or sharding.clusterRole: configsvr) to run as a config server
Default port27019 for config servers started with the configsvr role
config.shardsCollection listing the shards in the cluster
config.databasesWhich databases are sharded and their primary shards
config.collectionsSharded collections and their shard keys
config.chunksChunk ranges and the shard that owns each (chunks collection in the config database)
Read by mongosRouters cache this metadata to route queries
Do not editNever 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.

MongoDB Sharding →

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful