Starting with MongoDB version 5.0, we can reshard a collection using the new shard key. But there are still something we think could be improved like:
Cluster metadata lack of details on chunks
TTL indexes
Improvements in the rebalancing process
Performance issues on rebalancing data
Observability
1 of 45
Download to read offline
More Related Content
Sharding and things we'd like to see improved
1. Sharding and things
Wed like to see
improved
息 Pythian Services Inc 2022 | Con鍖dential|
Igor Donchovski
May-17th 2022
2. 息 Pythian Services Inc 2022 | Con鍖dential | 2
About me
Igor Donchovski
Principal Consultant
Pythian - OSDB
3. 息 Pythian Services Inc 2022 | Con鍖dential | 3
25
Years in Business
450+
Experts across every Data Domain & Technology
400+
Global Customers
_______________
Gold Partner
40+ Certi鍖cations
5 Competencies,
Incl. Data Analytics
and Data Platform
_______________
Silver Partner
50+ Certi鍖cations
Migration Factory
Certi鍖ed Apps
Hosting
_______________
Advanced Partner
1O0+ Certs/Accreds
Migration & DevOps
Competencies
Pythian overview
________________
Premier Partner
120+ Certs/Creds
6 Specializations, MSP
Badge, Partner &
Technical Advisory Bds
Pythian maximizes the value of your data estate by delivering advanced on-prem, hybrid, cloud, and
multi-cloud solutions and solving your toughest data and analytics challenges.
_______________
Select Partner
10+ Certs/Accreds
_______________
Platinum Partner
60+ Certi鍖cations
Advisory Board
Member
4. 息 Pythian Services Inc 2022 | Con鍖dential | 4
Overview
Scaling (Vertical and Horizontal)
What is a sharded cluster in MongoDB
Cluster components - shards, config servers, mongos
Shard keys and chunks
Hashed and range based sharding
Choosing a shard key
Things wed like to see improved
QA
7. 息 Pythian Services Inc 2022 | Con鍖dential | 7
Scaling
Time for scaling?
The CPU and/or memory becomes overloaded, and the database server either cannot
respond to all the request throughput or do so in a reasonable amount of time.
Your database server runs out of storage, and thus cannot store all the data.
Your network interface is overloaded, so it cannot support all the network traffic received.
8. 息 Pythian Services Inc 2022 | Con鍖dential | 8
Scaling
Vertical scaling
Adding more power (CPU, RAM, DISK) to an
existing machine
Might require downtime while scaling up
Horizontal scaling
Adding more machines into your pool of
resources
Partitioning the data on multiple machines
Parallelizing the processing
More complex to implement and maintain
13. Scaling per tenant
息 Pythian Services Inc 2022 | Con鍖dential | 13
App
Tenant A
Tenant B
Tenant C
Tenant DB
Standalone App
14. Scaling per tenant
息 Pythian Services Inc 2022 | Con鍖dential | 14
App
Tenant A
Tenant B
Tenant C
Tenant DB
App
A B C
Standalone App Database per Tenant
App
A
C
Sharded Multi-Tenant
Catalog
B
Catalog
15. Scaling per tenant
息 Pythian Services Inc 2022 | Con鍖dential | 15
App Catalog
Tenant A Tenant B Tenant C
Sharded Multi-Tenant
N
16. Scaling with MongoDB - Sharding
Shard/Replica set
(subset of the sharded data)
Config servers
(metadata and config settings)
mongos
(query router, cluster interface)
mongos> sh.addShard("shardN")
息 Pythian Services Inc 2022 | Con鍖dential | 16
17. Scaling with MongoDB - Shards
Contains subset of sharded data
Replica set for redundancy and HA
Primary shard (picked by mongos with
least amount of data)
Non sharded collections
--shardsvr in config file (port 27018)
息 Pythian Services Inc 2022 | Con鍖dential | 17
18. Scaling with MongoDB - Con鍖g Servers
Stores the metadata for sharded cluster in
config database
Authentication configuration information in
admin database
Config servers as replica set only (>= 3.4)
Holds balancer on Primary node (>= 3.4)
--configsvr in config file (port 27019)
息 Pythian Services Inc 2022 | Con鍖dential | 18
19. Scaling with MongoDB - mongos
Caching metadata from config servers
Routes queries to shards
No persistent state
Updates cache on metadata changes
Holds balancer (MongoDB <= 3.2)
Starting in MongoDB 4.0, the mongos binary
will crash when attempting to connect to
mongod instances whose (fCV) is greater than
that of the mongos
息 Pythian Services Inc 2022 | Con鍖dential | 19
20. Monotonically changing
Frequency
Choosing shard key
Cardinality
息 Pythian Services Inc 2022 | Con鍖dential | 20
Choose a key that is included in most of your queries
Ideally you dont want huge number of documents to share the same shard key
Choose something that will co-locate data you wish to retrieve together
21. A contiguous range of shard key values within a particular shard
Chunk ranges are inclusive of the lower boundary and exclusive of the upper boundary
Chunks split when they grow beyond the configured chunk size (default is 64MB).
Configurable between 1MB and 1GB
MongoDB migrates chunks when a shard contains too many chunks of a collection
relative to other shards
Chunks
息 Pythian Services Inc 2022 | Con鍖dential | 21
Number of chunks Migration threshold
< 20 2
20 - 79 4
>= 80 8
22. Ranged based sharding
Dividing data into contiguous ranges determined by the shard key values
Documents with close shard key values are likely to be in the same chunk or shard
Query Isolation - more likely to target single shard for range queries
息 Pythian Services Inc 2022 | Con鍖dential | 22
23. Hashed based sharding
Uses a hashed index of a single or compound key to partition data
More even data distribution at the cost of reducing Query Isolation
Applications do not need to compute hashes
息 Pythian Services Inc 2022 | Con鍖dential | 23
24. Balancer
Background process that monitors the number of chunks on each shard
Migrates chunks between shards to reach an equal number of chunks per shard
Runs on the Primary of the config servers replica set
息 Pythian Services Inc 2022 | Con鍖dential | 24
Shard1 Shard2 Shard3
Migrate
64MB 64MB 64MB
64MB
64MB 64MB
64MB 64MB
64MB
64MB 64MB
64MB
64MB
25. Balancer
Background process that monitors the number of chunks on each shard
Migrates chunks between shards to reach an equal number of chunks per shard
Runs on the Primary of the config servers replica set
息 Pythian Services Inc 2022 | Con鍖dential | 25
Shard1 Shard2 Shard3
64MB 64MB
64MB
64MB
64MB 64MB
64MB
64MB
64MB 64MB
64MB
64MB
26. Collection tracking that has documents for {client_id, asset_id, pushed_at.}
Each client has their assets tracked by GPS location
Tens of thousands of clients
Ranges between tens to hundreds of thousands of assets per client
Millions of document inserts per minute on cluster level
Data expires after 12 months, TTL index on pushed_at
Clients request to get trajectories of asset_id[s] in time range
Sharding a collection
息 Pythian Services Inc 2022 | Con鍖dential | 26
30. Things wed like improved
息 Pythian Services Inc 2022 | Con鍖dential | 30
31. Balancing
Goal state: Reach an equal number of chunks per shard
Does not balance data and has no info how much actual data exists per chunk
息 Pythian Services Inc 2022 | Con鍖dential | 31
Shard1 Shard2 Shard3
64MB 64MB
64MB
64MB
64MB 64MB
64MB
64MB
64MB 64MB
64MB
64MB
32. Balancing
Data expires, gets deleted over time (TTL)
Empty chunks are being created
There is no internal thread to clean empty chunks
息 Pythian Services Inc 2022 | Con鍖dential | 32
Shard1 Shard2 Shard3
50MB 64MB 32MB
40MB
48MB
5MB 5MB
10MB
33. Balancing
Real data distribution
息 Pythian Services Inc 2022 | Con鍖dential | 33
Shard1 Shard2 Shard3
50MB 184MB 20MB
36. You have to run separate script to get all chunks with their size
dataSize command returns the size in bytes for the specified data
Requires scanning all of the chunks and documents associated with
Merging empty chunks using the mergeChunks operation
Rebalancing the chunks after the initial merge
moveChunk command to manually move
Let the balancer redistribute the chunks
Cluster metadata
息 Pythian Services Inc 2022 | Con鍖dential | 36
37. Merging chunks that are continuous range on the same shard
Merging empty chunks
息 Pythian Services Inc 2022 | Con鍖dential | 37
Shard1 Shard2 Shard3
50MB 64MB 32MB
40MB
48MB
5MB 5MB
10MB
38. Rebalancing only moves chunks that are not used - hot
Merging empty chunks
息 Pythian Services Inc 2022 | Con鍖dential | 38
Shard1 Shard2 Shard3
50MB 64MB 32MB
40MB
48MB
5MB 5MB
10MB
Migrate Migrate
39. Migrating chunks
1. The balancer process sends the moveChunk command to the source shard
2. The source starts the move with an internal moveChunk command. During the migration process,
operations to the chunk route to the source shard. The source shard is responsible for incoming write
operations for the chunk
3. The destination shard builds any indexes required by the source that do not exist on the destination
4. The destination shard begins requesting documents in the chunk and starts receiving copies of the data.
5. After receiving the final document in the chunk, the destination shard starts a synchronization process to
ensure that it has the changes to the migrated documents that occurred during the migration
6. When fully synchronized, the source shard connects to the config database and updates the
cluster metadata with the new location for the chunk
7. After the source shard completes the update of the metadata, and once there are no open cursors on the
chunk, the source shard deletes its copy of the documents
息 Pythian Services Inc 2022 | Con鍖dential | 39
40. Migrating chunks
Collection level lock to update the cluster metadata
Rebalancing only moves chunks that are not hot
Perform this in low peak hours
息 Pythian Services Inc 2022 | Con鍖dential | 40
44. The balancer only cares if the cluster has equal number of chunks per shard
Migrating a chunk from source to target shard requires collection level lock
Each chunk moved is being written 3 times (write on 1, write on 2, delete on 1)
Choose a shard key that will avoid broadcast operations on the cluster
Hashed based sharding usually requires more resources compared to range based
Choose a shard key that is included in most of your queries and with high cardinality
Scaling out might take days or weeks before the data gets redistributed
Only use sharding for your large collections
Avoid sharding if you can manage your data in replica sets
Summary
息 Pythian Services Inc 2022 | Con鍖dential | 44