Going a BIT crazy with Redis.

Kyle
2 min readSep 22, 2017

--

Bits (1s and 0s) are a fundamental of computing, but for most people they don’t get much attention outside of a couple of contexts:

  • Introductions to how computers work. I grew up in the 1980s — the era of home computers with 64kb of RAM — and every computer came with a dead tree manual that had a section about binary data: bits, bytes, kilobytes. Today, the modern corollary of a home computer, the smartphone, abstracts the experience from requiring any knowledge of such things, so knowledge of bits is relegated to Intro to CS courses or not known at all.
  • Hardware-interfaces. Your local IoT enthusiast or device driver / kernel developers will be pretty familiar with bits. In the case of IoT, they’ll study an IC datasheet to enable in-built functions, might know bit-level protocols (I²C, etc.) and ultimately wire up tiny devices to something higher level. As for those individuals that do the yeoman’s work of low-level programming — obsessing over bits means that your modern application can do amazing things without having to worry about how your code interfaces with the underlying hardware.

Outside of the occasional work with microcontrollers, I didn’t think much about bits. I was guilty of storing true and false and a “1” or “0” char — it’s only 8 bits, after all. In my latest two pieces, however, you can observe my descent into obsession over Redis bit manipulation commands.

In my last blog, I wrote about using Redis to set and count bits to do some lightweight, real-time analytics: determine if something is “hot” (lots of recent activity) and/or popular (activity over the items lifetime) in the context of static images. This was done with SETBIT and BITCOUNT. The intoxicating thing about this method is that you can store micro-time series data in itsy-bitsy (get it?) amounts of storage space.

Yesterday, I had a piece published in Infoworld about using the BITFIELD command and BITOP as access control methods. I encourage you to check it out — the take-away is that by using bits in Redis as access flags you can literally store access control for millions (or even billions) of users in mere gigabytes of space with lightening speed.

I know I extol the virtues of Redis all the time (and it’s worth all the extoling) but if you’re a higher-level developer you should really start thinking about and manipulating bits even if you don’t use Redis. It has changed my perspective and I think I’m a better developer for it. Although, I do now have a twinge of guilt for wasting bits in the past.

Won’t someone think of all the wasted bits?

--

--

Kyle

Developer of things. Node.js + all the frontend jazz. Also, not from Stockholm, don’t do UX. Long story.