However, to keep it simple, let's have:
status: one of ['draft', 'under_review', 'approved', 'rejected', 'active', 'frozen']
But note: an approved CI might be in a ramp (rollout 50%) and then we increase to 100% and then set to active? Or when rollout reaches 100%, we automatically set to active?
Alternatively, we can have:
- draft: not yet submitted for review.
- under_review: submitted, waiting for approvals.
- approved: passed review, but not yet rolled out (rollout 0) or in ramp.
- active: rollout 100% and not in freeze.
And we can have a separate flag for freeze? Actually, the freeze is a system-wide or environment-wide setting that prevents activation or changes.
Let's define:
ConfigurationItem:
id, key, value, environment, version,
approval_status: ['draft', 'under_review', 'approved', 'rejected'],
rollout_percentage: 0-100,
is_active: boolean (derived from rollout_percentage? or set when rollout_percentage=100 and not frozen?).
But we also have freeze periods. So even if rollout_percentage=100, if we are in a freeze, we might not serve the config? Or we just don't allow changes during freeze.
Actually, the freeze might prevent any changes to rollout_percentage and also prevent new CIs from being approved? Or just prevent activation?
Let's define:
Freeze: a period (start_time, end_time) for an environment. During a freeze:
- No configuration changes (create, update, delete) are allowed.
- Existing approved CIs cannot have their rollout_percentage changed (so if they are in a ramp, they are frozen at the current percentage).
So we need to check for freeze when:
- Submitting a CI for review? (maybe we allow drafting but not submission? Or we block any write during freeze)
- Changing rollout_percentage on an approved CI.
Let's decide: during freeze, we block all write operations on CIs (create, update, delete, change rollout, change approval status).
So the steps:
Step 1: Create a CI in draft.
Step 2: Submit for review -> status becomes 'under_review'. (This is a write, so if in freeze, it's blocked.)
Step 3: Reviewers approve. Once enough approvals, set approval_status to 'approved'. (Again, write, so blocked in freeze.)
Step 4: Set rollout_percentage (if not set already). We can set it during creation or after approval? Let's say we set it after approval. (Write, blocked in freeze.)
Step 5: Gradually increase rollout_percentage. (Write, blocked in freeze.)
So during freeze, we cannot do steps 2,3,4,5. We can still read.
Now, how do we determine which config is served to a user?
For a given environment and user (or request), we need to fetch all CIs that are:
- approval_status: 'approved'
- rollout_percentage: >0 (and we use a consistent method to decide if the user gets the new value or the old one)
- and also we must consider that there might be multiple CIs and we need to decide which one to use if there are multiple with the same key?
Actually, we should have unique keys per environment. So we can have:
For a given environment and key, we have one active CI (the one with the highest version that is approved and rollout>0 and we are in the rollout percentage).
But what if we have two CIs for the same key? That shouldn't happen. We should enforce that at the database level: (environment, key) unique.
So the active CI for a key in an environment is the one with the highest version that is approved and rollout>0 and the user is in the rollout percentage.
However, we might have a situation where we are rolling out a new CI for a key and the old CI is still active for some users? We need to version and replace.
Let's think: when we create a new CI for an existing key, we are essentially proposing a new value. The old CI might be at 100% and active. We want to ramp the new one in.
So we have:
CI1: key='color', value='blue', version=1, approval_status='approved', rollout_percentage=100, environment='prod'
CI2: key='color', value='red', version=2, approval_status='approved', rollout_percentage=50, environment='prod'
How do we serve?
We need to decide based on version and rollout. The idea is that the higher version is the intended new value, but we only serve it to a percentage of users.
So for a user:
- If the user is in the 50% for version2, we serve 'red'.
- Otherwise, we serve the highest version that is approved and rollout>0 and the user is not in the new rollout? But wait, version1 is at 100% and version2 is at 50%. So for the 50% that get version2, we serve red, and for the other 50%, we serve blue.
But what if we then increase version2 to 100%? Then we want all users to get red. And we can set version1's rollout to 0? Or we can just leave it? But we don't want two active CIs for the same key.
So we need a way to "deactivate" the old CI when the new one reaches 100%. Alternatively, we can have a rule: for a given key, the active CI is the one with the highest version that is approved and rollout>0 and the user is in the rollout set? But then if we have two CIs with the same key and both are approved and both have rollout>0, we have a conflict.
So we must ensure that for a given key, only one CI is active (rollout>0) at a time? Or we can have multiple but we need a way to resolve.
Let's define:
For a given environment and key, we consider the CI with the highest version that is approved and rollout>0. But then if we have two:
version1: rollout 100%
version2: rollout 50%
Then for a user in the 50% of version2, we use version2. For the other 50%, we use version1? But version1 is lower version and we want to phase it out.
Alternatively, we can have:
The system should only have one active CI per key (rollout>0) at a time. When we create a new CI for an existing key, we set the old CI's rollout to 0? But then we lose the ability to roll back quickly.
Another approach: we allow multiple active CIs for the same key but we serve the one with the highest version that the user is eligible for (based on rollout percentage). Then, when we increase the rollout of the new CI, we are effectively moving users from the old to the new.
But then we have to track two rollouts for the same key? And what if we want to roll back? We can set the new CI's rollout to 0 and then the old CI's rollout to 100% again.
So let's allow multiple active CIs for the same key, but we define the serving rule:
For a given key, we have a list of CIs (for that environment) that are approved and rollout>0, sorted by version (descending). Then for a user, we check the highest version first: if the user is in the rollout set for that version, we use it. Otherwise, we move to the next version.
But note: if we have two CIs with rollouts that are not complementary (e.g., 50% and 50% but not the same 50% of users), then we might have users that are in both? We don't want that.
So we must design the rollout mechanism such that for a given key, the rollouts are mutually exclusive and cover 0-100%? Actually, we want to move from one to the other.
Let's say we have:
version1: rollout 100% (so all users get it)
Now we want to roll out version2 to 50% of users.
We set version2: rollout 50%. Then for a user, if they are in the 50% of version2, they get version2. Otherwise, they get version1.
Then we increase version2 to 100%, and then we can set version1 to 0% (or leave it, but then if we have a bug, we can roll back by setting version2 to 0 and version1 to 100).
But note: if we set version1 to 0%, then no one gets version1, and version2 at 100% gets everyone. That's fine.
However, if we want to roll back, we set version2 to 0% and version1 to 100%. But what if we have version3 in the future? We need to manage the rollouts carefully.
Alternatively, we can have a single active CI per key and when we create a new one, we set the old one's rollout to 0 automatically? But then we lose the ability to quickly roll back.
Let's stick with the multiple active CIs and the serving rule:
For a key, we have a list of CIs (approved, rollout>0) sorted by version descending.
For a user, we iterate from highest version to lowest:
- if the user is in the rollout set for that CI, then we use that CI's value and break.
- if not, we continue to the next.
But note: if we have two CIs with rollouts that are not disjoint, we might have a user in both. We must ensure that the rollouts are disjoint? Actually, we want the higher version to take precedence.
Example:
version3: rollout 30% (users A)
version2: rollout 50% (users B)
version1: rollout 100% (users C)
Then the total coverage is at least 30% (if A, B, C are disjoint) but if they overlap, then the higher version wins.
We can design the rollout function to be:
Let H be a hash of the user (or request) modulo 100. Then for a CI with rollout percentage P, the user is in the rollout if H < P.
But then if we have multiple CIs, we want the highest version that the user is eligible for.
So:
user_hash = hash(user_id) % 100
for ci in sorted_by_version_desc:
if user_hash < ci.rollout_percentage:
return ci.value
This way, if a user is eligible for multiple (because rollout percentages are increasing with version? not necessarily), we take the highest version.
But note: if we have:
version1: rollout 100% -> every user_hash < 100, so every user gets version1.
version2: rollout 50% -> user_hash < 50, then we return version2 for those users.
So for user_hash=25:
version2: 25<50 -> return version2.
version1: not reached because we break.
For user_hash=75:
version2: 75<50? no -> continue.
version1: 75<100 -> yes -> return version1.
This works.
But what if we have:
version1: rollout 50%
version2: rollout 100%
Then for user_hash=25:
version2: 25<100 -> yes -> return version2.
So version1 is never used? That's because version2 covers 100% and is higher version.
So we don't need to set version1 to 0% when version2 is at 100%? Actually, we can leave it, but it's redundant.
However, if we want to roll back, we set version2 to 0% and then version1 (which is at 50%) will be used for user_hash<50, but what about the other 50%? They get nothing? We need to have a fallback.
We should have a default value? Or we ensure that the rollouts cover 100%? Actually, we don't require 100% coverage. We can have gaps.
But in practice, we want to eventually have 100% for the latest version. So we can have:
When we create a new CI for an existing key, we set the old CI's rollout to 0? Or we leave it and adjust the new one's rollout to be the complement? That's complicated.
Alternatively, we can have a rule: for a given key, the sum of rollouts of all approved CIs must be <=100%? Not necessarily, because we might want to test multiple variants? Actually, we are talking about a single key with one value. So we want exactly one value per user for that key.
So we need to ensure that for a key, the rollouts are disjoint? Not necessarily, because we want the highest version to win. But if we have two CIs with rollouts that overlap, the higher version will win in the overlap region.
Example:
version1: rollout 80% (say, user_hash < 80)
version2: rollout 50% (user_hash < 50)
Then:
user_hash in [0,50): version2
user_hash in [50,80): version1
user_hash in [80,100): none? We don't have a value.
So we need to ensure that the lowest version (the oldest) covers the remaining percentage? Or we have a default.
Let's have a default value (maybe from a previous version or a system default). But we don't model that in the CI.
Alternatively, we can require that the lowest version (by version) for a key must have rollout 100%? Then when we introduce a new version, we set the old one to 0? That's one way.
But then we lose the ability to have a quick rollback because we set the old one to 0. However, we can set the old one to 100% again if we roll back.
Let's change the rule:
When we create a new CI for a key that already has an active CI (rollout>0), we set the old CI's rollout to 0? And then we set the new CI's rollout to the desired percentage (maybe starting at 0 and then ramp up).
This way, we only have one active CI per key at a time.
Steps:
- Existing active CI (version N) for key K with rollout 100%.
- Create new CI (version N+1) for key K, set its rollout to 0% (or a small percentage) and set the old CI's rollout to 0%? But then we have a period where no one gets the key? We don't want that.
Instead, we can set the new CI's rollout to X% and leave the old one at 100%? Then we have two active, but we want to gradually move users.
But with the serving rule (highest version first), if we set new CI to X% and old to 100%, then:
For user_hash in [0, X): new CI (because higher version and user_hash < X)
For user_hash in [X, 100): old CI (because new CI condition fails, then old CI condition: user_hash < 100 -> always true)
So we are effectively moving X% of users to the new version, and the rest remain on the old.
Then we can increase X until 100, and then we can set the old CI's rollout to 0? Or we can leave it, but then it's not used because the new CI covers 100% and is higher version.
So we don't need to set the old CI's rollout to 0 until we delete it? But we might want to keep the old CI for rollback.
So let's allow multiple active CIs for the same key, and we use the serving rule above.
But we must be cautious: if we have more than two, we might have gaps? Let's see:
version3: rollout 20%
version2: rollout 50%
version1: rollout 100%
Then:
[0,20): version3
[20,50): version2? Wait, version2 condition: user_hash<50 -> true for [0,50). But we already have version3 for [0,20). Since we check version3 first, and for [0,20) we return version3, then for [20,50) we check version3 (false) then version2 (true) -> version2.
[50,100): version1.
So we have full coverage? Not necessarily:
What if we have:
version3: rollout 20%
version2: rollout 30%
version1: rollout 100%
Then:
[0,20): version3
[20,30): version2? Actually, for user_hash=25:
version3: 25<20? no.
version2: 25<30? yes -> version2.
[30,100): version1.
So we have coverage for [0,100). Good.
But what if we have:
version3: rollout 20%
version2: rollout 10%
version1: rollout 100%
Then:
[0,10): version3? because version3: user_hash<20 -> true for [0,20) but we have version3 first, so [0,20) gets version3? Wait, version3 is higher version than version2? Yes, version3>version2.
So:
[0,20): version3 (because version3 condition: user_hash<20 -> true, so we return version3)
[20,100): version2? No, for user_hash=25:
version3: 25<20? no.
version2: 25<10? no.
version1: 25<100? yes -> version1.
So we have:
[0,20): version3
[20,100): version1
But version2 is never used? Because version3 covers [0,20) and version1 covers [20,100). So version2's 10% is overlapped by version3? Actually, version2's condition is user_hash<10, which is a subset of version3's condition (user_hash<20). So version2 is completely shadowed by version3.
This is acceptable? It's a way to phase out version2? But we set version2 to 10% and version3 to 20%, and then we realize version3 is better and we set it to 20% and leave version2? Then version2 is not used for any user because version3 covers its entire rollout.
So we need to be careful when setting multiple active CIs for the same key.
Alternatively, we can enforce that for a given key, the rollouts must be non-overlapping? But that's hard to enforce because we might want to overlap for testing?
Actually, the intended use is:
We have one active CI (the one we are rolling out) and the previous one (which we are rolling out from). So we have at most two active for a key? But we can have more if we do multiple experiments? Not typical.
Let's stick to the serving rule and allow multiple, but we must design the rollout strategy carefully.
We can also have a rule: when we create a new CI for a key, we set the old CI's rollout to 0? Then we only have one active. But then we lose the ability to have two active at the same time (for gradual rollout). So we need two active during a ramp.
So we allow multiple, and we rely on the version ordering and the rollout percentages to define the migration.
But note: if we have two active CIs for the same key, and we want to move from version1 to version2, we set version2 to X% and leave version1 at 100%. Then we increase X to 100. At that point, version1 is still at 100% but version2 is at 100% and higher version, so everyone gets version2. Then we can set version1 to 0% (or delete it).
So the process:
Step 1: Create version2 (draft) and set value.
Step 2: Submit and get approved.
Step 3: Set version2 rollout to X% (e.g., 10%).
Step 4: Gradually increase X to 100%.
Step 5: Set version1 rollout to 0% (or delete version1).
But note: during the ramp, version1 is still at 100% and version2 at X%. So we have two active.
This works.
So we don't require that only one active per key. We just need to manage the rollouts.
Now, back to the data model:
We'll have:
Table: configuration_items
id: primary key
key: string
value: JSON (or text, but we'll use JSON for flexibility)
environment: string
version: integer (auto-increment per key? or global? We want per key and environment? Let's have per (key, environment) so that we can have different versions for different environments. But we also want to track the version history for a key in an environment.
So version should be unique per (key, environment). We can have a composite unique index on (key, environment, version) but version should be increasing for a given key and environment.
Alternatively, we can have a separate version for each CI (global) and then we link by environment and key? But then we might have two CIs with the same key and environment but different versions? That's what we want.
So:
Unique constraint on (key, environment, version) is not necessary because version is unique per (key, environment). We can have:
(key, environment) -> multiple versions.
But we want to enforce that for a given (key, environment), the versions are increasing and we don't have two with the same version.
So we can have a sequence per (key, environment) or we can use a global auto-increment and then when we create a new version for a key in an environment, we set the version to the max version for that key and environment + 1.
Let's do:
When creating a new CI for a key in an environment, we set version = (select max(version) from configuration_items where key=:key and environment=:environment) + 1.
Then we have:
Table: configuration_items
id: bigserial
key: varchar
environment: varchar
version: integer
value: jsonb
approval_status: varchar (draft, under_review, approved, rejected)
rollout_percentage: integer (0-100)
created_by, created_at, updated_by, updated_at
We also need a table for reviews:
Table: reviews
id: bigserial
configuration_item_id: foreign key to configuration_items.id
reviewer_id: string (or user id)
approved: boolean
comment: text
created_at
And we need to know how many approvals are required per environment or per key? Let's have a fixed number (e.g., 2) for now.
We can have a table for review_policies:
environment: string
required_approvals: integer
But for simplicity, we can have a global setting or per environment.
Workflow:
1. Create a new CI:
- Insert a new row with version = max_version_for_key_and_env + 1, status='draft', rollout_percentage=0 (or null? but we'll set to 0).
2. Update the CI (change value, etc.) while in draft: we can update the value and increment the version? Or we update the same row?
- We want to track changes. So we can update the same row (same version) until we submit for review? Then when we submit, we lock the version?
- Alternatively, every change creates a new version? That might be too many versions.
Let's do:
- When in draft, we can update the value without creating a new version. The version is assigned when we submit for review? Or when we create?
We want to have a version for every approved change. So:
- Create: version 1 (draft)
- Update: we can update the draft, version remains 1? Then when we submit, we keep version 1? But then if we update again, we are still on version 1? That's okay because it's not approved yet.
But then when we approve, we have version 1. Then we create a new CI (version 2) for the next change? That's the typical pattern.
So:
Step 1: Create a new change:
INSERT ... version = (select max(version) from configuration_items where key=:key and environment=:env) + 1.
status='draft'
Step 2: While draft, we can update the value (UPDATE) without changing version.
Step 3: Submit for review: set status='under_review'. We cannot update the value now until it's rejected? Or we can update and create a new version? Let's say we cannot update while under review. We must reject to go back to draft.
Step 4: Reviewers approve. We count approvals. When enough, set status='approved'.
Step 5: Set rollout_percentage (if not set). We can set it during approval or after? Let's set it after approval.
Step 6: Increase rollout_percentage gradually.
Step 7: When rollout_percentage reaches 100, we can optionally set status to 'active'? But we don't need to because we are using rollout_percentage. We can just leave it as approved and rollout=100.
But we have the freeze: during freeze, we cannot change rollout_percentage. So if we are in a ramp and we hit a freeze, we stop.
Now, the freeze:
We have a table for freezes:
id, environment, start_time, end_time, created_by, reason.
We check if the current time is within any freeze for the environment when we try to:
- submit for review (set status to under_review)
- approve (set status to approved)
- change rollout_percentage (on an approved CI)
Also, we might want to block creation of new CIs? Let's say during freeze, we block all writes (create, update, delete) on configuration_items for that environment? But we might want to allow drafting? Actually, we don't want to allow any change during freeze.
So we block:
- INSERT (new CI)
- UPDATE (any change to value, status, rollout_percentage) for CIs in that environment.
But note: we might have a freeze that is only for production, and we might want to allow changes in staging.
So we check the environment of the CI against the freeze's environment.
How to check:
When we try to write to a CI (any write), we check if there is an active freeze for that CI's environment. If yes, block the operation.
Also, when we try to create a new CI, we check the environment we are creating for.
Now, the review gate:
We have a table for review_policies:
environment: string (unique)
required_approvals: integer
When a CI is in 'under_review', we count the number of approvals in the reviews table for that CI. When the count reaches the required_approvals, we set the CI's status to 'approved'.
We can have a background job that periodically checks for CIs that have reached the required approvals and promotes them.
Alternatively, when a review is added, we check and promote immediately.
Let's design the API:
POST /configs
body: { key, environment, value }
-> creates a new CI in draft.
PUT /configs/{id}
body: { value? , rollout_percentage? }
-> if the CI is in draft, we can update value.
-> if the CI is approved, we can update rollout_percentage (but not value? We might want to update value? But if we update value, we are creating a new change? So we should not allow updating value on an approved CI. Instead, we create a new CI.
So:
For an existing key and environment, we have multiple versions. We don't update an approved CI's value. We create a new version.
So the PUT /configs/{id} for an approved CI should only allow changing rollout_percentage? And maybe we allow changing value only if the CI is in draft or rejected? Let's define:
- draft: can update value and rollout_percentage? Actually, rollout_percentage should be 0 in draft. We set rollout after approval.
So:
In draft:
- can update value.
- cannot set rollout_percentage (must be 0).
In approved:
- cannot update value.
- can update rollout_percentage (if not in freeze).
In under_review:
- cannot update anything.
In rejected:
- can update value (and then we can resubmit?).
So we need to handle the state transitions carefully.
Let's define the state machine:
draft -> under_review (submit)
under_review -> approved (when enough approvals)
under_review -> draft (reject? or we can have a reject state? Let's have rejected and then we can go back to draft? Or we can have a 'rejected' state and then we can create a new version? Actually, we might want to fix and resubmit the same version? So we can go from rejected to draft? But then we are updating the same version? That's okay.
Alternatively, we can have:
draft -> under_review -> approved
under_review -> rejected (by a reviewer) and then we can go back to draft? Or we can have a new version.
Let's keep it simple:
- draft: initial state.
- submit: goes to under_review.
- approve: when enough approvals, goes to approved.
- reject: a reviewer can reject, then it goes to rejected.
- from rejected, we can go back to draft (and then update and resubmit).
So:
draft -> under_review
under_review -> approved
under_review -> rejected
rejected -> draft
Also, we might want to withdraw from review? Then from under_review -> draft.
So:
under_review -> draft (withdraw)
Now, for an approved CI, we can only change rollout_percentage (and only if not in freeze). We cannot change value.
If we want to change the value, we create a new CI (new version) for the same key and environment.
Now, the freeze:
We have a table:
freezes:
id, environment, start_time, end_time, created_by, reason, created_at
We need to check on every write operation (create, update) if there is an active freeze for the environment.
How to check: current_timestamp between start_time and end_time.
We can have a function that checks for active freezes for an environment.
Now, the serving side:
We need a service that, given an environment and a key (and a user identifier), returns the value.
Steps:
1. For the given environment and key, get all CIs that are:
- approval_status = 'approved'
- rollout_percentage > 0
ordered by version DESC.
2. Compute the user's hash (e.g., from user_id) modulo 100.
3. Iterate through the CIs in order:
if user_hash < ci.rollout_percentage, then return ci.value.
4. If none matches, then we don't have a value? We can return a default? Or we can return the value from the highest version that has rollout_percentage>0 but the user is not in the rollout? That doesn't make sense.
Actually, we want to return the value from the highest version that the user is eligible for. But if the user is not eligible for any, then we don't have a value? We must have a fallback.
We can have a system default? Or we can require that the oldest active CI (lowest version) must have rollout 100%? But we don't enforce that.
Alternatively, we can change the serving rule:
We want to serve the highest version that the user is eligible for, but if the user is not eligible for any, then we serve the highest version that has rollout>0? But that might be a version that the user is not eligible for? That doesn't make sense.
Let's think: we have:
version3: rollout 20%
version2: rollout 30%
version1: rollout 100%
For a user with hash=50:
version3: 50<20? no.
version2: 50<30? no.
version1: 50<100? yes -> version1.
So it works.
What if we have:
version3: rollout 20%
version2: rollout 30%
version1: rollout 0% (or not approved)
Then for user_hash=50:
version3: no.
version2: no.
version1: no (rollout=0 or not approved) -> then we have no value.
So we must ensure that for a key, there is at least one CI with rollout 100%? Actually, we want to eventually have 100% for the latest version. But during a ramp, we might not have 100% for any version? Then some users get no value.
We can have a default value stored separately? Or we can require that the oldest active version (the one with the lowest version that is approved and rollout>0) must have rollout 100%? That would ensure full coverage.
But we don't want to enforce that because we might be doing an experiment with two variants (A and B) and we want 50% A and 50% B. Then we don't have a 100% version.
So we need a fallback. Let's have a system default value for each key? Or we can have a special CI that is always active and has rollout 100% and is the lowest version? That would be the baseline.
Alternatively, we can change the serving algorithm:
Instead of returning the first CI that the user is eligible for, we return the CI with the highest version among those that the user is eligible for? That's what we are doing.
But if the user is not eligible for any, then we return nothing? We can return a default (maybe from a configuration that is not versioned?).
Let's have a table for default values per key and environment? Or we can have a special CI with version 0 that is always active and has rollout 100% and is the baseline.
We can have:
For each key and environment, we have a baseline CI (version 0) that is always approved and rollout 100%? Then when we create a new CI, we don't change the baseline. And the serving rule will always return something because baseline is always there.
But then when we roll out a new version, we set its rollout to X% and the baseline remains at 100%. Then for a user:
If the user is in the new version's rollout, they get the new version. Otherwise, they get the baseline.
This is a common pattern.
So we can have:
- A baseline CI for each key and environment? That might be too many. Alternatively, we can have a default value stored in the configuration system that is not versioned.
Let's separate:
We have a table for configuration_schema:
key: primary key
default_value: jsonb
description: text
Then, for a given environment and key, we have a set of versioned CIs (with approval_status='approved' and rollout_percentage>0). And we also have the default.
Then the serving rule:
For a given environment and key:
Let candidate_ci = null.
For each ci in (select * from configuration_items where key=:key and environment=:env and approval_status='approved' and rollout_percentage>0 order by version desc):
if user_hash < ci.rollout_percentage:
candidate_ci = ci
break
If candidate_ci is not null, return candidate_ci.value.
Else, return the default_value from configuration_schema for that key.
But note: we might have multiple environments and the default might be environment-specific? Let's have:
Table: configuration_defaults
environment: string
key: string
default_value: jsonb
Then we can have environment-specific defaults.
Alternatively, we can store the default in the configuration_schema and then override per environment? Let's keep it simple and have:
Table: configuration_defaults
environment: string
key: string
value: jsonb
And we require that for every key, there is a default for every environment? Or we can have a global default? We can have:
When fetching for an environment, we look for a default for that environment and key. If not found, we look for a default with environment='global'? Or we require that every environment has a default.
Let's require that every environment has a default for every key that we use? But we might not know all keys in advance.
Alternatively, we can have:
The configuration_schema table defines the keys and their default value (global). Then per environment, we can override? But we already have versioned CIs per environment.
Actually, the versioned CIs are per environment. So we can have:
For a given environment and key:
- First, look for an approved CI with rollout>0 that the user is eligible for (using the highest version first).
- If none, then use the default value for that key in that environment? Or use the global default.
Let's have:
Table: configuration_keys
key: primary key
global_default: jsonb
description: text
Table: configuration_environment_defaults
environment: string
key: string (foreign key to configuration_keys.key)
default_value: jsonb
Then when fetching:
Step 1: get the user_hash.
Step 2: for the given environment and key, get the list of approved CIs with rollout>0, ordered by version desc.
Step 3: iterate and find the first where user_hash < rollout_percentage.
Step 4: if found, return that CI's value.
Step 5: else, check if there is an environment-specific default (in configuration_environment_defaults for that environment and key). If yes, return that.
Step 6: else, return the global default from configuration_keys.
This gives us flexibility.
Now, let's summarize the tables:
1. configuration_keys:
key (PK), global_default, description
2. configuration_environment_defaults:
environment, key (composite PK), default_value
3. configuration_items:
id (PK)
key (FK to configuration_keys.key)
environment
version (integer, unique per (key, environment))
value (jsonb)
approval_status: enum('draft','under_review','approved','rejected')
rollout_percentage: integer (0-100)
created_by, created_at, updated_by, updated_at
Unique constraint on (key, environment, version) to ensure version uniqueness per key and environment.
4. reviews:
id (PK)
configuration_item_id (FK to configuration_items.id)
reviewer_id
approved: boolean
comment: text
created_at
5. review_policies:
environment (PK)
required_approvals: integer
6. freezes:
id (PK)
environment
start_time
end_time
created_by
reason
created_at
Workflows:
Create a new configuration change:
1. Check if there is an active freeze for the environment. If yes, abort.
2. Insert a new configuration_item:
version = (select coalesce(max(version),0) from configuration_items where key=:key and environment=:env) + 1
approval_status='draft'
rollout_percentage=0
value=:value
Update a configuration_item (while in draft or rejected):
1. Check freeze for the environment.
2. Update the value (and maybe other fields? but not version, not approval_status, not rollout_percentage? We can allow updating rollout_percentage in draft? But we set it to 0 and then we set it after approval. So we can disallow changing rollout_percentage in draft.
Submit for review:
1. Check freeze? We block during freeze, so we check.
2. Update the configuration_item: set approval_status='under_review'.
Add a review:
1. Check if the CI is in 'under_review'. If not, abort.
2. Insert a review record.
3. Count the number of approvals for this CI. If it reaches the required_approvals for the environment (from review_policies), then update the CI to approval_status='approved'.
Change rollout_percentage:
1. Check if the CI is in 'approved'. If not, abort.
2. Check freeze for the environment. If frozen, abort.
3. Update rollout_percentage (must be between 0 and 100).
Withdraw from review:
1. Check if the CI is in 'under_review'. If yes, set approval_status='draft'.
Reject a review:
We don't have a direct reject. Instead, a reviewer can add a review with approved=false. But that doesn't change the state. We need a way to reject.
We can have:
When adding a review, if the review is not approved, we don't change the CI state. But then we need a separate action to reject? Or we can have a threshold of negative reviews? Let's keep it simple: we only use approvals. If we have enough approvals, we approve. Otherwise, we stay in under_review. And we allow withdrawing.
Alternatively, we can have:
- Any reviewer can reject, which sets the CI to 'rejected'. Then we can resubmit from rejected to draft? That's a different flow.
Let's do:
We have two ways to change the state:
- Submit: draft -> under_review
- Withdraw: under_review -> draft
- Approve: when enough approvals, under_review -> approved (automatic)
- Reject: a reviewer can reject, which sets under_review -> rejected. Then from rejected, we can go to draft (by the owner?).
So we need an endpoint to reject a CI (by a reviewer). Then we set the CI's status to 'rejected'.
And then we can have an endpoint to resubmit from rejected to draft? Or we can allow the owner to update the draft and then resubmit? Actually, from rejected, we can set back to draft (by the owner?).
Let's have:
Reject:
- only by a reviewer (who hasn't reviewed yet? or any reviewer?).
- sets CI status to 'rejected'.
Then, the owner can update the CI (while in rejected? or we require going to draft first?).
We can allow:
rejected -> draft (by the owner? or by a reviewer? Let's allow the owner to resubmit? Actually, after reject, the owner can fix and then resubmit. So we need a way to go from rejected to draft? Or we can have:
rejected -> draft (by the owner) and then draft -> under_review again.
Alternatively, we can have:
When rejected, the CI is in rejected state. The owner can update the value (and then we are still in rejected? or we automatically go to draft?).
Let's keep:
rejected: we can update the value (and then we remain in rejected). Then we can resubmit (set to under_review).
So:
rejected -> (update value) -> still rejected.
then: submit -> under_review.
But then we have to track that the value changed after reject? That's okay.
So:
Steps for rejection:
1. A reviewer adds a review with approved=false? Or we have a separate reject action? Let's have:
- A reviewer can add a review (approved or not). But the state change to rejected is separate? We can have:
When a reviewer adds a review with approved=false, we don't change the state. We just record the review.
Then, we can have a separate endpoint for a reviewer to reject the CI (which sets the status to 'rejected').
And then, any reviewer can approve (which counts towards the required approvals) or reject (which sets the status to rejected).
But then if we have multiple reviewers, one reject might set to rejected, but others might approve? We need to define:
Let's say:
- The CI is in under_review.
- Reviewers can add reviews (approve or not).
- If any reviewer rejects (explicitly), then the CI is immediately rejected? Or we wait for enough rejections?
Alternatively, we can have:
- The CI is approved when the number of approvals >= required_approvals.
- The CI is rejected when the number of rejections >= some threshold? Or when a reviewer with special power rejects?
This is getting complex.
Let's simplify:
We only track approvals. We don't track rejections. The CI stays in under_review until it gets enough approvals. If it doesn't, it stays there. We can allow the owner to withdraw (under_review->draft) and then fix and resubmit.
So we don't have a reject state. We only have draft, under_review, approved.
And we allow withdraw.
So:
- draft -> under_review (submit)
- under_review -> draft (withdraw)
- under_review -> approved (auto when approvals >= required)
And we don't have reject.
This is simpler.
But what if a reviewer finds a problem and wants to block? They can just not approve. And the owner can withdraw and fix.
So we don't need a reject state.
Let's go with that.
Now, the freeze:
We check for active freezes on every write operation (create, update, submit, change rollout).
Serving:
We have an endpoint:
GET /configs/{key}?environment=:env&user_id=:user_id
Steps:
1. Get the user_hash = hash(user_id) % 100.
2. For the given environment and key, get all configuration_items that are:
approval_status='approved' and rollout_percentage>0
order by version desc.
3. For each ci in that list:
if user_hash < ci.rollout_percentage, return ci.value.
4. If none, then get the default for that environment and key (from configuration_environment_defaults, or if not exists, from configuration_keys.global_default).
Now, we need to consider:
- What if the environment is not in the configuration_environment_defaults? Then we use the global default.
- We might want to cache the configuration for a user? But that's an implementation detail.
This is a basic design. We can extend with more features.
Let's write the SQL for the tables:
Note: We are using PostgreSQL.
Table: configuration_keys
key varchar primary key,
global_default jsonb not null,
description text
Table: configuration_environment_defaults
environment varchar,
key varchar references configuration_keys(key),
default_value jsonb not null,
primary key (environment, key)
Table: configuration_items
id bigserial primary key,
key varchar references configuration_keys(key),
environment varchar,
version integer not null,
value jsonb not null,
approval_status varchar not null check (approval_status in ('draft','under_review','approved')),
rollout_percentage integer not null check (rollout_percentage between 0 and 100),
created_by varchar not null,
created_at timestamp not null default now(),
updated_by varchar not null,
updated_at timestamp not null default now(),
unique (key, environment, version)
Table: reviews
id bigserial primary key,
configuration_item_id bigint references configuration_items(id),
reviewer_id varchar not null,
approved boolean not null,
comment text,
created_at timestamp not null default now(),
unique (configuration_item_id, reviewer_id) -- one review per reviewer per CI? Or can they change their mind? We can allow multiple reviews from the same reviewer? But then we count only the latest? Let's allow only one review per reviewer per CI.
Table: review_policies
environment varchar primary key,
required_approvals integer not null check (required_approvals > 0)
Table: freezes
id bigserial primary key,
environment varchar not null,
start_time timestamp not null,
end_time timestamp not null,
created_by varchar not null,
reason text,
created_at timestamp not null default now()
We'll also need indexes for performance:
On configuration_items: (key, environment, version) is already unique.
We will query by (key, environment, approval_status, rollout_percentage) for serving, so we might want an index on (key, environment, approval_status, rollout_percentage) but we also order by version desc. So maybe:
CREATE INDEX idx_configuration_items_active ON configuration_items (key, environment, approval_status, rollout_percentage, version DESC) WHERE approval_status='approved' and rollout_percentage>0;
But note: we only care about approved and rollout>0 for serving. So a partial index would be good.
Also, for the admin queries (list CIs, etc.) we might need other indexes.
Now, the application logic:
We need to enforce the state transitions and the freeze checks in the application code (or with database triggers). We'll do it in the application.
Pseudocode for creating a new CI:
function create_configuration(key, environment, value, user):
if is_frozen(environment):
throw "Environment is frozen"
version = get_next_version(key, environment)
insert into configuration_items (key, environment, version, value, approval_status, rollout_percentage, created_by, updated_by)
values (key, environment, version, value, 'draft', 0, user, user)
Pseudocode for updating a CI (only in draft or rejected? We don't have rejected, so only draft):
function update_configuration(id, new_value, user):
ci = get_configuration_item(id)
if ci.approval_status != 'draft':
throw "Can only update draft configurations"
if is_frozen(ci.environment):
throw "Environment is frozen"
update configuration_items set value=new_value, updated_by=user, updated_at=now() where id=id
Pseudocode for submit:
function submit_configuration(id, user):
ci = get_configuration_item(id)
if ci.approval_status != 'draft':
throw "Only draft configurations can be submitted"
if is_frozen(ci.environment):
throw "Environment is frozen"
update configuration_items set approval_status='under_review', updated_by=user, updated_at=now() where id=id
Pseudocode for adding a review:
function add_review(configuration_item_id, reviewer_id, approved, comment):
ci = get_configuration_item(configuration_item_id)
if ci.approval_status != 'under_review':
throw "Only configurations under review can be reviewed"
// Check if the reviewer has already reviewed? We can allow multiple but we only count the latest? Let's allow only one review per reviewer per CI.
// We have a unique constraint on (configuration_item_id, reviewer_id) so we can upsert.
insert into reviews (configuration_item_id, reviewer_id, approved, comment)
values (configuration_item_id, reviewer_id, approved, comment)
on conflict (configuration_item_id, reviewer_id) do update set approved=excluded.approved, comment=excluded.comment, created_at=now();
// Now check if we have enough approvals
required = get_required_approvals(ci.environment)
count = select count(*) from reviews where configuration_item_id=configuration_item_id and approved=true
if count >= required:
update configuration_items set approval_status='approved', updated_by=ci.updated_by, updated_at=now() where id=configuration_item_id
Pseudocode for changing rollout:
function set_rollout(configuration_item_id, rollout_percentage, user):
ci = get_configuration_item(configuration_item_id)
if ci.approval_status != 'approved':
throw "Only approved configurations can have rollout changed"
if is_frozen(ci.environment):
throw "Environment is frozen"
if rollout_percentage < 0 or rollout_percentage > 100:
throw "Invalid rollout percentage"
update configuration_items set rollout_percentage=rollout_percentage, updated_by=user, updated_at=now() where id=configuration_item_id
Pseudocode for withdraw:
function withdraw_configuration(id, user):
ci = get_configuration_item(id)
if ci.approval_status != 'under_review':
throw "Only configurations under review can be withdrawn"
if is_frozen(ci.environment):
throw "Environment is frozen"
update configuration_items set approval_status='draft', updated_by=user, updated_at=now() where id=id
Pseudocode for getting the effective configuration:
function get_configuration(key, environment, user_id):
user_hash = hash(user_id) % 100
// Get active CIs for this key and environment
active_cis = select * from configuration_items
where key=key and environment=environment and approval_status='approved' and rollout_percentage>0
order by version desc
for ci in active_cis:
if user_hash < ci.rollout_percentage:
return ci.value
// No active CI for this user, get default
default = select default_value from configuration_environment_defaults
where environment=environment and key=key
union
select global_default from configuration_keys where key=key
limit 1
if default is not null:
return default
else:
throw "No configuration found for key "+key
We might want to cache the active CIs for a key and environment and the default, and then only compute the user_hash and check the rollout.
Now, the freeze check:
function is_frozen(environment):
now = current_timestamp
select 1 from freezes where environment=environment and now between start_time and end_time limit 1
return (row exists)
This is a basic design. We might need to handle:
- Deleting a configuration item? We might not want to delete because we want history. We can mark as deleted? Or we can just leave it and not use it.
- We might want to have a way to force a configuration for a specific user (bypass the rollout)? That could be a separate feature.
- We might want to have multiple rollout strategies (not just hash-based). But we can extend the rollout_percentage to be a JSON that defines the rollout strategy? For now, we assume hash-based.
This should be a good starting point.</think>