November 202314:1024k views

Storing feature flags as JSON (with indexing)

About

Storing feature flags as JSON in MySQL. This is the second of three videos, exploring different ways to store flags.

Transcript

Everything I said

00:00

This is the second video out of three where I show you different techniques for storing feature flags in MySQL. Doesn't have to be feature flags, could be any on/off, yes/no, true/false configuration, anything to prevent you from adding 15 different columns to your user model. In a previous video, we shoved it all in an integer column and used bit masks. Kinda unhinged, kinda awesome.

00:21

Don't know if I would recommend it. In this video, we're gonna look at two separate ways to store it in a JSON column, a JSON object, and a JSON array. In the third and final video, we'll put all of that in a separate table and use many-to-many relationships to link all of that up. As a reminder, in every video, we're gonna look at finding users with and without the flags, finding flags not used, adding a new flag, removing a flag, indexing, selecting with the flags, and the pros and cons to each approach.

00:47

Let's start using some JSON. Much has been said by me about JSON in MySQL, so we're just gonna dive right in. If you want a full crash course on JSON in MySQL, I have another video on that that'll be somewhere. You can find it on YouTube. Um, so let's take a look at this. We've got a column named flags, and in that column is a JSON object that is pretty basic.

01:10

We are gonna start with a JSON object where the keys are the names of the features or the flags or the experiments or whatever. Then we will move on to a JSON array. Um, right now it's just key value pair, but we can get away with just a key array. So we'll move on to that in a little bit. So the first thing that we need to do, the first thing that we're doing in all three videos where we do this, is finding users that have a certain flag enabled, and let's go with dark mode.

01:38

So before we do that, we need to figure out, how do we pull these things out of the object? We use this JSON unquoting operator and then use the dollar sign to target the root node, and then we can say dark mode, and that should give us back, that should give us back all of the dark modes. Let's just drop down to ID only. There we go. We got a bunch of falses for dark mode.

02:00

So we can use that in a where clause now. So if we say where flags dark mode equals true, you'll see it doesn't actually work. The problem here, the problem here is that this is the JSON unquoting operator, and in the process of unquoting, it has cast it to a string. And so if we run this again with both of these using just the JSON extraction operator, now you'll see we're back to where, we're back to where we thought we should be.

02:30

So let's turn on-- Let's go ahead and turn on flags too, so we can see the whole object. Okay, so it looks like we're getting the correct ones here. I think an, uh, an easy question is, what if we just did that? And that just tests the presence of the key. It doesn't compare it to true. So we do actually have to do the explicit true comparison.

02:52

The next thing we gotta figure out is finding users who explicitly have this feature flag disabled, or it's completely missing from their JSON object altogether, which is gonna require us to jump through at least one hoop. If we just say equals false, we're gonna get all of the ones where the feature flag is explicitly disabled. But let's do something interesting here. Let's say update users set flags equal to null where ID equals one.

03:19

So we're just gonna null out that object all together for that person. And if we run this again, you would expect, you would expect that first person to not show up. Maybe not based on this query, but based on the intent of the query, I wanna find users who have this flag not enabled. You would expect user ID number one to show up.

03:41

So what's the problem here? If we remove that, you'll see the extraction just extracted null. Right? So there are two ways that we could do this. We could say equals false or just simply do an is null. You can also check for the presence of a key using one of the JSON operators.

04:01

Um, I think it's much easier to just coalesce this guy. So we're gonna coalesce it, and if it's null, we're just gonna treat that as false, and then that gets us the result set that we're looking for. So to find people with true or to find people with the flag on, we can just compare to true. But to find people with the flag off or missing, we have to jump through this little coalescing hoop.

04:23

The next thing we're gonna do is add and remove flags from users, and we're gonna use the JSON set function to do that because that updates the document in place instead of pulling the whole object out, changing the value, and putting the entire object back in. Let's just operate on a single user now. So select star from users where ID equals two. We're gonna have to do a little bit of hoop jumping because we nulled out user ID number one, but, eh, that's a problem for future me and future you, in fact.

04:50

So the first thing we wanna do is dark mode is off for this guy, Tony. What we can, what we can do is we can just say update users set flags equal to JSON set. So this is the function that will update it in place. The root document is, uh, the flags column, and then we do our little path dance.

05:11

So we do, uh, dollar.darkmode, and this will create that key if it doesn't exist. So we set dollar dark mode to true where ID equals two. So if we run this guy and then we hop back up here, we'll see that dark mode has been turned on to true. And if we just do a new foobar and set that to true and come back, we see that the key was added.

05:39

So not only will it set the value, it will create it if it doesn't exist. Now, what about user ID number one? Unfortunately, if we run JSON set on a null document- You still get a null document back. This is where I would potentially say that having a nullable JSON column is a mistake.

05:59

If you know that it's always gonna be an object, make the default an empty object. If you know it's gonna be an array, make the default an empty array. 'Cause what we're gonna have to do is we're gonna have to coalesce this column as an empty object if it's null, and I don't know if this takes away the in-place updating capabilities of MySQL. So, let me show you how we do it. Mechanically, this is very simple.

06:19

All we do is we say COALESCE flags to be an empty object if it's null. We run that, we read it back, and voila, we have an object where we previously had null, and that key of foobar has been set to true. This raises an interesting question. Do you know what the question is? I will tell you. The question is, that object is very compact. It's very small. We don't have all of the false values.

06:40

So if we go back here and we say LIMIT 10, you'll see we've got all these false values. Is that valuable information? I don't know. That's for you to decide. Sometimes it is nice to know when a value has been explicitly set, and sometimes you only need to know whether it's been explicitly turned on. You don't need to know if it's missing or explicitly false.

07:02

