Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

Several services encode status flags as bit positions in an int column shared through a database and in message payloads. Teams keep adding bits and two teams have reused the same bit. How do you redesign this and migrate safely?

The real problem is that the bit positions are a shared contract that nobody owns and no compiler checks, so two teams could reuse the same bit. We fix it in two moves: first give the flags one owner and one registry, a shared enum where each constant has an explicit, never-reused bit number pinned by tests, and then move new contracts to named fields or lists of flag names. We migrate safely with a dual-write period, a backfill that maps the old bits, including the collided one, to the new representation, and consumers that can read both until the old form is retired.

Why this question is asked

Packing status flags into an int column is a reasonable choice when one team owns one service. It turns into a slow-motion incident when the same int is shared through a database and through message payloads, and several teams add bits on their own. Interviewers ask this question to senior engineers because the technical fix is easy and the safe migration is not: data is already stored with ambiguous meaning, and producers and consumers cannot all change on the same day. In this answer, we will look at why the current design fails, how to establish ownership, what the new representation should be, how to migrate step by step, and how to stop it from happening again.

1Why the Current Design Fails

A bit position in a shared int is a contract: when service A sets bit 5, it promises every reader that bit 5 means, say, ON_HOLD. But the contract is written down nowhere that a tool can check. Each team keeps its own constants, so team A's ON_HOLD = 1 << 5 and team B's PRIORITY = 1 << 5 both compile, both pass their own tests, and the collision shows up only when a reader interprets the other team's data.

So the failure is organisational before it is technical. There is no single owner of the list of bits, no review that sees all of them at once, and no test that would fail when a number is reused. Any redesign has to fix that, or the new representation will drift the same way.

Java
// Team A, in the orders service
static final int ON_HOLD = 1 << 5;
// Team B, in the fulfilment service, written a year later
static final int PRIORITY = 1 << 5; // same bit, nobody noticed
int flags = row.getInt("status_flags"); // 32
boolean onHold = (flags & ON_HOLD) != 0; // true, but team B meant priority

2Step One: One Owner and One Registry

The first move is to put every flag in one place with one owner, for example a small shared library that every service depends on. In it, a single enum lists every flag with an explicit bit number. The number is a field of the constant, not its position in the enum, so reordering or adding constants never changes the meaning of stored data. Retired flags stay in the enum, marked deprecated, so their numbers can never be handed out again.

Before writing the enum we need an inventory: search every repository for the column and the payload field, list every constant and every bit each producer sets, and find every collision. For the collided bit 5, we let one meaning keep the number and give the other a number that was never used, here PRIORITY moves to 12, and we keep a record of which producer meant which, because the backfill will need it.

Java
public enum StatusFlag {
VERIFIED(0), ON_HOLD(5), PRIORITY(12), // PRIORITY moved off bit 5
@Deprecated LEGACY_EXPORT(7); // retired, number reserved forever
public final int bit;
StatusFlag(int bit) { this.bit = bit; }
}
@Test void bitNumbersArePinned() {
assertEquals(5, StatusFlag.ON_HOLD.bit); // a change shows up in review
assertEquals(12, StatusFlag.PRIORITY.bit);
long distinct = Arrays.stream(StatusFlag.values()).mapToInt(f -> f.bit).distinct().count();
assertEquals(StatusFlag.values().length, distinct); // no bit is used twice
}

Notice that the tests do two jobs. Pinning each number means that changing one is a visible diff that a reviewer must approve. The distinct check means that a new constant reusing a number fails the build in the shared library, before any service ever ships it.

3Step Two: Choose the New Representation

With ownership fixed, we can decide how the flags should travel. For new message contracts, we prefer named fields or a list of flag names, such as "flags": ["VERIFIED", "ON_HOLD"]. A payload like that explains itself, a consumer that does not know a new name can ignore it instead of misreading it, and schema tools can validate it. The cost is a few dozen extra bytes per message, which almost never matters.

In the database, the choice depends on how the flags are queried. Boolean columns or a text array of names are readable and easy to index for the flags people filter on. A compact bitmask is still fine where size or speed really matters, for example a huge table where the flags are only read whole, but then it must use the registry's numbers and nothing else.

4Step Three: Migrate Without a Big Bang

Producers and consumers cannot all deploy on the same day, so we migrate in phases where every phase is safe on its own. This is the expand and contract pattern: first add the new form beside the old one, then move everyone across, and only then remove the old form.

