{"id":65419,"date":"2024-09-25T09:50:07","date_gmt":"2024-09-25T04:20:07","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=65419"},"modified":"2024-09-26T15:43:49","modified_gmt":"2024-09-26T10:13:49","slug":"sharding-distributing-data-across-multiple-machines-with-spring-boot-and-mongodb","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/sharding-distributing-data-across-multiple-machines-with-spring-boot-and-mongodb\/","title":{"rendered":"Sharding: Distributing Data Across Multiple Machines with Spring Boot and MongoDB"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-66078\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-15-23-35-38.png\" alt=\"image\" width=\"1112\" height=\"201\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-15-23-35-38.png 1112w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-15-23-35-38-300x54.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-15-23-35-38-1024x185.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-15-23-35-38-768x139.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-15-23-35-38-624x113.png 624w\" sizes=\"(max-width: 1112px) 100vw, 1112px\" \/><\/p>\n<p>Imagine your coffee shop is thriving, but one location can&#8217;t keep up with the growing demand. To fix this, you open more shops across the city, easing the load.<\/p>\n<p><span style=\"color: #000080;\"><strong>Sharding<\/strong><\/span> works similarly for databases. When one <a href=\"https:\/\/www.tothenew.com\/blog\/enabling-client-side-encryption-for-mongodb\/\">MongoDB<\/a> server can&#8217;t handle the volume, <a href=\"https:\/\/www.tothenew.com\/data-analytics\">data<\/a> is distributed across multiple machines (shards), making your system more efficient.<\/p>\n<p>In this blog, we\u2019ll walk through what and how to implement Sharding in MongoDB and access it using a Spring Boot application. The goal is to provide a beginner-friendly, hands-on experience with sharding in MongoDB, all while creating a scalable system. We&#8217;ll also walk through the steps of setting up sharding with MongoDB Community Edition on Linux.<\/p>\n<h4>Read More: <a href=\"https:\/\/www.tothenew.com\/blog\/should-you-choose-aws-dynamodb-for-your-next-project\/\">Should You Choose AWS DynamoDB for Your Next Project?<\/a><\/h4>\n<h3>What is Sharding in MongoDB?<\/h3>\n<p>MongoDB uses sharding to distribute large datasets across multiple servers, called <strong>shards<\/strong>. This ensures that the database can grow as your data grows, keeping performance high without hitting bottlenecks. Each shard holds a portion of the entire data collection, so queries and operations can be split across different shards for faster results.<\/p>\n<h4>Why Sharding Matters?<\/h4>\n<p>When your database grows beyond the capacity of a single server, things get slow. That\u2019s where <strong>sharding<\/strong> comes in to:<\/p>\n<ul>\n<li><strong>Scale horizontally<\/strong>: Add more servers to handle more data.<\/li>\n<li><strong>Maintain performance<\/strong>: By distributing data, MongoDB ensures that no single server is overwhelmed.<\/li>\n<li><strong>Handle large datasets<\/strong>: Even with massive data, operations remain quick and efficient.<\/li>\n<\/ul>\n<h4>Key Terminologies Before Diving In<\/h4>\n<ul>\n<li><strong>Shard<\/strong>: A single server (or replica set) that holds part of the data.<\/li>\n<li><strong>Shard key<\/strong>: A field or combination of fields used to decide how to split the data across shards.<\/li>\n<li><strong>Config servers<\/strong>: Special servers that store metadata and manage how data is distributed.<\/li>\n<li><strong>Mongos<\/strong>: The router that directs queries to the right shards.<\/li>\n<\/ul>\n<div id=\"attachment_66070\" style=\"width: 807px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-66070\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-66070 size-full\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-15-23-19-23-1.png\" alt=\"image\" width=\"797\" height=\"440\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-15-23-19-23-1.png 797w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-15-23-19-23-1-300x166.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-15-23-19-23-1-768x424.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-15-23-19-23-1-624x344.png 624w\" sizes=\"(max-width: 797px) 100vw, 797px\" \/><p id=\"caption-attachment-66070\" class=\"wp-caption-text\">Sharded Cluster Architecture<\/p><\/div>\n<h3>Types of Sharding in MongoDB<\/h3>\n<p>Now, let\u2019s talk about the types of sharding and how MongoDB decides where to put your data.<\/p>\n<h4><span style=\"color: #339966;\"><strong><br \/>\n<\/strong><\/span>1. Range-Based Sharding<\/h4>\n<p>In range-based sharding, MongoDB divides data based on a shard key value, splitting it into ranges (like dividing numbers from 1-100 into smaller chunks). Each shard is responsible for a particular range of data.<em><br \/>\n<\/em><strong><br \/>\nHow It Works:<\/strong><br \/>\nIf your shard key is a date field, for example, MongoDB could split data into shards like:<br \/>\nShard 1: 2010\u20132015<br \/>\nShard 2: 2016\u2013present<\/p>\n<h4>2. Hash-Based Sharding<\/h4>\n<p>In hash-based sharding, <strong>MongoDB<\/strong> applies a hash function to the shard key to ensure an even distribution of data. The data is then randomly spread across all shards based on this hash value.<\/p>\n<p><strong>How It Works:<\/strong><br \/>\nLet\u2019s say your shard key is a customer ID. MongoDB would hash these IDs, ensuring they are distributed evenly across all shards, regardless of the ID value.<\/p>\n<h4>3. Zone-Based Sharding (or Tag-Aware Sharding)<\/h4>\n<p>Zone-based sharding is a combination of the two methods mentioned above but adds the flexibility of defining zones for specific ranges of data. You can \u201ctag\u201d certain shards to handle specific data ranges based on the shard key, which is useful for geographical distribution or regulatory compliance.<\/p>\n<p><strong>How It Works:<\/strong><br \/>\nYou might set up zones like:<br \/>\nShard 1: Data for customers from the India.<br \/>\nShard 2: Data for customers from U.S.<\/p>\n<h4><span style=\"color: #000080;\">How to Choose the Right Sharding Strategy?<\/span><\/h4>\n<p>Choosing a sharding method depends on how you plan to query the data:<\/p>\n<ul>\n<li><strong>Range-Based Sharding:<\/strong> Best for applications that run range queries often, like \u201cfind all orders placed between certain dates.\u201d However, beware of imbalanced data.<\/li>\n<li><strong>Hash-Based Sharding:<\/strong> Ideal when you want an even data distribution and you\u2019re not running range queries. Great for large-scale systems where performance and balance are critical.<\/li>\n<li><strong>Zone-Based Sharding:<\/strong> If you have regulatory constraints or need geographical data distribution, zone-based sharding offers flexibility while controlling where your data lives.<\/li>\n<\/ul>\n<h4>Setting Up MongoDB Sharding Locally<\/h4>\n<p><strong>Prerequisites:<\/strong><br \/>\n<span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 MongoDB Community Edition installed on Linux<br \/>\nSpring Boot and Java are set up locally<\/span><\/p>\n<p>Now, let&#8217;s walk through the step-by-step process of configuring MongoDB sharding on your local machine.<\/p>\n<p><strong>Step 1: Set Up a Config Server<\/strong><\/p>\n<p>In a MongoDB sharded cluster, the config server stores metadata and routing information. To start a config server, we need to run :<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-65381\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-14-55.png\" alt=\"directory cmd\" width=\"870\" height=\"181\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-14-55.png 870w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-14-55-300x62.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-14-55-768x160.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-14-55-624x130.png 624w\" sizes=\"(max-width: 870px) 100vw, 870px\" \/><\/p>\n<p>Once the config server is running, connect to it:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-65385\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-17-42-1.png\" alt=\"terminal\" width=\"876\" height=\"155\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-17-42-1.png 876w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-17-42-1-300x53.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-17-42-1-768x136.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-17-42-1-624x110.png 624w\" sizes=\"(max-width: 876px) 100vw, 876px\" \/><\/p>\n<p><strong><br \/>\nStep 2: Set Up Shards<\/strong><\/p>\n<p>Each shard is an individual MongoDB instance or replica set. For simplicity, let\u2019s create two shards:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-65396\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-38-24.png\" alt=\"cmd\" width=\"876\" height=\"274\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-38-24.png 876w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-38-24-300x94.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-38-24-768x240.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-38-24-624x195.png 624w\" sizes=\"(max-width: 876px) 100vw, 876px\" \/><\/p>\n<p>Now, initiate the replica sets for both shards:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-65397\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-40-15.png\" alt=\"cmd\" width=\"868\" height=\"229\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-40-15.png 868w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-40-15-300x79.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-40-15-768x203.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-40-15-624x165.png 624w\" sizes=\"(max-width: 868px) 100vw, 868px\" \/><\/p>\n<p><strong><br \/>\nStep 3: Start the Mongos Router<\/strong><\/p>\n<p>The mongos process acts as the query router for the sharded cluster, directing operations to the correct shard.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-65398\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-41-31.png\" alt=\"cmd\" width=\"878\" height=\"87\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-41-31.png 878w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-41-31-300x30.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-41-31-768x76.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-41-31-624x62.png 624w\" sizes=\"(max-width: 878px) 100vw, 878px\" \/><\/p>\n<p><strong>Step 4: Add Shards to the Cluster<\/strong><\/p>\n<p>Now, connect to the mongos instance and add both shards to the cluster:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-65399\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-42-41.png\" alt=\"cmd\" width=\"880\" height=\"175\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-42-41.png 880w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-42-41-300x60.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-42-41-768x153.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-08-19-42-41-624x124.png 624w\" sizes=\"(max-width: 880px) 100vw, 880px\" \/><\/p>\n<p><strong>Now, How Sharding is Applied to Collections?<\/strong><br \/>\nIn MongoDB, sharding is applied at the <strong>collection level<\/strong>. To shard a collection, follow these steps:<br \/>\n<strong><br \/>\nEnable sharding for the database:<\/strong> You first need to enable sharding on the database that contains the collection.<br \/>\n<em>Note :- use coffeeShop, before enabling sharding.<\/em><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-66611\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-18-19-32-40.png\" alt=\"image\" width=\"848\" height=\"64\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-18-19-32-40.png 848w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-18-19-32-40-300x23.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-18-19-32-40-768x58.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-18-19-32-40-624x47.png 624w\" sizes=\"(max-width: 848px) 100vw, 848px\" \/><\/p>\n<p><strong>Shard the collection:<\/strong> \u00a0We\u2019ll be using <span style=\"color: #000080;\">range sharding<\/span>, which divides the data into continuous value ranges based on the shard key. This allows efficient querying of specific data ranges.<\/p>\n<div id=\"attachment_66765\" style=\"width: 853px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-66765\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-66765\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-01-13-53.png\" alt=\"image\" width=\"843\" height=\"64\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-01-13-53.png 843w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-01-13-53-300x23.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-01-13-53-768x58.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-01-13-53-624x47.png 624w\" sizes=\"(max-width: 843px) 100vw, 843px\" \/><p id=\"caption-attachment-66765\" class=\"wp-caption-text\">The collection coffeeShop.orders is now sharded based on the customerId field.<\/p><\/div>\n<p><strong><span style=\"color: #008000;\">Important<\/span>: Once the collection is sharded, MongoDB will distribute the data into chunks across different shards based on the specified shard key.<\/strong><\/p>\n<p><span style=\"text-decoration: underline;\"><strong><span style=\"color: #000080; text-decoration: underline;\"><em>Split the Data at customerId: 100<\/em><\/span><\/strong><\/span><br \/>\nTo distribute data across shards, split the data at customerId: 100. This will create two ranges:<br \/>\n<em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 customerId \u2264 100 (will go to shard1ReplSet)<\/em><br \/>\n<em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 customerId &gt; 100 (will go to shard2ReplSet)<br \/>\n<\/em><br \/>\nExecute the following command to split the data:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-66626\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-19-00-41-09.png\" alt=\"img\" width=\"611\" height=\"60\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-19-00-41-09.png 611w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-19-00-41-09-300x29.png 300w\" sizes=\"(max-width: 611px) 100vw, 611px\" \/><\/p>\n<p><strong>Move Chunks to the Appropriate Shards<\/strong><\/p>\n<p>Now, move the chunks based on customerId:<br \/>\n<em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0Move customerId \u2264 100 to shard1ReplSet<\/em><br \/>\n<em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0Move customerId &gt; 100 to shard2ReplSet<br \/>\n<\/em><br \/>\n<strong>Move data for customerId values up to 100 to shard1ReplSet:<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-66627\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-19-00-43-08.png\" alt=\"image\" width=\"784\" height=\"56\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-19-00-43-08.png 784w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-19-00-43-08-300x21.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-19-00-43-08-768x55.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-19-00-43-08-624x45.png 624w\" sizes=\"(max-width: 784px) 100vw, 784px\" \/><\/p>\n<p><strong>whereas customerId values greater than 100, move to shard2ReplSet.<\/strong><\/p>\n<h4><span style=\"color: #333333;\">Summary of the Approach<\/span><\/h4>\n<p><span style=\"color: #333333;\"><em>Split at customerId<\/em><\/span>: 100 creates two logical ranges in the orders collection.<br \/>\n<span style=\"color: #333333;\"><em>Moving chunks ensures that<\/em><\/span>: Orders with customerId values less than or equal to 100 are stored in <span style=\"color: #339966;\">shard1ReplSet<\/span>. Orders with customerId values greater than 100 are stored in <span style=\"color: #339966;\">shard2ReplSet<\/span>.<\/p>\n<h4>Modifying the shard key in MongoDB<\/h4>\n<p>It is not directly supported. Once a collection has been sharded using a specific key, it cannot be changed. If you need to change the shard key, you have a few options:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Resharding (since MongoDB 4.4):<br \/>\n<\/em><\/span>MongoDB has introduced resharding in newer versions, where you can define a new shard key and MongoDB will redistribute the data according to the new key.<br \/>\nExample:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-66615\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-18-19-49-20.png\" alt=\"img\" width=\"669\" height=\"37\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-18-19-49-20.png 669w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-18-19-49-20-300x17.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-18-19-49-20-624x35.png 624w\" sizes=\"(max-width: 669px) 100vw, 669px\" \/><\/p>\n<p><span style=\"text-decoration: underline;\"><em>Manual Approach<\/em>:<\/span><br \/>\nIf you&#8217;re using an older version, the alternative is to create a new collection with the desired shard key and migrate the data manually. This is cumbersome and should be avoided in production environments.<\/p>\n<p>For a comprehensive list of all commands and steps involved in setting up sharding, please refer to the <a href=\"https:\/\/github.com\/Vikash-Rai\/Sharding\/blob\/master\/README.md#adding-shards\">README<\/a> file.<\/p>\n<div id=\"attachment_66076\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-66076\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-66076 size-medium\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-14-20-29-13-1-300x237.png\" alt=\"meme\" width=\"300\" height=\"237\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-14-20-29-13-1-300x237.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-14-20-29-13-1-768x607.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-14-20-29-13-1-624x493.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-14-20-29-13-1.png 774w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><p id=\"caption-attachment-66076\" class=\"wp-caption-text\">meme<\/p><\/div>\n<h3><strong><br \/>\n<\/strong>Spring Boot Application for Sharded MongoDB<\/h3>\n<p>Now that we have MongoDB sharding set up, let&#8217;s build a Spring Boot application to interact with this sharded setup.<\/p>\n<p><strong>Step 1: Create a Spring Boot Project<\/strong><\/p>\n<p>First, create a new Spring Boot project with the necessary dependencies.<\/p>\n<p><strong>Step 2: MongoDB Configuration in application.properties<\/strong><\/p>\n<p>Now, configure your Spring Boot application to connect to the Mongos router:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-66828\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-32-04.png\" alt=\"img\" width=\"852\" height=\"37\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-32-04.png 852w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-32-04-300x13.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-32-04-768x33.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-32-04-624x27.png 624w\" sizes=\"(max-width: 852px) 100vw, 852px\" \/><\/p>\n<p><strong>Step 3: Create MongoDB Entity<\/strong><\/p>\n<p>Create an entity class to represent orders in your database:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-66830\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-29-29.png\" alt=\"image\" width=\"1244\" height=\"579\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-29-29.png 1244w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-29-29-300x140.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-29-29-1024x477.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-29-29-768x357.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-29-29-624x290.png 624w\" sizes=\"(max-width: 1244px) 100vw, 1244px\" \/><\/p>\n<p><strong>Step 4: Create a Repository<\/strong><\/p>\n<p>Define a repository to handle CRUD operations for Order:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-66832\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-50-16.png\" alt=\"repo\" width=\"1015\" height=\"257\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-50-16.png 1015w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-50-16-300x76.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-50-16-768x194.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-50-16-624x158.png 624w\" sizes=\"(max-width: 1015px) 100vw, 1015px\" \/><\/p>\n<p><strong>Step 5: Service Layer<\/strong><\/p>\n<p>Now, let\u2019s create a service that interacts with the repository:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-66833\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-53-30.png\" alt=\"img\" width=\"1006\" height=\"686\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-53-30.png 1006w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-53-30-300x205.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-53-30-768x524.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-53-30-624x426.png 624w\" sizes=\"(max-width: 1006px) 100vw, 1006px\" \/><\/p>\n<p><strong>Step 6: REST Controller<\/strong><\/p>\n<p>Lastly, expose the service through a REST API:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-66838\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-58-49.png\" alt=\"image\" width=\"1253\" height=\"796\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-58-49.png 1253w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-58-49-300x191.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-58-49-1024x651.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-58-49-768x488.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-12-58-49-624x396.png 624w\" sizes=\"(max-width: 1253px) 100vw, 1253px\" \/><\/p>\n<p><strong>Testing the Sharded Application<\/strong><\/p>\n<p>Start your Spring Boot application and test it using hitting post API on <span style=\"color: #ff6600;\">Postman<\/span>.<\/p>\n<div id=\"attachment_66840\" style=\"width: 635px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-66840\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-66840 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-13-01-23-1024x277.png\" alt=\"image\" width=\"625\" height=\"169\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-13-01-23-1024x277.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-13-01-23-300x81.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-13-01-23-768x208.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-13-01-23-624x169.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-13-01-23.png 1350w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-66840\" class=\"wp-caption-text\">postman<\/p><\/div>\n<p><span style=\"color: #339966;\">MongoDB<\/span> will automatically route your data to the appropriate shard based on the customerId.<br \/>\nfor e.g. Added 11 data entries, with 7 moving to the shard2ReplSet and 4 to the shard1ReplSet, as shown in the snapshot below.<\/p>\n<div id=\"attachment_66860\" style=\"width: 635px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-66860\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-66860\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-14-18-13-1024x551.png\" alt=\"img\" width=\"625\" height=\"336\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-14-18-13-1024x551.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-14-18-13-300x161.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-14-18-13-768x413.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-14-18-13-624x336.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/09\/Screenshot-from-2024-09-20-14-18-13.png 1456w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-66860\" class=\"wp-caption-text\">data distribution<\/p><\/div>\n<h3><strong>Conclusion<\/strong><\/h3>\n<p>With <strong>sharding<\/strong>, you\u2019re essentially splitting your coffee shop into multiple locations. Each <span style=\"color: #339966;\">MongoDB<\/span> shard handles part of the data, and your application seamlessly interacts with the entire dataset. Whether you&#8217;re dealing with a small application or planning for large-scale growth, sharding ensures <span style=\"color: #339966;\">MongoDB<\/span> can scale with your needs.<\/p>\n<p>By setting up a sharded MongoDB cluster and connecting it to a Spring Boot application, you&#8217;ve taken the first steps toward building scalable, distributed systems.<\/p>\n<p>To access the project repository, visit <a href=\"https:\/\/github.com\/Vikash-Rai\/Sharding\">here<\/a>. TO THE NEW offers end-to-end <a href=\"https:\/\/www.tothenew.com\/data-analytics\">Data &amp; Analytics services<\/a> to help our customers leverage the power of Data and unlock actionable business insights. <a href=\"https:\/\/www.tothenew.com\/contact-us\">Click here<\/a> to start your project with us.<\/p>\n<p><strong><span style=\"color: #000080;\">Happy Coding! \ud83d\ude42<\/span><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Imagine your coffee shop is thriving, but one location can&#8217;t keep up with the growing demand. To fix this, you open more shops across the city, easing the load. Sharding works similarly for databases. When one MongoDB server can&#8217;t handle the volume, data is distributed across multiple machines (shards), making your system more efficient. In [&hellip;]<\/p>\n","protected":false},"author":1889,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":115},"categories":[446],"tags":[4843,4846,6451,2072],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/65419"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1889"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=65419"}],"version-history":[{"count":30,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/65419\/revisions"}],"predecessor-version":[{"id":67898,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/65419\/revisions\/67898"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=65419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=65419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=65419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}