VALKEY-BITFIELDS(7)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

πŸ’– VALKEY-BITFIELDS πŸ’–

okay bestie, welcome to the valkey-bitfields page β€” yes that's the manual name, we're keeping it, she's iconic πŸ’…


✨ NAME ✨

valkey-bitfields β€” Bitfields πŸ¦‹


🌸 DESCRIPTION 🌸

Bitfields are literally the moment when you need to set, increment, and get integer values of any bit length you want. like, no cap β€” you can operate on anything from unsigned 1-bit integers all the way up to signed 63-bit integers. the range?? unmatched. πŸ‘‘

These values are stored using binary-encoded Strings. and here's the thing that makes them highkey amazing: Bitfields support atomic read, write and increment operations, which makes them a whole vibe for managing counters and similar numerical values. obsessed fr fr. πŸ”₯

πŸŽ€ Basic commands πŸŽ€

  • BITFIELD β€” atomically sets, increments and reads one or more values. the main character energy command, periodt.
  • BITFIELD_RO β€” a read-only variant of BITFIELD. same energy, but she only reads. πŸ™ˆ

πŸ“ Example πŸ“

so imagine you wanna maintain two metrics for various bicycles: the current price πŸ’° and the number of owners over time. you can represent these counters with a 32-bit wide bitfield per for each bike.

  • Bike 1 initially costs 1,000 (counter in offset 0) and has never had an owner. after being sold, it's now considered used and the price instantly drops to reflect its new condition, and it now has an owner (offset 1). after quite some time, the bike becomes a classic ✨. the original owner sells it for a profit, so the price goes up and the number of owners does as well. Finally, you can look at the bike's current price and number of owners.
127.0.0.1:6379> BITFIELD bike:1:stats SET u32 #0 1000
1) (integer) 0
127.0.0.1:6379> BITFIELD bike:1:stats INCRBY u32 #0 -50 INCRBY u32 #1 1
1) (integer) 950
2) (integer) 1
127.0.0.1:6379> BITFIELD bike:1:stats INCRBY u32 #0 500 INCRBY u32 #1 1
1) (integer) 1450
2) (integer) 2
127.0.0.1:6379> BITFIELD bike:1:stats GET u32 #0 GET u32 #1
1) (integer) 1450
2) (integer) 2

look at her go. slay. πŸ’–

🌈 Performance 🌈

BITFIELD is O(n), where n is the number of counters accessed. simple as that, no drama. ✨


k that's the whole page bestie, you ate this 🫢 this was Valkey version 9.0.0, dated 2025-10-21 β€” go forth and bitfield, iykyk πŸ’œ

πŸ“¦ where this page comes from