Continuous Backup and Point-in-Time Restore
Complete the full lesson to earn 25 points — 50 with Pro
Work through each section, then tap “Mark as Complete” on the last one.
✦ Skip the page breaks, the wait, and see fewer ads — read each lesson on a single page with Pro
Mastering Continuous Backup and Point-in-Time Restore in Azure Cosmos DB
Introduction: Why Data Protection Matters
In the world of distributed databases, data loss is not just a technical issue; it is a business-critical failure. Azure Cosmos DB, as a globally distributed, multi-model database service, is designed for high availability and low latency. However, even with the best architecture, human error, application bugs, or malicious intent can lead to accidental data deletion or corruption. This is where the backup and restore strategy becomes the ultimate safety net for your production environments.
Historically, database backups were static snapshots taken at specific intervals. If you needed to restore data, you had to revert to the exact moment the last backup was taken, losing all work performed between that snapshot and the present. Modern applications, especially those handling high-frequency transactional data, require a more granular approach. Continuous Backup and Point-in-Time Restore (PITR) in Azure Cosmos DB solves this by allowing you to restore your database to any specific microsecond within a retention window.
Understanding how to maintain this feature is essential for any database administrator or cloud engineer. This lesson explores the mechanics of continuous backup, how to configure it, how to trigger a restoration, and the best practices to ensure your data remains protected against unforeseen incidents.
Understanding Continuous Backup vs. Periodic Backup
To appreciate the value of continuous backup, we must first distinguish it from the traditional periodic backup model. Azure Cosmos DB offers two primary backup modes: Periodic and Continuous.
Periodic Backup
This is the default mode for older accounts or specific configurations. Backups are taken at fixed intervals, and the frequency and retention period are defined by the user. If you delete a container or a database, the periodic backup might not include the most recent changes, leading to significant data loss.
Continuous Backup
Continuous backup mode captures data changes in real-time. It effectively stores a stream of updates, allowing you to "rewind" your database state to any point in time within the retention period (currently up to 30 days). This is critical for mission-critical applications where even a few minutes of data loss is unacceptable.
Callout: The Difference in Recovery Objectives Periodic backup is analogous to taking a photograph of a room every hour. If something breaks at 10:45 AM, you only have the 10:00 AM photo to reference. Continuous backup is like having a video recording of the room. You can pause the video at 10:44:59 AM and see exactly what the room looked like just before the incident occurred.
Enabling Continuous Backup
Continuous backup must be enabled at the time of account creation. While you can migrate certain accounts to continuous backup, it is a permanent change in many scenarios. When you enable this feature, you choose between two tiers of retention:
- Continuous Backup 7-day retention: This allows you to restore data from any point in the last 7 days.
- Continuous Backup 30-day retention: This extends the window, providing a longer safety net for detecting data corruption that might go unnoticed for weeks.
Step-by-Step: Enabling via Azure Portal
- Navigate to your Azure Cosmos DB account in the Azure Portal.
- In the left-hand menu, scroll down to the Settings section and select Backup & Restore.
- Look for the Backup Policy tab. You will see the current backup mode.
- If your account is eligible, you will see an option to select Continuous.
- Choose your desired retention period (7 or 30 days).
- Click Save. Note that the transition might take some time to propagate across the distributed system.
Warning: Irreversibility of Mode Changes Transitioning an account from periodic to continuous backup is generally a one-way street. Once you have moved to continuous backup, you cannot revert that specific account back to periodic backup. Ensure you have evaluated the cost implications, as continuous backup typically carries a different pricing structure compared to periodic snapshots.
Executing a Point-in-Time Restore (PITR)
When an emergency occurs—such as a developer accidentally dropping a critical collection or an application bug overwriting valid records—you need to initiate a restore. The restore process does not overwrite your existing database; instead, it creates a new database account containing the restored data.
The Restore Workflow
- Identify the Timestamp: Determine the exact time (in UTC) just before the data corruption or deletion occurred.
- Initiate Restore: Use the Azure Portal or Azure CLI to start the restore process.
- Create New Account: Specify the name of the new account where the restored data will reside.
- Verification: Once the restore completes, verify the data integrity in the new account.
- Migration: Move the verified data back to the primary production account if necessary.
Using Azure CLI for Restoration
The Azure CLI provides a powerful way to script your recovery process. Below is an example command to trigger a restore:
# Define your variables
source_account="my-prod-cosmos-db"
target_account="my-restored-cosmos-db"
restore_timestamp="2023-10-27T10:00:00Z"
resource_group="my-resource-group"
# Trigger the restore
az cosmosdb restore \
--resource-group $resource_group \
--target-database-account-name $target_account \
--restore-source $source_account \
--restore-timestamp $restore_timestamp \
--location "East US"
Explanation of the script:
restore-source: The identifier or resource ID of the source account that experienced the data loss.restore-timestamp: The precise moment in time you wish to restore to. This must be in ISO 8601 format.target-database-account-name: The name of the new account that will be created. It must not exist prior to the command.
Best Practices for Backup and Restore
Maintaining a robust backup strategy goes beyond just enabling the feature. You must operationalize your recovery process to ensure that when a crisis hits, your team knows exactly what to do.
1. Regularly Test Your Restore Process
The most common mistake teams make is assuming their backups work without ever testing them. Conduct "restore drills" at least quarterly. Create a restored account, verify that the data is consistent, and then delete the test account. This ensures your team is familiar with the CLI/Portal workflow and that you understand the time required for a full restoration.
2. Monitor Recovery Time Objectives (RTO)
Understand how long a restore takes. Restoring a small collection takes significantly less time than restoring a multi-terabyte database account. By performing periodic drills, you can estimate your RTO and communicate realistic expectations to stakeholders during an actual outage.
3. Implement Role-Based Access Control (RBAC)
The ability to trigger a restore is a powerful permission. Limit who can execute these commands. Use Azure RBAC to ensure that only authorized personnel can initiate a restore, preventing accidental or unauthorized data overwrites.
4. Optimize Data Partitioning
During a restore, the data is re-ingested into a new account. If your data is not partitioned correctly, the restore process might take longer, or the new account might hit throughput limits. Ensure your partition keys are well-defined to facilitate efficient data distribution.
Tip: Data Consistency During Restore Remember that the restore process creates a new account. You will need to update your application connection strings to point to the new account once you are ready to switch over. Plan for a short maintenance window or a blue/green deployment strategy to swap the traffic from the old account to the new one.
Common Pitfalls and How to Avoid Them
Pitfall 1: Incorrect Time Zone Calculations
Azure Cosmos DB expects timestamps in UTC. A common error is providing a local time instead of UTC, which leads to restoring data from the wrong point in time.
- Solution: Always standardize your monitoring and logging tools to UTC. When calculating the restore timestamp, double-check the conversion to ensure you are targeting the correct window.
Pitfall 2: Forgetting Throughput Settings
When you restore to a new account, the new account starts with default settings. You may need to manually reconfigure Request Units (RUs) to match your production load.
- Solution: Maintain an "Infrastructure as Code" (IaC) template (like Bicep or Terraform) for your production account. When restoring, use this template to quickly configure the new account so it matches the performance characteristics of the original.
Pitfall 3: Assuming "Global" Restore
If your account is globally distributed, the restore process recovers the state across all regions. However, if you have specific regional configurations, ensure you understand how the restore impacts these settings.
- Solution: Review the regional configuration of the restored account immediately after creation to ensure it meets your latency and availability requirements.
Comparison: Periodic vs. Continuous Backup
| Feature | Periodic Backup | Continuous Backup |
|---|---|---|
| Backup Frequency | Hourly/Daily intervals | Real-time streams |
| Recovery Granularity | Snapshot-based (specific time) | Point-in-time (any second) |
| Data Loss Potential | Up to the last snapshot | Minimal (near zero) |
| Resource Overhead | Managed by system | Included in service cost |
| Best For | Non-critical, dev/test | Mission-critical production |
Advanced Considerations: Managing Throughput and Storage
When you perform a restore, the system does not just copy files; it re-processes the change feed to reconstruct the database state. This is a resource-intensive operation.
Throughput during Restore
The time taken to restore is proportional to the amount of data and the number of operations performed. If you have a massive database, the restore might take several hours. During this time, the target account will be in a "Restoring" state. You cannot write to the account while it is being restored.
Cost Implications
Continuous backup is generally priced based on the volume of data stored and the retention period. Because you are essentially keeping a continuous log of changes, the storage cost can be higher than periodic backups. It is important to monitor your Azure bill to understand how your data ingestion rate impacts your backup costs.
Callout: Understanding the Restore Window The retention window (7 or 30 days) is a rolling window. If you discover a corruption on day 31, you will be unable to restore to the required point using the built-in continuous backup feature. If your compliance requirements dictate longer retention, you must implement a custom solution, such as exporting data to Azure Blob Storage using the Change Feed.
Developing a Custom Backup Strategy (Beyond PITR)
While PITR is excellent for short-term recovery, some organizations require long-term archival (e.g., keeping data for 7 years for regulatory purposes). For these cases, PITR is not enough. You should complement your continuous backup strategy with a secondary archival process.
Using the Change Feed for Archiving
The Change Feed in Azure Cosmos DB is a persistent log of all changes. You can build a small Azure Function that triggers on every change and writes the document to an Azure Data Lake or Blob Storage.
// Simplified example of a Change Feed processor snippet
public async Task Run([CosmosDBTrigger(
databaseName: "OrdersDB",
collectionName: "Orders",
ConnectionStringSetting = "CosmosDBConnection")] IReadOnlyList<Document> documents)
{
foreach (var doc in documents)
{
// Archive the document to Blob Storage
await blobClient.UploadAsync(new BinaryData(doc.ToString()));
}
}
This approach allows you to maintain an infinite history of your data, independent of the 30-day PITR limit. It is an industry-standard practice for high-compliance environments.
Step-by-Step: Validating the Restored Data
Once the restoration process finishes, you cannot simply assume the data is correct. You need a validation script to perform a "sanity check."
- Count Verification: Compare the document count in the restored collection against the count from your logs or monitoring tools at the time of the incident.
- Sample Checking: Select a random set of documents and compare their fields against the source (if the source is partially available) or against known values.
- Application Smoke Test: Point a non-production version of your application to the restored account and perform basic read/write operations to ensure the schema and indexing are functioning correctly.
- Index Verification: Ensure that the indexing policy was correctly applied during the restoration. Sometimes, indexes take time to build, and querying before they are ready can lead to false negatives.
Addressing Common Questions
Q: Can I restore a deleted database?
Yes. If you have continuous backup enabled, you can restore a deleted database or container. The restore process will recreate the database/container within the new account.
Q: Does the restore process include stored procedures and triggers?
Yes. The restore process is designed to bring back the entire account configuration, including stored procedures, triggers, and user-defined functions (UDFs).
Q: Will the restored account have the same URI?
No. The restored account will have a unique URI. You must update your application configuration to point to the new endpoint.
Q: Can I restore to a different region?
Yes. You can choose the target location for the restored account, which is useful if you are trying to recover from a regional outage or if you simply prefer the data to live in a different geographic area.
Industry Best Practices: The "Defense in Depth" Approach
Data protection should never rely on a single mechanism. Even with Azure Cosmos DB's excellent continuous backup, you should adopt a defense-in-depth strategy:
- Soft Deletes: Implement application-level "soft deletes" where you mark a record as
isDeleted: truerather than executing aDELETEcommand. This allows you to "undelete" data instantly without needing a full database restore. - Infrastructure as Code (IaC): Always define your database schema, indexing policies, and throughput settings in templates. This makes restoring the structure of your database instantaneous, leaving only the data to be restored via PITR.
- Regular Auditing: Use Azure Monitor to track who is accessing your data and when. If an accidental deletion occurs, having an audit log helps you pinpoint the exact time the event happened, which is crucial for determining your
restore-timestamp. - Data Partitioning Strategy: Ensure your partition keys are granular. If you have a poorly partitioned container, a restore operation might trigger massive data movement that impacts your overall performance.
Key Takeaways for Maintaining Azure Cosmos DB Backups
- Continuous vs. Periodic: Continuous backup is the gold standard for production environments because it provides microsecond-level restoration, whereas periodic backups are limited to static snapshots.
- Irreversibility: Once you move to continuous backup, you generally cannot move back. Carefully plan your move and account for the cost differences.
- The Restore Process: Restoration always creates a new account. It does not overwrite the existing one. This protects you from accidental "double-overwriting" but requires you to manage connection strings and traffic redirection.
- The 30-Day Limit: Continuous backup is not a long-term archiving solution. For data that must be kept for months or years, you must implement a custom archival pipeline using the Change Feed.
- Drills are Mandatory: Never wait for a real disaster to learn how to restore. Regular, scheduled restore drills are the only way to guarantee your Recovery Time Objectives (RTO) and Recovery Point Objectives (RPO).
- UTC Standardization: Always work in UTC to avoid errors in timestamp selection. A one-hour error in your timestamp calculation can be the difference between a successful recovery and a failed one.
- Automation: Use Azure CLI or SDKs to automate your restore procedures. Manual steps in the portal are prone to human error during high-stress incident response situations.
By mastering these concepts, you transition from simply "using" a database to actively managing and protecting your organization's most valuable asset: its data. The tools provided by Azure are powerful, but their effectiveness depends entirely on your preparation, your testing, and your ability to execute under pressure. Focus on building these habits into your standard operational procedures, and you will ensure that your data remains resilient against any threat.
Reach the last section to complete this lesson and earn points — you're on section 1 of 11.
- Introduction to Cosmos DB Data Modeling
- Introduction to Cosmos DB Data Modeling Quiz5q
- Multiple Entity Types in Same Container
- Multiple Entity Types in Same Container Quiz5q
- Storing Related Entities in Same Document
- Storing Related Entities in Same Document Quiz5q
- Denormalizing Data Across Documents
- Denormalizing Data Across Documents Quiz5q
- Referencing Between Documents
- Referencing Between Documents Quiz5q
- Partition Keys and Document IDs
- Partition Keys and Document IDs Quiz5q
- Time to Live (TTL) Configuration
- Time to Live (TTL) Configuration Quiz5q
- Document Versioning Strategies
- Document Versioning Strategies Quiz5q
- Schema Versioning Patterns
- Schema Versioning Patterns Quiz5q
- Choosing Partition Strategies
- Choosing Partition Strategies Quiz5q
- Partition Key Selection Best Practices
- Partition Key Selection Best Practices Quiz5q
- Transactions and Partition Keys
- Transactions and Partition Keys Quiz5q
- Cross-Partition Query Costs
- Cross-Partition Query Costs Quiz5q
- Data Distribution Analysis
- Data Distribution Analysis Quiz5q
- Throughput Distribution Planning
- Throughput Distribution Planning Quiz5q
- Synthetic Partition Keys
- Synthetic Partition Keys Quiz5q
- Hierarchical Partition Keys
- Hierarchical Partition Keys Quiz5q
- Throughput and Storage Requirements
- Throughput and Storage Requirements Quiz5q
- Serverless vs Provisioned Throughput
- Serverless vs Provisioned Throughput Quiz5q
- Database-Level Provisioned Throughput
- Database-Level Provisioned Throughput Quiz5q
- Granular Scale Units
- Granular Scale Units Quiz5q
- Global Distribution Costs
- Global Distribution Costs Quiz5q
- Configuring Throughput in Portal
- Configuring Throughput in Portal Quiz5q
- Gateway vs Direct Connectivity Mode
- Gateway vs Direct Connectivity Mode Quiz5q
- Creating Database Connections
- Creating Database Connections Quiz5q
- Azure Cosmos DB Emulator
- Azure Cosmos DB Emulator Quiz5q
- Connection Error Handling
- Connection Error Handling Quiz5q
- Singleton Pattern for Clients
- Singleton Pattern for Clients Quiz5q
- Global Distribution Regions
- Global Distribution Regions Quiz5q
- Threading and Parallelism
- Threading and Parallelism Quiz5q
- Arrays and Nested Objects Queries
- Arrays and Nested Objects Queries Quiz5q
- Correlated Subqueries
- Correlated Subqueries Quiz5q
- Array and Type-Checking Functions
- Array and Type-Checking Functions Quiz5q
- Mathematical and String Functions
- Mathematical and String Functions Quiz5q
- Date Functions in Queries
- Date Functions in Queries Quiz5q
- Point Operations vs Query Operations
- Point Operations vs Query Operations Quiz5q
- CRUD Point Operations
- CRUD Point Operations Quiz5q
- Patch Operations for Updates
- Patch Operations for Updates Quiz5q
- Transactional Batch Operations
- Transactional Batch Operations Quiz5q
- Bulk Operations with SDK
- Bulk Operations with SDK Quiz5q
- Optimistic Concurrency with ETags
- Optimistic Concurrency with ETags Quiz5q
- Query Pagination and Continuation
- Query Pagination and Continuation Quiz5q
- Cosmos DB Mirroring for Fabric
- Cosmos DB Mirroring for Fabric Quiz5q
- Mirroring vs Spark Connector
- Mirroring vs Spark Connector Quiz5q
- Enabling Analytical Store
- Enabling Analytical Store Quiz5q
- Synapse Spark and SQL Queries
- Synapse Spark and SQL Queries Quiz5q
- Change Data Capture in Analytical Store
- Change Data Capture in Analytical Store Quiz5q
- Azure Functions and Event Hubs Integration
- Azure Functions and Event Hubs Integration Quiz5q
- Denormalization with Change Feed
- Denormalization with Change Feed Quiz5q
- Referential Integrity with Change Feed
- Referential Integrity with Change Feed Quiz5q
- Azure AI Search Integration
- Azure AI Search Integration Quiz5q
- Azure Functions Change Feed Trigger
- Azure Functions Change Feed Trigger Quiz5q
- Consuming Change Feed with SDK
- Consuming Change Feed with SDK Quiz5q
- Change Feed Estimator
- Change Feed Estimator Quiz5q
- Denormalization via Change Feed
- Denormalization via Change Feed Quiz5q
- Aggregation Persistence with Change Feed
- Aggregation Persistence with Change Feed Quiz5q
- Read-Heavy vs Write-Heavy Indexing
- Read-Heavy vs Write-Heavy Indexing Quiz5q
- Index Type Selection
- Index Type Selection Quiz5q
- Custom Indexing Policies
- Custom Indexing Policies Quiz5q
- Composite Index Implementation
- Composite Index Implementation Quiz5q
- Index Performance Optimization
- Index Performance Optimization Quiz5q
- Response Status Codes and Metrics
- Response Status Codes and Metrics Quiz5q
- Normalized RU Consumption Monitoring
- Normalized RU Consumption Monitoring Quiz5q
- Server-Side Latency Metrics
- Server-Side Latency Metrics Quiz5q
- Data Replication Monitoring
- Data Replication Monitoring Quiz5q
- Azure Monitor Alerts Configuration
- Azure Monitor Alerts Configuration Quiz5q
- Resource Logs Implementation
- Resource Logs Implementation Quiz5q
- Partition Throughput Monitoring
- Partition Throughput Monitoring Quiz5q
- Encryption Key Management
- Encryption Key Management Quiz5q
- Network-Level Access Control
- Network-Level Access Control Quiz5q
- Data Encryption Configuration
- Data Encryption Configuration Quiz5q
- Azure RBAC for Control Plane
- Azure RBAC for Control Plane Quiz5q
- Microsoft Entra ID for Data Plane
- Microsoft Entra ID for Data Plane Quiz5q
- CORS Settings Configuration
- CORS Settings Configuration Quiz5q
- Customer-Managed Keys
- Customer-Managed Keys Quiz5q
- Always Encrypted Implementation
- Always Encrypted Implementation Quiz5q
- Data Movement Strategy Selection
- Data Movement Strategy Selection Quiz5q
- SDK Bulk Operations for Data Movement
- SDK Bulk Operations for Data Movement Quiz5q
- Azure Data Factory Pipelines
- Azure Data Factory Pipelines Quiz5q
- Kafka Connector Integration
- Kafka Connector Integration Quiz5q
- Azure Stream Analytics Integration
- Azure Stream Analytics Integration Quiz5q
- Cosmos DB Spark Connector
- Cosmos DB Spark Connector Quiz5q
Enjoying the courses?
Everything stays free. Pro shows fewer ads, doubles the points you earn on every lesson and quiz so you progress twice as fast, unlocks half of every practice exam — plus full case studies — with the Learn & Exam study modes, and lets you read each lesson on one page.
- ✓ Fewer advertisements
- ✓ 2× points per lesson & quiz
- ✓ 50% of every exam unlocked
- ✓ Learn & Exam modes
- ✓ Distraction-free lessons