SlideShare a Scribd company logo
The Care & Feeding of a
Large MongoDB Cluster
                          2012

Chris Henry
@chrishnry
hello.
Who uses MongoDB?

In Production?
MongoDB @ Behance


 Activity Feed v1
 ~ 40 Nodes
 ~ 250 Million Docs
 ~ ext3 Filesystem

 Ran for around 3 months, then...
MongoDB @ Behance
MongoDB @ Behance


 Activity Feed v2
 • ext4 Filesystem
 • 2 TB data
 • 3 Collections
 • 15k Chunks
 • 19 Shards
 • 60 Nodes
 • 400M Docs at peak
 • Ranges ~120-250M
Why MongoDB?


  • Easy to use.
  • Easy to Iterate on.
  • Devs like it.
  • Fantastic Community built by 10Gen.
  • “Fast.”
Why NOT MongoDB?


  • Bleeding Edge.
  • Not enough battle scars.
  • Fewer tried and true fixes.
  • No Transactional support.
Why MongoDB at Scale?


  • Autosharding. (Stop hacking your app.)
  • Smart Replica Sets / High Availability.
  • Horizontal Scalability
  • Easy to grow and shrink.
  • Good fit for cloud*.
Why NOT MongoDB at Scale?


  • Data can take up more space on disk.
  • Disk IO in the cloud sucks.
  • Database-level write lock.
  • More Management than a MySQL cluster.
Behance’s Use Case + Fit


  • Data is ephemeral.
  • Denormalization of existing data.
  • Fan Out Approach.
  • Sharded by User.
Care & Feeding. Srsly?


  • As a less mature DB, admins need to be a
  bit more aware.
  • No Different than MySQL (Need to take into
  account memory, disk, usage patterns,
  indexes)
  • Watch Error Logs, Disk Use, Data Size, # of
  Chunks, Old files, Sharding Status, Padding
  Factors...
MongoDB Basics




    MongoDB Docs are great, and always improving.

    http://docs.mongodb.org/manual/
Indexes



  You need them.




  duh and/or hello.
Profiling


  • The profiler is equivalent to the slow log in
  MySQL.

  • Logs all operations slower than X seconds
  to a collection.
  // log slow operations, slow threshold=50ms
  > db.setProfilingLevel(1,50)

  // get operations that were slow.
  db.system.profile.find( { millis : { $gt : 5 } } )


  http://www.mongodb.org/display/DOCS/Database+Profiler
Explain


   • Equivalent to MySQL’s EXPLAIN
   • From Profiler, grab $query + $orderby, build
   into real query.

 // Explain a query
 db.collection.find({ x: 1 }).explain()


 http://www.mongodb.org/display/DOCS/Explain
Replica Sets


 • Equivalent to MySQL’s replication, but not quite.
 • Resiliency and availability through cleverness.
 • ReplicaSet setups
 • rs.stepDown()
 • rs.slaveOk()
 • w parameter
Replica Sets

// mongod.conf
replSet = myreplica

// Initiate the Replica Set
> rs.initiate()

//Add a node
> rs.add(“myreplica1:27017”);

// Allow reads from the secondaries
> rs.slaveOk()

// Write something.
> db.replica.insert({x:1});

// Make sure write propogates to majority of servers.
> db.runCommand( { getlasterror : 1 , w : "majority" } )


http://www.mongodb.org/display/DOCS/Replica+Set+Commands
http://docs.mongodb.org/manual/applications/replication/#read-preference
http://www.mongodb.org/display/DOCS/Verifying+Propagation+of+Writes+with+getLastError
Sharding


  • Goal: Distribute data across many nodes / replica sets.
  • Provides baked-in horizontal scalability.
Sharding


  • Chunks.
  • Routing process - mongos (manages balancing, and query routing)
  • Shards - mongod configured with shardsvr = 1.
  • Config servers - 3 mongod servers configured on mongos.conf
  • Can be replica sets, or stand alone servers.
  • Shard Key
Sharding


// mongos.conf
configdb = server,server,server

// Initiate
// connect to mongos the same way you would connect to mongod
> db.runCommand( { addshard : "<serverhostname>[:<port>]" } );

