APPEND(3valkey)
β¨ 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:
STRLENcan be used in order to obtain the number of samples.GETRANGEallows 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 combiningGETRANGEwith the Lua scripting engine. big brain behavior fr fr. π§ β¨SETRANGEcan 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
- fedora 44: valkey-doc 9.0.3-2.fc44 Β· CC-BY-SA-4.0
- fedora 44: valkey-doc 9.0.6-1.fc44 Β· CC-BY-SA-4.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms