Key-based flashback

Updated at:

ApsaraDB for MongoDB instances support key-based flashback, which lets you back up a specific collection and quickly restore data that is corrupted or accidentally lost due to system vulnerabilities or failures. The _id field is used as the restore key by default.

Note

Key-based flashback has been in public preview since February 26, 2024. It is free of charge during public preview. SLAs are not provided during this period.

Prerequisites

  • The instance is a replica set or a sharded cluster.

  • The instance uses cloud disks for storage.

  • The instance runs MongoDB 5.0 or a later version.

  • The key-based flashback feature is in public preview. To use this feature, submit a ticket to contact technical support and request activation.

Considerations

Enabling key-based flashback can reduce your instance's transactions per second (TPS) by up to 3%.

Note

The performance impact described is for reference only. Actual performance may vary.

Features

Category

Item

Description

Task configuration

Time required for a flashback configuration to take effect

10 minutes.

Flashback data retention period

1 to 7 days.

Recoverable time range

The recoverable time range starts when the flashback backup task is enabled and extends to the current time.

Within the retention period, you can flash back data to a specified point in time using a restore key.

Flashback operations

Supported flashback operations

  • You can restore data from UPDATE and DELETE operations.

  • You cannot roll back data from INSERT operations.

  • You cannot restore data changes caused by DDL or other operations.

  • You cannot flash back data that has not changed.

Supported index types

  • The default primary key _id is supported.

  • User-defined general indexes are supported.

Supported data types for index fields

Only the following data types are supported:

  • String

  • Double

  • ObjectId

  • Boolean

  • Date

  • 32-bit integer

  • 64-bit integer

  • Decimal128

Index field naming

Field names can contain only letters, numbers, and underscores (_), and must start with a letter or an underscore (_).

Index length and count

The maximum length for a single index field is 128 characters, and you can specify up to 10 index fields.

Maximum index value size for queries

16 KB. If the data exceeds 16 KB, the query may not return any results.

Maximum size of a single document in query results

Unlimited.

Flashback and restoration

Maximum number of documents per flashback

1,000 documents. If this limit is exceeded, the data is truncated.

Previewing flashback data

The text in the Restore Index Key Preview field cannot exceed 48 KB.

Database and collection naming

The combined length of the database and collection names cannot exceed 254 characters.

For example, for a collection named table in the test database (represented as test.table), the combined length is 10 characters.

Billing

This feature is free of charge during the public preview period.

Enable flashback backup

  1. Go to the ApsaraDB for MongoDB replica set instances or ApsaraDB for MongoDB sharded cluster instances page. At the top of the page, select a resource group and a region, and then click the ID of the target instance.

  2. In the left-side navigation pane, click Backup and Restoration.

  3. Click the Flashback by key tab.

  4. Click Flashback Backup Settings.

    Note

    After you perform this operation, you may be prompted with an Insufficient Permissions error. To continue, you must enter the username and password of an account that has read and write permissions on the database to be flashed back. For more information about database accounts and permissions, see Create a database account and grant permissions.

  5. In the Flashback Backup Settings panel, configure the following parameters and click Confirm.

    Parameter

    Description

    Backup retention period

    The number of days to retain backup files. Valid values: 1 to 7.

    For example, if you set the retention period to 1 day, the system automatically deletes a backup file 24 hours after it is created.

    Select collection to back up

    Select the collection that you want to back up.

    Preview and set indexes in the collection to back up

    These fields serve as restore keys for the backup and are not related to the indexes returned by getIndexes(). The default is _id. Separate multiple index fields with a comma (,).

    You can specify up to 10 general index fields.

  6. In the Flashback Backup Setting Modification dialog box, click Confirm.

Important

After you enable it, flashback backup takes 10 minutes to become active. You can only restore data written after this activation period.