// Shard a collection
> db.runCommand( { shardcollection : "test.fs.chunks", key :
{ files_id : 1 } } )


http://www.mongodb.org/display/DOCS/Replica+Set+Commands
Indexing Big - Sharded Index


 • Always run on mongos
 • Background Indexing
 • Sparse indexes
Maintenance


 • Replica Sets
 • Add Node / Remove Node
 • rs.stepDown()


 • Shards
 • Drain Shard / Add Shard
Gotchas


 • A Sharded cluster with a shard down will
 return no results.

 • If a chunk has too much data dedicated to a
 single shard key, and cannot split it, balancing
 will become blocked, and the cluster will
 become unbalanced.
Hardware / OS


 • No knobs in MongoDB.
 • Filesystem. (ext4 or xfs)
 • Memory.
 • Unix distro. Linux kernel >= 2.6.23
 http://www.mongodb.org/display/DOCS/Production+Notes#ProductionNotes-LinuxFileSystems
That’s it!


 Thank you all for coming!

 Feedback is welcome!

More Related Content

PDF
ODP
An Introduction to REDIS NoSQL database
PDF
XtraDB 5.6 and 5.7: Key Performance Algorithms
PDF
Perl Programming - 04 Programming Database
PPTX
Redis/Lessons learned
PPTX
PPTX
Introduction to Redis
PDF
Storage Methods for Nonstandard Data Patterns
An Introduction to REDIS NoSQL database
XtraDB 5.6 and 5.7: Key Performance Algorithms
Perl Programming - 04 Programming Database
Redis/Lessons learned
Introduction to Redis
Storage Methods for Nonstandard Data Patterns

What's hot (20)

ODP
Introduction to Redis
PDF
Installing postgres & postgis
PPTX
A simple introduction to redis
KEY
Redis overview for Software Architecture Forum
ODP
MongoDB & PHP
PDF
Perl Programming - 03 Programming File
PDF
Perl for System Automation - 01 Advanced File Processing
ODP
phptek13 - Caching and tuning fun tutorial
PPTX
Redis and it's data types
PDF
Mysql Fulltext Search 1
PDF
Redis basics
PDF
XtraDB 5.7: key performance algorithms
PDF
Redis 101
PDF
Elastic Search Training#1 (brief tutorial)-ESCC#1
PDF
Ugif 10 2012 beauty ofifmxdiskstructs ugif
ODP
Introduction to MongoDB with PHP
PDF
Non-Framework MVC sites with PHP
PDF
PGDAY FR 2014 : presentation de Postgresql chez leboncoin.fr
PPT
Introduction to redis
PDF
Thinking in documents
Introduction to Redis
Installing postgres & postgis
A simple introduction to redis
Redis overview for Software Architecture Forum
MongoDB & PHP
Perl Programming - 03 Programming File
Perl for System Automation - 01 Advanced File Processing
phptek13 - Caching and tuning fun tutorial
Redis and it's data types
Mysql Fulltext Search 1
Redis basics
XtraDB 5.7: key performance algorithms
Redis 101
Elastic Search Training#1 (brief tutorial)-ESCC#1
Ugif 10 2012 beauty ofifmxdiskstructs ugif
Introduction to MongoDB with PHP
Non-Framework MVC sites with PHP
PGDAY FR 2014 : presentation de Postgresql chez leboncoin.fr
Introduction to redis
Thinking in documents
Ad

Viewers also liked (6)

PPTX
Cost savings and expert system advice with athene ES/1
PPTX
Big Data Performance and Capacity Management
PDF
Scaling Spark Workloads on YARN - Boulder/Denver July 2015
PPTX
Capacity Management and BigData/Hadoop - Hitchhiker's guide for the Capacity ...
PPTX
Provisioning and Capacity Planning (Travel Meets Big Data)
PPTX
Modern Data Architecture
Cost savings and expert system advice with athene ES/1
Big Data Performance and Capacity Management
Scaling Spark Workloads on YARN - Boulder/Denver July 2015
Capacity Management and BigData/Hadoop - Hitchhiker's guide for the Capacity ...
Provisioning and Capacity Planning (Travel Meets Big Data)
Modern Data Architecture
Ad

Similar to The Care + Feeding of a Mongodb Cluster (20)

KEY
Deployment Strategy
PPT
MongoDB Pros and Cons
PDF
PDF
Mongo db first steps with csharp
PPTX
MongoDB
KEY
Deployment Strategies
PPTX
MongoDB : Scaling, Security & Performance
PPTX
MongoDB 2.4 and spring data
PPT
MongoDB Knowledge Shareing
KEY
PPT
5 Pitfalls to Avoid with MongoDB
PDF
Cassandra Community Webinar: From Mongo to Cassandra, Architectural Lessons
KEY
MongoDB vs Mysql. A devops point of view
PPTX
MongoDB Internals
PDF
Mongodb
PDF
Deployment
PDF
Introduction to Mongodb
PPTX
MongoDB Introduction - Document Oriented Nosql Database
PPTX
Conceptos básicos. Seminario web 6: Despliegue de producción
PDF
mongodb tutorial
Deployment Strategy
MongoDB Pros and Cons
Mongo db first steps with csharp
MongoDB
Deployment Strategies
MongoDB : Scaling, Security & Performance
MongoDB 2.4 and spring data
MongoDB Knowledge Shareing
5 Pitfalls to Avoid with MongoDB
Cassandra Community Webinar: From Mongo to Cassandra, Architectural Lessons
MongoDB vs Mysql. A devops point of view
MongoDB Internals
Mongodb
Deployment
Introduction to Mongodb
MongoDB Introduction - Document Oriented Nosql Database
Conceptos básicos. Seminario web 6: Despliegue de producción
mongodb tutorial

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Encapsulation theory and applications.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
A Presentation on Artificial Intelligence
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
The AUB Centre for AI in Media Proposal.docx
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Diabetes mellitus diagnosis method based random forest with bat algorithm
MYSQL Presentation for SQL database connectivity
Digital-Transformation-Roadmap-for-Companies.pptx
Network Security Unit 5.pdf for BCA BBA.
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Encapsulation theory and applications.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Big Data Technologies - Introduction.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Machine learning based COVID-19 study performance prediction
Spectral efficient network and resource selection model in 5G networks
Advanced methodologies resolving dimensionality complications for autism neur...
A Presentation on Artificial Intelligence
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
CIFDAQ's Market Insight: SEC Turns Pro Crypto

The Care + Feeding of a Mongodb Cluster

  • 1. The Care & Feeding of a Large MongoDB Cluster 2012 Chris Henry @chrishnry
  • 3. Who uses MongoDB? In Production?
  • 4. MongoDB @ Behance Activity Feed v1 ~ 40 Nodes ~ 250 Million Docs ~ ext3 Filesystem Ran for around 3 months, then...
  • 6. MongoDB @ Behance Activity Feed v2 • ext4 Filesystem • 2 TB data • 3 Collections • 15k Chunks • 19 Shards • 60 Nodes • 400M Docs at peak • Ranges ~120-250M
  • 7. Why MongoDB? • Easy to use. • Easy to Iterate on. • Devs like it. • Fantastic Community built by 10Gen. • “Fast.”
  • 8. Why NOT MongoDB? • Bleeding Edge. • Not enough battle scars. • Fewer tried and true fixes. • No Transactional support.
  • 9. Why MongoDB at Scale? • Autosharding. (Stop hacking your app.) • Smart Replica Sets / High Availability. • Horizontal Scalability • Easy to grow and shrink. • Good fit for cloud*.
  • 10. Why NOT MongoDB at Scale? • Data can take up more space on disk. • Disk IO in the cloud sucks. • Database-level write lock. • More Management than a MySQL cluster.
  • 11. Behance’s Use Case + Fit • Data is ephemeral. • Denormalization of existing data. • Fan Out Approach. • Sharded by User.
  • 12. Care & Feeding. Srsly? • As a less mature DB, admins need to be a bit more aware. • No Different than MySQL (Need to take into account memory, disk, usage patterns, indexes) • Watch Error Logs, Disk Use, Data Size, # of Chunks, Old files, Sharding Status, Padding Factors...
  • 13. MongoDB Basics MongoDB Docs are great, and always improving. http://docs.mongodb.org/manual/
  • 14. Indexes You need them. duh and/or hello.
  • 15. Profiling • The profiler is equivalent to the slow log in MySQL. • Logs all operations slower than X seconds to a collection. // log slow operations, slow threshold=50ms > db.setProfilingLevel(1,50) // get operations that were slow. db.system.profile.find( { millis : { $gt : 5 } } ) http://www.mongodb.org/display/DOCS/Database+Profiler
  • 16. Explain • Equivalent to MySQL’s EXPLAIN • From Profiler, grab $query + $orderby, build into real query. // Explain a query db.collection.find({ x: 1 }).explain() http://www.mongodb.org/display/DOCS/Explain
  • 17. Replica Sets • Equivalent to MySQL’s replication, but not quite. • Resiliency and availability through cleverness. • ReplicaSet setups • rs.stepDown() • rs.slaveOk() • w parameter
  • 18. Replica Sets // mongod.conf replSet = myreplica // Initiate the Replica Set > rs.initiate() //Add a node > rs.add(“myreplica1:27017”); // Allow reads from the secondaries > rs.slaveOk() // Write something. > db.replica.insert({x:1}); // Make sure write propogates to majority of servers. > db.runCommand( { getlasterror : 1 , w : "majority" } ) http://www.mongodb.org/display/DOCS/Replica+Set+Commands http://docs.mongodb.org/manual/applications/replication/#read-preference http://www.mongodb.org/display/DOCS/Verifying+Propagation+of+Writes+with+getLastError
  • 19. Sharding • Goal: Distribute data across many nodes / replica sets. • Provides baked-in horizontal scalability.
  • 20. Sharding • Chunks. • Routing process - mongos (manages balancing, and query routing) • Shards - mongod configured with shardsvr = 1. • Config servers - 3 mongod servers configured on mongos.conf • Can be replica sets, or stand alone servers. • Shard Key
  • 21. Sharding // mongos.conf configdb = server,server,server // Initiate // connect to mongos the same way you would connect to mongod > db.runCommand( { addshard : "<serverhostname>[:<port>]" } ); // Shard a collection > db.runCommand( { shardcollection : "test.fs.chunks", key : { files_id : 1 } } ) http://www.mongodb.org/display/DOCS/Replica+Set+Commands
  • 22. Indexing Big - Sharded Index • Always run on mongos • Background Indexing • Sparse indexes
  • 23. Maintenance • Replica Sets • Add Node / Remove Node • rs.stepDown() • Shards • Drain Shard / Add Shard
  • 24. Gotchas • A Sharded cluster with a shard down will return no results. • If a chunk has too much data dedicated to a single shard key, and cannot split it, balancing will become blocked, and the cluster will become unbalanced.
  • 25. Hardware / OS • No knobs in MongoDB. • Filesystem. (ext4 or xfs) • Memory. • Unix distro. Linux kernel >= 2.6.23 http://www.mongodb.org/display/DOCS/Production+Notes#ProductionNotes-LinuxFileSystems
  • 26. That’s it! Thank you all for coming! Feedback is welcome!

Editor's Notes

  • #2: \n
  • #3: I&amp;#x2019;m Chris Henry, CTO of Behance.\n\nGoals of this class -&gt; Learn a bit about MongoDB itself, and learn if MongoDB is the solution you want, learn how to deal with some pitfalls that aren&amp;#x2019;t exactly clear in the docs....or y&amp;#x2019;know, anywhere.\n\nThis is meant to be a conversation, stop me any time something isn&amp;#x2019;t clear.\n
  • #4: Ask.\n\nI&amp;#x2019;ve been using MongoDB for 3 years now, for any number of failed projects no longer in existence.\n
  • #5: Show the Activity Feed\n\nData Porn\n\n\n
  • #6: \n
  • #7: Show the Activity Feed\n\nData Porn\n\n\n
  • #8: Easy to use -&gt; installation on most systems is a single line. Updating is easy. There&amp;#x2019;s a driver for basically every language. JSON - like Storage makes modeling easy.\n\nEasy to Iterate on -&gt; Once modeled, making changes is really easy. Just add the key to the document you need. to remove, iterate through and use the $unset operator\n\nWhy is &amp;#x201C;Fast&amp;#x201D; in quotes? Just like anything piece of software, the way you manage it / deploy it / write code for it really determine how &amp;#x201C;Fast&amp;#x201D; it is.\n\n\n
  • #9: Bleeding edge -&gt; will cut you. Definitely production ready, but beware that you will need to devote serious time and effort, and will potentially have problems scaling.\n\nBattle scars -&gt; software gets better by being in production for a long time. MySQL / Postgres all have the benefit of this. MongoDB is still the new kid in town.\n\nTried and true -&gt; Many paradigms are document design are still developing.\n\nTxn support -&gt; don&amp;#x2019;t put important data that requires transaction support.\n\nGood news\n10Gen is a aware of most of Mongo&amp;#x2019;s flaws, and is doing a stellar job of listening to the community and making changes.\n
  • #10: Autosharding -&gt; if your data gets too big, just add more capacity, without having any thing about the way your app connects to mongo.\n\nReplica Sets -&gt; replicas of data that are smart enough to handle outages, and members disappearing.\n\nHorizontal -&gt; Too much data? Just add more shards / nodes. More Nodes = More Scalability.\n\nCloud* -&gt; Good fit for the on-demand, super fast provisioning of new instances. Why Asterisk? Shitty fit for Disk IO in cloud.\n
  • #11: Data -&gt; BSON format allows for much more flixibility, but as documents change size, they need to be moved, which takes up more space. Same problem as memcached.\n\nIO -&gt; AWS has bad neighbor problem. Never sure who else on your virtualized machine will be thrashing the disk. When they do, writes take longer\n\nGlobal lock -&gt; huge problem for write intensive standalone servers.In 2.2, this is changing to a collection level lock. In Behance&amp;#x2019;s use case, this isn&amp;#x2019;t really helpful, since we have one main collection.\n\nMgmt. -&gt; Debatable. However, in our case, we have keep a much closer eye on data size, index size against available memory.\n
  • #12: \n
  • #13: Running any large Database cluster takes some work. However, Mongo seems to be a bit more on the needy side than MySQL.\n
  • #14: \n
  • #15: Get the Sterling Archer image here.\n\n\n
  • #16: What&amp;#x2019;s nice about keeping slow operations in a collection is that you can query them the same way you would query your collection.\n\n\nShard10-3 has profiling enabled. \n\ndb.system.profile.find( { millis : { $gt : 5 } } ).limit(1).pretty()\n
  • #17: \ndb.activity.find({ &quot;user&quot; : NumberLong(981122), &quot;verb&quot; : { &quot;$in&quot; : [300] }, &quot;type&quot; : 1}).sort({&quot;ts_mo&quot; : -1}).explain()\n
  • #18: cleverness -&gt; unlike MySQL, MongoDB replica nodes keep track of each other&amp;#x2019;s state. If one goes down, an election is held between the rest of the nodes, and a new node is elected primary. Since all drivers will detect nodes in the set, writes are then directed there.\n\nsetups -&gt; 2 Nodes + Arbiter OR 3 nodes\n\nrs.stepDown() -&gt; force the primary to relinquish role as primary, and elect a secondary as primary\n\nslaveOk -&gt; setting this parameter in the driver will send reads to the secondaries.\n\n\n
  • #19: cleverness -&gt; unlike MySQL, MongoDB replica nodes keep track of each other&amp;#x2019;s state. If one goes down, an election is held between the rest of the nodes, and a new node is elected primary. Since all drivers will detect nodes in the set, writes are then directed there.\n\nsetups -&gt; 2 Nodes + Arbiter OR 3 nodes\n\nrs.stepDown() -&gt; force the primary to relinquish role as primary, and elect a secondary as primary\n\nslaveOk -&gt; setting this parameter in the driver will send reads to the secondaries.\n\n\n
  • #20: \n
  • #21: \n
  • #22: \n
  • #23: &amp;#xA0;- Beware: Backgrounding will index in the background on the primary, but in the foreground if on secondary. Use only primary when indexing. Do it at off peak hours.\n
  • #24: \n
  • #25: \n
  • #26: \n
  • #27: \n