In Mongo what is the difference between sharding and replication?

后端 未结 8 1674
渐次进展
渐次进展 2020-12-12 14:43

Replication seems to be a lot simpler than sharding, unless I am missing the benefits of what sharding is actually trying to achieve. Don\'t they both provide horizontal sca

8条回答
  •  渐次进展
    2020-12-12 15:15

    Sharding

    Sharding is a technique of splitting up a large collection amongst multiple servers. When we shard, we deploy multiple mongod servers. And in the front, mongos which is a router. The application talks to this router. This router then talks to various servers, the mongods. The application and the mongos are usually co-located on the same server. We can have multiple mongos services running on the same machine. It's also recommended to keep set of multiple mongods (together called replica set), instead of one single mongod on each server. A replica set keeps the data in sync across several different instances so that if one of them goes down, we won't lose any data. Logically, each replica set can be seen as a shard. It's transparent to the application, the way MongoDB chooses to shard is we choose a shard key.

    Assume, for student collection we have stdt_id as the shard key or it could be a compound key. And the mongos server, it's a range based system. So based on the stdt_id that we send as the shard key, it'll send the request to the right mongod instance.

    So, what do we need to really know as a developer?

    • insert must include a shard key, so if it's a multi-parted shard key, we must include the entire shard key
    • we've to understand what the shard key is on collection itself
    • for an update, remove, find - if mongos is not given a shard key - then it's going to have to broadcast the request to all the different shards that cover the collection.
    • for an update - if we don't specify the entire shard key, we have to make it a multi update so that it knows that it needs to broadcast it

提交回复
热议问题