Introducing Dragonfly Cloud! Learn More

Question: How do you configure the connection string for MongoDB sharding?

Answer

MongoDB sharding is a method used to distribute data across multiple servers or clusters. It's a way to scale horizontally by partitioning data into smaller chunks or 'shards'. To connect to a sharded cluster in MongoDB, you need to use a specific format for your connection string. This connection string will include details about the mongos instances (query routers) that interface with client applications and direct operations to the appropriate shard.

Standard Connection String URI Format

The MongoDB connection string URI (Uniform Resource Identifier) format for a sharded cluster generally looks like this:

mongodb://mongos1:port,mongos2:port,.../database?option1=value1&option2=value2
  • mongodb:// is the required prefix to identify that this is a string in the standard MongoDB URI format.
  • mongos1:port,mongos2:port,... are the hostnames and port numbers of the mongos instances. You can specify one or more mongos hosts separated by commas. Including multiple mongos instances increases high availability.
  • /database is the name of the database to connect to. If the database does not exist, MongoDB creates it when you first store data for that database.
  • option1=value1&option2=value2 are the connection options as key-value pairs. While many options are available, some commonly used ones include readPreference to set the read preference, replicaSet for the name of the replica set, and ssl=true to enable SSL connection.

Example

Connecting to a sharded cluster with two mongos instances might look like this:

mongodb://mongos1.example.com:27017,mongos2.example.com:27017/myDatabase?replicaSet=myReplicaSet&readPreference=nearest

In this example:

  • mongos1.example.com:27017 and mongos2.example.com:27017 are the addresses and ports of the mongos query routers.
  • myDatabase is the database name.
  • The query includes options specifying the replica set name (myReplicaSet) and setting the read preference to the nearest node (readPreference=nearest).

Important Considerations

  • Ensure that all specified mongos instances are accessible from the client application's network environment.
  • Use DNS hostnames instead of IP addresses where possible to make future infrastructure changes easier.
  • The choice of read preference and other connection options can significantly impact your application's performance and resilience. Review the MongoDB documentation for guidance on these settings.

By correctly configuring the connection string, your application can efficiently communicate with a MongoDB sharded cluster, leveraging its scalability and availability features.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.