Skip to main content
0 reactions

How to Append to, Read and Change Part of a String in Valkey

Featured image for How to Append to, Read and Change Part of a String in Valkey, showing APPEND replying 11, GETRANGE replying Hello and SETRANGE replying 12.
Valkey APPEND, GETRANGE and SETRANGE.

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

SyntaxPlain text
APPEND key value
GETRANGE key start end
SETRANGE key offset value
STRLEN key
  • value: the text to add or write.
  • start and end: positions, counted from 0, and both included. -1 is the last character, -2 the one before it.
  • offset: the position where SETRANGE starts 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

127.0.0.1:6379>Plain text
SET message "Hello"
OutputPlain text
OK
127.0.0.1:6379>Plain text
APPEND message " World"
OutputPlain text
(integer) 11
127.0.0.1:6379>Plain text
GET message
OutputPlain text
"Hello World"

APPEND replies with the new length: 11 characters.

Step 2: Read Part of It with GETRANGE

127.0.0.1:6379>Plain text
GETRANGE message 0 4
OutputPlain text
"Hello"
127.0.0.1:6379>Plain text
GETRANGE message -5 -1
OutputPlain text
"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

127.0.0.1:6379>Plain text
SETRANGE message 6 "Valkey"
OutputPlain text
(integer) 12
127.0.0.1:6379>Plain text
GET message
OutputPlain text
"Hello Valkey"
127.0.0.1:6379>Plain text
STRLEN message
OutputPlain text
(integer) 12

SETRANGE wrote Valkey from position 6, over World, and the string grew to 12 bytes.

valkey-cli session where APPEND turns Hello into Hello World, GETRANGE reads Hello and World, and SETRANGE changes the string to Hello Valkey, 12 bytes long.
Adding to, reading and changing part of a string.

Clean Up

127.0.0.1:6379>Plain text
DEL message
OutputPlain text
(integer) 1

Common Mistakes

MistakeWhat happensDo this instead
Expecting SETRANGE to insert textIt overwrites from the offsetRebuild the string and save it with SET
Counting positions from 1You get one character too manyCount 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.

Additional Resources

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