5/28/2025 Assignment 3
Advanced Database Systems
Iqra Hayder (FA23-BCS-036)
Isha Dastgir(FA23-BCS-037)
Habib ur Rehman(FA23-BCS-116)
Advanced Database Systems
Assignment 4
Total Marks: 10 CLO 3: Apply concurrency control and scaling strategies
in distributed systems
Solution:
Use Case 1
A large e-commerce platform is experiencing rapid exponential user growth and a
significant increase in product listings.
Use Case 2
When a user searches for a specific product, the request is routed directly to the
shard containing that product’s data, enabling fast and responsive retrieval.
1. What Scaling and Sharding You Will Apply for the Use Cases
Use Case 1
Apply horizontal scaling using sharding to split the data across multiple servers. Use
replication for high availability and fault tolerance. The products collection should be
sharded on a high-cardinality field like product_id or category_id.
Use Case 2
Apply query-based sharding so that the query for a specific product is routed to the
exact shard using a shard key such as product_id. Use load balancing through
MongoDB's mongos router to distribute search requests efficiently.
2. Why Sharding is Effective in That Scenario
In Use Case 1, sharding helps scale horizontally as user traffic and data size grow. It
distributes the products collection across multiple shards, avoiding overload on a
single server. This ensures better performance and system scalability.
In Use Case 2, sharding makes searches faster by directing queries to the relevant
shard based on the shard key. Since the data is divided intelligently, it improves
query response time and overall user experience.
3. Steps to Apply Shard Keys in MongoDB with an Example
Suppose we want to shard the products collection in the ecommerce database using
product_id as the shard key.
Step 1: Enable Sharding on the Database
sh.enableSharding("ecommerce")
Step 2: Create the Collection (Optional but Recommended)
use ecommerce db.createCollection("products") Step 3:
Create an Index on the Shard Key db.products.createIndex({
product_id: 1 }) Step 4: Shard the Collection Using the Shard
Key sh.shardCollection("ecommerce.products", {
product_id: 1 })
This process sets up the products collection to be distributed across shards based
on the product_id field.
4. Apply Scaling Strategies (Sharding, Replication, Load Balancing) to a Use Case
Chosen Use Case: Use Case 1 (E-commerce Platform with Rapid
Growth)
• Sharding: Split the products and orders collections using compound keys like
{ category_id: 1, product_id: 1 }. This groups products by category while
maintaining uniqueness by product ID.
• Replication: Each shard is part of a replica set. This ensures that if one node
fails, another node can take over without downtime, improving data
availability and reliability.
• Load Balancing: MongoDB uses a query router (mongos) to distribute queries
evenly across shards. This prevents any single shard from being overwhelmed
and balances the load dynamically as traffic grows.