November 202316:1741.7k views

Store 8 feature flags in 1 integer (Bitwise operations in MySQL)

About

This video is a tutorial on using bits & bitmasks for storing feature flags in a database. It involves compressing all data into a single integer column and using bitmasks to activate specific features.

While this method is technically interesting and efficient for compact data storage, it can be convoluted to understand there's no way to properly index it.

Transcript

Everything I said

00:00

Okay, this is the first of three separate videos where I'm gonna show you how to do the same thing, which is storing feature flags in the database. But we're gonna use different techniques each time, and in this video, we're gonna use the most unhinged technique possible, which is compressing all of the data into a single integer column and using bit masks. If you wanna look really smart, use a bit mask. If you want your coworkers to like you, don't, don't, don't use a bit mask.

00:24

This is a viable technique. It is really hard to read. It's very inscrutable, but it's super interesting, and so we're gonna do it. In future videos, we'll look at storing all of these flags or these configuration values in a JSON column. We'll look at storing them all in a separate table and using a many-to-many relationship to maintain that mapping.

00:45

And in each video, we're gonna look at finding users with or without a flag, finding flags not used, adding a new flag, removing a flag, indexing, selecting with the flag, and the pros and cons to the approach. Let's talk about bit masking first. If you've never worked with bitwise operations, you're gonna see some weird-looking stuff here. If we select one and one, congratulations, you have just run your first bit mask.

01:06

I ran it, but we'll count it for you anyway. Select one and one gives you one. Select two and one gives you zero. Select three and one gives you one. Select seventeen and sixteen gives you sixteen. So what the heck is going on here? This is where we're gonna need a lot of help from Steve, the editor. Steve, let's hit them with a byte.

01:26

A tinyint column is a one-byte column. That byte is made up of eight individual bits. Usually, we're treating that byte as a singular unit, right? We're treating it as an integer value. Let's say we're treating it as a value of six. To store the value six, we flip a few of these bits on. This is binary counting, so we're gonna flip on the bit that represents two, and we're gonna flip on the bit that represents four.

01:49

If you add those up, you get the integer value six. We normally stop at the integer value level, but for bit masking, we're gonna drop down to the bits instead of the byte itself. And if we're working at the bit level, that means we have eight discrete spaces for on/off information, and this is where we're gonna put our feature flags or our configuration values.

02:10

We're gonna assign a feature to every individual bit, and when that feature is enabled for a user, we're gonna turn that bit on, and when it's disabled for a user, we will turn that bit off. Here are the features or maybe the experiments that we're assigning to each individual bit. Dark mode is gonna be bit number one, super admin bit number two, all the way down to new legal disclaimer at bit number eight.

02:34

That's pretty important knowledge to know how integers are stored under the hood using bits and bytes, but the question remains, select seventeen and sixteen. What in the world could that possibly mean? Gonna need some more help from Steve. Steve, hit them with a graphic. When we say select seventeen and sixteen, we're using the bitwise and operator here, so it's probably better if we turn these numbers into their bit representations.

02:57

There we go. Now we have two sequences of bits, and if we stack these on top of each other, this gets a little bit easier to see. The bitwise and operator is going to tell us which bits are on in both sets. In this case, only the sixteen bit is on in both sets, and so a new number is constructed out of just the bits that are on in both sets, in this case, just sixteen.

03:21

So if we were to say one and one, a new number would be constructed of one. If we were to say two and one, the two bit is on in the first set and the one bit is on in the second set, and so there are no bits in common, meaning the resulting value will be zero in that case. However, if we say three and one, in the top row, both two and one are turned on.

03:44

In the second row, one is turned on, and so the resulting value will be one. Now that you know more than you probably wanted to know about bits, bit masks, and bitwise operators, let's take a look at how that applies to SQL. So if we do select star from users limit ten. If we run this, you'll see we've got name, email, and flags, and flags looks like a random number, but we know, you and I know, we both know that's not a random number.

04:09

That is a sequence of bits where some have been flipped on to indicate that features or flags are turned on. As a reminder, here are the flags that we're working with. So we have dark mode, super admin, notification opt-in, and a whole bunch more. What if we wanted to find users where dark mode is on? So the mask that we are applying is one, which is the dark mode bit.

04:35

Eh, it's actually pretty easy. Select star from users where, and we take the users value, the users flags value, and we say, "I want to mask that with the dark mode bit." And on the other side, those, those two bits should line up if dark mode is on, and so I want to make sure that dark mode is on.

