VALKEY-BITMAPS(7)

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

okay so bestie, welcome to valkey-bitmaps(7) โ€” straight outta the Valkey Manual ๐Ÿ’– this is the whole vibe on bitmaps, and ngl it's lowkey one of the prettiest corners of Valkey. let's go ๐Ÿฆ‹


๐ŸŒˆ NAME ๐ŸŒˆ

valkey-bitmaps โ€” Bitmaps โœจ


๐Ÿ’… DESCRIPTION ๐Ÿ’…

okay first thing, and this is important: bitmaps are NOT an actual data type. no cap. it's giving "not a type, it's a lifestyle" โ€” they're a set of bit-oriented operations that live on the String type, and the string just gets treated like a bit vector ๐ŸŽ€

and because strings are binary safe blobs whose max length is 512 MB, they're literally perfect for setting up to 2^32 different bits. the math is mathing ๐Ÿ‘‘

you can do bitwise operations on one or more strings. some bitmap use cases, iconic behaviour only:

  • efficient set representations for when the members of a set correspond to the integers 0-N ๐Ÿ“
  • object permissions, where each bit represents a particular permission โ€” same energy as how file systems store permissions ๐Ÿ’œ

๐Ÿงš Basic commands ๐Ÿงš

  • SETBIT โ€” sets a bit at the provided offset to 0 or 1 โœจ
  • GETBIT โ€” returns the value of a bit at a given offset ๐Ÿ‘€

see the valkey-commands(7) complete list of bitmap commands if you wanna go shopping ๐Ÿ’…

๐ŸŒธ Example ๐ŸŒธ

so imagine: 1000 cyclists racing through the country-side, with sensors on their bikes labeled 0-999. you wanna quickly determine whether a given sensor has pinged a tracking server within the hour, so you can check in on a rider ๐Ÿšดโ™€๏ธ

you can represent this whole scenario using a bitmap whose key references the current hour. periodt.

  • rider 123 pings the server on January 1, 2024 within the 00:00 hour. you can then confirm that rider 123 pinged the server. you can also check to see if rider 456 has pinged the server for that same hour ๐Ÿ‘‘
127.0.0.1:6379> SETBIT pings:2024-01-01-00:00 123 1
(integer) 0
127.0.0.1:6379> GETBIT pings:2024-01-01-00:00 123
(integer) 1
127.0.0.1:6379> GETBIT pings:2024-01-01-00:00 456
(integer) 0

๐Ÿ”ฅ Bit Operations ๐Ÿ”ฅ

bit operations split into two groups, bestie:

  1. constant-time single bit operations โ€” like setting a bit to 1 or 0, or getting its value
  2. operations on groups of bits โ€” for example counting the number of set bits in a given range of bits (e.g., population counting)

one of the biggest advantages of bitmaps is that they often provide extreme space savings when storing information. like, for example, in a system where different users are represented by incremental user IDs, it is possible to remember a single bit information (for example, knowing whether a user wants to receive a newsletter) of 4 billion users using just 512 MB of memory. the way that is so efficient, I'm obsessed ๐Ÿ˜ญโœจ

the SETBIT command takes as its first argument the bit number, and as its second argument the value to set the bit to, which is 1 or 0. the command automatically enlarges the string if the addressed bit is outside the current string length ๐Ÿซถ

GETBIT just returns the value of the bit at the specified index. out of range bits (addressing a bit that is outside the length of the string stored into the target key) are always considered to be zero ๐Ÿ’€

there are three commands operating on group of bits:

  1. BITOP performs bit-wise operations between different strings. the provided operations are AND, OR, XOR and NOT ๐ŸŒˆ
  2. BITCOUNT performs population counting, reporting the number of bits set to 1 ๐Ÿ”ข
  3. BITPOS finds the first bit having the specified value of 0 or 1 ๐Ÿ”

both BITPOS and BITCOUNT are able to operate with byte ranges of the string, instead of running for the whole length of the string. we can trivially see the number of bits that have been set in a bitmap ๐Ÿ‘‡

127.0.0.1:6379> BITCOUNT pings:2024-01-01-00:00
(integer) 1

for example imagine you want to know the longest streak of daily visits of your web site users ๐ŸŒธ you start counting days starting from zero, that is the day you made your web site public, and set a bit with SETBIT every time the user visits the web site. as a bit index you simply take the current unix time, subtract the initial offset, and divide by the number of seconds in a day (normally, 3600*24). the math is mathing again, I fear ๐Ÿงฎ

this way for each user you have a small string containing the visit information for each day. with BITCOUNT it is possible to easily get the number of days a given user visited the web site, while with a few BITPOS calls, or simply fetching and analyzing the bitmap client-side, it is possible to easily compute the longest streak. main character data structure fr fr ๐Ÿ‘‘

bitmaps are trivial to split into multiple keys โ€” for example for the sake of sharding the data set, and because in general it is better to avoid working with huge keys ๐Ÿฆ‹ to split a bitmap across different keys instead of setting all the bits into a key, a trivial strategy is just to store M bits per key and obtain the key name with bit-number/M and the Nth bit to address inside the key with bit-number MOD M โœจ

โญ Performance โญ

  • SETBIT and GETBIT are O(1) ๐Ÿ’…
  • BITOP is O(n), where n is the length of the longest string in the comparison ๐Ÿ”ฅ

and that's the whole page, bestie!! ๐ŸŽ€ no notes, ate and left no crumbs. this was Valkey 9.0.0, dated 2025-10-21 โ€” go forth and set some bits ๐Ÿ’–๐Ÿฆ‹โœจ๐Ÿ‘‘

๐Ÿ“ฆ where this page comes from