Skip to content
Safigo
Go back

Quick review of the most popular ways to implement flags

Introduction

There are many ways to store flags and use them for communication between client <-> backend service or service <-> service. In the article, we will review the most popular options and try to help to choose the correct way for the next project.

The most popular ways to work with flags are:

  1. Store in separate column in DB or separate property in a class/struct
  2. Store as an array or in a set
  3. Store as binary data in a variable

Options

Separate field

It means we will create a separate column in DB for each flag.

DB Example

In an example, is_new will be a flag.

idusernameis_new
1test1true
2test2false

JSON Example

In a code or a NoSQL database representation will be:

[
	{
		"id": 1,
		"username": "test1",
		"is_new": true
	},
	{
		"id": 2,
		"username": "test2",
		"is_new": false
	}
]

Pros

Cons

Use Cases

In my opinion, before using this approach, we should answer “yes” for all listed options in the checklist:

Store In Array

In the current implementation, we will store all flags in plain names.

DB Example

In a DB, we are going to store values in a separate table as one to many relationships.

Table user:

idusername
1test1
2test2

Table user_flag

iduser_idflag
11is_new
21is_test

Every user_flag row is linked to a row in user table by user_id field. It can be optimized to not store the flag as a string, but it is not so important in our current topic.

JSON Example

In a code or a NoSQL database representation will be:


[
	{
		"id": 1,
		"username": "test1",
		"flags": [
			"is_new",
			"is_test"
		]
	},
	{
		"id": 2,
		"username": "test2",
		"flags": []
	}
]

Pros

Cons

User Cases

In my opinion, before using this approach, we should answer “yes” for all listed options in the checklist:

Store In Bitmask

Bitmask is a way when we use bitwise operations to get access to specific flag. Every flag stores in binary data in memory, for example it can be stored in: byte, short, int, uint, long, []byte, []short, map[int]byte, etc.

Simply read README.md from binflags library.

DB Example

In DB, we can store bitmask in various types, like: TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT, BLOB types.

idusernameflags
1test11
2test20

In the current example, flags field has the type INT, and the first bit is is_new flag.

JSON Example

In a code or a NoSQL database representation will be:


[
	{
		"id": 1,
		"username": "test1",
		"flags": 1
	},
	{
		"id": 2,
		"username": "test2",
		"flags": 0
	}
]

Pros

Cons

Use Cases

In my opinion, before using this approach, we should answer “yes” for all listed options in the checklist:

Conclusion

As we can see, as usual, we do not have a silver bullet for all use cases and systems. But the provided list of implementations can help find the best solution for a specific project.

If you want to use the most efficient option in Go, I would suggest checking Go implementation of binary flags for various types.


Share this post on:

Previous Post
Accept header parser and matcher