Backup and Restore
Backup and Restore Interview with follow-up questions
1. What is the importance of backing up data in MongoDB?
Backups exist so that any failure — human, hardware, or malicious — is recoverable rather than catastrophic. Replication gives you high availability, but it is not a backup: a bad deleteMany, a buggy migration, or ransomware replicates instantly to every secondary. Backups are your independent, point-in-time safety net.
Why it matters:
- Data loss prevention: recover from accidental deletes, app bugs, or corruption that replication faithfully copies everywhere.
- Disaster recovery: rebuild after a region outage, hardware failure, or attack — measured by your RPO (how much data you can afford to lose) and RTO (how fast you must be back).
- Point-in-time recovery (PITR): restore to the exact moment before a destructive change, not just the last nightly dump.
- Compliance & auditing: many regimes (GDPR, HIPAA, SOC 2) mandate retention and tested recovery.
What interviewers actually want: that you distinguish backup from replication, that you cite RPO/RTO, and that you stress an untested backup is no backup — you must regularly test restores. In production, the common answer is MongoDB Atlas managed backups with continuous PITR, or volume/filesystem snapshots, rather than relying on mongodump alone for large clusters.
Follow-up 1
What are the different strategies for backing up data in MongoDB?
There are several strategies for backing up data in MongoDB:
Full backups: This strategy involves taking a complete copy of the entire MongoDB database. It provides a comprehensive backup but can be time-consuming and resource-intensive.
Incremental backups: This strategy involves taking backups of only the changes made since the last backup. It is faster and requires less storage space compared to full backups.
Continuous backups: This strategy involves capturing changes to the database in real-time, ensuring near-zero data loss in case of a failure. It requires a specialized backup solution that can capture and replicate changes as they occur.
Cloud backups: This strategy involves using cloud-based backup services to store copies of the data in remote servers. It provides off-site storage and can be useful for disaster recovery scenarios.
The choice of backup strategy depends on factors such as data size, recovery time objectives, available resources, and compliance requirements.
Follow-up 2
What factors should you consider when choosing a backup strategy?
When choosing a backup strategy for MongoDB, consider the following factors:
Data size: The size of your MongoDB database can impact the choice of backup strategy. Full backups may be time-consuming and resource-intensive for large databases, while incremental backups may be more efficient.
Recovery time objectives (RTO): The RTO defines the maximum acceptable downtime in case of a failure. If you have strict RTO requirements, a continuous backup strategy that provides near-real-time recovery may be necessary.
Available resources: The resources available for backups, such as storage space, network bandwidth, and processing power, can influence the choice of strategy. Ensure that your infrastructure can support the chosen backup strategy.
Compliance requirements: If your industry has specific compliance requirements for data retention and backup, ensure that the chosen strategy meets these requirements.
Cost: Consider the cost implications of the backup strategy, including storage costs, backup software or services, and any additional infrastructure required.
Follow-up 3
How often should you backup your data in MongoDB?
The frequency of backups in MongoDB depends on factors such as data volatility, recovery point objectives (RPO), and available resources. Here are some considerations:
Data volatility: If your data changes frequently, you may need more frequent backups to minimize data loss in case of a failure. On the other hand, if your data is relatively stable, less frequent backups may be sufficient.
Recovery point objectives (RPO): The RPO defines the maximum acceptable data loss in case of a failure. If you have strict RPO requirements, you may need more frequent backups to minimize data loss.
Available resources: The resources available for backups, such as storage space, network bandwidth, and processing power, can impact the frequency of backups. Ensure that your infrastructure can support the chosen backup frequency.
It is recommended to have a backup strategy that balances the need for data protection with the available resources and requirements of your organization.
2. How do you perform a backup in MongoDB?
There are three tiers of backup, and a good answer names all three and when each fits.
1. mongodump (logical, BSON) — fine for small/medium databases:
// Run against a secondary to avoid loading the primary; --oplog captures a consistent point
mongodump --uri="mongodb://host:27017" --db myapp --out /backups/$(date +%F) --oplog --gzip
Restore with mongorestore. Logical dumps are portable but slow to restore and weak for very large data or precise point-in-time recovery.
2. Filesystem / volume snapshots — the production choice for large deployments. A storage-level (e.g. LVM, EBS) snapshot of the data files is near-instant and low-overhead; combined with journaling it's crash-consistent.
3. MongoDB Atlas managed backups — for managed clusters, Atlas provides scheduled snapshots plus continuous, point-in-time recovery (PITR) via oplog replay, with retention policies handled for you.
What interviewers want:
- Run backups on a secondary (or use snapshots) to avoid I/O load on the primary.
- Use
--oplogso the dump captures a single consistent moment on a live system. - For sharded clusters, you can't just dump each shard independently — stop the balancer first (or use Atlas/coordinated snapshot tooling) so chunk migrations don't corrupt consistency.
- Above all: test your restores.
Follow-up 1
What are the steps to restore data in MongoDB?
To restore data in MongoDB, you can use the mongorestore tool. Here are the steps to restore data:
- Open a command prompt or terminal.
- Navigate to the MongoDB bin directory.
- Run the
mongorestorecommand with the appropriate options.
For example, to restore a backup of a local MongoDB instance, you can use the following command:
mongorestore --db
This will restore the backup data to the specified database.
Follow-up 2
What tools can you use to backup and restore data in MongoDB?
MongoDB provides two main tools for backup and restore operations:
mongodump: This tool is used to create backups of MongoDB databases. It can be used to backup a single database or all databases on a MongoDB instance.mongorestore: This tool is used to restore data from backups created withmongodump. It can restore a single database or all databases from a backup.
Both tools are included with the MongoDB installation and can be found in the MongoDB bin directory.
Follow-up 3
Can you explain the process of backing up data in a sharded cluster?
Backing up data in a sharded cluster involves backing up each shard individually. Here are the steps to back up data in a sharded cluster:
- Connect to the mongos instance using the
mongoshell. - Use the
sh.status()command to get information about the sharded cluster. - For each shard, use the
mongodumptool to create a backup of the shard's data. - Optionally, you can also create a backup of the config servers.
To restore the data, you would follow similar steps, using the mongorestore tool to restore the backups to the appropriate shards.
3. What is the role of the 'mongodump' and 'mongorestore' utilities in MongoDB?
mongodump and mongorestore are MongoDB's logical backup utilities — they work at the document level, producing BSON rather than copying raw data files.
mongodumpreads a database (or collection) and writes a binary BSON export of the documents plus a JSON metadata file describing indexes and options. With--oplogit also captures oplog entries so the dump reflects a single consistent point in time on a live system.mongorestorereads those BSON files back and inserts the documents into a target deployment, recreating indexes from the metadata. With--oplogReplayit applies the captured oplog to reach a consistent state.
mongodump --db myapp --out /backups/today --oplog --gzip
mongorestore --db myapp --gzip --oplogReplay /backups/today/myapp
What interviewers want you to know about the limits:
- They're logical tools — great for small/medium DBs, migrations, and moving data between environments, but slow and I/O-heavy for very large clusters, where filesystem snapshots or Atlas backups win.
- Restore re-inserts every document and rebuilds indexes, so it can be much slower than the dump.
- Don't confuse them with
mongoexport/mongoimport, which produce JSON/CSV for data interchange and lose type fidelity — those aren't full-fidelity backups. - Run
mongodumpagainst a secondary to spare the primary.
Follow-up 1
What are the limitations of using 'mongodump' and 'mongorestore'?
Some limitations of using 'mongodump' and 'mongorestore' include:
- 'mongodump' and 'mongorestore' can only be used with MongoDB databases running the same version as the utilities.
- 'mongodump' and 'mongorestore' do not preserve certain aspects of the data, such as the original order of documents in a collection or the shard key metadata.
- 'mongodump' and 'mongorestore' do not support backing up or restoring data from sharded clusters directly. Instead, you need to backup and restore each shard individually.
- 'mongodump' and 'mongorestore' do not support backing up or restoring data from encrypted databases directly. You need to decrypt the data before using these utilities.
Follow-up 2
Can you explain the difference between 'mongodump' and 'mongorestore'?
The main difference between 'mongodump' and 'mongorestore' is their purpose. 'mongodump' is used to create a binary export of a MongoDB database, while 'mongorestore' is used to restore data from a binary dump created by 'mongodump'.
Another difference is the command-line options they support. 'mongodump' allows you to specify the database or collection to dump, as well as various options for controlling the dump process. 'mongorestore' allows you to specify the database to restore the data into, as well as options for controlling the restore process.
Additionally, 'mongodump' creates a binary dump of the data, indexes, and metadata, while 'mongorestore' reads the binary dump files and inserts the data into a MongoDB database.
Follow-up 3
How do you use 'mongodump' and 'mongorestore' to backup and restore data?
To use 'mongodump' to backup data, you can run the following command:
mongodump --db --out
This will create a binary dump of the specified database and save it in the specified output directory.
To use 'mongorestore' to restore data from a binary dump, you can run the following command:
mongorestore --db
This will read the binary dump files from the specified input directory and insert the data into the specified database.
Note that you may need to provide additional options, such as authentication credentials, depending on your MongoDB setup.
4. What is a point-in-time recovery in MongoDB?
Point-in-time recovery (PITR) restores a deployment to any specific moment — say, the second before an accidental deleteMany — rather than only to the last full backup. It's the difference between losing a day of data and losing a few seconds.
How it works: PITR combines a base snapshot (a full backup taken at some earlier time) with the oplog (the operation log of every write since). To recover to time T, MongoDB restores the snapshot, then replays oplog entries up to T and stops. Because oplog entries are idempotent, replay is safe and exact.
// Logical PITR: restore a base dump, then replay oplog up to a chosen timestamp
mongorestore --oplogReplay --oplogLimit "1719500000:1" /backups/base
What interviewers want:
- The mechanism: snapshot + oplog replay. Your recovery granularity (RPO) is limited by how continuously you capture the oplog.
- In production this is rarely hand-rolled — MongoDB Atlas continuous backups provide PITR out of the box, letting you pick a timestamp within the retention window.
- The oplog window must be long enough to cover the gap between snapshots; if it wraps (it's a capped collection), you lose the ability to replay across that gap.
- PITR is the standard answer for "how do you recover from a logical error you didn't notice immediately."
Follow-up 1
How do you perform a point-in-time recovery in MongoDB?
To perform a point-in-time recovery in MongoDB, you need to follow these steps:
- Enable the oplog on the primary replica set member.
- Create regular backups of your MongoDB data using tools like mongodump or a MongoDB backup service.
- To restore to a specific point in time, restore the latest backup and then apply the oplog entries from the backup to the desired timestamp using the mongorestore command with the --oplogReplay option.
Follow-up 2
What are the prerequisites for performing a point-in-time recovery?
To perform a point-in-time recovery in MongoDB, you need to meet the following prerequisites:
- You must be using a replica set configuration with at least three members.
- The replica set must have the oplog enabled.
- Regular backups of the MongoDB data must be taken using tools like mongodump or a MongoDB backup service.
Follow-up 3
What are the limitations of point-in-time recovery in MongoDB?
Point-in-time recovery in MongoDB has the following limitations:
- It requires a replica set configuration with at least three members.
- The oplog size is limited, so point-in-time recovery is only possible within the oplog retention period.
- Point-in-time recovery does not protect against logical errors or application-level data corruption.
- It may have performance implications during the recovery process, especially for large datasets.
5. What is the impact of backup operations on the performance of a MongoDB database?
Backups compete with live traffic for resources, so a naive backup of a busy primary can noticeably slow the application.
The costs:
- Disk I/O and CPU: reading the whole dataset and writing it out (or compressing it) saturates disk bandwidth and burns CPU, raising latency for concurrent queries.
- Cache pollution: a full scan can evict your hot working set from the WiredTiger cache, so reads page-fault from disk afterward.
- Replication lag: backing up the primary competes with oplog replication; under load this can widen lag and, in the worst case, threaten availability.
- Lock/consistency overhead: maintaining a consistent view during a logical dump adds bookkeeping.
How to minimize the impact — and what interviewers want to hear:
- Back up a secondary, not the primary, so production reads/writes on the primary are untouched.
- Prefer filesystem/volume snapshots, which are near-instant and low-overhead versus a full logical
mongodump. - Schedule backups during off-peak windows.
- Use MongoDB Atlas managed backups, which take snapshots without loading your cluster.
- Throttle/compress thoughtfully —
--gzipsaves space but costs CPU.
The headline trade-off: logical dumps are heavy and intrusive at scale; snapshots and secondary-targeted or managed backups keep production performance intact.
Follow-up 1
How can you minimize the impact of backup operations on database performance?
There are several strategies to minimize the impact of backup operations on database performance:
Perform backups during off-peak hours: Schedule backup operations during periods of low database activity to minimize the impact on regular operations.
Use a secondary replica set member for backups: Instead of performing backups on the primary replica set member, use a secondary member to offload the backup operations and reduce the impact on the primary.
Use incremental backups: Instead of performing full backups every time, use incremental backups to only backup the changes since the last backup. This can significantly reduce the backup time and impact on performance.
Utilize backup compression: Enable compression during backup operations to reduce the size of the backup files and minimize the disk I/O and network bandwidth usage.
Consider sharding: If your database is sharded, distribute the backup load across multiple shards to minimize the impact on individual shards.
Follow-up 2
What is the difference between hot, warm, and cold backup in terms of performance impact?
In terms of performance impact, the difference between hot, warm, and cold backups is as follows:
Hot Backup: A hot backup is performed while the database is running and serving live traffic. It has the least impact on performance as it does not require any downtime or interruption of database operations. However, it may still cause some additional disk I/O and CPU usage.
Warm Backup: A warm backup is performed when the database is still running but with reduced activity. It may involve temporarily pausing certain operations or reducing the load on the database. While it has a slightly higher impact on performance compared to a hot backup, it allows for more consistent backups as the database is not actively serving live traffic.
Cold Backup: A cold backup is performed when the database is completely offline or in a read-only state. It has the highest impact on performance as it requires the database to be stopped or put into a non-operational mode. However, it provides the most consistent and reliable backups as there is no concurrent activity during the backup process.
Follow-up 3
What strategies can you use to ensure consistent backups without affecting database performance?
To ensure consistent backups without affecting database performance, you can use the following strategies:
Use database snapshots: Take advantage of the snapshot feature provided by your storage system or cloud provider. Snapshots provide a point-in-time copy of the database without impacting performance. This can be used as a basis for consistent backups.
Utilize replication: Set up a replica set with multiple members and perform backups on secondary members. This allows for backups to be taken without impacting the primary member's performance. Ensure that the replication lag is minimal to maintain consistency.
Implement point-in-time recovery: Use the oplog (operation log) to perform point-in-time recovery. This allows you to restore the database to a specific point in time, ensuring consistency while minimizing the impact on performance.
Test backup and restore procedures: Regularly test your backup and restore procedures to ensure they are working correctly. This helps identify any potential issues or performance bottlenecks before they impact production systems.
Live mock interview
Mock interview: Backup and Restore
- Read your scene and goals
- Talk it out; goals tick off live
- Get a score and stronger lines
Your voice and your AI key never touch our servers; the key stays in this browser and is sent only to Google. Only your round scores are saved to track progress.