GETRANGE and SETRANGE in Redis - Time & Space Complexity
When working with strings in Redis, it's important to know how fast operations like GETRANGE and SETRANGE run.
We want to understand how the time to get or set parts of a string changes as the string grows.
Analyze the time complexity of the following Redis commands.
# Get part of a string from start to end index
GETRANGE mykey 0 99
# Set part of a string starting at offset
SETRANGE mykey 50 "hello"
GETRANGE reads a substring from a stored string. SETRANGE overwrites part of the string starting at a given position.
Both commands work by accessing a part of the string stored in Redis.
- Primary operation: Reading or writing a segment of the string.
- How many times: The operation depends on the length of the substring accessed or modified.
The time to get or set a substring grows with the length of that substring, not the whole string.
| Substring Length (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The work grows linearly with the substring length you access or modify.
Time Complexity: O(n)
This means the time to get or set part of a string grows directly with the size of that part.
[X] Wrong: "GETRANGE and SETRANGE always run in constant time regardless of substring size."
[OK] Correct: Actually, these commands must process each character in the substring, so bigger substrings take more time.
Understanding how substring operations scale helps you reason about performance in real Redis use cases.
"What if we used SETRANGE to overwrite the entire string instead of a part? How would the time complexity change?"
Practice
GETRANGE key 0 4 do?Solution
Step 1: Understand GETRANGE parameters
The commandGETRANGE key start endreturns the substring from the start index to the end index inclusive.Step 2: Apply to given indices
With start=0 and end=4, it returns characters at positions 0,1,2,3,4 from the string stored atkey.Final Answer:
Returns substring from index 0 to 4 inclusive -> Option AQuick Check:
GETRANGE extracts substring = A [OK]
- Thinking GETRANGE deletes or modifies the string
- Confusing GETRANGE with SETRANGE
- Assuming end index is exclusive
Solution
Step 1: Recall SETRANGE syntax
The correct syntax isSETRANGE key offset value, where offset is the start position to replace and value is the new substring.Step 2: Check options
Only SETRANGE key offset value matches this syntax exactly.Final Answer:
SETRANGE key offset value -> Option BQuick Check:
Correct SETRANGE syntax = A [OK]
- Swapping offset and value positions
- Using GETRANGE instead of SETRANGE
- Using SET instead of SETRANGE
mykey is "HelloWorld", what will be the result of GETRANGE mykey 5 7?Solution
Step 1: Identify substring indices
The string is "HelloWorld" with indices: H(0), e(1), l(2), l(3), o(4), W(5), o(6), r(7), l(8), d(9).Step 2: Extract substring from index 5 to 7
Characters at 5,6,7 are 'W', 'o', 'r', so the substring is "Wor".Final Answer:
"Wor" -> Option DQuick Check:
GETRANGE returns substring = B [OK]
- Counting indexes starting at 1
- Including characters outside the range
- Confusing GETRANGE with SETRANGE
SETRANGE mykey 3 "abc" on a Redis string "abcdef". The result is unexpected. What is the likely mistake?Solution
Step 1: Understand SETRANGE usage
SETRANGE expects the substring value without quotes; quotes are interpreted literally.Step 2: Identify the mistake
Using quotes like "abc" inserts the quotes as characters, causing unexpected results.Final Answer:
Using quotes around substring causes wrong replacement -> Option AQuick Check:
Quotes in SETRANGE value cause errors = C [OK]
- Including quotes around substring value
- Assuming offset must be within string length
- Thinking SETRANGE creates new keys automatically
text holds the string "abcdefghij", what will be the final string after executing SETRANGE text 5 "XYZ" followed by GETRANGE text 3 7?Solution
Step 1: Apply SETRANGE at offset 5
Original string: "abcdefghij". SETRANGE replaces characters at positions 5,6,7 with "XYZ". Positions: a(0), b(1), c(2), d(3), e(4), f(5), g(6), h(7), i(8), j(9). After replacement: "abcdeXYZij".Step 2: Apply GETRANGE from 3 to 7
Characters at positions 3 to 7 are: d(3), e(4), X(5), Y(6), Z(7). So substring is "deXYZ".Final Answer:
"deXYZ" -> Option CQuick Check:
SETRANGE modifies substring, GETRANGE extracts it = D [OK]
- Not updating string before GETRANGE
- Miscounting offset positions
- Assuming GETRANGE returns original string
Ready to prove it?
Pass the test and this pattern moves unknown
