Quick revision
The short answer
A long bitmask is compact and makes a permission check a single AND, but it caps at 64 permissions, so 90 permissions across several products no longer fit, and it is opaque in SQL, audits and support tools. Adding bits needs coordinated code changes, and reusing a retired bit silently grants its old meaning to stale data. I would move to a normalised model, a user-permission join table or a role-based model, or a PostgreSQL array or bit varying column if we must keep it compact, migrate with dual writes, backfill and verification before removing the bitmask, and keep fast in-memory checks by building BitSets or roaring bitmaps from the normalised data.
What to remember
In this answer, we evaluated a long bitmask for permissions. It is compact and fast, but it caps at 64 bits, is opaque in SQL and audits, needs coordinated code changes, and is dangerous when retired bits are reused. We compared a join table, a role-based model and PostgreSQL array or bit varying columns, then planned a migration with a permission catalog, dual writes, backfill, comparison jobs and shadow reads before switching reads and removing the column. Finally, we kept fast checks by building BitSets or roaring bitmaps in memory from the normalised data.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A long bitmask is compact and makes a permission check a single AND, but it caps at 64 permissions, so 90 permissions across several products no longer fit, and it is opaque in SQL, audits and support tools. Adding bits needs coordinated code changes, and reusing a retired bit silently grants its old meaning to stale data. I would move to a normalised model, a user-permission join table or a role-based model, or a PostgreSQL array or bit varying column if we must keep it compact, migrate with dual writes, backfill and verification before removing the bitmask, and keep fast in-memory checks by building BitSets or roaring bitmaps from the normalised data.
Why this question is asked
Bitmask permission columns are a classic design that starts well. With 20 permissions, one BIGINT per user is tiny, checks are instant, and the code is short. A few years later, the product has grown, a second and third product share the user table, and the design is suddenly blocking work. Interviewers ask this question to see whether we can evaluate a design fairly, keep what was good about it, and move away from it safely in a live system. In this answer, we will recap how the bitmask works, look at what breaks as it grows, compare the replacement options, plan the migration step by step, and keep the fast checks with in-memory bitsets.
1How the Bitmask Design Works
Each permission is assigned a bit position, for example VIEW_REPORTS is bit 0, EDIT_USERS is bit 1, and so on. A user's permissions are stored as one BIGINT column, and granting a permission sets its bit. Checking is a single AND in Java, and even in SQL a query like WHERE permissions & 4 <> 0 finds users with bit 2. Storage is 8 bytes per user, and the check costs nanoseconds.
The last two lines show the hard limit. A long has 64 bits, and in Java a shift by 64 wraps to a shift by 0, so the 65th permission would silently alias the first. With 90 permissions the design simply cannot represent the data.
2What Breaks as the System Grows
Beyond the 64-bit cap, the bitmask has costs that grow with the organisation. It is opaque: a support engineer looking at permissions = 1152921504606847013 cannot tell what the user can do, and an auditor asking who can export data needs a decoding step in every report. SQL queries on individual bits cannot use normal indexes efficiently, so 'all users with permission X' scans the table.
Changes are fragile. The mapping from bit to meaning lives in code, so every service that reads the column must agree on it and be deployed together when it changes. And the most dangerous problem is bit reuse. If permission 17, DELETE_ACCOUNTS, is retired and later someone assigns bit 17 to a new permission, every user who had DELETE_ACCOUNTS years ago and never had the bit cleared suddenly holds the new permission. Multiple products make all of this worse, because they must share one bit namespace.
| Concern | long bitmask | Join table or roles |
|---|---|---|
| Number of permissions | max 64 per long | unlimited |
| Readable in SQL and audits | no, needs decoding | yes, names in rows |
| Who has permission X? | full table scan | index on permission_id |
| Adding a permission | code change in every reader | insert a row |
| Retiring a permission | bit reuse grants old meaning | row deleted, id never reused |
| Check speed | one AND | query or cached set |
3Options for the New Model
The most common choice is a join table, user_permission(user_id, permission_id, product_id, granted_at, granted_by), with a permission table that holds names and descriptions. It is readable, indexable, unlimited and auditable, and the granted_by column gives us a history for free. Its cost is more rows and a join or a cache for every check.
A role-based model, RBAC, adds a level: permissions belong to roles such as REPORT_VIEWER, and users are assigned roles, possibly per product. With 90 permissions and several products, this is usually what the business really wants, because administrators think in roles, and it shrinks the number of assignments dramatically.
If we must keep the data compact in one row, PostgreSQL offers alternatives: a text[] or int[] array column with a GIN index for 'contains' queries, or a bit varying column with no fixed limit. These remove the 64-bit cap, but keep some of the opacity and the reuse risk, so I would see them as a short-term step rather than the destination.
4Migrating Without Downtime
The migration is the risky part, because permissions are security-critical and every request depends on them. We move in steps that can each be verified and reversed. First, we create the new tables and a permission catalog that maps each existing bit to a permanent permission id, marking retired bits explicitly so they are never assigned. Second, we change the write path to dual write: every grant or revoke updates both the bitmask and the new tables in one transaction. Third, we backfill existing users in batches, decoding each bitmask into rows, in a way that is safe to rerun.
Fourth, we verify. A comparison job decodes every user's bitmask and compares it with their rows, and we also run shadow reads in production, computing the decision from both sources and logging any mismatch, while still enforcing the old one. Only when mismatches have been zero for an agreed period do we switch reads to the new model, keep dual writes for a while as a fallback, and finally drop the bitmask column and the old code.
- Create tables and permission catalogmap every bit, mark retired ones
- Dual write grants and revokessame transaction
- Backfill in batchesidempotent, rerunnable
- Verify: compare job and shadow readslog every mismatch
- Zero mismatches for the agreed period?YesSwitch reads to the new modelNoFix the cause and re-verify
- Stop dual writes, drop the columnafter a safe period
6 steps in order. Play them, or step through them one at a time.
5Keeping Fast In-Memory Checks
The one real advantage of the bitmask was speed, and we can keep it without keeping it as the stored format. When a user's session or token is created, we load their permissions from the normalised tables, map each permission id to a dense index, and build a java.util.BitSet, or a RoaringBitmap for large sparse sets. A check is then a single bit test, just as fast as before, and it scales beyond 64 bits.
Notice the difference: the bitset is now a derived cache, rebuilt from the source of truth, so its bit positions can change between deployments without corrupting stored data, and reuse of an index is harmless. In Spring Security, the same data would usually surface as GrantedAuthority values, with the bitset used only where checks are very hot.
6Follow-up questions
Why not just switch to two long columns?
It postpones the problem rather than solving it. The cap moves to 128, but the opacity, coordinated code changes and bit reuse risk remain, and the second product will need more bits again. It might be acceptable as an emergency step while the real migration happens.
How do you prevent bit or id reuse in the new model?
Permission ids are never reused: retired permissions stay in the catalog marked as retired, and a database constraint or a review check stops a new permission from taking an old id. The in-memory bitset index is derived per deployment, so it can be reassigned safely.
How does this fit with tokens such as JWTs?
Putting 90 permission names in every token makes tokens large. Common options are putting roles rather than permissions in the token, or putting a compact encoded bitset with a catalog version in it, so services can decode it with the matching catalog and refuse tokens with an unknown version.
7Conclusion
In this answer, we evaluated a long bitmask for permissions. It is compact and fast, but it caps at 64 bits, is opaque in SQL and audits, needs coordinated code changes, and is dangerous when retired bits are reused. We compared a join table, a role-based model and PostgreSQL array or bit varying columns, then planned a migration with a permission catalog, dual writes, backfill, comparison jobs and shadow reads before switching reads and removing the column. Finally, we kept fast checks by building BitSets or roaring bitmaps in memory from the normalised data.
Open with
I would start by saying what the bitmask does well: eight bytes per user and a permission check that is a single AND. But with 90 permissions it is already broken, because a long holds 64 bits and in Java a shift by 64 wraps around to bit zero, so the 65th permission would alias the first.
Then, if they want more
It also has costs that grow with the company: the value is unreadable in SQL, audits and support tools, finding everyone with one permission is a table scan, every service has to agree on the bit mapping and deploy together, and the worst risk is reusing a retired bit, which silently grants its new meaning to every user who still has the old one set.
Several products sharing one bit space makes all of that worse. I would move to a normalised model, a permission catalog plus a user permission table with product id, or a role-based model if administrators think in roles, which they usually do.
For the migration I would map every bit to a permanent id and mark retired ones, dual write grants and revokes, backfill in idempotent batches, then verify with a comparison job and shadow reads that log mismatches while still enforcing the old data. After an agreed period with zero mismatches, I switch reads, then remove the column.
To keep checks fast, I build a BitSet or roaring bitmap per session from the normalised rows, which is a derived cache, so it can grow and change safely.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A feature flag and permission system stores user permissions as a long bitmask in a...": https://scrivio-interview-library.netlify.app/java/algorithms-and-data-structures/8503b2a4e8/