APPEND(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

✨ APPEND β€” the valkey manual page, but make it cute ✨

okay so bestie, this is append(3valkey) from the Valkey Command Manual, and honestly? it's giving "small command, huge main character energy." πŸ’…


🌸 NAME 🌸

append β€” appends a string to the value of a key. Creates the key if it doesn't exist. periodt. 🫢


πŸŽ€ SYNOPSIS πŸŽ€

APPEND key value

πŸ’– DESCRIPTION πŸ’–

So here's the tea β˜•: if key already exists and it's a string, this command appends value to the end of that string. Simple as that.

If key does not exist, it gets created and set as an empty string β€” which means in that specific case, APPEND is basically doing the same thing as SET. cute little overachiever. 😌


πŸ¦‹ REPLY πŸ¦‹

valkey-protocol(7) Integer reply: the length of the string after the append operation. so you always know how big your string got. no mystery, no guesswork, we love a transparent queen. ✨


πŸ”₯ COMPLEXITY πŸ”₯

O(1). and the amortized time complexity is also O(1), assuming the appended value is small and the already present value is of any size β€” because the dynamic string library the server uses will double the free space available on every reallocation. she's efficient AND she's prepared. πŸ’…


πŸ‘‘ ACL CATEGORIES πŸ‘‘

  • @fast
  • @string
  • @write

🌈 HISTORY 🌈

  • Available since: 2.0.0 β€” an OG, a legend, been here since forever. ⭐

πŸ“ EXAMPLES πŸ“

okay let's actually do the thing, watch this:

127.0.0.1:6379> EXISTS mykey
(integer) 0
127.0.0.1:6379> APPEND mykey "Hello"
(integer) 5
127.0.0.1:6379> APPEND mykey " World"
(integer) 11
127.0.0.1:6379> GET mykey
"Hello World"

see how EXISTS said 0 (not here, bestie), then the first APPEND made the key and handed back 5 (the length of "Hello"), then the second one bumped it to 11? and GET just shows the whole glow-up: "Hello World". ate. no crumbs. 🧚


⏰ Pattern: Time series ⏰

ngl this is the part where APPEND really shows out. you can use it to build a very compact representation of a list of fixed-size samples β€” usually called a time series. every time a new sample arrives, you just store it like this:

APPEND timeseries "fixed-size sample"

and getting individual elements back out? not hard at all:

  • STRLEN can be used in order to obtain the number of samples.
  • GETRANGE allows for random access of elements. and if your time series has associated time information, you can easily implement a binary search to get a range by combining GETRANGE with the Lua scripting engine. big brain behavior fr fr. 🧠✨
  • SETRANGE can be used to overwrite an existing time series.
the limitation of this pattern, bestie, is that we're forced into an append-only mode of operation. there is no way to cut the time series to a given size easily, because Valkey currently lacks a command able to trim string objects. so just know that going in. πŸ™ˆ

but the space efficiency of time series stored this way? remarkable. iconic, honestly. πŸ’œ

Hint: it is possible to switch to a different key based on the current Unix time. that way you can have just a relatively small amount of samples per key, avoid dealing with very big keys, and make this pattern more friendly to be distributed across many Valkey instances. very demure, very scalable. πŸ¦‹

here's an example sampling the temperature of a sensor using fixed-size strings (using a binary format is better in real implementations):

127.0.0.1:6379> APPEND ts "0043"
(integer) 4
127.0.0.1:6379> APPEND ts "0035"
(integer) 8
127.0.0.1:6379> GETRANGE ts 0 3
"0043"
127.0.0.1:6379> GETRANGE ts 4 7
"0035"

look at those little slices coming back out exactly how they went in. we love a consistent legend. πŸŽ€


πŸ’« SEE ALSO πŸ’«

go read these too, they're all in the group chat:

decr(3valkey), decrby(3valkey), delifeq(3valkey), get(3valkey), getdel(3valkey), getex(3valkey), getrange(3valkey), getset(3valkey), incr(3valkey), incrby(3valkey), incrbyfloat(3valkey), lcs(3valkey), mget(3valkey), mset(3valkey), msetnx(3valkey), psetex(3valkey), set(3valkey), setex(3valkey), setnx(3valkey), setrange(3valkey), strlen(3valkey), substr(3valkey)


and that's the whole page, bestie!! this one's from version 9.0.0, dated 2025-10-21 β€” mwah, sending you love, go append something iconic. πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from