@@ -13,15 +13,17 @@ Sharded Cluster Balancer
13
13
:depth: 1
14
14
:class: singlecol
15
15
16
- The MongoDB balancer is a background process that monitors the number of
17
- :term:`chunks <chunk>` on each :term:`shard`. When the number of chunks on a
18
- given shard reaches specific :ref:`migration thresholds
19
- <sharding-migration-thresholds>`, the balancer attempts to automatically
20
- migrate chunks between shards and reach an equal number of chunks per shard.
21
-
22
- The balancing procedure for :term:`sharded clusters <sharded cluster>` is
23
- entirely transparent to the user and application layer, though there may be
24
- some performance impact while the procedure takes place.
16
+ The MongoDB balancer is a background process that monitors the amount of
17
+ data on each :term:`shard` for each sharded collection. When the amount
18
+ of data for a sharded collection on a given shard reaches specific
19
+ :ref:`migration thresholds <sharding-migration-thresholds>`, the balancer
20
+ attempts to automatically migrate data between shards and reach an even
21
+ amount of data per shard while respecting the :ref:`zones
22
+ <zone-sharding>`. By default, the balancer process is always enabled.
23
+
24
+ The balancing procedure for :term:`sharded clusters <sharded cluster>`
25
+ is entirely transparent to the user and application layer, though there
26
+ may be some performance impact while the procedure takes place.
25
27
26
28
.. include:: /images/sharding-migrating.rst
27
29
@@ -39,43 +41,28 @@ The balancer runs on the primary of the config server replica set
39
41
.. _sharding-balancing-internals:
40
42
.. _sharding-internals-balancing:
41
43
42
- Cluster Balancer
43
- ----------------
44
-
45
- The :term:`balancer` process is responsible for redistributing the
46
- chunks of a sharded collection evenly among the shards for every
47
- sharded collection. By default, the balancer process is always enabled.
48
-
49
- To address uneven chunk distribution for a sharded collection, the
50
- balancer :doc:`migrates chunks </core/sharding-balancer-administration>` from
51
- shards with more chunks to shards with a fewer number of chunks. The
52
- balancer migrates the chunks until there is an even
53
- distribution of chunks for the collection across the shards. For details
54
- about chunk migration, see :ref:`chunk-migration-procedure`.
44
+ Balancer Internals
45
+ ------------------
55
46
56
- .. include:: /includes/fact-archiveMovedChunks.rst
57
-
58
- Chunk migrations carry some overhead in terms of bandwidth and
59
- workload, both of which can impact database performance. [#auto-distribute]_ The
60
- :term:`balancer` attempts to minimize the impact by:
47
+ Range migrations carry some overhead in terms of bandwidth and
48
+ workload, both of which can impact database performance.
49
+ The :term:`balancer` attempts to minimize the impact by:
61
50
62
51
- Restricting a shard to at most one migration at any given time.
63
- Specifically, a shard cannot participate in multiple chunk migrations
64
- at the same time. To migrate multiple chunks from a shard, the
65
- balancer migrates the chunks one at a time.
52
+ Specifically, a shard cannot participate in multiple data migrations
53
+ at the same time. The balancer migrates ranges one at a time.
66
54
67
- MongoDB can perform parallel chunk migrations, but a shard can
55
+ MongoDB can perform parallel data migrations, but a shard can
68
56
participate in at most one migration at a time. For a sharded cluster
69
57
with *n* shards, MongoDB can perform at most *n/2* (rounded down)
70
- simultaneous chunk migrations.
58
+ simultaneous migrations.
71
59
72
- See also :ref:`chunk -migration-queuing`.
60
+ See also :ref:`range -migration-queuing`.
73
61
74
- - Starting a balancing round **only** when the difference in the
75
- number of chunks between the shard with the greatest number of chunks
76
- for a sharded collection and the shard with the lowest number of
77
- chunks for that collection reaches the :ref:`migration threshold
78
- <sharding-migration-thresholds>`.
62
+ - Starting a balancing round **only** when the difference in the amount
63
+ of data between the shard with the most data for a sharded collection
64
+ and the shard with the least data for that collection reaches the
65
+ :ref:`migration threshold <sharding-migration-thresholds>`.
79
66
80
67
You may disable the balancer temporarily for maintenance. See
81
68
:ref:`sharding-balancing-disable-temporally` for details.
@@ -91,76 +78,69 @@ Window <sharding-schedule-balancing-window>` for details.
91
78
92
79
.. seealso::
93
80
94
- :doc:`/tutorial/manage-sharded-cluster-balancer`
95
-
96
-
97
- .. [#auto-distribute]
98
-
99
- .. include:: /includes/extracts/zoned-sharding-shard-operation-chunk-distribution.rst
100
-
101
- .. include:: /includes/extracts/zoned-sharding-shard-operation-chunk-distribution-hashed-short.rst
102
-
103
- See :ref:`pre-define-zone-range-hashed-example` for an example.
81
+ :ref:`<sharded-cluster-balancer>`
104
82
105
83
106
84
Adding and Removing Shards from the Cluster
107
85
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108
86
109
87
Adding a shard to a cluster creates an imbalance, since the new
110
- shard has no chunks . While MongoDB begins migrating data to the new
111
- shard immediately, it can take some time before the cluster balances. See the
112
- :doc:`/tutorial/add-shards-to -shard-cluster` tutorial for instructions on
113
- adding a shard to a cluster.
88
+ shard has no data . While MongoDB begins migrating data to the new
89
+ shard immediately, it can take some time before the cluster balances.
90
+ See the :ref:`Add Shards to a Cluster <sharding-procedure-add -shard>`
91
+ tutorial for instructions on adding a shard to a cluster.
114
92
115
- Removing a shard from a cluster creates a similar imbalance, since chunks
116
- residing on that shard must be redistributed throughout the cluster. While
117
- MongoDB begins draining a removed shard immediately, it can take some time
118
- before the cluster balances. *Do not* shutdown the servers associated
119
- to the removed shard during this process.
93
+ Removing a shard from a cluster creates a similar imbalance, since data
94
+ residing on that shard must be redistributed throughout the cluster.
95
+ While MongoDB begins draining a removed shard immediately, it can take
96
+ some time before the cluster balances. *Do not* shutdown the servers
97
+ associated to the removed shard during this process.
120
98
121
99
.. include:: /includes/fact-remove-shard-balance-order.rst
122
100
123
- See the :doc:`/tutorial/remove-shards-from-cluster` tutorial for
124
- instructions on safely removing a shard from a cluster.
101
+ See the :ref:`Remove Shards from a Cluster
102
+ <remove-shards-from-cluster-tutorial>` tutorial for instructions on
103
+ safely removing a shard from a cluster.
125
104
126
105
.. seealso::
127
106
128
107
:method:`sh.balancerCollectionStatus()`
129
108
130
109
131
110
.. _chunk-migration-procedure:
111
+ .. _range-migration-procedure:
132
112
133
- Chunk Migration Procedure
113
+ Range Migration Procedure
134
114
-------------------------
135
115
136
- All chunk migrations use the following procedure:
116
+ All range migrations use the following procedure:
137
117
138
- #. The balancer process sends the :dbcommand:`moveChunk ` command to
118
+ #. The balancer process sends the :dbcommand:`moveRange ` command to
139
119
the source shard.
140
120
141
121
#. The source starts the move when it receives an internal
142
122
:dbcommand:`moveRange` command. During the migration process,
143
- operations to the chunk are sent to the source shard. The source
144
- shard is responsible for incoming write operations for the chunk .
123
+ operations to the range are sent to the source shard. The source
124
+ shard is responsible for incoming write operations for the range .
145
125
146
126
#. The destination shard builds any indexes required by the source
147
127
that do not exist on the destination.
148
128
149
- #. The destination shard begins requesting documents in the chunk and
129
+ #. The destination shard begins requesting documents in the range and
150
130
starts receiving copies of the data. See also
151
- :ref:`chunk -migration-replication`.
131
+ :ref:`range -migration-replication`.
152
132
153
- #. After receiving the final document in the chunk , the
133
+ #. After receiving the final document in the range , the
154
134
destination shard starts a synchronization process to ensure that it
155
135
has the changes to the migrated documents that occurred during the
156
136
migration.
157
137
158
138
#. When fully synchronized, the source shard connects to the
159
139
:term:`config database` and updates the cluster metadata with the new
160
- location for the chunk .
140
+ location for the range .
161
141
162
142
#. After the source shard completes the update of the metadata,
163
- and once there are no open cursors on the chunk , the source shard
143
+ and once there are no open cursors on the range , the source shard
164
144
deletes its copy of the documents.
165
145
166
146
.. note::
@@ -174,9 +154,6 @@ All chunk migrations use the following procedure:
174
154
175
155
:ref:`moveChunk-directory`
176
156
177
- The migration process ensures consistency and maximizes the availability of
178
- chunks during balancing.
179
-
180
157
.. seealso::
181
158
182
159
:serverstatus:`shardingStatistics.countDonorMoveChunkLockTimeout`
@@ -189,87 +166,69 @@ Migration Thresholds
189
166
190
167
To minimize the impact of balancing on the cluster, the
191
168
:term:`balancer` only begins balancing after the distribution of
192
- chunks for a sharded collection has reached certain thresholds. The
193
- thresholds apply to the difference in number of :term:`chunks <chunk>`
194
- between the shard with the most chunks for the collection and the shard
195
- with the fewest chunks for that collection. The balancer has the
196
- following thresholds:
197
-
198
- .. list-table::
199
- :header-rows: 1
200
-
201
- * - Number of Chunks
202
- - Migration Threshold
203
-
204
- * - Fewer than 20
205
- - 2
169
+ date for a sharded collection has reached certain thresholds.
206
170
207
- * - 20-79
208
- - 4
209
-
210
- * - 80 and greater
211
- - 8
212
-
213
- The balancer stops running on the target collection when the difference
214
- between the number of chunks on any two shards for that collection is *less
215
- than two*, or a chunk migration fails.
171
+ A collection is considered balanced if the difference in data between
172
+ shards (for that collection) is less than :ref:`migration thresholds
173
+ <sharding-migration-thresholds>`.
216
174
217
175
.. seealso::
218
176
219
177
:method:`sh.balancerCollectionStatus()`
220
178
221
179
222
180
.. _chunk-migration-queuing:
181
+ .. _range-migration-queuing:
223
182
.. _asynchronous-chunk-migration-cleanup:
224
183
225
- Asynchronous Chunk Migration Cleanup
184
+ Asynchronous Range Migration Cleanup
226
185
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
227
186
228
- To migrate multiple chunks from a shard, the balancer migrates the
229
- chunks one at a time. However, the balancer does not wait for the
230
- current migration's delete phase to complete before starting the next
231
- chunk migration. See :ref:`sharding-chunk -migration` for the chunk
187
+ To migrate data from a shard, the balancer migrates the
188
+ data one range at a time. However, the balancer does not wait for the
189
+ current migration's delete phase to complete before starting the next
190
+ range migration. See :ref:`sharding-range -migration` for the range
232
191
migration process and the delete phase.
233
192
234
- This queuing behavior allows shards to unload chunks more quickly in
193
+ This queuing behavior allows shards to unload data more quickly in
235
194
cases of heavily imbalanced cluster, such as when performing initial
236
195
data loads without pre-splitting and when adding new shards.
237
196
238
197
This behavior also affects the :dbcommand:`moveRange` command, and
239
198
migration scripts that use the :dbcommand:`moveRange` command may
240
199
proceed more quickly.
241
200
242
- In some cases, the delete phases may persist longer. Starting in MongoDB
243
- 4.4, chunk migrations are enhanced to be more resilient in the event of
244
- a failover during the delete phase. Orphaned documents are cleaned up
245
- even if a replica set's primary crashes or restarts during this phase.
201
+ In some cases, the delete phases may persist longer. Range migrations
202
+ are enhanced to be more resilient in the event of a failover during the
203
+ delete phase. Orphaned documents are cleaned up even if a replica set's
204
+ primary crashes or restarts during this phase.
246
205
247
- The ``_waitForDelete``, available as a setting for the balancer as well
248
- as the :dbcommand:`moveRange` command, can alter the behavior so that
206
+ The ``_waitForDelete`` balancer setting can alter the behavior so that
249
207
the delete phase of the current migration blocks the start of the next
250
208
chunk migration. The ``_waitForDelete`` is generally for internal
251
- testing purposes. For more information, see
209
+ testing purposes. For more information, see
252
210
:ref:`wait-for-delete-setting`.
253
211
254
212
.. _chunk-migration-replication:
213
+ .. _range-migration-replication:
255
214
256
- Chunk Migration and Replication
215
+ Range Migration and Replication
257
216
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
258
217
259
- During chunk migration, the ``_secondaryThrottle`` value determines
260
- when the migration proceeds with next document in the chunk .
218
+ During range migration, the ``_secondaryThrottle`` value determines
219
+ when the migration proceeds with next document in the range .
261
220
262
221
In the :data:`config.settings` collection:
263
222
264
223
- If the ``_secondaryThrottle`` setting for the balancer is set to a
265
- write concern, each document move during chunk migration must receive
224
+ write concern, each document move during range migration must receive
266
225
the requested acknowledgement before proceeding with the next
267
226
document.
268
227
269
228
- If the ``_secondaryThrottle`` setting for the balancer is set to
270
- ``true``, each document move during chunk migration must receive
229
+ ``true``, each document move during range migration must receive
271
230
acknowledgement from at least one secondary before the migration
272
- proceeds with the next document in the chunk . This is equivalent to a
231
+ proceeds with the next document in the range . This is equivalent to a
273
232
write concern of :writeconcern:`{ w: 2 } <\<number\>>`.
274
233
275
234
- If the ``_secondaryThrottle`` setting is unset, the migration process
@@ -280,17 +239,16 @@ To update the ``_secondaryThrottle`` parameter for the balancer, see
280
239
:ref:`sharded-cluster-config-secondary-throttle` for an example.
281
240
282
241
Independent of any ``_secondaryThrottle`` setting, certain phases of
283
- the chunk migration have the following replication policy:
284
-
285
- - MongoDB briefly pauses all application reads and writes to the
286
- collection being migrated, on the source shard, before updating the
287
- config servers with the new location for the chunk, and resumes the
288
- application reads and writes after the update. The chunk move requires
289
- all writes to be acknowledged by majority of the members of the
290
- replica set both before and after committing the chunk move to config
291
- servers.
292
-
293
- - When an outgoing chunk migration finishes and cleanup occurs, all
242
+ the range migration have the following replication policy:
243
+
244
+ - MongoDB briefly pauses all application reads and writes to the
245
+ collection being migrated to on the source shard before updating the
246
+ config servers with the range location. MongoDB resumes application
247
+ reads and writes after the update. The range move requires all writes
248
+ to be acknowledged by majority of the members of the replica set both
249
+ before and after committing the range move to config servers.
250
+
251
+ - When an outgoing migration finishes and cleanup occurs, all
294
252
writes must be replicated to a majority of servers before further
295
253
cleanup (from other outgoing migrations) or new incoming migrations
296
254
can proceed.
@@ -300,11 +258,12 @@ To update the ``_secondaryThrottle`` setting in the
300
258
:ref:`sharded-cluster-config-secondary-throttle` for an example.
301
259
302
260
.. _migration-chunk-size-limit:
261
+ .. _migration-range-size-limit:
303
262
304
- Maximum Number of Documents Per Chunk to Migrate
263
+ Maximum Number of Documents Per Range to Migrate
305
264
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
306
265
307
- .. include:: /includes/limits-sharding-maximum-documents-chunk .rst
266
+ .. include:: /includes/limits-sharding-maximum-documents-range .rst
308
267
309
268
.. _range-deletion-performance-tuning:
310
269
0 commit comments