I have Mongo DB with two replica set. Is it possible to shard these replica set.
Sharding is about distributing data among servers called as shards. Sharding is enabled on a database, but collection(s) is/are sharded (a shard cluster can have sharded and unsharded collections). A replica-set can become a shard, within a shard cluster.
So, two replica-sets can become two shards in a shard cluster. Still, you have to enable sharding on a database and shard the collections you want to.
The main components in shard cluster are:
These components need to be configured first; the shard replica-sets to be used in a shard cluster, the config server replica-set and the mongos process. Then sharding is to be enabled on the database (on the replica-set).
For example, you have a database called as 'receivables' with collections customers
andinvoices
on one of the two replica-sets. You choose to shard the customer collection only. When the customer collection is sharded, the customers data is distributed among the two shards, based on a defined shard key. Defining a shard key is part of the process of sharding a collection.
In fact deciding on which field is going to be the shard key is one of the most important aspects of sharding. This determines how effectively: (i) the queries are going to be, (ii) the data is going to be distributed evenly among the shards, and (iii) the future scaling with added shards to the cluster.
Shard key is important. This field on the collection must be immutable and also the values must be immutable. All the documents in the collection must have the shard key field.
If you already have a running application (with MongoDB database) and planning to shard the database, an important consideration: what is the shard key going to be and how it will affect the present application (the existing queries). Sharding is not just about distributing data among more servers, it is also about designing aspects of the application and its data.
Useful Links: