Consulting Manager, 10gen Richard Kreuter Right Ways and Wrong Ways to Implement MongoDB
Jan 15, 2015
Consulting Manager, 10gen
Richard Kreuter
Right Ways and Wrong Ways to Implement MongoDB
Right Ways, Wrong Ways – Richard Kreuter
This talk
• MongoDB is flexible.
• 3 areas where folks make mistakes
• Things you should remember
Flexibility
Right Ways, Wrong Ways – Richard Kreuter
MongoDB is very flexible
• Designed for cloud, commodity– Portable across OS, hardware, network
• HA/DR/Scaling options built-in– Replication: “How many copies of data?”– Sharding: “How are data & load distributed?”
• Many dimensions of choices– How many servers? Where? What hardware?
Right Ways, Wrong Ways – Richard Kreuter
These choices give you power
• Can provision for different fault tolerances• Can configure system to scale horizontally• Can use features for special requirements
(e.g., geographical distribution)
Right Ways, Wrong Ways – Richard Kreuter
With great power comes great responsibility1
• Some choices nonsensical 99% of the time
• (Contrived exceptions should be ignored.)
1: Stan Lee, author of Spider-Man
Right Ways, Wrong Ways – Richard Kreuter
Meta Rule of Thumb
To make good decisions about MongoDB implementations, you must understand MongoDB and your applications and the
workload your applications generate and your business requirements.
Deployment Topologies
Right Ways, Wrong Ways – Richard Kreuter
Replication Topology Mistakes
• All replica set members on one host
• Replica set members sharing same storage
• Replica set evenly split across 2 DCs– (Actually, two is a bad number for replica sets.)
Right Ways, Wrong Ways – Richard Kreuter
Replica Set Rule of Thumb:Decouple all modes of failure!
Right Ways, Wrong Ways – Richard Kreuter
Replication Topology Fixes
• All replica set members on one host– Put replicas on different hosts
• Replica set members sharing same storage– Give each replica separate storage
• Replica set evenly split across 2 DCs– Use more DCs– Understand RS elections
Right Ways, Wrong Ways – Richard Kreuter
Some Sharding Topology Mistakes
• Shards that aren’t Replica Sets
• Solitary config server
Right Ways, Wrong Ways – Richard Kreuter
Sharding Rule of Thumb #1:Avoid single points of failure!
Right Ways, Wrong Ways – Richard Kreuter
Some Sharding Topology Fixes
• Shards that aren’t Replica Sets– Each shard should be a replica set
• Solitary config server– Always use 3 config servers
Right Ways, Wrong Ways – Richard Kreuter
More Sharding Topology Mistakes
• Multiple shards on one box
• One/too few mongos processes
Right Ways, Wrong Ways – Richard Kreuter
Sharding Rule of Thumb #2:Add unshared resources for scaling!
Right Ways, Wrong Ways – Richard Kreuter
More Sharding Topology Fixes
• Multiple shards on one box– Shards should never share hardware.
• One/too few mongos processes– Run more mongos processes – (1+ per app server, say)
Performance Considerations
Right Ways, Wrong Ways – Richard Kreuter
Indexing Mistakes
• Failing to build necessary indexes
• Building unnecessary indexes
• Running ad-hoc queries in production
Right Ways, Wrong Ways – Richard Kreuter
Indexing Rule of Thumb:Build all and only necessary indexes!
Right Ways, Wrong Ways – Richard Kreuter
Indexing Fixes
• Failing to build necessary indexes– Learn to use the slow query log & db profiler
• Building unnecessary indexes– Talk to your app developers about usage
• Running ad-hoc queries in production- Have a staging environment, use secondaries
Right Ways, Wrong Ways – Richard Kreuter
Shard Key Selection Mistakes
• Shard key that’s never used
• Low cardinality shard key
• Picking a shard key for initial import, not regular usage
Right Ways, Wrong Ways – Richard Kreuter
Shard Key Rule of Thumb:Choose shard keys carefully!
Right Ways, Wrong Ways – Richard Kreuter
Shard Key Selection Fixes
• Shard key that’s never used– Understand your query/update patterns
• Low cardinality shard key– Find a higher cardinality option (or just concatenate the
_id field)
• Picking a shard key for initial import, not regular usage– Find other ways to make initial import fast
Hardware Choices
Right Ways, Wrong Ways – Richard Kreuter
Hardware Selection Mistakes
• Skimping on RAM
• Skimping on disk speeds
Right Ways, Wrong Ways – Richard Kreuter
Hardware Rule of Thumb #1:Choose your hardware with your use of MongoDB’s architecture in mind!
Right Ways, Wrong Ways – Richard Kreuter
Hardware Selection Fixes
• Skimping on RAM– Understand/estimate your Working Set
• Skimping on disk speeds– Understand/estimate your write load
Right Ways, Wrong Ways – Richard Kreuter
Hardware Selection Oddities
• (Re-)Provisioning hardware intended for other systems
• Not controlling virtual machine locations
Right Ways, Wrong Ways – Richard Kreuter
Hardware Rule of Thumb #2:Pick hardware/virtualization that serves your use of MongoDB!
Right Ways, Wrong Ways – Richard Kreuter
Hardware Selection Fixes
• (Re-)Provisioning hardware intended for other systems• Understand how MongoDB uses hardware
• Not controlling virtual machine locations• Be sure you can control where VMs run
Things to remember!
Right Ways, Wrong Ways – Richard Kreuter
In Conclusion…• Learn how MongoDB works.
• Do know what your technical & business requirements are.
• Work backwards from requirements to choices.
• Don’t make MongoDB choices based on legacy habits/practices
• Expect that things will change.
• Ask 10gen to Help!
Consulting Manager, 10gen
Richard Kreuter
Thank you!