0
0
Redisquery~10 mins

GETRANGE and SETRANGE in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - GETRANGE and SETRANGE
Start with a string value
Use SETRANGE to modify part of the string
Use GETRANGE to extract substring
Return modified string or substring
You start with a string stored in Redis, then use SETRANGE to change part of it, and GETRANGE to get a substring from it.
Execution Sample
Redis
SET mykey "Hello World"
SETRANGE mykey 6 "Redis"
GETRANGE mykey 0 10
Set a string, replace part starting at position 6, then get substring from 0 to 10.
Execution Table
StepCommandInput StringActionResult StringOutput
1SET mykey "Hello World"N/ASet key to 'Hello World'Hello WorldOK
2SETRANGE mykey 6 "Redis"Hello WorldReplace from index 6 with 'Redis'Hello Redis11
3GETRANGE mykey 0 10Hello RedisGet substring from 0 to 10Hello RedisHello Redis
4GETRANGE mykey 0 4Hello RedisGet substring from 0 to 4HelloHello
5SETRANGE mykey 11 "!"Hello RedisExtend string by adding '!' at index 11Hello Redis!12
6GETRANGE mykey 0 11Hello Redis!Get substring from 0 to 11Hello Redis!Hello Redis!
7GETRANGE mykey 12 15Hello Redis!Get substring beyond string length
💡 Commands complete; GETRANGE returns empty string when range is outside string length.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 5
mykeyN/AHello WorldHello RedisHello Redis!
Key Moments - 3 Insights
Why does SETRANGE change the string length when replacing beyond current length?
SETRANGE extends the string if the offset is beyond current length by padding with zero bytes, as shown in step 5 where adding '!' at index 11 extends 'Hello Redis' to 'Hello Redis!'.
What happens if GETRANGE requests a substring beyond the string's length?
GETRANGE returns an empty string if the requested range is outside the string length, as in step 7 where indices 12 to 15 return ''.
Does SETRANGE overwrite or insert the new substring?
SETRANGE overwrites existing characters starting at the offset without shifting the rest, as in step 2 where 'World' is replaced by 'Redis' starting at index 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the string value of 'mykey' after step 2?
A"Hello Redis!"
B"Hello World"
C"Hello Redis"
D"Redis World"
💡 Hint
Check the 'Result String' column in row for step 2.
At which step does the string 'mykey' get extended beyond its original length?
AStep 5
BStep 2
CStep 3
DStep 7
💡 Hint
Look for when the 'Result String' length increases beyond 'Hello Redis'.
If GETRANGE is called with start=12 and end=15 on 'Hello Redis!', what is the output?
A"Redis"
B"" (empty string)
C"!"
D"Hello"
💡 Hint
See step 7 in the execution table for GETRANGE beyond string length.
Concept Snapshot
GETRANGE key start end
- Returns substring from start to end (inclusive).
- Indices start at 0.

SETRANGE key offset value
- Overwrites string starting at offset with value.
- Extends string if offset beyond current length.
- Returns new string length.
Full Transcript
This visual execution trace shows how Redis commands GETRANGE and SETRANGE work on string values. First, a string 'Hello World' is set. Then SETRANGE replaces part of the string starting at index 6 with 'Redis', changing it to 'Hello Redis'. GETRANGE extracts substrings by specifying start and end indices, returning parts like 'Hello Redis' or 'Hello'. SETRANGE can also extend the string if the offset is beyond the current length, as when adding '!' at index 11. GETRANGE returns an empty string if the requested range is outside the string length. These steps help understand how to modify and read parts of strings stored in Redis.