|
| 1 | +.. _mongoid-sharding-configuration: |
| 2 | + |
| 3 | +====================== |
| 4 | +Sharding Configuration |
| 5 | +====================== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: ruby framework, odm, code example |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +Sharding is a way to distribute your data across multiple machines. MongoDB |
| 24 | +uses sharding to support deployments with large data sets and high |
| 25 | +throughput operations. In this guide, you can learn how to configure sharding in |
| 26 | +your {+odm+} application. |
| 27 | + |
| 28 | +Declare Shard keys |
| 29 | +------------------ |
| 30 | + |
| 31 | +MongoDB uses shard keys to distribute a collection's documents across |
| 32 | +shards. A shard key is an indexed field, or multiple fields covered by a |
| 33 | +compound index, that determines the distribution of the collection's |
| 34 | +documents among the cluster's shards. In your |
| 35 | +{+odm+} application, you can declare a shard key by |
| 36 | +using the ``shard_key`` macro when you create a model. |
| 37 | + |
| 38 | +The following example creates a ``Person`` class with a shard key on the |
| 39 | +``ssn`` field: |
| 40 | + |
| 41 | +.. literalinclude:: /includes/configuration/sharding.rb |
| 42 | + :language: ruby |
| 43 | + :start-after: # start-shard-key |
| 44 | + :end-before: # end-shard-key |
| 45 | + :emphasize-lines: 6 |
| 46 | + |
| 47 | +.. note:: |
| 48 | + |
| 49 | + To shard a collection, the collection must have an index that starts with the |
| 50 | + shard key. The index can be on only the shard key, or it can be a compound index |
| 51 | + where the shard key is the prefix. You can use {+odm+}'s index-management |
| 52 | + functionality to create the index. To learn more about index management with |
| 53 | + {+odm+}, see the :ref:`Index Management <mongoid-indexes>` guide. |
| 54 | + |
| 55 | +If a model declares a shard key, {+odm+} expects the sharded collection to |
| 56 | +use the declared key for sharding. When {+odm+} reloads models, it provides |
| 57 | +the shard key along with the ``_id`` field to the ``find`` command to improve query |
| 58 | +performance. If the collection is not sharded with the specified shard key, |
| 59 | +queries might not return the expected results. |
| 60 | + |
| 61 | +Syntax |
| 62 | +~~~~~~ |
| 63 | + |
| 64 | +You can declare shard keys by using either the full MongoDB |
| 65 | +syntax or by using a shorthand syntax. |
| 66 | + |
| 67 | +The full syntax follows the format |
| 68 | +of the ``mongosh`` :manual:`shardCollection() |
| 69 | +</reference/method/sh.shardCollection>` method, and allows you to specify the |
| 70 | +following types of shard keys: |
| 71 | + |
| 72 | +- Ranged keys |
| 73 | +- Hashed keys |
| 74 | +- Compound keys |
| 75 | + |
| 76 | +The full syntax also allows you to specify collection and sharding options. |
| 77 | + |
| 78 | +The following example creates each of the preceding type of shard key on the |
| 79 | +``sson`` field: |
| 80 | + |
| 81 | +.. literalinclude:: /includes/configuration/sharding.rb |
| 82 | + :language: ruby |
| 83 | + :start-after: # start-shard-key-formats |
| 84 | + :end-before: # end-shard-key-formats |
| 85 | + |
| 86 | +The shorthand syntax allows you to declare a shard key by specifying only the |
| 87 | +field name. This syntax supports only ranged and compound shard keys, and does not allow you |
| 88 | +to specify collection or sharding options. |
| 89 | + |
| 90 | +The following example creates a ranged and a compound shard key: |
| 91 | + |
| 92 | +.. literalinclude:: /includes/configuration/sharding.rb |
| 93 | + :language: ruby |
| 94 | + :start-after: # start-shard-key-shorthand |
| 95 | + :end-before: # end-shard-key-shorthand |
| 96 | + |
| 97 | +Specify Associated and Embedded Fields |
| 98 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 99 | + |
| 100 | +You can specify a shard key on a ``belongs_to`` association in place of a field |
| 101 | +name. When doing so, {+odm+} creates the shard key on the primary key of the |
| 102 | +associated collection. |
| 103 | + |
| 104 | +The following example creates a shard key on the ``belongs_to`` association in a |
| 105 | +``Person`` model. Because the associated ``country`` collection has a primary |
| 106 | +key called ``country_id``, {+odm+} shards by that field: |
| 107 | + |
| 108 | +.. literalinclude:: /includes/configuration/sharding.rb |
| 109 | + :language: ruby |
| 110 | + :start-after: # start-shard-key-association |
| 111 | + :end-before: # end-shard-key-association |
| 112 | + |
| 113 | +You can specify a shard key on an embedded document by using dot notation to |
| 114 | +delimit the field names. The following example creates a shard key on the |
| 115 | +``address.city`` field: |
| 116 | + |
| 117 | +.. literalinclude:: /includes/configuration/sharding.rb |
| 118 | + :language: ruby |
| 119 | + :start-after: # start-shard-key-embedded |
| 120 | + :end-before: # end-shard-key-embedded |
| 121 | + |
| 122 | +.. note:: |
| 123 | + |
| 124 | + Because the period (``.``) character is used to delimit embedded fields, {+odm+} does |
| 125 | + not support creating shard keys on fields with names that contain a period |
| 126 | + character. |
| 127 | + |
| 128 | +Sharding Management Rake Tasks |
| 129 | +------------------------------ |
| 130 | + |
| 131 | +You can shard collections in your database according to the shard keys defined |
| 132 | +in your {+odm+} models by running the ``db:mongoid:shard_collections`` rake |
| 133 | +task. To ensure that the collections contain indexes that start with the shard |
| 134 | +key, you can first run the ``db:mongoid:create_indexes`` rake task. |
| 135 | + |
| 136 | +Run the following rake commands to create the indexes and shard the collections |
| 137 | +based on your model's shard keys: |
| 138 | + |
| 139 | +.. code-block:: bash |
| 140 | + |
| 141 | + rake db:mongoid:create_indexes |
| 142 | + rake db:mongoid:shard_collections |
| 143 | + |
| 144 | +Index management and sharding rake tasks do not stop when they encounter an |
| 145 | +error with a particular model class. Instead, they log the error and continue |
| 146 | +processing the next model. To ensure the rake tasks did not encounter any |
| 147 | +errors, check the output of the {+odm+} logger configured for your application. |
| 148 | + |
| 149 | +.. note:: |
| 150 | + |
| 151 | + When performing schema-related operations in a sharded cluster, nodes might |
| 152 | + contain out-of-date local configuration-related cache data. To clear the caches, |
| 153 | + run the :manual:`flushRouterConfig </reference/command/flushRouterConfig/>` |
| 154 | + command on each ``mongos`` node. |
| 155 | + |
| 156 | + |
| 157 | +Additional Information |
| 158 | +---------------------- |
| 159 | + |
| 160 | +To learn more about sharding with MongoDB, see the :manual:`Sharding |
| 161 | +</sharding>` guide in the MongoDB {+server-manual+}. |
| 162 | + |
| 163 | +API Documentation |
| 164 | +~~~~~~~~~~~~~~~~~ |
| 165 | + |
| 166 | +To learn more about the ``shard_key`` macro discussed in this |
| 167 | +guide, see the `shard_key |
| 168 | +<{+api-root+}/Shardable/ClassMethods.html#shard_key-instance_method>`__ API |
| 169 | +documentation. |
0 commit comments