{"id":1089,"date":"2015-09-11T21:16:16","date_gmt":"2015-09-12T03:16:16","guid":{"rendered":"http:\/\/jaredrobinson.com\/blog\/?p=1089"},"modified":"2015-09-11T21:22:25","modified_gmt":"2015-09-12T03:22:25","slug":"mongodb-pre-splitting-a-sharded-collection","status":"publish","type":"post","link":"https:\/\/jaredrobinson.com\/blog\/mongodb-pre-splitting-a-sharded-collection\/","title":{"rendered":"MongoDB: Pre-splitting a sharded collection"},"content":{"rendered":"<p>When suddenly\u00a0writing high volumes of data to a MongoDB collection that&#8217;s had little or no data previously, it&#8217;s important to pre-split the collection so that there&#8217;s good write performance &#8212; we don&#8217;t want to write all data to a single shard while waiting for the MongoDB balancer to figure things out. While it&#8217;s possible to programattically specify the split points in advance, MongoDB has an easier way: <a href=\"http:\/\/docs.mongodb.org\/master\/tutorial\/shard-collection-with-a-hashed-shard-key\/\">Hashed shard keys<\/a>. E.g.:<\/p>\n<pre>db.adminCommand({shardCollection: 'test.user', \\\r\n  key: {uid: 'hashed'}, \\\r\n  numInitialChunks: 500})<\/pre>\n<p>Equivalent python code looks approximately like this:<\/p>\n<pre>my_pymongo_connection[\"admin\"].command(\"shardcollection\", \"test.user\", \\\r\n  key={'uid': 'hashed'}, \\\r\n  numInitialChunks=500)<\/pre>\n<p>The downside is that it can take a while to shard the collection &#8212; the call doesn&#8217;t return until it&#8217;s complete, and it <a href=\"https:\/\/jira.mongodb.org\/browse\/SERVER-16770\">reportedly blocks all other clients of a given mongos instance until it finishes<\/a>.<\/p>\n<p>Apparently it&#8217;s <a href=\"https:\/\/jira.mongodb.org\/browse\/SERVER-8810\">not possible to specify more than 8129 initial chunks<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When suddenly\u00a0writing high volumes of data to a MongoDB collection that&#8217;s had little or no data previously, it&#8217;s important to pre-split the collection so that there&#8217;s good write performance &#8212; we don&#8217;t want to write all data to a single shard while waiting for the MongoDB balancer to figure things out. While it&#8217;s possible to &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/jaredrobinson.com\/blog\/mongodb-pre-splitting-a-sharded-collection\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;MongoDB: Pre-splitting a sharded collection&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-1089","post","type-post","status-publish","format-standard","hentry","category-tech"],"_links":{"self":[{"href":"https:\/\/jaredrobinson.com\/blog\/wp-json\/wp\/v2\/posts\/1089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jaredrobinson.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jaredrobinson.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jaredrobinson.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jaredrobinson.com\/blog\/wp-json\/wp\/v2\/comments?post=1089"}],"version-history":[{"count":10,"href":"https:\/\/jaredrobinson.com\/blog\/wp-json\/wp\/v2\/posts\/1089\/revisions"}],"predecessor-version":[{"id":1099,"href":"https:\/\/jaredrobinson.com\/blog\/wp-json\/wp\/v2\/posts\/1089\/revisions\/1099"}],"wp:attachment":[{"href":"https:\/\/jaredrobinson.com\/blog\/wp-json\/wp\/v2\/media?parent=1089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jaredrobinson.com\/blog\/wp-json\/wp\/v2\/categories?post=1089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jaredrobinson.com\/blog\/wp-json\/wp\/v2\/tags?post=1089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}