@@ -10,19 +10,19 @@ Sharding Internals
10
10
11
11
This document introduces lower level sharding concepts for users who
12
12
are familiar with :term:`sharding` generally and want to learn more
13
- about the internals of sharding in MongoDB. The
14
- ":doc:`/core/sharding`" document provides an overview of higher level
15
- sharding concepts while the ":doc:`/administration/sharding`" provides
16
- an overview of common administrative tasks.
13
+ about the internals of sharding in MongoDB. The ":doc:`/core/sharding`"
14
+ document provides an overview of higher level sharding concepts while
15
+ the ":doc:`/administration/sharding`" provides an overview of common
16
+ administrative tasks.
17
17
18
18
.. index:: shard key; internals
19
19
.. _sharding-internals-shard-keys:
20
20
21
21
Shard Keys
22
22
----------
23
23
24
- Shard keys are the field in the collection that MongoDB uses to
25
- distribute :term:`documents` among a shard cluster. See the
24
+ Shard keys are the field in a collection that MongoDB uses to
25
+ distribute :term:`documents` within a sharded cluster. See the
26
26
:ref:`overview of shard keys <sharding-shard-keys>` for an
27
27
introduction these topics.
28
28
@@ -32,36 +32,38 @@ introduction these topics.
32
32
Cardinality
33
33
~~~~~~~~~~~
34
34
35
- Cardinality refers to the property of the data set that allows MongoDB
36
- to split it into :term:`chunks`. For example, consider a collection
37
- of data such as an "address book" that stores address records:
35
+ In the context of MongoDB, Cardinality, which generally refers to the
36
+ concept of counting or measuring the number of items in a set, represents
37
+ the number of possible :term:`chunks` that data can be partitioned into.
38
38
39
- - Consider using a ``state`` field:
39
+ For example, consider a collection of data such as an "address book"
40
+ that stores address records:
40
41
41
- This would hold the US state for an address document, as a shard
42
- key. This field has a *low cardinality*. All documents that have the
42
+ - Consider the use of a ``state`` field as a shard key:
43
+
44
+ The state key's value holds the US state for a given address document.
45
+ This field has a *low cardinality* as all documents that have the
43
46
same value in the ``state`` field *must* reside on the same shard,
44
- even if the chunk exceeds the chunk size.
47
+ even if a particular state's chunk exceeds the maximum chunk size.
45
48
46
49
Because there are a limited number of possible values for this
47
- field, it is easier for your data may not be evenly distributed, you
48
- risk having data distributed unevenly among a fixed or small number
49
- of chunks. In this may have a number of effects:
50
+ field, you risk having data unevenly distributed among a small
51
+ number of fixed chunks. This may have a number of effects:
50
52
51
53
- If MongoDB cannot split a chunk because it all of its documents
52
- have the same shard key, migrations involving these chunk will take
53
- longer than other migrations, and it will be more difficult for
54
- your data to balance evenly .
54
+ have the same shard key, migrations involving these un-splittable
55
+ chunks will take longer than other migrations, and it will be more
56
+ difficult for your data to stay balanced .
55
57
56
- - If you have a fixed maximum number of chunks you will never be
58
+ - If you have a fixed maximum number of chunks, you will never be
57
59
able to use more than that number of shards for this collection.
58
60
59
- - Consider using the ``postal-code`` field (i.e. zip code:)
61
+ - Consider the use of a ``postal-code`` field (i.e. zip code) as a shard key:
60
62
61
63
While this field has a large number of possible values, and thus has
62
64
*higher cardinality,* it's possible that a large number of users
63
65
could have the same value for the shard key, which would make this
64
- chunk of users un-splitable .
66
+ chunk of users un-splittable .
65
67
66
68
In these cases, cardinality depends on the data. If your address book
67
69
stores records for a geographically distributed contact list
@@ -70,18 +72,17 @@ of data such as an "address book" that stores address records:
70
72
more geographically concentrated (e.g "ice cream stores in Boston
71
73
Massachusetts,") then you may have a much lower cardinality.
72
74
73
- - Consider using the ``phone-number`` field:
75
+ - Consider the use of a ``phone-number`` field as a shard key :
74
76
75
77
The contact's telephone number has a *higher cardinality,* because
76
78
most users will have a unique value for this field, MongoDB will be
77
79
able to split in as many chunks as needed.
78
80
79
81
While "high cardinality," is necessary for ensuring an even
80
- distribution of data, having a high cardinality does not garen tee
82
+ distribution of data, having a high cardinality does not guarantee
81
83
sufficient :ref:`query isolation <sharding-shard-key-query-isolation>`
82
- or appropriate :ref:`write scaling
83
- <sharding-shard-key-write-scaling>`. Continue reading for more
84
- information on these topics.
84
+ or appropriate :ref:`write scaling <sharding-shard-key-write-scaling>`.
85
+ Continue reading for more information on these topics.
85
86
86
87
.. index:: shard key; write scaling
87
88
.. _sharding-shard-key-write-scaling:
@@ -94,15 +95,15 @@ the increased write capacity that the shard cluster can provide, while
94
95
others do not. Consider the following example where you shard by the
95
96
default :term:`_id` field, which holds an :term:`ObjectID`.
96
97
97
- The ``ObjectID`` holds a value, computed upon creation, that is a
98
- unique identifier for the object. However, the most significant data in
99
- this value a is time stamp, which means that they increment
98
+ The ``ObjectID`` holds a value, computed upon document creation, that is a
99
+ unique identifier for the object. However, the most significant bits of data
100
+ in this value represent a time stamp, which means that they increment
100
101
in a regular and predictable pattern. Even though this value has
101
- :ref:`high cardinality <sharding-shard-key-cardinality>`, when
102
- this, or *any date or other incrementing number* as the shard key all
103
- insert operations will always end up on the same shard. As a result,
104
- the capacity of this node will define the effective capacity of the
105
- cluster.
102
+ :ref:`high cardinality <sharding-shard-key-cardinality>`, when using
103
+ this, or *any date or other monotonically increasing number* as the shard
104
+ key, all insert operations will be storing data into a single chunk, and
105
+ therefore, a single shard. As a result, the write capacity of this node
106
+ will define the effective write capacity of the cluster.
106
107
107
108
In most cases want to avoid these kinds of shard keys, except in some
108
109
situations: For example if you have a very low insert rate, most of
@@ -113,7 +114,7 @@ have *both* high cardinality and that will generally distribute write
113
114
operations across the *entire cluster*.
114
115
115
116
Typically, a computed shard key that has some amount of "randomness,"
116
- such as ones that include a cryptograpphic hash (i.e. MD5 or SHA1) of
117
+ such as ones that include a cryptographic hash (i.e. MD5 or SHA1) of
117
118
other content in the document, will allow the cluster to scale write
118
119
operations. However, random shard keys do not typically provide
119
120
:ref:`query isolation <sharding-shard-key-query-isolation>`, which is
@@ -122,16 +123,16 @@ another important characteristic of shard keys.
122
123
Querying
123
124
~~~~~~~~
124
125
125
- The :program:`mongos` provides an interface for applications that use
126
- sharded database instances. The :program:`mongos` hides all of the
127
- complexity of :term:`partitioning <partition>` from the
128
- application. The :program:`mongos` receives queries from applications,
129
- and then using the metadata from the :ref:`config server
130
- <sharding-config-database>` to route the query to the
131
- :program:`mongod` instances that provide the :term:`shards
132
- <shard>`. While the :program:`mongos` succeeds in making all querying
133
- operational in sharded environments, the :term:`shard key` you select
134
- can have a profound affect on query performance.
126
+ The :program:`mongos` program provides an interface for applications
127
+ that query sharded clusters and :program:`mongos` hides all of the
128
+ complexity of data :term:`partitioning <partition>` from the
129
+ application. A :program:`mongos` receives queries from applications,
130
+ and then, using the metadata from the :ref:`config server
131
+ <sharding-config-database>`, routes queries to the :program:`mongod`
132
+ instances that hold the appropriate the data. While the :program:`mongos`
133
+ succeeds in making all querying operational in sharded environments,
134
+ the :term:`shard key` you select can have a profound affect on query
135
+ performance.
135
136
136
137
.. seealso:: The ":ref:`mongos and Sharding <sharding-mongos>`" and
137
138
":ref:`config server <sharding-config-server>`" sections for a more
@@ -153,15 +154,15 @@ application, which can be a long running operation.
153
154
If your query includes the first component of a compound :term:`shard
154
155
key` [#shard-key-index], then the :program:`mongos` can route the
155
156
query directly to a single shard, or a small number of shards, which
156
- provides much greater performance. Even you query values of the shard
157
+ provides much greater performance. Even if you query values of the shard
157
158
key that reside in different chunks, the :program:`mongos` will route
158
- queires directly to the specific shard.
159
+ queries directly to the specific shard.
159
160
160
- To select a shard key for a collection: determine which fields your
161
- queries select by most frequently and then which of these operations
161
+ To select a shard key for a collection: determine which fields are included
162
+ most frequently in queries for a given application and which of these operations
162
163
are most performance dependent. If this field is not sufficiently
163
164
selective (i.e. has low cardinality) you can add a second field to the
164
- compound shard key to make the cluster more splitable .
165
+ compound shard key to make the data more splittable .
165
166
166
167
.. see:: ":ref:`sharding-mongos`" for more information on query
167
168
operations in the context of sharded clusters.
@@ -197,13 +198,13 @@ are:
197
198
- to ensure that :program:`mongos` can isolate most to specific
198
199
:program:`mongod` instances.
199
200
200
- In addition, consider the following operation consideration that the
201
+ In addition, consider the following operational factors that the
201
202
shard key can affect.
202
203
203
204
Because each shard should be a :term:`replica set`, if a specific
204
205
:program:`mongod` instance fails, the replica set will elect another
205
- member of that set to :term:`primary` and continue function. However,
206
- if an entire shard is unreachable or fails for some reason then that
206
+ member of that set to be :term:`primary` and continue to function. However,
207
+ if an entire shard is unreachable or fails for some reason, that
207
208
data will be unavailable. If your shard key distributes data required
208
209
for every operation throughout the cluster, then the failure of the
209
210
entire shard will render the entire cluster unusable. By contrast, if
@@ -236,23 +237,23 @@ are three options:
236
237
is insignificant in your use case given limited write volume,
237
238
expected data size, or query patterns and demands.
238
239
239
- From a decision making stand point, begin by finding the the field
240
+ From a decision making stand point, begin by finding the field
240
241
that will provide the required :ref:`query isolation
241
242
<sharding-shard-key-query-isolation>`, ensure that :ref:`writes will
242
243
scale across the cluster <sharding-shard-key-query-isolation>`, and
243
244
then add an additional field to provide additional :ref:`cardinality
244
245
<sharding-shard-key-cardinality>` if your primary key does not have
245
- split-ability.
246
+ sufficient split-ability.
246
247
247
248
.. index:: balancing; internals
248
249
.. _sharding-balancing-internals:
249
250
250
251
Sharding Balancer
251
252
-----------------
252
253
253
- The :ref:`balancer <sharding-balancing>` process is responsible for
254
+ The :ref:`balancer <sharding-balancing>` sub- process is responsible for
254
255
redistributing chunks evenly among the shards and ensuring that each
255
- member of the cluster is responsible for the same amount of data.
256
+ member of the cluster is responsible for the same volume of data.
256
257
257
258
This section contains complete documentation of the balancer process
258
259
and operations. For a higher level introduction see
@@ -261,35 +262,33 @@ the :ref:`Balancing <sharding-balancer>` section.
261
262
Balancing Internals
262
263
~~~~~~~~~~~~~~~~~~~
263
264
264
- The balancer originates from an arbitrary :program:`mongos`
265
+ A balancing round originates from an arbitrary :program:`mongos`
265
266
instance. Because your shard cluster can have a number of
266
267
:program:`mongos` instances, when a balancer process is active it
267
- creates a "lock" document in the ``locks`` collection of the
268
- ``config`` database on the :term:`config server`.
268
+ acquires a "lock" by modifying a document on the :term:`config server`.
269
269
270
270
By default, the balancer process is always running. When the number of
271
271
chunks in a collection is unevenly distributed among the shards, the
272
272
balancer begins migrating :term:`chunks` from shards with a
273
- disproportionate number of chunks to a shard with fewer number of
274
- chunks. The balancer will continue migrating chunks, one at a time
275
- beginning with the shard that has the lowest shard key, until the data
276
- is evenly distributed among the shards (i.e. the difference between
277
- any two chunks is less than 2 chunks.)
278
-
279
- While these automatic chunk migrations crucial for distributing data
280
- they carry some overhead in terms of bandwidth and system workload,
273
+ disproportionate number of chunks to shards with a fewer number of
274
+ chunks. The balancer will continue migrating chunks, one at a time,
275
+ until the data is evenly distributed among the shards (i.e. the
276
+ difference between any two shards is less than 2 chunks.)
277
+
278
+ While these automatic chunk migrations are crucial for distributing
279
+ data, they carry some overhead in terms of bandwidth and workload,
281
280
both of which can impact database performance. As a result, MongoDB
282
281
attempts to minimize the effect of balancing by only migrating chunks
283
282
when the disparity between numbers of chunks on a shard is greater
284
283
than 8.
285
284
286
285
.. index:: balancing; migration
287
286
288
- The migration process ensures consistency and maximize availability of
287
+ The migration process ensures consistency and maximizes availability of
289
288
chunks during balancing: when MongoDB begins migrating a chunk, the
290
289
database begins copying the data to the new server and tracks incoming
291
290
write operations. After migrating the chunks, the "from"
292
- :program:`mongod` sends all new writes, to the "to" server, and *then*
291
+ :program:`mongod` sends all new writes, to the receiving server, and *then*
293
292
updates the chunk record in the :term:`config database` to reflect the
294
293
new location of the chunk.
295
294
@@ -301,14 +300,14 @@ Chunk Size
301
300
302
301
.. TODO link this section to <glossary:chunk size>
303
302
304
- The default :term:`chunk` size in MongoDB is 64 megabytes.
303
+ The default maximum :term:`chunk` size in MongoDB is 64 megabytes.
305
304
306
- When chunks grow beyond the :ref:`specified chunk size
305
+ When chunks grow beyond the :ref:`specified maximum chunk size
307
306
<sharding-chunk-size>` a :program:`mongos` instance will split the
308
307
chunk in half, which will eventually lead to migrations, when chunks
309
308
become unevenly distributed among the cluster, the :program:`mongos`
310
- instances will initiate a round migrations to redistribute data in the
311
- cluster.
309
+ instances will initiate a round of migrations to redistribute data
310
+ in the cluster.
312
311
313
312
Chunk size is somewhat arbitrary and must account for the
314
313
following effects:
0 commit comments