VALKEY-STRINGS(7)

๐Ÿ“ฆ
version 9.0.6 ยท 2 versions
, in fedora 44

๐Ÿ’– 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 ๐Ÿ“

  • SET stores a string value.
  • SETNX stores a string value only if the key doesn't already exist. useful for implementing locks. ๐Ÿ”’
  • GET retrieves a string value.
  • MGET retrieves multiple string values in a single operation.

๐Ÿงฎ Managing counters ๐Ÿงฎ

  • INCRBY atomically 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