Skip to content

Commit 4c26eca

Browse files
committed
Sharding-internals draft review and updates
1 parent 3a283e1 commit 4c26eca

File tree

1 file changed

+74
-75
lines changed

1 file changed

+74
-75
lines changed

draft/core/sharding-internals.txt

Lines changed: 74 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ Sharding Internals
1010

1111
This document introduces lower level sharding concepts for users who
1212
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.
1717

1818
.. index:: shard key; internals
1919
.. _sharding-internals-shard-keys:
2020

2121
Shard Keys
2222
----------
2323

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
2626
:ref:`overview of shard keys <sharding-shard-keys>` for an
2727
introduction these topics.
2828

@@ -32,36 +32,38 @@ introduction these topics.
3232
Cardinality
3333
~~~~~~~~~~~
3434

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.
3838

39-
- Consider using a ``state`` field:
39+
For example, consider a collection of data such as an "address book"
40+
that stores address records:
4041

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
4346
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.
4548

4649
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:
5052

5153
- 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.
5557

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
5759
able to use more than that number of shards for this collection.
5860

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:
6062

6163
While this field has a large number of possible values, and thus has
6264
*higher cardinality,* it's possible that a large number of users
6365
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.
6567

6668
In these cases, cardinality depends on the data. If your address book
6769
stores records for a geographically distributed contact list
@@ -70,18 +72,17 @@ of data such as an "address book" that stores address records:
7072
more geographically concentrated (e.g "ice cream stores in Boston
7173
Massachusetts,") then you may have a much lower cardinality.
7274

73-
- Consider using the ``phone-number`` field:
75+
- Consider the use of a ``phone-number`` field as a shard key:
7476

7577
The contact's telephone number has a *higher cardinality,* because
7678
most users will have a unique value for this field, MongoDB will be
7779
able to split in as many chunks as needed.
7880

7981
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
8183
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.
8586

8687
.. index:: shard key; write scaling
8788
.. _sharding-shard-key-write-scaling:
@@ -94,15 +95,15 @@ the increased write capacity that the shard cluster can provide, while
9495
others do not. Consider the following example where you shard by the
9596
default :term:`_id` field, which holds an :term:`ObjectID`.
9697

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
100101
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.
106107

107108
In most cases want to avoid these kinds of shard keys, except in some
108109
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
113114
operations across the *entire cluster*.
114115

115116
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
117118
other content in the document, will allow the cluster to scale write
118119
operations. However, random shard keys do not typically provide
119120
:ref:`query isolation <sharding-shard-key-query-isolation>`, which is
@@ -122,16 +123,16 @@ another important characteristic of shard keys.
122123
Querying
123124
~~~~~~~~
124125

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.
135136

136137
.. seealso:: The ":ref:`mongos and Sharding <sharding-mongos>`" and
137138
":ref:`config server <sharding-config-server>`" sections for a more
@@ -153,15 +154,15 @@ application, which can be a long running operation.
153154
If your query includes the first component of a compound :term:`shard
154155
key` [#shard-key-index], then the :program:`mongos` can route the
155156
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
157158
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.
159160

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
162163
are most performance dependent. If this field is not sufficiently
163164
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.
165166

166167
.. see:: ":ref:`sharding-mongos`" for more information on query
167168
operations in the context of sharded clusters.
@@ -197,13 +198,13 @@ are:
197198
- to ensure that :program:`mongos` can isolate most to specific
198199
:program:`mongod` instances.
199200

200-
In addition, consider the following operation consideration that the
201+
In addition, consider the following operational factors that the
201202
shard key can affect.
202203

203204
Because each shard should be a :term:`replica set`, if a specific
204205
: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
207208
data will be unavailable. If your shard key distributes data required
208209
for every operation throughout the cluster, then the failure of the
209210
entire shard will render the entire cluster unusable. By contrast, if
@@ -236,23 +237,23 @@ are three options:
236237
is insignificant in your use case given limited write volume,
237238
expected data size, or query patterns and demands.
238239

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
240241
that will provide the required :ref:`query isolation
241242
<sharding-shard-key-query-isolation>`, ensure that :ref:`writes will
242243
scale across the cluster <sharding-shard-key-query-isolation>`, and
243244
then add an additional field to provide additional :ref:`cardinality
244245
<sharding-shard-key-cardinality>` if your primary key does not have
245-
split-ability.
246+
sufficient split-ability.
246247

247248
.. index:: balancing; internals
248249
.. _sharding-balancing-internals:
249250

250251
Sharding Balancer
251252
-----------------
252253

253-
The :ref:`balancer <sharding-balancing>` process is responsible for
254+
The :ref:`balancer <sharding-balancing>` sub-process is responsible for
254255
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.
256257

257258
This section contains complete documentation of the balancer process
258259
and operations. For a higher level introduction see
@@ -261,35 +262,33 @@ the :ref:`Balancing <sharding-balancer>` section.
261262
Balancing Internals
262263
~~~~~~~~~~~~~~~~~~~
263264

264-
The balancer originates from an arbitrary :program:`mongos`
265+
A balancing round originates from an arbitrary :program:`mongos`
265266
instance. Because your shard cluster can have a number of
266267
: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`.
269269

270270
By default, the balancer process is always running. When the number of
271271
chunks in a collection is unevenly distributed among the shards, the
272272
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,
281280
both of which can impact database performance. As a result, MongoDB
282281
attempts to minimize the effect of balancing by only migrating chunks
283282
when the disparity between numbers of chunks on a shard is greater
284283
than 8.
285284

286285
.. index:: balancing; migration
287286

288-
The migration process ensures consistency and maximize availability of
287+
The migration process ensures consistency and maximizes availability of
289288
chunks during balancing: when MongoDB begins migrating a chunk, the
290289
database begins copying the data to the new server and tracks incoming
291290
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*
293292
updates the chunk record in the :term:`config database` to reflect the
294293
new location of the chunk.
295294

@@ -301,14 +300,14 @@ Chunk Size
301300

302301
.. TODO link this section to <glossary:chunk size>
303302

304-
The default :term:`chunk` size in MongoDB is 64 megabytes.
303+
The default maximum :term:`chunk` size in MongoDB is 64 megabytes.
305304

306-
When chunks grow beyond the :ref:`specified chunk size
305+
When chunks grow beyond the :ref:`specified maximum chunk size
307306
<sharding-chunk-size>` a :program:`mongos` instance will split the
308307
chunk in half, which will eventually lead to migrations, when chunks
309308
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.
312311

313312
Chunk size is somewhat arbitrary and must account for the
314313
following effects:

0 commit comments

Comments
 (0)