MongoDB in Production: Atlas, Monitoring, and Backups
Deploy MongoDB to production with Atlas, set up monitoring dashboards, configure alerts, schedule backups, and use the Performance Advisor. Lesson 10 of MongoDB & NoSQL Mastery.

Running MongoDB in production is an operational discipline, not just a configuration exercise. This final lesson covers MongoDB Atlas — the managed cloud service that handles replication, patching, and backups automatically — alongside the monitoring, alerting, and backup strategies you need regardless of whether you run managed or self-hosted deployments.
Previous: Lesson 9 — Replication & High Availability
MongoDB Atlas Overview
Atlas is MongoDB's fully managed cloud service, available on AWS, GCP, and Azure. It removes the operational burden of managing mongod processes, replica sets, OS patches, and storage provisioning.
Cluster Tiers
| Tier | vCPU | RAM | Storage | Use Case |
|---|---|---|---|---|
| M0 (Free) | Shared | 512MB | 512MB | Development, testing |
| M2 / M5 | Shared | 2–5GB | 2–5GB | Low-traffic apps |
| M10 | 2 | 2GB | 10GB | Production minimum |
| M30 | 8 | 32GB | 80GB | Mid-scale production |
| M60+ | 16+ | 128GB+ | Configurable | High-scale production |
Creating a Cluster
- Sign in at and create a project
- Click Build a Database → choose cloud provider and region
- Select cluster tier (M0 for this course)
- Set cluster name and click Create
Connecting Your Application
Atlas generates connection strings for every driver. For mongosh:
mongosh "mongodb+srv://username:password@cluster0.abc123.mongodb.net/myapp"For Node.js (Mongoose):
mongoose.connect("mongodb+srv://username:password@cluster0.abc123.mongodb.net/myapp?retryWrites=true&w=majority")
retryWrites=true: automatically retries single-statement write operations on transient network errors. Always include this in production connection strings.
Connection String Best Practices
mongodb+srv://<username>:<password>@<host>/<database>?retryWrites=true&w=majority&readPreference=primaryPreferred&connectTimeoutMS=10000&serverSelectionTimeoutMS=10000| Parameter | Purpose |
|---|---|
retryWrites=true | Retry transient write failures automatically |
w=majority | Acknowledge writes on majority of members |
readPreference=primaryPreferred | Fall back to secondary if primary unavailable |
connectTimeoutMS | Timeout for initial TCP connection |
serverSelectionTimeoutMS | Timeout for finding a suitable server |
Never hardcode connection strings — inject via environment variables:
export MONGODB_URI="mongodb+srv://app:${DB_PASSWORD}@cluster0.abc123.mongodb.net/myapp?retryWrites=true&w=majority"Monitoring with Atlas
Real-Time Performance Panel
Atlas provides a real-time performance panel at Cluster → Real-Time Performance Panel:
- Opcounters: reads/s, writes/s, commands/s
- Connections: active connections vs available limit
- Replication lag: how far secondaries are behind
- Disk I/O: read/write throughput and utilisation
- Network: bytes in/out per second
Metrics and Charts
Under Cluster → Metrics, Atlas exposes 100+ time-series metrics with configurable time ranges. The most important for day-to-day operations:
| Metric | Alert Threshold |
|---|---|
| Query targeting ratio | > 1000 (keys examined per document returned) |
| Replication lag | > 60 seconds |
| Connection count | > 80% of configured limit |
| Disk IOPS utilisation | > 90% |
| System CPU | > 80% sustained |
Setting Up Alerts
Atlas → Cluster → Alerts → Add AlertCritical alerts to configure:
- Replication lag > 60s → pages on-call
- Primary election → always notify
- Disk space > 80% → warning; > 90% → critical
- CPU > 80% for 10 minutes → investigate indexing
- Query targeting > 1000 → index missing; add immediately
The Performance Advisor
Atlas Performance Advisor analyses slow query logs and recommends indexes automatically. Access it at Cluster → Performance Advisor.
For each suggested index it shows:
- The query shape it would optimise
- Estimated improvement in query execution time
- The
createIndex()command to run
// Suggested index from Performance Advisor
db.orders.createIndex({ customerId: 1, status: 1, createdAt: -1 },
{ name: "orders_customer_status_date" })Backups
Atlas Continuous Backup
Atlas M10+ clusters include continuous backup with point-in-time restore:
- Snapshots taken every 6 hours by default (configurable)
- Oplog retained for up to 24 hours enabling restore to any second within the retention window
- Retained snapshots: daily (7 days), weekly (4 weeks), monthly (12 months) — configurable
Restore from Atlas UI:
Cluster → Backup → Restore → Select snapshot or point-in-time → Choose target clustermongodump / mongorestore (Self-Hosted)
For self-managed deployments:
# Dump an entire database
mongodump \
--uri "mongodb://admin:password@localhost:27017" \
--db myapp \
--out /var/backups/mongodb/$(date +%Y%m%d)
# Dump a single collection
mongodump \
--uri "mongodb://admin:password@localhost:27017" \
--db myapp \
--collection orders \
--out /var/backups/mongodb/orders_$(date +%Y%m%d)
# Restore a database
mongorestore \
--uri "mongodb://admin:password@localhost:27017" \
--db myapp \
/var/backups/mongodb/20260520/myapp/
# Restore with drop (replace existing collection)
mongorestore --drop \
--uri "mongodb://admin:password@localhost:27017" \
--db myapp \
/var/backups/mongodb/20260520/myapp/Backup as a Scheduled Job
#!/usr/bin/env bash
# /usr/local/bin/mongodb_backup.sh
set -euo pipefail
readonly BACKUP_ROOT="/var/backups/mongodb"
readonly DATE="$(date +%Y%m%d_%H%M%S)"
readonly DEST="${BACKUP_ROOT}/${DATE}"
readonly S3_BUCKET="${MONGODB_BACKUP_BUCKET:?'Must be set'}"
mongodump --uri "$MONGODB_URI" --out "$DEST"
tar -czf "${DEST}.tar.gz" -C "$BACKUP_ROOT" "$DATE"
aws s3 cp "${DEST}.tar.gz" "s3://${S3_BUCKET}/mongodb/"
rm -rf "$DEST" "${DEST}.tar.gz"
# Retain 30 days of backups
find "$BACKUP_ROOT" -name "*.tar.gz" -mtime +30 -delete# Cron: daily at 1 AM
0 1 * * * root /usr/local/bin/mongodb_backup.sh >> /var/log/mongodb_backup.log 2>&1Production Checklist
Before going live, verify:
- Replica set of at least 3 members (or Atlas M10+)
- Authentication enabled with scoped users per service
- TLS enabled for all connections
- Firewall restricts port 27017 to application servers only
- Write concern set to
w: majorityfor critical writes - Indexes created for all production query patterns (
explain()verified) - Atlas alerts configured for replication lag, elections, disk, and CPU
- Backup schedule tested — restore verified at least once
- Connection string includes
retryWrites=trueandserverSelectionTimeoutMS - Oplog sized to cover planned maintenance windows (24h minimum)
