MongoDB on Ubuntu: Performance Tuning Strategies
Optimizing MongoDB performance on Ubuntu involves a combination of hardware provisioning, system configuration, MongoDB parameter tuning, indexing strategies, and ongoing monitoring. Below are actionable steps categorized by their impact area:
vm.swappiness to 0 (via sudo sysctl -w vm.swappiness=0) to prevent excessive swapping, which hurts performance./etc/mongod.conf to set storage.wiredTiger.engineConfig.cacheSizeGB to 50%-75% of available system memory (e.g., cacheSizeGB: 4 for an 8GB server). This ensures sufficient memory for caching data and indexes.operationProfiling in mongod.conf to monitor slow queries. For example:operationProfiling:
mode: slowOp
slowOpThresholdMs: 100 # Log queries taking longer than 100ms
This helps identify bottlenecks.net.maxIncomingConnections (e.g., to 1000) to handle high concurrency, but balance against system resources to avoid overload.storage.journal.enabled is true (default) to guarantee data durability. For write-heavy workloads, consider increasing journal.commitIntervalMs (e.g., to 100) to batch commits and improve write throughput.db.collection.createIndex({ field: 1 }) to index fields frequently used in queries (e.g., username, timestamp). Compound indexes (e.g., db.collection.createIndex({ field1: 1, field2: -1 })) can optimize multi-field queries.db.collection.getIndexes() to list indexes and remove those not used by queries (identified via explain()). Too many indexes increase write overhead.db.collection.createIndex({ name: 1, age: 1 }) for find({ name: "John" }, { name: 1, age: 1 })). This eliminates the need to access the underlying collection, reducing I/O.explain("executionStats") (e.g., db.users.find({ age: { $gt: 18 } }).explain("executionStats")) to check if queries use indexes. Look for “winningPlan” and “executionTimeMillis” to identify inefficiencies.find({ status: "active" }) should have an index on status.find({ age: { $gt: 18 } }, { name: 1, age: 1, _id: 0 })) and paginate results (e.g., find().skip(20).limit(10)) to reduce data transfer.insertMany() instead of multiple insertOne() calls to minimize network round-trips.replication.replSetName in mongod.conf) to enable read scaling (distribute reads to secondary nodes) and high availability._id or user_id) to horizontally scale writes and reads. Choose a shard key with high cardinality to evenly distribute data.mongostat (monitors operations per second) and mongotop (tracks read/write times by collection) to identify performance hotspots.db.collection.reIndex()) to reduce fragmentation.compact command) to reclaim disk space from deleted documents./etc/security/limits.conf:mongodb soft nofile 64000
mongodb hard nofile 64000
echo never > /sys/kernel/mm/transparent_hugepage/enabled (add to startup scripts for persistence).mongod.conf, set net.bindIp to the server’s IP (e.g., 192.168.1.100) to limit network exposure.By systematically applying these strategies—prioritizing hardware upgrades, optimizing MongoDB configuration, and maintaining indexes—you can significantly enhance MongoDB’s performance on Ubuntu. Always test changes in a staging environment before deploying to production.