MongoDB mongos
What Is mongos?
In a sharded cluster, applications do not talk to shards directly. They connect to mongos, a lightweight router process. For each request, mongos reads cluster metadata (cached from the config servers) to find which shards hold the relevant data, forwards the operation, and merges results into a single response. To your application, a sharded cluster looks like one database.
Why It Matters
| Reason | Benefit |
|---|---|
| Hides complexity | Your application code does not need to know where data is stored. |
| Scales independently | Add more mongos instances as client load grows. |
| Efficient routing | Targeted queries avoid touching irrelevant shards. |
| Single entry point | Admins run sharding commands in one place. |
How To Use It
- Start the config server replica set (CSRS) and the shard replica sets first.
- Start mongos with --configdb pointing at the config server replica set.
- Connect to it with mongosh on port 27017.
- Add shards with sh.addShard() using each replica set's connection string.
- Enable sharding on a database and shard a collection with sh.shardCollection().
- Point your application connection string at one or more mongos instances.
- Use explain() to see whether queries are targeted or hit all shards.
mongosh Examples
# Start a router pointing at the config server replica set
mongos --configdb csrs/cfg1:27019,cfg2:27019,cfg3:27019 --bind_ip localhost,router1 --port 27017
// In mongosh connected to mongos
sh.addShard("shardA/a1:27018,a2:27018,a3:27018")
sh.addShard("shardB/b1:27018,b2:27018,b3:27018")
sh.enableSharding("shop")
sh.shardCollection("shop.orders", { customerId: "hashed" })
sh.status()
// Targeted vs scatter-gather: check the plan's shards
db.getSiblingDB("shop").orders.find({ customerId: 42 }).explain()
// Application connection string with two routers
// mongodb://router1:27017,router2:27017/shop Edge Cases & Pitfalls
Do not connect to shards directly
Writing to a shard bypassing mongos can create inconsistent data. Always go through the router.
Single mongos is a single point of failure
Run at least two for availability, and list them in the connection string.
Queries without the shard key are costly
They hit every shard, and the merge work happens on mongos.
Metadata cache
mongos caches routing information; it refreshes automatically when chunks move.
Cheat Sheet
| Topic | Detail |
|---|---|
| Role | Routes client operations to the correct shard(s) and merges results |
| Stateless | Does not store data; caches metadata from config servers |
| Default port | 27017 |
| Targeted query | Filter includes the shard key, so only some shards are contacted |
| Broadcast (scatter-gather) | Without the shard key, the query goes to all shards |
| Multiple mongos | Run several for scale and availability; clients list them in the URI |
| Start | mongos --configdb csrs/host1:27019,host2:27019,host3:27019 |
| sh.status() | Run from a mongos to view cluster layout |
| Connection string | mongodb://mongos1:27017,mongos2:27017/shop |
| Admin commands | Sharding commands such as sh.shardCollection run through mongos |
Key Takeaways
Connect applications to one or more mongos instances, never to individual shards. Include the shard key in queries so mongos can target specific shards.
Frequently Asked Questions
What is mongos?
The query router for a sharded cluster. It receives client requests, sends them to the right shards, and returns merged results.
Does mongos store data?
No. It is stateless apart from a cache of cluster metadata from the config servers.
How many mongos instances do I need?
At least two for availability in production. You can run one per application server or a pool behind a load balancer, since the driver can connect to many.
How does mongos know where data lives?
It reads chunk and shard metadata stored on the config servers and caches it.
What is a scatter-gather query?
A query without the shard key that mongos must send to every shard and then combine.
Can I connect to a shard directly?
You can technically, but you should not for application work. Use mongos so routing and consistency are handled correctly.
Next: MongoDB Config Servers
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
