0
0
Redisquery~5 mins

PERSIST to remove expiry in Redis - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: PERSIST to remove expiry
O(1)
Understanding Time Complexity

We want to understand how long it takes to remove the expiration from a key in Redis using the PERSIST command.

How does the time needed change as the database grows?

Scenario Under Consideration

Analyze the time complexity of the following Redis command.


PERSIST mykey
    

This command removes the expiration time from the key named "mykey", making it persistent.

Identify Repeating Operations

Look for any repeated steps or loops inside the command.

  • Primary operation: Checking if the key has an expiration and removing it.
  • How many times: This happens once per command call, no loops or repeated scans.
How Execution Grows With Input

The time to remove expiration does not depend on the number of keys or database size.

Input Size (n)Approx. Operations
101
1001
10001

Pattern observation: The operation takes about the same time no matter how many keys exist.

Final Time Complexity

Time Complexity: O(1)

This means removing the expiration from a key takes a constant amount of time regardless of database size.

Common Mistake

[X] Wrong: "Removing expiration takes longer if the database has many keys."

[OK] Correct: The PERSIST command works directly on the specified key without scanning others, so its time does not grow with database size.

Interview Connect

Understanding that some Redis commands run in constant time helps you reason about performance and choose the right commands for your needs.

Self-Check

"What if we used EXPIRE to set a new expiration instead of PERSIST? How would the time complexity compare?"