04:55

And so if I run that, we see... Well, we see a bunch of interesting stuff, right? We see a couple of ones in here, which we do expect, right? Because a one masked with a one gives us a one. But we also see nine, forty-one, nineteen, one sixty-one, and this is where it gets really powerful because now we have discrete information stored in that flags column, but it is still individually queryable.

05:20

So in this nine situation, what we're seeing, what we're seeing is the number nine, but under the hood, what is being stored is the bit in the eight position is on and the bit in the one position is on. Both of those have turned on, and so the value comes out to nine, but we can still query the individual bits.

05:41

So we see this person, ID number nine, has come back. If we were to hit them with flags and eight equals eight, ID number nine would still come back. So in this case, it's really, it's really discrete information and not an individual number, but we're compacting it, we're shoving it all into a single column. These pieces of information are individually queryable, like we talked about, but we can, we can query them in any part that we want.

06:07

And that's what makes it different than just treating it as a number. We're treating it as a series of bytes, because if we were to do this-- So we, here we did flags and eight equals eight. If we wanted to find people that had eight and flag one enabled, so they have two flags enabled, eight and one, we could run it like this, right? We could run it like this, and we would see a bunch of nines, 'cause that's eight and one, and then we would see, uh, a twenty-five, 'cause that's eight and one and sixteen, and then forty-one.

06:36

So y-you see all the different combinations. You could run it like this, because again, we're just using a mask. We're just using a mask. So we could break the mask apart into two pieces, or we could keep the mask together, and if we ran it like that, we get the same number back. This is very, very different from saying where flags equal nine, 'cause that gives you only nines.

06:57

That gives you people where only the one flag and the eight flag are enabled, not where the one and the eight and any other number of flags are enabled. So when you use the bitmask, you get the benefit of getting all of these other guys, even if one and eight are enabled, but a bunch of other stuff is enabled as well. You see already the drawback of this method, and it's that it's completely impossible for humans to understand.

07:22

If you, as the human, are going to be writing queries like this, I would suggest that you don't. Don't, don't do it this way. If your application is going to be issuing all of these queries, and you can wrap this up into a nice, readable interface for the humans to interact with, this is fine. I have absolutely no problems with this.

07:44

If on the application side, you can wrap this up, so instead of saying, "Where flags and one equals one," which is not understandable, if on your application, you can just say, "Where dark mode," and then your query builder, your ORM, whatever it is, your layer that translates into actual SQL can do this hoop jumping right here, that's fine.

08:06

You as the human get to interact with a nice, well-named interface, and then under the hood, it's using this really compact, weird storage type. That's fine. I just wouldn't write these by hand as a human because i-it's, it's kinda hard to read through. Um, if you wanna find people where the flag is off, you simply, you simply turn it off.

08:27

And so this will show us people that don't have that one flag on or that two flag on. So you see we get people with one and nine and eight and four, but nobody in the three, the six, the two, trying to do all this bit math off the top of my head. So this is how you would query for people that don't have that flag turned on.

08:50

And you could do it the same way. You could say, "I want people with, um, both the one flag and the two flag turned off," so that that mask comes back to zero, and you can do that as well. So you can query multiple things the opposite direction as well. To add and remove flags from a user, we need two other bitwise operators. That's the or operator and the not operator.

09:10

Steve, I'm gonna need some help here. Let's look at a graphic. On the top, we have a series of bits that represents the value one. So the one bit is turned on. The second row is a series of bits that represents the value two. So the two bit is turned on, and the one bit is turned off. Now, historically, we've been running an and over these, and that would result in zero matching bits, which is a zero value.

09:34

If we combine these two with an or, the resulting set of bits is going to have the one bit turned on and the two bit turned on, because instead of the and operator looking for the bits to be on in both sets, the or operator just looks for the bits to be on in one or the other set, resulting in the one and the two being on in the final set.

09:55

Let's take a look at that with some SQL, 'cause that was a little bit confusing. So if we do select one and one, we know that we're gonna get back one, right? If we do select two and one, we know that we're going to get back zero. But if we select two or one, bitwise or one, we're gonna get back three, because in the first value here, two, the two bit is turned on.

10:17

In the second value, the one bit is turned on. And when you or them together, the two and the one bits will be turned on, resulting in a value of three. How does that apply to users? Select star from users limit ten. If we want to turn on a particular bit for user number two, what we can do is we can say update users, uh, set flags equal to flags or, what do we wanna turn on?

10:43

Let's turn on the two bit where ID equals two. So now if we run that and then we select this back, select star from users, we'll see that user number two now has their two bit turned on because we took all of their flags, and then we just added in the two bit with or.

11:04

The good thing is we can run this over and over and over, and it's still gonna be set to two because we're not adding two. We're doing a bitwise or to turn on the two bit, even if that bit is already turned on. If you're still here, we're having a lot of fun. If you're not here, that's a logical impossibility. I don't even have to say anything. But if you're here, you're having fun like I'm having fun, let's figure out how we turn off a flag.

11:28

How do we turn off a bit, even if that bit is maybe not on, but how do we definitely turn it off without messing anything up? We're gonna use the bitwise not operator here, and the not operator just flips all the bits. It just flips every single bit. So we can select one, and that's gonna give us one, and we can select not one, and that's gonna give us a huge, huge number because that is a lot of bits, and it's flipped them all except for the one bit.

11:57

Well, it did flip it, but it flipped that one to a zero. So it turned on a whole bunch of bits and turned off one bit. So we can use that to our advantage. So if we do select star from users again, let's throw a limit on it, limit 10, and we wanna turn off this two bit here. What we can do is we can say update user set flags equal to flags, and we're gonna do a bitmask again, but with the inverse of two, because that's the one that we wanna

12:27

turn off. So if we run that and we read it back, we see that we have turned off the two bit because we made a bitmask that is the perfect inverse of the thing that we're trying to turn off, and then we combined, we combined that with the and operator, and so we turned it off. And again, the good news is you can just run that over and over and over, and it just, it, it doesn't mess anything up.

12:52

So it's not like we're subtracting two. And again, if we run it on this one that has zero, you're not gonna end up at negative two. You're just gonna end up, you're just gonna end up at zero again. So that's how you turn a flag off, is you use the and operator along with the not operator, and you flip the flag, um, to be the perfect inverse of the original flag.

13:14

At the beginning of this video, I told you a list of things that we would go through. There are two that I have missed. That is finding unused flags, finding flags that nobody is using at all, and indexing. Well, for finding flags that nobody is using at all, you have to go through each individual flag. In some of the other solutions, you can do it all at once and find a bunch of unused flags.

13:35

You can't do it with bitmasks. You have to go through each one and query to see if any come back. As far as indexing goes, there's no way to index this. There's just, there's simply no way to index a bitmasked value. That is mostly true. You can put an index on the column, right? You can put an index on the flags column. That's only gonna work for traditional querying, strict equality ranges, that sort of stuff, not for bitmasking.

14:02

Bitmasking is a function, and so you could technically put a functional index over a particular bit or a particular value or a particular operation, but you can't create an index on a column that you then want to bitmask against that is generally applicable. So if you have one particular hot bit that you're constantly querying, you could put a functional index on that.

14:27

I've got a lot of videos on functional indexes, but frankly, the indexing story here is just not very good. So I don't know. I don't know if that would be viable for your situation. Um, finally, the, the last thing would be bringing all the flags along when you select the user. That part is pretty easy with this setup because it's just a column. So you just bring it into your application, and then you can do the same bitwise bitmasking operations over there to see which flags are on.

14:53

So in terms of pros and cons of this approach, pro is really interesting technically. I truly do believe this is a very technically interesting way to solve the problem and can be really valuable in situations where the application is talking to the database and the humans are kind of left out of it. Another pro is the storage is super-duper small.

15:15

You can get away with a tiny int if you need eight pieces of information, or I think an integer is four bytes, so you would get, like, 32 pieces of information in there. And adding a new flag is just as simple as adding a new, the next level up in the bitwise operations. Those are the pros. The cons are it's completely, it's completely impossible to understand as a human.

15:39

It's completely impossible to communicate about with other humans. You have to remember, what do these numbers mean? It's just, I honestly, I probably wouldn't use it. There are a few situations where you're gonna think, "You know what would work really great here? Bitwise operations." So now you know how they work. In the next videos, we'll look at JSON and proper full-on tables, many-to-many relationships.

16:01

If you like this video, please check us out at planetscale.com/youtube. Please subscribe so you can make my boss, Holly, very happy, and please let me know what you think of bitwise operations. See ya.

Databases

More videos on databases

All videos

Get the next one by email

I’ll send you some more. Not too many.

Join 7,500+ other readers.

Read past issues