VALKEY-BITMAPS(7)
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 to0or1โจ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
123pings the server on January 1, 2024 within the00:00hour. you can then confirm that rider123pinged the server. you can also check to see if rider456has 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:
- constant-time single bit operations โ like setting a bit to
1or0, or getting its value - 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:
BITOPperforms bit-wise operations between different strings. the provided operations areAND,OR,XORandNOT๐BITCOUNTperforms population counting, reporting the number of bits set to1๐ขBITPOSfinds the first bit having the specified value of0or1๐
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 โญ
SETBITandGETBITareO(1)๐BITOPisO(n), wherenis 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
- 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