memory) •OS controls what data in RAM •When a piece of data isn't found, a page fault occurs (Expensive + Locking!) •OS goes to disk to fetch the data •Indexes are part of the Regular Database files •Deployment Trick: Pre-Warm your Database (PreWarming your cache) to prevent cold start slowdown Operating System map files on the Filesystem to Virtual Memory Monday, October 10, 11
“Foo, Bar” and “Foo, Bar, Baz” • The Query Optimizer figures out the order but can’t do things in reverse • You can pass hints to force a specific index: db.collection.find({ username: ‘foo’, city: ‘New York’ }).hint({username: 1}) • Missing Values are indexed as “null” • This includes unique indexes • Deployment Trick: 1.8 has Sparse and Covered Indexes! • Dig into system.indexes • Common Sense Trick: Make sure your Queries are properly indexed! Monday, October 10, 11
storage needs •e.g. 4 20 gig disks gives you 40 gigs of usable space •LVM of RAID 10 on EBS seems to smooth out performance and reliability best for MongoDB RAID 10 (Mirrored sets inside a striped set; minimum 4 disks) Monday, October 10, 11
bit MongoDB Build •32 Bit has a 2 gig limit; imposed by the operating systems for memory mapped files •Clients can be 32 bit •MongoDB Supports (little endian only) •Linux, FreeBSD, OS X (on Intel, not PowerPC) •Windows •Solaris (Intel only, Joyent offers a cloud service which works for Mongo) OS Monday, October 10, 11
can be a bottleneck in large datasets where working set > ram •~200-300Mb/s on XL EC2 instances, but YMMV (EBS is slower) •On Amazon Latency spikes are common, 400-600ms (No, this is not a good thing) Similarly, iostat ships on most Linux machines (or can be installed) Monday, October 10, 11
different disks •Best to aggregate your IO across multiple disks •File Allocation All data & namespace files are stored in the 'data' directory (--dbpath) Monday, October 10, 11
ext4 by default) •For best performance reformat to EXT4 / XFS •Make sure you use a recent version of EXT4 •Striping (MDADM / LVM) aggregates I/O •See previous recommendations about RAID 10 EC2 Monday, October 10, 11
be needed occasionally on indices and datafiles • db.repair() •Replica Sets: •Rolling repairs, start nodes up with --repair param • Deployment Trick: For large bulk data operations consider removing indexes and re-adding them later! (better : a new DB may help) Maintenance Monday, October 10, 11
• Compacts and defragments individual collections • Requires much less disk space overhead to accomplish compaction (small amount simply to create new extent(s)) Monday, October 10, 11
• Compacts and defragments individual collections • Requires much less disk space overhead to accomplish compaction (small amount simply to create new extent(s)) • Markedly faster than “repair” Monday, October 10, 11
• Compacts and defragments individual collections • Requires much less disk space overhead to accomplish compaction (small amount simply to create new extent(s)) • Markedly faster than “repair” • Still blocks – use with care! Monday, October 10, 11
• Compacts and defragments individual collections • Requires much less disk space overhead to accomplish compaction (small amount simply to create new extent(s)) • Markedly faster than “repair” • Still blocks – use with care! • Secondaries running a repair will now automatically demote to “recovery” state Monday, October 10, 11
necessarily consistent from start to finish (Unless you lock the database) •mongorestore to restore binary dump •Deployment Trick #1: database doesn't have to be up to restore, can use dbpath • Deployment Trick #2: mongodump with replSetName/ <hostlist> will automatically read from a slave! mongodump / mongorestore Monday, October 10, 11
term backups •USE AMAZON AVAILABILITY ZONES •DR / HA is important. The cloud is neither a panacea nor a magic disaster recovery recipe With Journaling, you can run an LVM or EBS snapshot and recover later without locking Monday, October 10, 11
Setup servers again (including config server) • Restore via mongorestore (all data including config metadata is in backup and will restore) Monday, October 10, 11
Make sure all servers are stopped (mongod, mongos, etc) • Restore all sharding and config servers individually • restart cluster Monday, October 10, 11
information about the sharding setup is stored in the config servers; it’s important you don’t lose them • You may have 1 or 3 config servers; this is the only valid configuration (Two Phase Commit) • Production deployments should always have 3 • If any config server fails ... • Chunk splitting will stop • Migration / balancing will stop • ... Until all 3 servers are back up • This can lead to unbalanced shard situations • Through mongos the config info is in the “config” db Monday, October 10, 11
sharding • Basic unit of transfer: “chunk” • Default size of 64 MB proves to be a “sweet spot” • More: Migration takes too long, queries la • Less: Overhead of moving doesn’t pay off • The idea is to keep a balance of data & load on each server. Even is good! • Once a threshold of “imbalance” is reached, the balancer kicks in • Usually about ~8 chunks: Don’t want to balance on one doc diff. Monday, October 10, 11
time • Known as balancer “rounds” • Balancing rounds continue until difference between any two shards is only 2 chunks • Common Question – “Why isn’t collection $x being balanced?!” • Commonly, it just doesn’t need to. Not enough chunk diff, and the cost of balancing would outweigh the benefit. • Alternately, the balancer may be running but not progressing Monday, October 10, 11