假如MongoDB分片集群采用单调递增的字段作为片键,例如系统时间,那么随着插入的增多,越来越多的文档将插入到区间到正无穷的分片上,这样,分片将不均衡,MongoDB提供了自动均衡的方法来将数据量进行均衡。
均衡进程的运行机制
The balancer is a background process that manages chunk migrations. The balancer runs in all of the query routers in a cluster.
When the distribution of a sharded collection in a cluster is uneven, the balancer process migrates chunks from the shard that has the largest number of chunks to the shard with the least number of chunks until the collection balances. For example: if collection users has 100 chunks on shard 1 and 50 chunks on shard 2, the balancer will migrate chunks from shard 1 to shard 2 until the collection achieves balance.
The shards manage chunk migrations as a background operation between an origin shard and a destination shard. During a chunk migration, the destination shard is sent all the current documents in the chunk from the origin shard. Next, the destination shard captures and applies all changes made to the data during the migration process. Finally, the metadata regarding the location of the chunk on config server is updated.
If there’s an error during the migration, the balancer aborts the process leaving the chunk unchanged on the origin shard. MongoDB removes the chunk’s data from the origin shard after the migration completes successfully.
在实际中,通常会把MongoDB自动均衡这个选项禁掉,也就是不允许MongoDB做自动均衡,那么问题来了,当新增一个分片时,如果把原来分片中的数据移动到新分片,做到数据均衡
相关推荐
在本篇 MongoDB 学习笔记中,我们将聚焦于 MongoDB 的集群和分片(sharding)配置,这是实现大规模数据存储和处理的关键特性。 1. **MongoDB 集群**: - 集群是由多个独立的 MongoDB 实例组成的,它们共同提供高...
数据可以根据特定的分片键自动分布在多个服务器上,当数据量增大时,系统能自动调整数据分布,保持集群的负载均衡。 5. **查询语言**:MongoDB 拥有强大的查询语言,类似于面向对象的查询,支持丰富的查询、排序、...
Redis集群的建立和管理可以进一步提升数据库的高可用性和扩展性,支持数据分片和负载均衡。 在分布式系统中,Redis可用于实现分布式Session管理,提高系统的可扩展性和灵活性。通过合理的配置和使用Redis的特性,...
这将涉及文档型数据库的特点,如CRUD操作、聚合框架、分片和副本集等,对于处理大规模非结构化数据的场景尤其重要。 `Tomcat面试专题及答案`讨论了Java应用服务器Tomcat,面试者应理解Tomcat的工作原理,包括部署...
- 分片(Sharding):将大型数据库分成多个较小的部分,分布到多个节点上。 - 数据复制(Replication):保持多个数据库节点间的数据同步。 - 负载均衡:自动分配数据库请求,优化系统性能。 9. **云数据库**: ...
这份文档可能包含了MongoDB的基本操作、数据模型、查询语法、副本集和分片等主题。 9. **Nginx课程笔记.pdf**:Nginx是一个高性能的HTTP和反向代理服务器,以其高效的并发处理能力而闻名。笔记可能涵盖Nginx的配置...