Those are discrete pieces of information. Explicitly false and null are entirely different things, but it may not matter. If it doesn't matter for your application, we can thin these objects out by just removing all of the false values, leaving all of the true values, and then checking for the presence or an absence of a key. I'm gonna reseed this database such that only true values remain.

07:26

Steve, can you give me a little graphic cover here? Wow, that was great, Steve. I have no idea what you did, but I trust that it turned out wonderfully. If we read this table back, you'll see that the flags column now only contains true values. We've gotten rid of all the false values. Were they set to false? Were they null?

07:47

Who can say? We lost that piece of information. Eh, it's probably just fine. Now we can check to see, instead of, uh, true or coalescing false, we can say WHERE JSON CONTAINS PATH. And all of this, all this is going to do is check to see if the path exists, to say nothing of the value at that path. So the first thing we need to do is pass in the column, and then we can pass either ALL or ONE.

08:12

Not like this. You have to choose one of them. ALL or ONE says, "I'm looking for multiple keys that must all exist," or, "I just need one of the following keys to exist." This is interesting. We're gonna say ALL. So I want all of the keys to exist, and then we'll start at the root node and we'll say log verbose, just 'cause that's the first guy there. And now if we run that, you'll see all of those, all of the results have log verbose set to true.

08:38

The nice thing about this is we can test multiple flags at the same time. So we can say log verbose and dark mode must both be present. And if we were to switch this to one, this would say, uh, log verbose or dark mode must be present. And so you kinda get both, either, and, or. So this is a little bit more powerful or maybe, um, a little bit more power and a little bit easier.

09:04

You can, you can do this the other way. You just have to write multiple conditions. But I find this to be quite simple. Now, what does this return? So if we c- pop up here and we just do the ID, you'll see that that returns one. So to check to see that that is not the case, we could turn that off, and we'll just go back to everything there. This first person does have log verbose, but it doesn't have dark mode, and since we specified ALL, that is false.

09:30

So this is a way that you could check for the presence or absence of a single flag or multiple flags. At this point, we're getting pretty close to just using an array, right? We've got an object full of keys that are only true, and we're just checking to see if that key exists. Why not just use an array? And that's what we're gonna do next, because there's no good indexing strategy for a JSON object used this way.

09:51

You can index a single JSON object key, but since we have multiple flags in there with different key names, there's no way to put a general index over that. But you can put a general index over an array, and so that's what we're gonna do now. I've got the table here again, and I reseeded it such that we're using arrays instead of objects now. So anytime a flag is on, the value is gonna show up in the array, and anytime it's off, it's just simply not there.

10:17

And anytime there are no flags at all, the empty state is an array instead of an object. Pretty normal stuff so far. We can test to see if dark mode is on by saying dark mode, and then using this MEMBER OF operator, which operates on a JSON array. This is a standard MySQL thing, and so we'll just say dark mode MEMBER OF flags, and that will give us all of the users, all of the rows where dark mode is present in that feature flags array.

10:43

The good news about MEMBER OF operator is it's totally indexable. So the object wasn't indexable, because we would have to pick a specific key and put a generated column or a functional index on that. But this MEMBER OF operator, it operates on one in a, one of MySQL's only multi-value index entries, so you can put an index over an entire array.

11:07

So if we ALTER TABLE users ADD INDEX, we'll call it flags. We'll open a double set of parentheses. This denotes to MySQL that we're starting a functional key part index or a functional index. And you might think you could just throw flags in there. Unfortunately, it's not that straightforward. It's not very hard, though. We have to cast the flags as something.

11:28

So we're gonna tell MySQL, "Cast flags as a two hundred and fifty-five length string." There is no cast as varying length string. This actually comes out to be a varying length string. So we just say character two hundred and fifty-five, and then we tell MySQL, "This is actually an array," and that's gonna flip on the ability to store multiple values in a sing-single index entry.

11:49

And if we run this, perfect, it did work. So if we read it back, SHOW INDEXES FROM users, and we take a look, you see it is down here. And if we scroll over to the right, we see the expression because it's not on a column. So the column name is null, but the expression is populated. We run this again. We see it does still work. And if we explain it back, we're now using an index.

12:11

So we're now using an index on that JSON array to get really fast lookups on these values. And that's why I like the JSON array style, because you can put that index over the multiple values and get a fast lookup, no matter what flag you're looking for. You can't do that with a JSON object style. Using the JSON array style does have at least one drawback, and that's that you have to do your adding and removing of flags- in the application layer.

12:36

Removing a flag is an incredibly complicated process when you have a JSON array. You have to inspect the array, find the element where the flag is, and then remove that element by, um, index instead of by value. So it's just terrible. Do it in the application side. I was gonna show you how to do it in SQL, and I thought, "No one should ever do this.

12:56

This is gonna make a terrible video," so I didn't even show you. So regardless of which JSON style you choose, if you choose the object style, you've got all kinds of operators that operate on objects and make it easy to add and remove flags, but you can't index the whole thing. If you choose the array style, you can index the whole thing, but adding and removing of flags, you'll probably wanna do in the application layer.

13:17

Whichever one of these you choose, I would go, I would go with JSON. We're talking about three different styles. One is an integer column, um, and using bit masks. It's kinda crazy. It's kinda awesome. I wouldn't do it. The other is proper tables, relationships, many-to-many. That's fine. It feels a little heavy. I like putting all of the stuff in a JSON column.

13:38

I find that to be the easiest. When you select the user or the row out of the table, all of the flags come along with it. You don't have to do some joins or some, uh, eager loading or anything like that. So I like the JSON column. In the next video, we'll talk about, um, associating it with many-to-many relationships in other tables. Check out the previous video if you didn't see the integer column with the bit masks, and please subscribe to this channel so you can make my boss, Holly, very, very happy.

14:05

Until the next time, 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