Initiate a flashback task

  1. On the Flashback by key tab of the Backup and Recovery page, find the target backup task and click Point-in-time Flashback in the Actions column.

  2. In the Point-in-time Flashback panel, configure the following parameters and click Confirm.

    Parameter

    Description

    Select collection to restore

    You do not need to change this parameter.

    Create temporary collection

    The name of the temporary collection that stores the flashback data.

    Select point in time for restoration

    Select the point in time to which you want to flash back.

    Note

    The feature takes 10 minutes to activate after being enabled. You can only select a time after this activation.

    Enter restore key

    • Separate multiple sets of primary keys with a semicolon (;) to represent an OR relationship.

      For example, if the index is _id and the values to restore are key1 and key2, enter key1;key2. The Index Key Restoration Preview field displays [ { "_id": "key1" }, { "_id": "key2" } ].

    • Separate keys within a set with a comma (,) to represent an AND relationship. You can query a specific column by using the column=value syntax.

      For example, if you enter key1;test1=v1,key2, the Index Key Restoration Preview field displays [ { "_id": "key1" }, { "test1": "v1", "_id": "key2" } ].

    • Use a backslash (\) as an escape character if a primary key contains a comma (,) or a semicolon (;).

      For example, if the primary key is a,b, enter a\,b in the text box.

    Note
    • The restore keys must be a subset of the index fields configured for the backup.

    • If the value you enter is too long, the system automatically splits it into multiple flashback tasks. This does not affect the flashback backup process.

  3. In the Data Flashback Preview dialog box, select the data that you want to flash back and click Confirm.

    • For primary keys separated by a semicolon (;), such as test1=v1; test2=v2, the preview displays documents that satisfy test1=v1 and documents that satisfy test2=v2.

    • For a comma-separated primary key, if an _id index exists, such as key,test1=v1,test2=v2, the system displays the record that satisfies _id=key and ignores the other indexes test1,test2. If no _id index exists, such as test1=v1,test2=v2, the system displays records that satisfy test1=v1 and test2=v2.

You can restore this data to the original collection at any time before the temporary collection expires. The expiration period matches the backup retention period you configured.

Restore to the original collection

  1. On the Backup and Recovery page, click the Flashback by key tab.

  2. Click Restore to Original Table to restore the data from the most recent flashback task. Alternatively, click Flashback Records to view all flashback tasks and select the data that you want to restore to the original collection.

  3. In the Restore to Original Table dialog box, click Confirm.

    Note
    • This operation restores data from the temporary collection, overwriting the data in the original collection.

    • To prevent data inconsistencies, do not write to the affected keys during this process, which takes about 1 to 2 minutes.

  4. You can view all flashback restoration tasks on the Restoration Task Records page.

Disable flashback backup

Disable a specific backup task

On the Flashback by key tab of the Backup and Recovery page, find the target backup task and click Disable in the Actions column.

Disable all backup tasks

On the Flashback by key tab of the Backup and Recovery page, click Disable Flashback Backup in the upper-left corner.

Important

When you disable flashback backup, all existing flashback data becomes unavailable for restoration.

Example

Step 1: Enable flashback backup

  1. View the data in the test1.test001 collection.

    test1 || primary || =>db.test001.find()
    [
      {
        _id: ObjectId('689d56e5fb3153809d48949e'),
        name: 'xiaomei',
        age: 27,
        address: 'Shanghai'
      },
      {
        _id: ObjectId('689d56fdfb3153809d48949f'),
        name: 'xiaoli',
        age: 26,
        address: 'Zhangjiakou'
      },
      {
        _id: ObjectId('689d5715fb3153809d4894a1'),
        name: 'xiaomi',
        age: 23,
        address: 'Tianjin'
      },
      {
        _id: ObjectId('689d5e41fb3153809d4894a3'),
        name: 'xiaoli',
        age: 27,
        address: 'Zhangjiakou'
      },
      {
        _id: ObjectId('689d6011fb3153809d4894a4'),
        name: 'xiaoli',
        age: 28,
        address: 'Kunming'
      }
    ]
  2. Set the backup retention period to 1 day, select the test001 collection to back up, and specify _id,name as the index fields for backup.

    On the Backup and Restoration page of the instance, select the Key-based Flashback tab, and click Flashback Backup Settings. Configure the parameters in the panel that appears. In the preview list, the database name is test1.

  3. After you enable the feature, wait 10 minutes for it to take effect before proceeding.