The hardest part is the backfill for the collided bit. For each old row with bit 5 set, we must decide whether it meant ON_HOLD or PRIORITY. Often we can tell from which service wrote the row, from an audit column, or from the date team B started using the bit. Rows we cannot classify go to a small review list instead of being guessed, because a wrong guess becomes silent bad data.

SQL
-- expand: add the new column beside the old one
ALTER TABLE orders ADD COLUMN status_names text[];
-- backfill: resolve bit 5 by the service that wrote the row
UPDATE orders SET status_names = array_remove(ARRAY[
CASE WHEN (status_flags & 1) <> 0 THEN 'VERIFIED' END,
CASE WHEN (status_flags & 32) <> 0 AND written_by = 'orders' THEN 'ON_HOLD' END,
CASE WHEN (status_flags & 32) <> 0 AND written_by = 'fulfilment' THEN 'PRIORITY' END
], NULL)
WHERE status_names IS NULL;
Step throughExpand and contract migration of the flags
  1. Publish the registryshared enum, pinned bit numbers, collisions resolved
  2. Expandadd the new column and the names field to messages
  3. Dual-writeproducers write old and new forms together
  4. Backfillmap old bits to names, resolving bit 5 by writer
  5. Do old and new forms agree?compare them with a daily job
    YesMove consumers to read the new formNoFix the mapping and backfill again
  6. Contractstop writing the old form, then drop it

6 steps in order. Play them, or step through them one at a time.

Every phase is safe on its own, so producers and consumers can deploy on different days.

Notice that the backfill only touches rows not yet written in the new form, so it can run in batches while the dual-write is live without overwriting fresh data.

5Keeping It Fixed

The registry and its tests stop reuse inside the code, but people still need a process. The shared library has named owners who review every new flag, the payload schema is checked for compatibility in the build of every producer, and a reconciliation job compares the old and new forms during the transition and alerts on any difference. Once the old column is gone, the remaining compact bitmasks are generated from the registry instead of being typed by hand, so a hidden second list cannot grow back.

6Follow-up questions

How do consumers read both forms during the transition?

They prefer the new form when it is present and fall back to decoding the old int with the registry's numbers when it is not. That logic lives in one shared reader in the library, so every consumer behaves the same way, and it is deleted in the contract phase.

What if some rows with the collided bit cannot be classified?

We do not guess. We mark them as ambiguous, report how many there are, and let the owning business teams decide, sometimes by asking the source system again. A few hundred rows reviewed by hand are far cheaper than silently putting thousands of orders on hold.

Would you ever keep a bitmask in a new design?

Yes, where size truly matters, such as a very large table read whole or a compact binary protocol. Even then the bits come from the single registry, are generated rather than hand-typed, and are pinned by tests, so the bitmask is just an encoding of named flags, not a contract of its own.

7Conclusion

In this answer, we saw that shared bit positions are a contract with no owner and no compiler check, which is how two teams reused one bit. We fixed ownership with one registry, an enum with explicit, never-reused bit numbers pinned by tests, moved new contracts to named fields or lists of names, and migrated with an expand and contract plan: dual-write, a backfill that resolves the collided bit by who wrote it, consumers that read both forms, and only then removal of the old form.

What to say in the interviewAbout 80 seconds aloud

Open with

I would first name the real problem: the bit positions are a shared contract that nobody owns, and nothing stops two teams choosing the same number. So the first step is ownership.

Then, if they want more

I would create one registry, a shared enum in a small library, where every flag has an explicit bit number as a field, never its ordinal, retired numbers stay reserved, and tests pin each number and check that none is used twice.

Building it starts with an inventory of every producer, which is also how we find the collision and record which service meant which meaning.

Then I would change the contract for new work: messages carry named fields or a list of flag names, which are self-describing and safe to evolve, and in the database I would use names or boolean columns unless size really demands a bitmask. The migration would be expand and contract.

Add the new form, have producers dual-write, backfill old rows, resolving the collided bit by which service wrote the row and sending anything unclear to review, let consumers read both forms through one shared reader, reconcile the two daily, and only then stop writing and drop the old int.

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 documentation below. 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 "Several services encode status flags as bit positions in an int column shared through a...": https://scrivio-interview-library.netlify.app/java/operators-and-control-flow/f3a2199dce/