Search code examples
mongodbsharding

Is it possible to replicaset and shard a collection?


I've seen that for making a data distribution i can choose between ReplicaSet or Sharding. Using a mongodb code with those parameters --shardsvr --replSet.

mongod --shardsvr --port 27021 --replSet rs1 --dbpath C:\data\data5 --bind_ip localhost

What i'm doing is making a shard server that is also a replicaSet or i'm just doing a shard? And if i'm not doing both what should i write?

Edit

mongod --shardsvr --port 27020 --replSet rs1 --dbpath C:\data\data4 --bind_ip localhost
mongod --shardsvr --port 27021 --replSet rs1 --dbpath C:\data\data5 --bind_ip localhost
mongod --shardsvr --port 27022 --replSet rs1 --dbpath C:\data\data6 --bind_ip localhost
mongos --port 40000 --configdb rs0/localhost:27017
mongo --port 40000
rs.initiate( { _id : "rs1",  members: [   { _id: 0, host: "localhost:27020" },   { _id: 1, host: "localhost:27021" },   { _id: 2, host: "localhost:27022" }    ] })
sh.addShard( "rs1/localhost:27020,localhost:27021,localhost:27022")
sh.enableSharding("db")
sh.shardCollection("db.collection", { "_id" : 1 } )

When i do this kind of a code what i get is a collection sharded by 3 "servers" that are sharding and repliceSet aswell? And with mongos on 40'000 port i can interface and make querys?


Solution

  • Replica sets and sharding serve different purposes:

    • replica sets provide redundancy (if one node fails, data is also on other nodes)
    • sharding provides scalability (if you have too much data for one server, you can spread it over multiple servers)

    When you set up a sharded cluster, each shard can be (in theory) backed by a single node or a replica set. MongoDB currently requires that shards are backed by replica sets, in older versions single nodes were also permitted.

    Therefore:

    • Sharded cluster with single nodes = no redundancy. Each document is stored once, if any single node fails you lose data. But you can get scalability based on number of shards you deploy.
    • Sharded cluster with backing replica sets = both scalability and redundancy.