EXISTS to check key existence in Redis - Time & Space Complexity
We want to understand how long it takes to check if a key exists in Redis using the EXISTS command.
How does the time needed change when we check more keys?
Analyze the time complexity of the following code snippet.
# Check if one or more keys exist in Redis
EXISTS key1 key2 key3
# Returns the number of keys that exist
This command checks if each given key exists and returns how many are found.
- Primary operation: Checking each key one by one to see if it exists.
- How many times: Once for each key given in the command.
As you add more keys to check, the time grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 key existence checks |
| 100 | 100 key existence checks |
| 1000 | 1000 key existence checks |
Pattern observation: The time grows linearly as you check more keys.
Time Complexity: O(n)
This means the time to check keys grows directly with the number of keys you ask about.
[X] Wrong: "Checking multiple keys with EXISTS is instant no matter how many keys."
[OK] Correct: Each key must be checked separately, so more keys take more time.
Knowing how EXISTS scales helps you write efficient Redis queries and understand performance when checking many keys.
"What if we checked keys one by one with separate EXISTS commands instead of all at once? How would the time complexity change?"
Practice
EXISTS key do?Solution
Step 1: Understand the purpose of EXISTS
The EXISTS command is used to check if a key is present in the Redis database.Step 2: Know the return values
It returns 1 if the key exists, and 0 if it does not.Final Answer:
Checks if the specified key exists and returns 1 if yes, 0 if no -> Option AQuick Check:
EXISTS returns 1 or 0 for key presence [OK]
- Thinking EXISTS deletes keys
- Assuming EXISTS returns the key's value
- Confusing EXISTS with SET command
user:1 exists in Redis?Solution
Step 1: Identify the correct Redis command
The correct command to check key existence isEXISTS.Step 2: Verify the syntax
The syntax isEXISTS keyname, soEXISTS user:1is correct.Final Answer:
EXISTS user:1 -> Option BQuick Check:
EXISTS keyname is correct syntax [OK]
- Using non-existent commands like EXIST or HASKEY
- Misspelling EXISTS
- Adding extra keywords after EXISTS
user:1 and user:2 exist, but user:3 does not. What is the result of EXISTS user:1 user:3 user:2?Solution
Step 1: Understand EXISTS with multiple keys
When given multiple keys, EXISTS returns the count of keys that exist.Step 2: Count existing keys in the list
Keysuser:1anduser:2exist, butuser:3does not, so count is 2.Final Answer:
2 -> Option AQuick Check:
EXISTS counts existing keys = 2 [OK]
- Assuming EXISTS returns 1 if any key exists
- Thinking it returns all keys found
- Confusing count with boolean result
EXISTS without any key arguments in Redis. What happens?Solution
Step 1: Check command requirements
The EXISTS command requires at least one key argument to check.Step 2: Understand Redis behavior on missing arguments
If no keys are provided, Redis returns a syntax error indicating missing arguments.Final Answer:
Returns an error due to missing key argument -> Option CQuick Check:
EXISTS needs keys, else error [OK]
- Assuming it returns 0 with no keys
- Thinking it returns 1 by default
- Believing it deletes keys without arguments
session:1, session:2, or session:3 exist, and if none exist, set session:1 with a value. Which Redis command sequence correctly uses EXISTS to do this safely?Solution
Step 1: Use EXISTS to check multiple keys
EXISTS returns the count of existing keys among the given list.Step 2: Use conditional logic to set key if none exist
If EXISTS returns 0, it means none of the keys exist, so we can safely setsession:1.Step 3: Choose the correct syntax for conditional check
EXISTS session:1 session:2 session:3\nIF 0 THEN SET session:1 'active' END shows the correct sequence: run EXISTS, then if result is 0, set the key.Final Answer:
EXISTS session:1 session:2 session:3\nIF 0 THEN SET session:1 'active' END -> Option DQuick Check:
EXISTS count 0 means no keys, then set key [OK]
- Using incorrect conditional syntax
- Not checking all keys at once
- Setting key without checking existence
Ready to prove it?
Pass the test and this pattern moves unknown
