Bird
Raised Fist0
Redisquery~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the Redis command GETRANGE key 0 4 do?
easy
A. Returns the substring of the value stored at key from index 0 to 4 inclusive.
B. Deletes the first 5 characters of the string stored at key.
C. Replaces the first 5 characters of the string stored at key with zeros.
D. Returns the length of the string stored at key.

Solution

  1. Step 1: Understand GETRANGE parameters

    The command GETRANGE key start end returns the substring from the start index to the end index inclusive.
  2. 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 at key.
  3. Final Answer:

    Returns substring from index 0 to 4 inclusive -> Option A
  4. Quick Check:

    GETRANGE extracts substring = A [OK]
Hint: GETRANGE extracts substring by start and end indexes [OK]
Common Mistakes:
  • Thinking GETRANGE deletes or modifies the string
  • Confusing GETRANGE with SETRANGE
  • Assuming end index is exclusive
2. Which of the following is the correct syntax to replace part of a string value in Redis using SETRANGE?
easy
A. SETRANGE key value offset
B. SETRANGE key offset value
C. SET key offset value
D. GETRANGE key offset value

Solution

  1. Step 1: Recall SETRANGE syntax

    The correct syntax is SETRANGE key offset value, where offset is the start position to replace and value is the new substring.
  2. Step 2: Check options

    Only SETRANGE key offset value matches this syntax exactly.
  3. Final Answer:

    SETRANGE key offset value -> Option B
  4. Quick Check:

    Correct SETRANGE syntax = A [OK]
Hint: Remember SETRANGE order: key, offset, then value [OK]
Common Mistakes:
  • Swapping offset and value positions
  • Using GETRANGE instead of SETRANGE
  • Using SET instead of SETRANGE
3. Given the Redis string value at key mykey is "HelloWorld", what will be the result of GETRANGE mykey 5 7?
medium
A. "World"
B. "Hello"
C. "loW"
D. "Wor"

Solution

  1. 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).
  2. Step 2: Extract substring from index 5 to 7

    Characters at 5,6,7 are 'W', 'o', 'r', so the substring is "Wor".
  3. Final Answer:

    "Wor" -> Option D
  4. Quick Check:

    GETRANGE returns substring = B [OK]
Hint: Count string indexes from 0 to get substring [OK]
Common Mistakes:
  • Counting indexes starting at 1
  • Including characters outside the range
  • Confusing GETRANGE with SETRANGE
4. You run SETRANGE mykey 3 "abc" on a Redis string "abcdef". The result is unexpected. What is the likely mistake?
medium
A. Using quotes around the substring value in the command.
B. Offset 3 is out of range for the string length.
C. SETRANGE cannot modify existing strings.
D. The key mykey does not exist.

Solution

  1. Step 1: Understand SETRANGE usage

    SETRANGE expects the substring value without quotes; quotes are interpreted literally.
  2. Step 2: Identify the mistake

    Using quotes like "abc" inserts the quotes as characters, causing unexpected results.
  3. Final Answer:

    Using quotes around substring causes wrong replacement -> Option A
  4. Quick Check:

    Quotes in SETRANGE value cause errors = C [OK]
Hint: Do not include quotes around substring in SETRANGE [OK]
Common Mistakes:
  • Including quotes around substring value
  • Assuming offset must be within string length
  • Thinking SETRANGE creates new keys automatically
5. If the Redis key text holds the string "abcdefghij", what will be the final string after executing SETRANGE text 5 "XYZ" followed by GETRANGE text 3 7?
hard
A. "defgh"
B. "fghij"
C. "deXYZ"
D. "defXY"

Solution

  1. 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".
  2. 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".
  3. Final Answer:

    "deXYZ" -> Option C
  4. Quick Check:

    SETRANGE modifies substring, GETRANGE extracts it = D [OK]
Hint: Apply SETRANGE first, then GETRANGE on updated string [OK]
Common Mistakes:
  • Not updating string before GETRANGE
  • Miscounting offset positions
  • Assuming GETRANGE returns original string
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard