SET and GET commands in Redis - Time & Space Complexity
When using Redis commands like SET and GET, it's helpful to know how the time to run these commands changes as data grows.
We want to understand how fast these commands work when storing or retrieving data.
Analyze the time complexity of the following code snippet.
SET user:1000 "Alice"
GET user:1000
SET user:1001 "Bob"
GET user:1001
SET user:1002 "Carol"
GET user:1002
This code stores and retrieves simple string values by key in Redis.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Each SET or GET command accesses a key in the database.
- How many times: Each command runs once per key, no loops or recursion involved.
Each command works directly with one key, so the time to run one command stays about the same no matter how many keys exist.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1 operation per command |
| 100 | About 1 operation per command |
| 1000 | About 1 operation per command |
Pattern observation: The time to run each command stays steady even as the number of keys grows.
Time Complexity: O(1)
This means each SET or GET command takes about the same time regardless of how many keys are stored.
[X] Wrong: "The GET command takes longer if there are more keys stored in Redis."
[OK] Correct: Redis uses a fast way to find keys, so GET runs in constant time no matter how many keys exist.
Knowing that SET and GET run quickly helps you explain how Redis can handle many requests fast, a useful skill in real projects and interviews.
"What if we changed the keys to very long strings? How would the time complexity change?"
Practice
SET command do?Solution
Step 1: Understand the purpose of SET
TheSETcommand in Redis is used to save or store a value associated with a key.Step 2: Compare with other commands
GETretrieves values,DELdeletes keys, andKEYSlists keys, so they are not correct here.Final Answer:
Stores a value with a specified key -> Option BQuick Check:
SET saves data = A [OK]
- Confusing SET with GET
- Thinking SET deletes data
- Mixing SET with key listing
Solution
Step 1: Recall Redis SET syntax
The correct syntax isSET key valuewithout commas or quotes unless the shell requires them.Step 2: Analyze options
SET greeting hello matches the correct syntax. GET greeting hello uses GET which is wrong. SET "greeting" "hello" uses quotes unnecessarily. SET greeting, hello uses a comma which is invalid.Final Answer:
SET greeting hello -> Option AQuick Check:
SET key value no commas [OK]
- Using GET instead of SET
- Adding commas between arguments
- Unnecessary quotes around key/value
SET color blue GET color SET color red GET color
What will be the output of the two
GET commands?Solution
Step 1: Analyze the first SET and GET
The firstSET color bluestores "blue". The firstGET colorreturns "blue".Step 2: Analyze the second SET and GET
The secondSET color redoverwrites the value with "red". The secondGET colorreturns "red".Final Answer:
"blue" and then "red" -> Option AQuick Check:
SET overwrites value, GET returns current value [OK]
- Assuming GET returns old value
- Thinking SET appends values
- Expecting nil on first GET
GET username but get a nil response. Which of the following could fix this issue?Solution
Step 1: Understand nil response from GET
Nil means the key "username" does not exist or has no value stored.Step 2: Fix by setting a value
To get a value, you must first store it usingSET username "guest". Other options do not create the key.Final Answer:
Run SET username "guest" before GET -> Option DQuick Check:
Nil means no key; SET creates it [OK]
- Retrying GET without SET
- Deleting key before GET
- Restarting server unnecessarily
Solution
Step 1: Store the session token
UseSET session:123 abc123tokento save the token under the key.Step 2: Retrieve the stored token
UseGET session:123to fetch the token value.Step 3: Analyze other options
GET session:123 SET session:123 abc123token tries to GET before SET, so returns nil. Options A and C delete the key before GET, so GET returns nil.Final Answer:
SET session:123 abc123token GET session:123 -> Option CQuick Check:
SET then GET returns stored value [OK]
- GET before SET returns nil
- Deleting key before GET
- Using quotes unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
