VALKEY-STRINGS(7)
๐ valkey-strings โ Strings, bestie ๐
okay so this is the Valkey Manual page all about the string type, and honestly? it's giving foundational. it's giving main character. let's get into it ๐ฆ
โจ NAME โจ
valkey-strings โ Strings, periodt. the blueprint, the origin story, the value type that runs the whole group chat ๐
๐ธ DESCRIPTION ๐ธ
So strings store sequences of bytes โ that includes text, serialized objects, and binary arrays. literally all of it. which makes strings the simplest type of value you can associate with a Valkey key. they're the "hey bestie" of data types.
People use them for caching a LOT, but that's not all they do โ they also support extra functionality that lets you implement counters and perform bitwise operations. versatile queen ๐
And here's a lil mind-blowing thing: since Valkey keys are strings, when we use the string type as a value too, we're basically mapping a string to another string. it's strings all the way down ๐ The string data type is useful for a number of use cases, like caching HTML fragments or pages.
127.0.0.1:6379> SET bike:1 Deimos
OK
127.0.0.1:6379> GET bike:1
"Deimos"
See how that works? using the SET and the GET commands are THE way we set and retrieve a string value. and bestie, listen โ note that SET will replace any existing value already stored into the key, in the case that the key already exists, even if the key is associated with a non-string value. so SET performs an assignment. no cap. it just does it. no questions asked ๐ญ
Values can be strings (including binary data) of every kind โ for instance you can store a jpeg image inside a value. iconic. but a value can't be bigger than 512 MB. that's the limit bestie, don't push it.
The SET command has interesting options, provided as additional arguments. for example, I may ask SET to fail if the key already exists, or the opposite โ that it only succeed if the key already exists:
127.0.0.1:6379> set bike:1 bike nx
(nil)
127.0.0.1:6379> set bike:1 bike xx
OK
There are a number of other commands for operating on strings. for example the GETSET command sets a key to a new value, returning the old value as the result. you can use this command, for example, if you have a system that increments a Valkey key using INCR every time your web site receives a new visitor. you may want to collect this information once every hour, without losing a single increment. so you GETSET the key, assigning it the new value of "0" and reading the old value back. chef's kiss, no data lost, we love to see it ๐
The ability to set or retrieve the value of multiple keys in a single command is also useful for reduced latency. for this reason there are the MSET and MGET commands:
127.0.0.1:6379> mset bike:1 "Deimos" bike:2 "Ares" bike:3 "Vanth"
OK
127.0.0.1:6379> mget bike:1 bike:2 bike:3
1) "Deimos"
2) "Ares"
3) "Vanth"
When MGET is used, Valkey returns an array of values. so cute, so organized ๐งบ
๐ข Strings as counters ๐ข
Even if strings are the basic values of Valkey, there are interesting operations you can perform with them. for instance, one is atomic increment:
127.0.0.1:6379> set total_crashes 0
OK
127.0.0.1:6379> incr total_crashes
(integer) 1
127.0.0.1:6379> incrby total_crashes 10
(integer) 11
The INCR command parses the string value as an integer, increments it by one, and finally sets the obtained value as the new value. there are other similar commands like INCRBY, DECR and DECRBY. internally it's always the same command, acting in a slightly different way. same energy, different font โจ
so what does it mean that INCR is atomic? it means that even multiple clients issuing INCR against the same key will never enter into a race condition. for instance, it will never happen that client 1 reads "10", client 2 reads "10" at the same time, both increment to 11, and set the new value to 11. the final value will always be 12, and the read-increment-set operation is performed while all the other clients are not executing a command at the same time. she does not share the mic. she takes the whole stage. periodt ๐
๐ Limits ๐
By default, a single String can be a maximum of 512 MB. that's it. that's the ceiling. respect it ๐
๐ ๏ธ Basic commands ๐ ๏ธ
๐ Getting and setting Strings ๐
SETstores a string value.SETNXstores a string value only if the key doesn't already exist. useful for implementing locks. ๐GETretrieves a string value.MGETretrieves multiple string values in a single operation.
๐งฎ Managing counters ๐งฎ
INCRBYatomically increments (and decrements when passing a negative number) counters stored at a given key.- Another command exists for floating point counters:
INCRBYFLOAT. yes, decimals can come to the party too ๐
๐ง Bitwise operations ๐ง
To perform bitwise operations on a string, see the valkey-bitmaps(7) bitmaps data type docs. go off, bits ๐
See the valkey-commands(7) complete list of string commands.
โก Performance โก
Most string operations are O(1), which means they're highly efficient. slay.
However โ and I need you to hear me on this โ be careful with the SUBSTR, GETRANGE, and SETRANGE commands, which can be O(n). these random-access string commands may cause performance issues when dealing with large strings.
โ ๏ธ fr fr bestie, watch out for those three when your strings get thicc. O(n) does not play ๐
๐ Alternatives ๐
If you're storing structured data as a serialized string, you may also want to consider Valkey valkey-hashes(7). just saying. options are cute, exploring is cuter ๐ฆ
and that's the whole page, bestie โ every last crumb of it ๐
This one's from the Valkey Manual, version 9.0.0, dated 2025-10-21. mwah, go forth and SET responsibly ๐ซถโจ๐
๐ฆ 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