Redis CLI (redis-cli) basics - Time & Space Complexity
When using Redis CLI commands, it's helpful to know how the time to run a command changes as the data grows.
We want to see how the cost of commands grows when we have more data in Redis.
Analyze the time complexity of the following Redis CLI commands.
SET key1 value1
GET key1
DEL key1
EXISTS key1
INCR counter
These commands set, get, delete, check existence, and increment keys in Redis.
Look for any repeated steps or loops inside these commands.
- Primary operation: Each command accesses a single key directly.
- How many times: Each command runs once per call, no loops over multiple keys.
As the number of keys in Redis grows, these commands still work on one key at a time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 operation |
| 100 | 1 operation |
| 1000 | 1 operation |
Pattern observation: The time to run these commands stays about the same no matter how many keys exist.
Time Complexity: O(1)
This means each command takes about the same time regardless of how much data Redis holds.
[X] Wrong: "Commands like GET or SET take longer if Redis has more keys."
[OK] Correct: These commands directly access the key, so their time does not depend on total keys stored.
Knowing that basic Redis commands run in constant time helps you explain how Redis stays fast even with lots of data.
"What if we used a command that scans all keys, like KEYS *? How would the time complexity change?"
Practice
SET key value do?Solution
Step 1: Understand the SET command
The SET command in Redis stores a value under a specified key.Step 2: Match command to action
Since SET assigns a value to a key, it stores data rather than deleting or retrieving it.Final Answer:
Stores the value under the given key in Redis -> Option CQuick Check:
SET command = store value [OK]
- Confusing SET with GET
- Thinking SET deletes keys
- Assuming SET lists keys
user1 in Redis CLI?Solution
Step 1: Identify the command to retrieve a value
The GET command is used to fetch the value stored at a key in Redis.Step 2: Check syntax correctness
GET followed by the key name is the correct syntax to get the value of 'user1'.Final Answer:
GET user1 -> Option BQuick Check:
GET command syntax = GET key [OK]
- Using SET instead of GET
- Using DEL to get value
- Using KEYS to get value
SET count 10 INCR count GET count
Solution
Step 1: Analyze the commands step-by-step
SET count 10 stores the string "10" under key 'count'. INCR count increments the integer value by 1.Step 2: Understand INCR effect and GET result
INCR converts the string "10" to integer 10, adds 1 to make 11. GET count returns "11" as a string.Final Answer:
"11" -> Option AQuick Check:
INCR on "10" = "11" [OK]
- Expecting original value "10"
- Thinking INCR causes error
- Expecting nil for existing key
GET mykey but get an error: WRONGTYPE Operation against a key holding the wrong kind of value. What is the likely cause?Solution
Step 1: Understand the error message
The error means GET was used on a key that stores a data type other than string, like a list or set.Step 2: Identify the cause from options
Since the key exists but is not a string, GET cannot retrieve it, causing the error.Final Answer:
The key holds a non-string value like a list or set -> Option AQuick Check:
WRONGTYPE error = wrong data type for GET [OK]
- Thinking key does not exist
- Assuming server is down
- Misspelling GET causes this error
Solution
Step 1: Identify the best data structure for multiple fields
Using a hash (HSET) allows storing multiple fields like name and age under one key.Step 2: Check commands for storing and retrieving fields
HSET user name Alice age 30 stores both fields. HGET user name and HGET user age retrieve them separately.Final Answer:
HSET user name Alice age 30 HGET user name HGET user age -> Option DQuick Check:
Use hashes for multiple related fields [OK]
- Using SET with multiple values
- Using list commands for key-value pairs
- Trying to GET multiple values at once
Ready to prove it?
Pass the test and this pattern moves unknown
