For enquiries call:

Phone

+1-469-442-0620

HomeBlogWeb DevelopmentWhat is MongoDB Sharding?

What is MongoDB Sharding?

Published
05th Sep, 2023
Views
view count loader
Read it in
8 Mins
In this article
    What is MongoDB Sharding?

    Let us understand the need for and importance of database sharding with the help of an example.

    Assume there is a database collection (with no sharding) of 50,000 employees working in a reputed Multi-National Company. The company maintains a database record of all the freshers and experienced employees in a single database.

    Your task is to access a profile of an employee from the 50000-employee database. Without sharding the database, finding the result will take a lot of effort and time. The search pattern will follow a step-by-step approach and need 50,000 transactions to display the details of a single piece of data you are looking for in the database.

    Going back to the same example, we now divide the total number of employees into sub-divisions such as freshers, experienced, job-profile, and other sections to make the search easier. For instance, if there are a total of 15000 Software-Developers working in the company, and you want the details of a particular developer, then the database will look only into the 15000 sub-divisions instead of looking through the entire database.

    1. Spot the difference?

    The database on the second scenario looks more organized, simplified, and clean after database sharding. Right? That’s where database sharding comes into the picture. The idea behind database sharding is to simplify the task into smaller divisions to reuse the data in a tech-savvy and efficient way. Technically, database sharding streamlines the searching process and attempts to find the search item from the list in the first go, thus saving time.

    2. What is Sharding?

    Database sharding is a data distribution process and stores a single data set into multiple databases. The purpose of database distribution is to enhance the scalability of applications. Sharding is an excellent way to keep the data safe across different resources. In MongoDB, database sharding is achievable by breaking down big data sets into sub-divided data sets across multiple MongoDB instances.

    Attention: MongoDB uses database sharding for deployment support, especially when there are high-volume data sets that are relatively increased throughput operations. It is also important to note that each shard is an independent database, and all the shards consist of a single local database.

    3. Sharded Cluster

    Sharded Cluster is a group of MongoDB instances. In simple words, these are a set of nodes that comprise MongoDB deployment. A sharded cluster has three main components:

    • A Shard: A shard is a single MongoDB instance that holds a subset of the sharded data. Each shard can be a replica set or a single mongos instance.
    • Config server: Config servers store the metadata for a sharded cluster. It includes the set of chunks on the individual shard and also the range defining the chunks.
    • Mongos instances: Mongos instances cache the data and route read and write operations to the right shards. Moreover, they also update the cache when metadata changes for the cluster.

    4. Shard Keys

    On sharding a MongoDB dataset, a shard key is automatically created by default. The shard key can be in the form of an indexed field or indexed compound fields that will be used to distribute the data among the shards. Generally, the “shard key” is used to distribute the MongoDB collection’s documents across all the shards, where the key consists of a single field or multiple fields in every document.

    MongoDB divides the range of shard key values into non-overlapping ranges of shard key values, where every range is linked with a chunk. Specifically, MongoDB tries to break down chunks in an even fashion among the different shards present in the cluster. 

    A shard key can be used to distribute data in the following

    • Hashed
    • Range 
    • Zones

    5. Balancer and Even Chunk Distribution

    The balancer is a process that holds the responsibility of distributing the chunks evenly among the different shards. There is a balance specifier for each cluster that handles the chunk distribution. The balance specifier take care of running the primary job and even distributing chunks across all shards evenly. The process of this type of chunk distribution carried out evenly is popularly known as even chunk distribution.

    6. Advantages of Sharding

    The fundamental idea behind database sharding is to break complex data into subparts for easy accessibility anytime, anywhere. Check out the advantages of sharding a database:

    1. Increased Storage capacity

    In database sharding, when data gets distributed across the shards in the cluster, each shard contains a subset of the total data in the cluster. On increasing the data volume, the additional shards grow which leads to expanding the cluster storage capacity.

    2. High Availability

    With an unsharded database, an outage in one database shard has the caliber to deteriorate the entire application and loosen its functionality or even stop. However, with a sharded database, if there is complete unavailability of one or more shard replicas, only a few parts of the application or website are unavailable to some users. However, the other shards continue their operation without any concern.

    3. Read/write

    In MongoDB, the read and write workloads are easily distributed across the shards in the sharded cluster. It allows each shard to process a subset of the cluster operation. Both the read and write performance can be directly scaled horizontally across the cluster by increasing the shard count.

    4. Facilitates horizontal scaling

    One more reason programmers love database sharding is that it facilitates horizontal scaling (also renowned as scaling out). That means it allows to have parallel backends and carry out tasks simultaneously with no hassle. Whether the focus is on writing or reading operations, scaling out can add a big advantage to enhance the performance and also eliminate complexities.

    5. Speedier query response

    Whenever you submit a query on an unsharded database, it looks for the searched query in all the rows and columns of the table until it finds the searched query. For low-volume data, it may look insignificant, but it becomes problematic with a high-volume database. Unlike the unsharded database, the sharded database distributes the database into sub-sections where queries have to go to fewer rows, and the results are thus quick and efficient.

    7. Sharded and Non-Sharded Collections

    A database collection is not always uniform. That means the database will have a mixture of both sharded as well as unsharded collections of data.

    Sharded Collection: A collection of data that are broken down in the cluster and are well partitioned is called a sharded collection.

    Non-Sharded Collection: The database collection stored on a primary shard (the shard carrying all the un-sharded collection) is known as a non-sharded collection.

    8. Connecting to a Sharded Cluster

    For connecting to a sharded cluster, you need to connect to the sharded router using the mongos process. That means you have to join the mongos router with collections (sharded and unsharded) in the sharded cluster. Never make the mistake of connecting to every individual shard for performing read and write operations.

    9. Sharding Strategy

    For the distribution of data across the shared clusters, the MongoDB sharding follows the following strategies:

    1. Hash-based Sharding
    2. Range based Sharding
    3. Directory-based Sharding
    4. Geo-based Sharding

    10. Hash-based Sharding

    Hash-based database sharding is also known as key-based sharding. Here values are taken from newly registered data into the database and plugged into the hash function. Key-value, or we can call it the hash value, is the shard ID that determines the location of incoming or the registered data. Make sure to keep the values on the hash function in a sequential arrangement so that there is no mismatch of value and the shard.

    11. Range-based sharding

    Ranged sharding involves data distribution based on the ranges of the given shard values. For instance, there is a collection of data storing the inventory details the products will get placed based on the volume of data availability. The biggest drawback of range-based sharding is that it needs a lookup table for reading and write queries, so it may retard the application performance.

    12. Directory-based Sharding

    Directory-based sharding is a sharding strategy used to maintain a record of shard data. There is a lookup table (also called location service), where it stores the sharded key and tracks all the data entries. Using the shard and key pair, the client engine takes consultation from the location service and then switches to a specific shard to proceed for further work.

    13. Geo-based Sharding

    Geo-based sharding has similarities to that of range-based database sharding, with the only difference that queries here are geographically based. The data procession is down with a shard that corresponds to the user region under the range of 100 miles. The perfect example is Tinder, a dating app that uses Geo-Based sharding to keep balancing the production load of the geo-shards.

    14. Considerations Before Sharding

    The perks of data sharding may impress you. However, there are many factors that need attention, else you may have to pay the price of data loss or damage. There are a few considerations you must focus on before proceeding with the database sharding:

    1. Before database sharding, keep in mind different aspects like planning, execution, and maintenance. Make sure to have a bird’s-eye view of all the sharded cluster infrastructure requirements and complexities involved.
    2. Be cautious when dealing with data collection, especially with the sharded database collection. Mind you, once you have shared a database collection, there is no way to undo it. Simply put, MongoDB does not permit unsharding after sharding database collection.
    3. The choice of Shard key you make for sharding plays a significant role in cluster behavior, overall efficiency, and performance. Make sure to check the cardinality, frequency, and monotonicity of the shard key properly. Do not miss to check the shard key limitation.
    4. Operational requirements and restrictions of database sharding are also hard to ignore. 

    15. Zones in Sharded Clusters

    Before we dive into the MongoDB zone, let us focus on understanding a zone.

    A group of shards with a particular set of tags is commonly known as a zone. MongoDB zones available in shading allow distributing chunks based on chunks across shards. All the work, read and write documentation within a zone is done on shards matching the zone. When creating sharded data zones in the sharded clusters, you can link one or more shards in the cluster. Best of all, you can freely associate a shard with any number of zones. Just keep in mind that whenever there is a balanced cluster, migration of chunks in MongoDB takes place such that only those shards associated with the zone get migrated, covered by the zone. 

    Attention: MongoDB routes reads and writes falling into a zone range only to those shards inside the sharded cluster zone. Shard zones are easily manageable. All the basic operations like creating a zone layer, adding or eliminating shard from the zone, or overviewing existing zones are possible. 

    16. Collations in Sharding

    A group of transactions belonging to a single shard is known as collations. It consists of a transaction list and a collation header. The collation header comprises information submitted to the main chain, and the transaction list is the sequence of transactions.

    Try using the shard Collection command along with the collation: { locale: "simple" } option to shard a collection with a default collation.

    1. Change Streams

    It becomes difficult for applications to respond to sudden changes. 

    From the upgraded MongoDB version 3.6, change streams enable applications to simplify the real-time data changes by leveraging MongoDB functionalities. That means applications can get data accessibility without the cost of tailing the operations log. Change streams come with robust and dynamic features like the total ordering that enables applications to receive changes sequentially as applied to the database.

    2. Transactions

    The organized way of representing the change of state is known as transactions. Ideally, there are four properties called ACID:

    1. Atomic – The overall transaction gets committed, or there is no transaction at all.
    2. Consistent – The database must be consistent before and after the transaction.
    3. Isolated – No-one gets to see any part of the transaction until it is committed.
    4. Durable – Even if there is a system failure or a restart, there is no change on the saved data.

    MongoDB supports multi-document transactions. The MongoDB version supports 4.0, multi-document transactions on replica sets, whereas the upgraded Mongo version 4.2, supports multi-document transactions on replica sets and the sharded clusters.

    Are you ready to level up your skills? Join our programming online course and unlock your full potential. From beginner to expert, we've got you covered. Enroll now and become a coding maestro!

    Wrapping Up

    Database sharding facilitates horizontal scaling and is a more effective way to speed up operational efficiency. Besides, sharding databases simplify the data-management and maintenance procedures. Perhaps, not all databases support database sharding. Worst of all, the sharded database cannot get unsharded. The biggest concern comes when dealing with complex data, especially when there is a data pull from multiple resources. Be careful and attentive, and remember the listed considerations mentioned above.  As a gentle reminder, database sharding will only turn to your advantage if you know to use them effectively. Otherwise, if not done the right way, you might corrupt tables and even lead to data loss.

    Profile

    Abhresh Sugandhi

    Author

    Abhresh is specialized as a corporate trainer, He has a decade of experience in technical training blended with virtual webinars and instructor-led session created courses, tutorials, and articles for organizations. He is also the founder of Nikasio.com, which offers multiple services in technical training, project consulting, content development, etc.

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming Web Development Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon