APPEND adds text to the end of a string in Valkey®, GETRANGE reads part of it and SETRANGE overwrites part of it. This guide builds a message.
Type the commands at the valkey-cli prompt. To open it, run valkey-cli in a terminal.
Note: Every command and output on this page is real, run on Valkey 9.2 on an Apple silicon Mac with macOS 26.
Syntax
APPEND key value
GETRANGE key start end
SETRANGE key offset value
STRLEN key
value: the text to add or write.startandend: positions, counted from 0, and both included.-1is the last character,-2the one before it.offset: the position whereSETRANGEstarts writing.STRLEN: the length in bytes.
From the APPEND (opens in a new tab), GETRANGE (opens in a new tab), SETRANGE (opens in a new tab) and STRLEN (opens in a new tab) documentation.
Step 1: Add to the End with APPEND
SET message "Hello"
OK
APPEND message " World"
(integer) 11
GET message
"Hello World"
APPEND replies with the new length: 11 characters.
Step 2: Read Part of It with GETRANGE
GETRANGE message 0 4
"Hello"
GETRANGE message -5 -1
"World"
Positions 0 to 4 are the first five characters, and −5 to −1 the last five.
Step 3: Overwrite Part of It with SETRANGE
SETRANGE message 6 "Valkey"
(integer) 12
GET message
"Hello Valkey"
STRLEN message
(integer) 12
SETRANGE wrote Valkey from position 6, over World, and the string grew to 12 bytes.

Clean Up
DEL message
(integer) 1
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Expecting SETRANGE to insert text | It overwrites from the offset | Rebuild the string and save it with SET |
| Counting positions from 1 | You get one character too many | Count from 0 |
FAQ
Are these commands slow on big strings?
GETRANGE and SETRANGE can be, as Valkey’s strings guide (opens in a new tab) warns. On a large string they take longer the more they read or write.
What does APPEND do on a key that doesn’t exist?
It creates the key, like SET.
Conclusion
APPEND adds to the end of a string, GETRANGE reads a slice of it, SETRANGE overwrites a slice and STRLEN measures it.
Related
- Valkey Tutorial — Part 4: Strings and Counters
- Valkey Tutorial — Part 2: Your First Valkey Commands with valkey-cli
Additional Resources
- APPEND (opens in a new tab)
- GETRANGE (opens in a new tab)
- SETRANGE (opens in a new tab)
- STRLEN (opens in a new tab)
- Valkey strings (opens in a new tab)
Valkey® is a registered trademark of LF Projects, LLC. Redis® is a registered trademark of Redis Ltd. Any rights therein are reserved to Redis Ltd. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with the Valkey project, LF Projects or Redis Ltd., and isn’t endorsed, supported or certified by them.
0 comments