SlideShare a Scribd company logo
MongoDBauto shardingAaron Stapleaaron@10gen.comMongo SeattleJuly 27, 2010
MongoDB v1.6out next week!
Why Scale Horizontally?Vertical scaling is expensiveHorizontal scaling is more incremental – works well in the cloudWill always be able to scale wider than higher
Distribution ModelsAd-hoc partitioningConsistent hashing (dynamo)Range based partitioning (BigTable/PNUTS)
Auto ShardingEach piece of data is exclusively controlled by a single node (shard)Each node (shard) has exclusive control over a well defined subset of the dataDatabase operations run against one shard when possible, multiple when necessaryAs system load changes, assignment of data to shards is rebalanced automatically
Mongo ShardingBasic goal is to make thismongodmongodmongod?client
Mongo ShardingLook like thismongodclient
Mongo ShardingMapping of documents to shards controlled by shard keyCan convert from single master to sharded cluster with 0 downtimeMost functionality of a single Mongo master is preservedFully consistent
Shard Key
Shard Key Examples{ user_id: 1 }{ state: 1 }{ lastname: 1, firstname: 1 }{ tag: 1, timestamp: -1 }{ _id: 1 }This is the defaultCareful when using ObjectId
Architecture Overview5500 <= user_id < +inf-inf <= user_id < 20002000 <= user_id < 5500mongodmongodmongodmongosclient
Query IShard key { user_id: 1 }db.users.find( { user_id: 5000 } )Query appropriate shard
Query IIShard key { user_id: 1 }db.users.find( { user_id: { $gt: 4000, $lt: 6000 } } )Query appropriate shard(s)
{ a : …, b : …, c : … }          a is declared shard keyfind( { a : { $gt : 333, $lt : 400 } )
{ a : …, b : …, c : … }          a is declared shard keyfind( { a : { $gt : 333, $lt : 2012 } )
Query IIIShard key { user_id: 1 }db.users.find( { hometown: ‘Seattle’ } )Query all shards
{ a : …, b : …, c : … }          a is declared shard keyfind( { a : { $gt : 333, $lt : 2012 } )
{ a : …, b : …, c : … }          secondary query, secondary indexensureIndex({b:1})find( { b : 99 } )This case good when m is small (such as here), also when the queries are large tasks
Query IVShard key { user_id: 1 }db.users.find( { hometown: ‘Seattle’ } ).sort( { user_id: 1 } )Query all shards, in sequence
Query VShard key { user_id: 1 }db.users.find( { hometown: ‘Seattle’ } ).sort( { lastname: 1 } )Query all shards in parallel, perform merge sortSecondary index in { lastname: 1 } can be used
Map/ReduceMap/Reduce was designed for distributed systemsMap/Reduce jobs will run on all relevant shards in parallel, subject to query spec
Map/Reduce> m = function() { emit(this.user_id, 1); }> r = function(k,vals) { return 1; }> res = db.events.mapReduce(m, r, { query : {type:'sale'} });> db[res.result].find().limit(2){ "_id" : 8321073716060 , "value" : 1 }{ "_id" : 7921232311289 , "value" : 1 }
WritesInserts routed to appropriate shard (inserted doc must contain shard key)Removes call upon matching shardsUpdates call upon matching shardsWrites parallel if asynchronous, sequential if synchronousUpdates cannot modify the shard key
Choice of Shard KeyKey per document that is generally a component of your queriesOften you want a unique keyIf not, consider granularity of key and potentially add fieldsThe shard key is generally comprised of fields you would put in an index if operating on a single machineBut in a sharded configuration, the shard key will be indexed automatically
Shard Key Examples (again){ user_id: 1 }{ state: 1 }{ lastname: 1, firstname: 1 }{ tag: 1, timestamp: -1 }{ _id: 1 }This is the defaultCareful when using ObjectId
Bit.ly Example~50M users~10K concurrently using server at peak~12.5B shortens per month (1K/sec peak)History of all shortens per user stored in mongo
Bit.ly Example - Schema{ "_id" : "lthp", "domain" : null, "keyword" : null, "title" : "bit.ly, a simple urlshortener", "url" : "http://jay.bit.ly/", "labels" : [	{"text" : "lthp",		"name" : "user_hash"},	{"text" : "BUFx",		"name" : "global_hash"},	{"text" : "http://jay.bit.ly/",		"name" : "url"},	{"text" : "bit.ly, a simple urlshortener",		"name" : "title"}], "ts" : 1229375771, "global_hash" : "BUFx", "user" : "jay", "media_type" : null }
Bit.ly ExampleShard key { user: 1 }Indices{ _id: 1 }{ user: 1 }{ global_hash: 1 }{ user: 1, ts: -1 }Querydb.history.find( { 	user : user,labels.text : … } ).sort( { ts: -1 } )
BalancingThe whole point of autosharding is that mongo balances the shards for youThe balancing algorithm is complicated, basic idea is that this1000 <= user_id < +inf-inf <= user_id < 1000
BalancingBecomes this3000 <= user_id < +inf-inf <= user_id < 3000
BalancingCurrent balancing metric is data sizeFuture possibilities – cpu, disk utilizationThere is some flexibility built into the partitioning algorithm – so we aren’t thrashing data back and forth between shardsOnly move one ‘chunk’ of data at a time – a conservative choice that limits total overhead of balancing
System Architecture
ShardRegular mongodprocess(es), storing all documents for a given key rangeHandles all reads/writes for this key range as wellEach shard indexes the data contained within itCan be single mongod, master/slave, or replica set
Shard - ChunkIn a sharded cluster, shards partitioned by shard keyWithin a shard, chunks partitioned by shard keyA chunk is the smallest unit of data for balancingData moves between chunks at chunk granularityUpper limit on chunk size is 200MBSpecial case if shard key range is open ended
Shard - Replica SetsReplica sets provide data redundancy and auto failoverIn the case of sharding, this means redundancy and failover per shardAll typical replica set operations are possibleFor example, write with w=NReplica sets were specifically designed to work as shards
System Architecture
MongosSharding router – distributes reads/writes to sharded clusterClient interface is the same as a mongodCan have as many mongos instances as you wantCan run on app server machine to avoid extra network trafficMongos also initiates balancing operationsKeeps metadata per chunk in RAM – 1MB RAM per 1TB of user data in cluster
System Architecture
Config Server3 Config serversChanges are made with a 2 phase commitIf any of the 3 servers goes down, config data becomes read onlySharded cluster will remain online as long as 1 of the config servers is runningConfig metadata size estimate1MB metadata per 1TB data in cluster
Shared Machines
Bit.ly Architecture
LimitationsUnique index constraints not expressed by shard key are not enforced across shardsUpdates to a document’s shard key aren’t allowed (you can remove and reinsert, but it’s not atomic)Balancing metric is limited to # of chunks right now – but this will be enhancedRight now only one chunk moves in the cluster at a time – this means balancing can be slow, it’s a conservative choice we’ve made to keep the overhead of balancing low for now20 petabyte size limit
Start Up Config Servers$ mkdir -p ~/dbs/config$ ./mongod --dbpath ~/dbs/config --port 20000Repeat as necessary
Start Up Mongos$ ./mongos --port 30000 --configdb localhost:20000No dbpathRepeat as necessary
Start Up Shards$ mkdir -p ~/dbs/shard1 $ ./mongod --dbpath ~/dbs/shard1 --port 10000Repeat as necessary
Configure Shards$ ./mongo localhost:30000/admin> db.runCommand({addshard : "localhost:10000", allowLocal : true}){"added" : "localhost:10000", "ok" : true}Repeat as necessary
Shard Data> db.runCommand({"enablesharding" : "foo"})> db.runCommand({"shardcollection" : "foo.bar", "key" : {"_id" : 1}})Repeat as necessary
Production ConfigurationMultiple config serversMultiple mongos serversReplica sets for each shardUse getLastError/w correctly
Looking at config dataMongo shell connected to config server, config database> db.shards.find() { "_id" : "shard0", "host" : "localhost:10000" } { "_id" : "shard1", "host" : "localhost:10001" }
Looking at config data> db.databases.find() { "_id" : "admin", "partitioned" : false, "primary" : "config" } { "_id" : "foo", "partitioned" : false, "primary" : "shard1" } { "_id" : "x", "partitioned" : false, "primary" : "shard0” }{"_id" : "test", "partitioned" : true, "primary" : "shard0", "sharded" :	 {		"test.foo" : { "key" : {"x" : 1}, "unique" : false }	}}
Looking at config data> db.chunks.find() {"_id" : "test.foo-x_MinKey", "lastmod" : { "t" : 1276636243000, "i" : 1 }, "ns" : "test.foo", "min" : {"x" : { $minKey : 1 } }, "max" : {"x" : { $maxKey : 1 } }, "shard" : "shard0”}
Looking at config data> db.printShardingStatus() --- Sharding Status ---sharding version: { "_id" : 1, "version" : 3 } shards:	{ "_id" : "shard0", "host" : "localhost:10000" }	{ "_id" : "shard1", "host" : "localhost:10001" } databases:	{ "_id" : "admin", "partitioned" : false, "primary" : "config" } 	{ "_id" : "foo", "partitioned" : false, "primary" : "shard1" } 	{ "_id" : "x", "partitioned" : false, "primary" : "shard0" } 	{ "_id" : "test", "partitioned" : true, "primary" : "shard0","sharded" : { "test.foo" : { "key" : { "x" : 1 }, "unique" : false } } }test.foo chunks:	{ "x" : { $minKey : 1 } } -->> { "x" : { $maxKey : 1 } } on : shard0 { "t" : 1276636243 …
Give it a Try!Download from mongodb.orgSharding production ready in 1.6, which is scheduled for release next weekFor now use 1.5 (unstable) to try sharding

More Related Content

What's hot (20)

PPTX
Back to Basics 2017: Introduction to Sharding
MongoDB
 
PPTX
MongoDB for Time Series Data: Sharding
MongoDB
 
PPTX
Sharding Methods for MongoDB
MongoDB
 
PDF
Sharding
MongoDB
 
PPTX
Webinar: Performance Tuning + Optimization
MongoDB
 
PPT
Migrating to MongoDB: Best Practices
MongoDB
 
PPTX
Mongo db multidc_webinar
MongoDB
 
PPTX
MongoDB at Scale
MongoDB
 
PDF
A New MongoDB Sharding Architecture for Higher Availability and Better Resour...
leifwalsh
 
PPTX
I have a good shard key now what - Advanced Sharding
David Murphy
 
KEY
Sharding with MongoDB (Eliot Horowitz)
MongoSF
 
PPTX
Lightning Talk: What You Need to Know Before You Shard in 20 Minutes
MongoDB
 
PPTX
MongoDB San Francisco 2013: Basic Sharding in MongoDB presented by Brandon Bl...
MongoDB
 
PPTX
Basic Sharding in MongoDB presented by Shaun Verch
MongoDB
 
PDF
Webinar: Schema Patterns and Your Storage Engine
MongoDB
 
PDF
Development to Production with Sharded MongoDB Clusters
Severalnines
 
PPTX
MongoDB Capacity Planning
Norberto Leite
 
PPTX
Webinar: Choosing the Right Shard Key for High Performance and Scale
MongoDB
 
PDF
MongoDB Sharding Fundamentals
Antonios Giannopoulos
 
PDF
Шардинг в MongoDB, Henrik Ingo (MongoDB)
Ontico
 
Back to Basics 2017: Introduction to Sharding
MongoDB
 
MongoDB for Time Series Data: Sharding
MongoDB
 
Sharding Methods for MongoDB
MongoDB
 
Sharding
MongoDB
 
Webinar: Performance Tuning + Optimization
MongoDB
 
Migrating to MongoDB: Best Practices
MongoDB
 
Mongo db multidc_webinar
MongoDB
 
MongoDB at Scale
MongoDB
 
A New MongoDB Sharding Architecture for Higher Availability and Better Resour...
leifwalsh
 
I have a good shard key now what - Advanced Sharding
David Murphy
 
Sharding with MongoDB (Eliot Horowitz)
MongoSF
 
Lightning Talk: What You Need to Know Before You Shard in 20 Minutes
MongoDB
 
MongoDB San Francisco 2013: Basic Sharding in MongoDB presented by Brandon Bl...
MongoDB
 
Basic Sharding in MongoDB presented by Shaun Verch
MongoDB
 
Webinar: Schema Patterns and Your Storage Engine
MongoDB
 
Development to Production with Sharded MongoDB Clusters
Severalnines
 
MongoDB Capacity Planning
Norberto Leite
 
Webinar: Choosing the Right Shard Key for High Performance and Scale
MongoDB
 
MongoDB Sharding Fundamentals
Antonios Giannopoulos
 
Шардинг в MongoDB, Henrik Ingo (MongoDB)
Ontico
 

Viewers also liked (20)

KEY
Mongo Seattle - The Business of MongoDB
Justin Smestad
 
PPTX
MongoDB for Time Series Data
MongoDB
 
PPTX
MongoDB for Time Series Data Part 3: Sharding
MongoDB
 
PPTX
MongoDB for Time Series Data Part 2: Analyzing Time Series Data Using the Agg...
MongoDB
 
PPTX
MongoDB for Time Series Data Part 1: Setting the Stage for Sensor Management
MongoDB
 
PPT
MongoDB Schema Design
MongoDB
 
PDF
Air Travel Market Segmentation Considerations
Turkish Airlines
 
PPTX
Common MongoDB Use Cases
MongoDB
 
PDF
Business Intelligence Presentation (1/2)
Bernardo Najlis
 
PPT
Data Mining Concepts
Dung Nguyen
 
PDF
14 steps to build a professional reseller partner program
Daniel Nilsson
 
PPTX
Slide guide for consulting-style presentations
reallygoodppts
 
PPSX
Experimental research design
Nursing Path
 
PPT
Business to business marketing ppt
Sukriti Mal
 
PPTX
MongoDB Schema Design: Four Real-World Examples
Mike Friedman
 
PPTX
Introduction to computer network
Ashita Agrawal
 
PPTX
An introduction to fundamental architecture concepts
wweinmeyer79
 
PDF
Mapping the customer experience: innovate using customer experience journey maps
Joyce Hostyn
 
PPTX
Big data ppt
Nasrin Hussain
 
PDF
Plan symbols
gopaltry
 
Mongo Seattle - The Business of MongoDB
Justin Smestad
 
MongoDB for Time Series Data
MongoDB
 
MongoDB for Time Series Data Part 3: Sharding
MongoDB
 
MongoDB for Time Series Data Part 2: Analyzing Time Series Data Using the Agg...
MongoDB
 
MongoDB for Time Series Data Part 1: Setting the Stage for Sensor Management
MongoDB
 
MongoDB Schema Design
MongoDB
 
Air Travel Market Segmentation Considerations
Turkish Airlines
 
Common MongoDB Use Cases
MongoDB
 
Business Intelligence Presentation (1/2)
Bernardo Najlis
 
Data Mining Concepts
Dung Nguyen
 
14 steps to build a professional reseller partner program
Daniel Nilsson
 
Slide guide for consulting-style presentations
reallygoodppts
 
Experimental research design
Nursing Path
 
Business to business marketing ppt
Sukriti Mal
 
MongoDB Schema Design: Four Real-World Examples
Mike Friedman
 
Introduction to computer network
Ashita Agrawal
 
An introduction to fundamental architecture concepts
wweinmeyer79
 
Mapping the customer experience: innovate using customer experience journey maps
Joyce Hostyn
 
Big data ppt
Nasrin Hussain
 
Plan symbols
gopaltry
 
Ad

Similar to MongoDB Auto-Sharding at Mongo Seattle (20)

PPT
2011 mongo FR - scaling with mongodb
antoinegirbal
 
PPT
2010 mongo berlin-shardinginternals (1)
MongoDB
 
PPTX
Webinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based Sharding
MongoDB
 
PPTX
Sharding
MongoDB
 
PPTX
Introduction to Sharding
MongoDB
 
PDF
Introduction to Sharding
MongoDB
 
KEY
Scaling MongoDB (Mongo Austin)
MongoDB
 
PDF
Sharding
MongoDB
 
PPTX
Webinar: Sharding
MongoDB
 
KEY
2011 mongo sf-scaling
MongoDB
 
PDF
OSDC 2012 | Scaling with MongoDB by Ross Lawley
NETWAYS
 
KEY
Sharding with MongoDB (Eliot Horowitz)
MongoDB
 
PPTX
Scaling with MongoDB
MongoDB
 
PDF
Sharding in MongoDB Days 2013
Randall Hunt
 
ODP
MongoDB Devops Madrid February 2012
Juan Vicente Herrera Ruiz de Alejo
 
PPTX
MongoDB San Francisco 2013: Hash-based Sharding in MongoDB 2.4 presented by B...
MongoDB
 
PDF
Evolution of MonogDB Sharding and Its Best Practices - Ranjith A - Mydbops Team
Mydbops
 
PPT
Mongodb
Gagan Gowda
 
KEY
Scaling with MongoDB
MongoDB
 
PPTX
Sharding
MongoDB
 
2011 mongo FR - scaling with mongodb
antoinegirbal
 
2010 mongo berlin-shardinginternals (1)
MongoDB
 
Webinar: MongoDB 2.4 Feature Demo and Q&A on Hash-based Sharding
MongoDB
 
Sharding
MongoDB
 
Introduction to Sharding
MongoDB
 
Introduction to Sharding
MongoDB
 
Scaling MongoDB (Mongo Austin)
MongoDB
 
Sharding
MongoDB
 
Webinar: Sharding
MongoDB
 
2011 mongo sf-scaling
MongoDB
 
OSDC 2012 | Scaling with MongoDB by Ross Lawley
NETWAYS
 
Sharding with MongoDB (Eliot Horowitz)
MongoDB
 
Scaling with MongoDB
MongoDB
 
Sharding in MongoDB Days 2013
Randall Hunt
 
MongoDB Devops Madrid February 2012
Juan Vicente Herrera Ruiz de Alejo
 
MongoDB San Francisco 2013: Hash-based Sharding in MongoDB 2.4 presented by B...
MongoDB
 
Evolution of MonogDB Sharding and Its Best Practices - Ranjith A - Mydbops Team
Mydbops
 
Mongodb
Gagan Gowda
 
Scaling with MongoDB
MongoDB
 
Sharding
MongoDB
 
Ad

More from MongoDB (20)

PDF
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB
 
PDF
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB
 
PDF
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB
 
PDF
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB
 
PDF
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB
 
PDF
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB
 
PDF
MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB
 
PDF
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB
 
PDF
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB
 
PDF
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB
 
PDF
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB
 
PDF
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB
 
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB
 
PDF
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB
 
PDF
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB
 
PDF
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB
 
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB
 
PDF
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB
 
PDF
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB
 
PDF
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB
 
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB
 
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB
 
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB
 
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB
 
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB
 
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB
 
MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB
 
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB
 
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB
 
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB
 
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB
 
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB
 
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB
 
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB
 
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB
 
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB
 
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB
 
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB
 
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB
 
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB
 

Recently uploaded (20)

PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
PDF
Complete Network Protection with Real-Time Security
L4RGINDIA
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
July Patch Tuesday
Ivanti
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
Complete Network Protection with Real-Time Security
L4RGINDIA
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
July Patch Tuesday
Ivanti
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 

MongoDB Auto-Sharding at Mongo Seattle

  • 3. Why Scale Horizontally?Vertical scaling is expensiveHorizontal scaling is more incremental – works well in the cloudWill always be able to scale wider than higher
  • 4. Distribution ModelsAd-hoc partitioningConsistent hashing (dynamo)Range based partitioning (BigTable/PNUTS)
  • 5. Auto ShardingEach piece of data is exclusively controlled by a single node (shard)Each node (shard) has exclusive control over a well defined subset of the dataDatabase operations run against one shard when possible, multiple when necessaryAs system load changes, assignment of data to shards is rebalanced automatically
  • 6. Mongo ShardingBasic goal is to make thismongodmongodmongod?client
  • 7. Mongo ShardingLook like thismongodclient
  • 8. Mongo ShardingMapping of documents to shards controlled by shard keyCan convert from single master to sharded cluster with 0 downtimeMost functionality of a single Mongo master is preservedFully consistent
  • 10. Shard Key Examples{ user_id: 1 }{ state: 1 }{ lastname: 1, firstname: 1 }{ tag: 1, timestamp: -1 }{ _id: 1 }This is the defaultCareful when using ObjectId
  • 11. Architecture Overview5500 <= user_id < +inf-inf <= user_id < 20002000 <= user_id < 5500mongodmongodmongodmongosclient
  • 12. Query IShard key { user_id: 1 }db.users.find( { user_id: 5000 } )Query appropriate shard
  • 13. Query IIShard key { user_id: 1 }db.users.find( { user_id: { $gt: 4000, $lt: 6000 } } )Query appropriate shard(s)
  • 14. { a : …, b : …, c : … } a is declared shard keyfind( { a : { $gt : 333, $lt : 400 } )
  • 15. { a : …, b : …, c : … } a is declared shard keyfind( { a : { $gt : 333, $lt : 2012 } )
  • 16. Query IIIShard key { user_id: 1 }db.users.find( { hometown: ‘Seattle’ } )Query all shards
  • 17. { a : …, b : …, c : … } a is declared shard keyfind( { a : { $gt : 333, $lt : 2012 } )
  • 18. { a : …, b : …, c : … } secondary query, secondary indexensureIndex({b:1})find( { b : 99 } )This case good when m is small (such as here), also when the queries are large tasks
  • 19. Query IVShard key { user_id: 1 }db.users.find( { hometown: ‘Seattle’ } ).sort( { user_id: 1 } )Query all shards, in sequence
  • 20. Query VShard key { user_id: 1 }db.users.find( { hometown: ‘Seattle’ } ).sort( { lastname: 1 } )Query all shards in parallel, perform merge sortSecondary index in { lastname: 1 } can be used
  • 21. Map/ReduceMap/Reduce was designed for distributed systemsMap/Reduce jobs will run on all relevant shards in parallel, subject to query spec
  • 22. Map/Reduce> m = function() { emit(this.user_id, 1); }> r = function(k,vals) { return 1; }> res = db.events.mapReduce(m, r, { query : {type:'sale'} });> db[res.result].find().limit(2){ "_id" : 8321073716060 , "value" : 1 }{ "_id" : 7921232311289 , "value" : 1 }
  • 23. WritesInserts routed to appropriate shard (inserted doc must contain shard key)Removes call upon matching shardsUpdates call upon matching shardsWrites parallel if asynchronous, sequential if synchronousUpdates cannot modify the shard key
  • 24. Choice of Shard KeyKey per document that is generally a component of your queriesOften you want a unique keyIf not, consider granularity of key and potentially add fieldsThe shard key is generally comprised of fields you would put in an index if operating on a single machineBut in a sharded configuration, the shard key will be indexed automatically
  • 25. Shard Key Examples (again){ user_id: 1 }{ state: 1 }{ lastname: 1, firstname: 1 }{ tag: 1, timestamp: -1 }{ _id: 1 }This is the defaultCareful when using ObjectId
  • 26. Bit.ly Example~50M users~10K concurrently using server at peak~12.5B shortens per month (1K/sec peak)History of all shortens per user stored in mongo
  • 27. Bit.ly Example - Schema{ "_id" : "lthp", "domain" : null, "keyword" : null, "title" : "bit.ly, a simple urlshortener", "url" : "http://jay.bit.ly/", "labels" : [ {"text" : "lthp", "name" : "user_hash"}, {"text" : "BUFx", "name" : "global_hash"}, {"text" : "http://jay.bit.ly/", "name" : "url"}, {"text" : "bit.ly, a simple urlshortener", "name" : "title"}], "ts" : 1229375771, "global_hash" : "BUFx", "user" : "jay", "media_type" : null }
  • 28. Bit.ly ExampleShard key { user: 1 }Indices{ _id: 1 }{ user: 1 }{ global_hash: 1 }{ user: 1, ts: -1 }Querydb.history.find( {  user : user,labels.text : … } ).sort( { ts: -1 } )
  • 29. BalancingThe whole point of autosharding is that mongo balances the shards for youThe balancing algorithm is complicated, basic idea is that this1000 <= user_id < +inf-inf <= user_id < 1000
  • 30. BalancingBecomes this3000 <= user_id < +inf-inf <= user_id < 3000
  • 31. BalancingCurrent balancing metric is data sizeFuture possibilities – cpu, disk utilizationThere is some flexibility built into the partitioning algorithm – so we aren’t thrashing data back and forth between shardsOnly move one ‘chunk’ of data at a time – a conservative choice that limits total overhead of balancing
  • 33. ShardRegular mongodprocess(es), storing all documents for a given key rangeHandles all reads/writes for this key range as wellEach shard indexes the data contained within itCan be single mongod, master/slave, or replica set
  • 34. Shard - ChunkIn a sharded cluster, shards partitioned by shard keyWithin a shard, chunks partitioned by shard keyA chunk is the smallest unit of data for balancingData moves between chunks at chunk granularityUpper limit on chunk size is 200MBSpecial case if shard key range is open ended
  • 35. Shard - Replica SetsReplica sets provide data redundancy and auto failoverIn the case of sharding, this means redundancy and failover per shardAll typical replica set operations are possibleFor example, write with w=NReplica sets were specifically designed to work as shards
  • 37. MongosSharding router – distributes reads/writes to sharded clusterClient interface is the same as a mongodCan have as many mongos instances as you wantCan run on app server machine to avoid extra network trafficMongos also initiates balancing operationsKeeps metadata per chunk in RAM – 1MB RAM per 1TB of user data in cluster
  • 39. Config Server3 Config serversChanges are made with a 2 phase commitIf any of the 3 servers goes down, config data becomes read onlySharded cluster will remain online as long as 1 of the config servers is runningConfig metadata size estimate1MB metadata per 1TB data in cluster
  • 42. LimitationsUnique index constraints not expressed by shard key are not enforced across shardsUpdates to a document’s shard key aren’t allowed (you can remove and reinsert, but it’s not atomic)Balancing metric is limited to # of chunks right now – but this will be enhancedRight now only one chunk moves in the cluster at a time – this means balancing can be slow, it’s a conservative choice we’ve made to keep the overhead of balancing low for now20 petabyte size limit
  • 43. Start Up Config Servers$ mkdir -p ~/dbs/config$ ./mongod --dbpath ~/dbs/config --port 20000Repeat as necessary
  • 44. Start Up Mongos$ ./mongos --port 30000 --configdb localhost:20000No dbpathRepeat as necessary
  • 45. Start Up Shards$ mkdir -p ~/dbs/shard1 $ ./mongod --dbpath ~/dbs/shard1 --port 10000Repeat as necessary
  • 46. Configure Shards$ ./mongo localhost:30000/admin> db.runCommand({addshard : "localhost:10000", allowLocal : true}){"added" : "localhost:10000", "ok" : true}Repeat as necessary
  • 47. Shard Data> db.runCommand({"enablesharding" : "foo"})> db.runCommand({"shardcollection" : "foo.bar", "key" : {"_id" : 1}})Repeat as necessary
  • 48. Production ConfigurationMultiple config serversMultiple mongos serversReplica sets for each shardUse getLastError/w correctly
  • 49. Looking at config dataMongo shell connected to config server, config database> db.shards.find() { "_id" : "shard0", "host" : "localhost:10000" } { "_id" : "shard1", "host" : "localhost:10001" }
  • 50. Looking at config data> db.databases.find() { "_id" : "admin", "partitioned" : false, "primary" : "config" } { "_id" : "foo", "partitioned" : false, "primary" : "shard1" } { "_id" : "x", "partitioned" : false, "primary" : "shard0” }{"_id" : "test", "partitioned" : true, "primary" : "shard0", "sharded" : { "test.foo" : { "key" : {"x" : 1}, "unique" : false } }}
  • 51. Looking at config data> db.chunks.find() {"_id" : "test.foo-x_MinKey", "lastmod" : { "t" : 1276636243000, "i" : 1 }, "ns" : "test.foo", "min" : {"x" : { $minKey : 1 } }, "max" : {"x" : { $maxKey : 1 } }, "shard" : "shard0”}
  • 52. Looking at config data> db.printShardingStatus() --- Sharding Status ---sharding version: { "_id" : 1, "version" : 3 } shards: { "_id" : "shard0", "host" : "localhost:10000" } { "_id" : "shard1", "host" : "localhost:10001" } databases: { "_id" : "admin", "partitioned" : false, "primary" : "config" } { "_id" : "foo", "partitioned" : false, "primary" : "shard1" } { "_id" : "x", "partitioned" : false, "primary" : "shard0" } { "_id" : "test", "partitioned" : true, "primary" : "shard0","sharded" : { "test.foo" : { "key" : { "x" : 1 }, "unique" : false } } }test.foo chunks: { "x" : { $minKey : 1 } } -->> { "x" : { $maxKey : 1 } } on : shard0 { "t" : 1276636243 …
  • 53. Give it a Try!Download from mongodb.orgSharding production ready in 1.6, which is scheduled for release next weekFor now use 1.5 (unstable) to try sharding