Continuous Backup and Point-in-Time Restore

Earn 25 points (50 with Pro) in two steps

  1. ① Read through the lesson — each section gets a ✓ as you scroll through it.
  2. ② When every section has a ✓, tap Complete lesson.

0 of 11 read · keep scrolling

✦ See fewer ads and earn double points — 50 a lesson instead of 25 — 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.


Not read yet

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.


Not read yet

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

  1. Navigate to your Azure Cosmos DB account in the Azure Portal.
  2. In the left-hand menu, scroll down to the Settings section and select Backup & Restore.
  3. Look for the Backup Policy tab. You will see the current backup mode.
  4. If your account is eligible, you will see an option to select Continuous.
  5. Choose your desired retention period (7 or 30 days).
  6. 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.


Not read yet

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

  1. Identify the Timestamp: Determine the exact time (in UTC) just before the data corruption or deletion occurred.
  2. Initiate Restore: Use the Azure Portal or Azure CLI to start the restore process.
  3. Create New Account: Specify the name of the new account where the restored data will reside.
  4. Verification: Once the restore completes, verify the data integrity in the new account.
  5. 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.

Not read yet

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.


Not read yet

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.

Not read yet

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.


Not read yet

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.


Not read yet

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."

  1. 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.
  2. 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.
  3. 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.
  4. 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.

Not read yet

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:

  1. Soft Deletes: Implement application-level "soft deletes" where you mark a record as isDeleted: true rather than executing a DELETE command. This allows you to "undelete" data instantly without needing a full database restore.
  2. 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.
  3. 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.
  4. 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.

Not read yet

Key Takeaways for Maintaining Azure Cosmos DB Backups

  1. 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.
  2. Irreversibility: Once you move to continuous backup, you generally cannot move back. Carefully plan your move and account for the cost differences.
  3. 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.
  4. 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.
  5. 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).
  6. 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.
  7. 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.

Not read yet

Each section gets a ✓ as you scroll through it. Tap the button to jump to the next one.