Step 2: Initiate a flashback task

  1. At 2025-08-14T19:27(UTC+8), modify the test1.test001 collection.

    Update one document matching {name=xiaoli} to set {age=29}, and delete one document matching {name=xiaomi}.

    test1 || primary || =>db.test001.updateOne({name:"xiaoli"},{$set:{age:29}})
    {
        acknowledged: true,
        insertedId: null,
        matchedCount: 1,
        modifiedCount: 1,
        upsertedCount: 0
    }
    test1 || primary || =>db.test001.deleteOne({name:"xiaomi"})
    { acknowledged: true, deletedCount: 1 }
  2. View the data in the test1.test001 collection.

    The preceding modifications have taken effect.

    test1 || primary || =>db.test001.find()
    [
      {
        _id: ObjectId('689d56e5fb3153809d48949e'),
        name: 'xiaomei',
        age: 27,
        address: 'Shanghai'
      },
      {
        _id: ObjectId('689d56fdfb3153809d48949f'),
        name: 'xiaoli',
        age: 29,
        address: 'Zhangjiakou'
      },
      {
        _id: ObjectId('689d5e41fb3153809d4894a3'),
        name: 'xiaoli',
        age: 27,
        address: 'Zhangjiakou'
      },
      {
        _id: ObjectId('689d6011fb3153809d4894a4'),
        name: 'xiaoli',
        age: 28,
        address: 'Kunming'
      }
    ]
  3. Initiate a flashback task in the console.

    1. Select test001 as the collection to restore, specify temp_flashback_test003 as the name of the temporary collection, select a point in time before the data modification, such as 2025-08-14T19:16(UTC+8), and enter 689d5715fb3153809d4894a1;name=xiaoli as the restore keys. The Restore Index Key Preview field shows [{"_id":"689d5715fb3153809d4894a1"},{"name":"xiaoli"}].

    2. Click OK to preview the flashback data.

      You can see the update and delete records from 2025-08-14T19:27(UTC+8). These correspond to update and insert flashback operations.

      In the preview, the collection name for both flashback records is test1.test001. The flashback data is a JSON document that includes fields such as address, name, and age.

    3. Select the data that you want to flash back and click OK to initiate the flashback task.

      The flashback task is displayed. You can view multiple flashback tasks in the flashback history.

      The flashback task details include the Flashback task ID, Original collection, Temporary collection name (for example, temp_flashback_test003), Flashback task progress (100% indicates completion), and Status (Successful). After the task is complete, you can click Restore to the original collection to restore the data from the temporary collection to the original collection, or click Clean Up to delete the temporary collection. You can also view historical operations on the Flashback Records and Restoration Task Records pages.

  4. View the data in the temporary collection.

    The temporary collection data that was flashed back for [{"_id":"689d5715fb3153809d4894a1"},{"name":"xiaoli"}] is as follows.

    test1 || primary || =>db.temp_flashback_test003.find()
    [
      {
        _id: ObjectId('689d56fdfb3153809d48949f'),
        name: 'xiaoli',
        age: 26,
        address: 'Zhangjiakou'
      },
      {
        _id: ObjectId('689d5715fb3153809d4894a1'),
        name: 'xiaomi',
        age: 23,
        address: 'Tianjin'
      }
    ]

Step 3: Restore to the original collection

  1. Click Restore to Original Table to restore the data from the most recent flashback task.

    Alternatively, you can click Flashback Records and select a flashback task to restore.

    On the Flashback Records page, each flashback task shows the corresponding temporary collection name, such as temp_flashback_test003. In the Actions column of the target task, click Restore to the original collection to restore the data, or click Clean Up to delete the temporary collection.

  2. Select the most recent flashback task to initiate the restoration.

  3. Click Restoration Task Records to view the progress of the restoration task.

    When Restoration task progress is 100% and Status is Successful, the data has been successfully restored to the original collection.

  4. View the data in the test1.test001 collection.

    The data has been restored to the state before it was modified at 2025-08-14T19:27(UTC+8). After the restoration, an example query result from the original table is: [ { _id: ObjectId('689d5715fb3153809d4894a1'), name: 'xiaomi', age: 23, address: 'Tianjin' } ]