Skip to Content
MongoDB: The Definitive Guide, 3rd Edition
book

MongoDB: The Definitive Guide, 3rd Edition

by Shannon Bradshaw, Eoin Brazil, Kristina Chodorow
December 2019
Intermediate to advanced
511 pages
12h 50m
English
O'Reilly Media, Inc.
Book available
Content preview from MongoDB: The Definitive Guide, 3rd Edition

Chapter 16. Choosing a Shard Key

The most important task when using sharding is choosing how your data will be distributed. To make intelligent choices about this, you have to understand how MongoDB distributes data. This chapter helps you make a good choice of shard key by covering:

  • How to decide among multiple possible shard keys

  • Shard keys for several use cases

  • What you can’t use as a shard key

  • Some alternative strategies if you want to customize how data is distributed

  • How to manually shard your data

It assumes that you understand the basic components of sharding as covered in the previous two chapters.

Taking Stock of Your Usage

When you shard a collection you choose a field or two to use to split up the data. This key (or keys) is called a shard key. Once you shard a collection you cannot change your shard key, so it is important to choose correctly.

To choose a good shard key, you need to understand your workload and how your shard key is going to distribute your application’s requests. This can be difficult to picture, so try to work out some examples—or, even better, try it out on a backup dataset with sample traffic. This section has lots of diagrams and explanations, but there is no substitute for trying it on your own data.

For each collection that you’re planning to shard, start by answering the following questions:

  • How many shards are you planning to grow to? A three-shard cluster has a great deal more flexibility than a thousand-shard cluster. As a cluster gets larger, you should ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

MongoDB - The Complete Developer's Guide

MongoDB - The Complete Developer's Guide

Maximilian Schwarzmüller
The Complete Node.js Developer Course (3rd Edition)

The Complete Node.js Developer Course (3rd Edition)

Andrew Mead, Codestars By Rob Percival

Publisher Resources

ISBN: 9781491954454Errata Page