Scanning Expired Keys

Background

There are two ways to delete a key in Redis.

Expired DCS Key Scan

DCS integrates these strategies and allows you to periodically release the memory used by expired keys. You can configure scheduled scans on the master nodes of your instances. The entire keyspace is traversed during the scans, triggering Redis to check whether the keys have expired and to remove expired keys if any.

  • This function is supported only by DCS Redis 4.0, 5.0, and 6.0 instances.
  • Perform expired key scans during off-peak hours to avoid 100% CPU usage.
  • Released expired keys cannot be queried.

Procedure

  1. Log in to the DCS console.
  2. Click in the upper left corner of the management console and select the region where your instance is located.
  3. In the navigation pane, choose Cache Manager.
  4. Click the name of a DCS Redis instance.
  5. Choose Analysis and Diagnosis > Cache Analysis.
  6. On the Expired Key Scan tab page, scan for expired keys and release them.

    • Click Start Scanning to scan for expired keys immediately.
    • Enable Scheduled to schedule automatic scans at a specified time. For details about how to configure automatic scans, see Scheduling Automatic Scans.

  7. After the expired key scan task is submitted, view it in the task list.

    Figure 1 Expired key scan tasks

The scan fails in the following scenarios:
  • An exception occurred.
  • There are too many keys, resulting in a timeout. Some keys have already been deleted before the timeout.

Scheduling Automatic Scans

To schedule automatic scans, click next to Scheduled. Set the parameters as required, and click OK.

Table 1 describes the parameters for scheduling automatic scans.

Table 1 Parameters for scheduling automatic scans

Parameter

Description

Value Range

Default Value

Remarks

Start At

The first scan can only start after the current time.

Format: YYYY-MM-DD hh:mm:ss

-

-

Interval

Interval between scans.

0 to 43,200 (unit: minute)

1440

  • If the previous scan is not complete when the start time arrives, the upcoming scan will be skipped.
  • If the previous scan is complete within five minutes after the start time, the upcoming scan will not be skipped.
    NOTE:

    Continuous scans may cause high CPU usage. Set this parameter based on the total number of keys in the instance and the increase of keys. For details, see the following performance description and configuration suggestions.

Timeout

This parameter is used to prevent scanning timeout due to unknown reasons. If scanning times out due to unknown reasons, subsequent scheduled tasks cannot be executed. After the specified timeout elapses, a failure message is returned and the next scan will be performed.

1 to 86,400 (unit: minute)

2880

  • Set the timeout to at least twice the interval.
  • You can set a value based on the time taken in previous scans and the maximum timeout that can be tolerated in the application scenario.

Keys to Iterate

The SCAN command is used to iterate the keys in the current database. The COUNT option is used to let the user tell the iteration command how many elements should be returned from the dataset in each iteration. For details, see the description of the SCAN command. Iterative scanning can reduce the risks of slowing down Redis when a large number of keys are scanned at a time.

10 to 1000

10

For example, if there are 10 million keys in Redis and the number of keys to iterate is set to 1000, a full scan will be complete after 10,000 iterations.

Performance

Reference test

A master/standby instance is scanned. There are 10 million keys that will not expire and 5 million keys that will expire. The expiration time is 1 to 10 seconds. A full scan is executed.

The following test results are for reference only. They may vary depending on the site environment and network fluctuation.

  • Natural deletion: 10,000 expired keys are deleted per second. It takes 8 minutes to delete 5 million expired keys. The CPU usage is about 5%.
  • Keys to Iterate set to 10: The scanning takes 125 minutes (15 million/2000/60 seconds) and the CPU usage is about 8%.
  • Keys to Iterate set to 50: The scanning takes 25 minutes (15 million/10,000/60 seconds) and the CPU usage is about 10%.
  • Keys to Iterate set to 100: The scanning takes 12.5 minutes (15 million/20,000/60 seconds) and the CPU usage is about 20%.
  • Keys to Iterate set to 1000: The scanning takes 1.25 minutes (15 million/200,000/60 seconds) and the CPU usage is about 25%.

Configuration suggestions