Changing a schema you cannot migrate
For a bit over two years I worked on Rule Management in Kibana's Security Solution, Elastic's security information and event management product. It collects a company's security logs and raises alerts on them. The unit of work is a detection rule, a saved query plus a schedule: Kibana runs it against your data every few minutes and writes an alert document whenever it matches. Customers write their own, and Elastic also ships several hundred, written by its threat research team and delivered as a versioned package that Kibana downloads. Those are the prebuilt rules.
Prebuilt rules used to be read-only. You could clone one and edit the clone, but the clone never received Elastic's updates again, so you were on your own with it forever. In 2024 we set out to remove that trade-off, and the first thing in the way was a boolean.
Every rule carried immutable. A prebuilt rule had immutable: true, a custom rule immutable: false. The moment we let people edit prebuilt rules, the field's name was a lie. That is the easy half of the story. The hard half is that Kibana is software customers install and run themselves, on versions they upgrade to whenever they choose. There is no fleet of installations I can see, no console where I watch a backfill go from 40% to 100%. Whatever I did to that field had to work without a migration ever finishing.
A boolean that was three questions
The first thing we settled was what should replace it. immutable answered one question, and the feature needed three, which is one more than a boolean can carry. We introduced a top-level rule_source field, a discriminated union, meaning the type field decides which other fields exist:
type RuleSource =
| { type: 'internal' }
| {
type: 'external';
is_customized: boolean;
source_updated_at?: string;
};
internal means the rule was created inside Kibana. external means it came from somewhere else, and Elastic's own package is one case of that, which left the door open for customers pointing at their own rule repositories later. source_updated_at records when the version the rule came from was published.
is_customized is what the old boolean could not express. It starts false when a prebuilt rule is installed and flips to true when any field diverges from the version Elastic shipped. It flips back to false if the user reverts, because the question it answers is "does this differ from the shipped version right now", not "has anyone ever edited this". Adding a Slack notification to a rule, we decided, does not count, since the detection logic is untouched.
The new field carries strictly more information than the old one, and the old one is derivable from it. immutable: true is exactly rule_source.type === 'external'. That derivability is what made the rest possible.
The whole design went into an RFC, #171856, opened in November 2023 and merged in April 2024 with 354 review comments. Of its 2,800 lines, about 400 are only about how to get existing rules from one shape to the other.
Why the normal migration was closed to us
Kibana rules are stored as saved objects, documents in Elasticsearch system indices with a registered type and a schema. Kibana has real machinery for changing those schemas: historically you described the change as a migration that ran when the customer upgraded Kibana to a specific version. My first assumption was that I would write one of those and move on.
I was wrong on two counts, and the second one is the interesting one.
The first was a matter of timing. The team that owned the old mechanism had stopped supporting it. Its replacement, the Model Version API, works on Elastic's serverless offering, where there is no discrete upgrade moment to hang a migration on. It does not support migrating encrypted saved objects, and detection rules are encrypted, because a rule carries the API key it executes under. So the supported path did not cover our documents, and the unsupported path was on its way out.
The second reason is the one worth generalizing. Even with working migration machinery, in self-hosted software a migration is not an event you observe. You ship it and then you wait, for years, while customers upgrade on their own schedules, and some of them never do. Code that says "by version X everything has the new shape" is a claim about other people's operations teams, not a claim about your data.
The same constraint had caught me before. A year earlier we stopped writing a saved object type we had introduced, and found we could not delete it, because Kibana's migration machinery fails on startup when it meets a type it does not recognize. The move was to add it to a core allow-list of intentionally unused types, which #147035 does, so every installation that still holds those documents keeps booting.
So we went the other way. The rule migrates itself, the next time anything writes to it.
Migrating on write, normalizing on read
The two halves do different jobs, and the split is the part I would carry to another codebase unchanged.
Migrating on write changes Elasticsearch. Every endpoint that writes a rule computes rule_source from whatever the rule currently has, writes it, and removes immutable from the stored document. In the RFC's endpoint table, that came to eight write endpoints: update, patch, their two bulk variants, bulk edit, import, the rule upgrade endpoint, and a legacy install endpoint we still had to support. Read endpoints do not write. A rule that nobody edits, nobody upgrades and nobody imports stays in the old shape indefinitely, and that is a supported state rather than a backlog.
Normalizing on read is the part that makes the first half tolerable. Every endpoint that returns a rule computes both fields in memory before responding, whatever is actually stored. Here it is, simplified from the real one, which carries more fields:
/** A rule as it currently sits in storage: migrated, not migrated, or written before either field existed. */
interface StoredRule {
rule_source?: RuleSource;
immutable?: boolean;
}
function normalizeRuleSourceOnRead(storedRule: StoredRule) {
if (storedRule.rule_source) {
return {
rule_source: storedRule.rule_source,
immutable: storedRule.rule_source.type === 'external',
};
}
// Nothing has written this rule since the change, so derive both fields from the old boolean.
if (storedRule.immutable) {
return { rule_source: { type: 'external', is_customized: false }, immutable: true };
}
return { rule_source: { type: 'internal' }, immutable: false };
}
That function is fourteen lines and it is why nobody outside the storage layer noticed the rollout. From the day the feature shipped, every API response carried both fields with consistent values, on every installation, whatever had actually moved in storage. Clients never see the mixed state. Only the storage layer does.
One detail is easy to miss until it bites. In the internal schema, both immutable and ruleSource had to be optional, because rule parameters are validated against that schema every time a rule executes and every time the alerting framework updates one. Declare the new field required and every unmigrated rule in the world starts failing validation at runtime, which is far worse than a slow migration.
We got lucky on mappings. Rule parameters live under a flattened field in the alert saved object's Elasticsearch mapping, which means Elasticsearch indexes the subtree as opaque key-value pairs instead of mapping each key, so adding a nested object inside it needed no mapping change. Had rule_source needed its own mapping, this story would be longer.
Every query gets written twice
Kibana filtered on immutable in a lot of places, using Kibana Query Language expressions against the saved object: counting how many prebuilt rules a user has installed, listing which ones are still available to install, the coverage table that maps rules onto MITRE ATT&CK, the filter bar on the rules page. Every one had to become a query that prefers params.rule_source.type: external and falls back to params.immutable: true when the first field does not exist. That is the bill for all of this, and I do not think there is a way to avoid paying it.
That fallback is permanent in practice. There is no date after which you can delete the second half of the condition, because there is no date by which you know every rule has been written at least once. A rule installed in 2021 and never touched since is still sitting there in the old shape.
The 2023 bug that taught me to take this seriously came from the same boolean. When a prebuilt rule changed its type between versions, the upgrade could not update it in place, so the code deleted the rule and created a new one. The create path did not pass immutable, so the rule came back as false. The query that fetched installed rules filtered on immutable: true, so it no longer saw that rule, so the install catalog offered it as available, and installing it produced a duplicate. One missing argument on one internal call, three layers away from the symptom the user reported. The fix, #161331, was 25 lines.
A flag written by many code paths and read by many filters, with nothing checking that writers and readers agree, will eventually disagree. So the migration spec has a row for it. A rule with rule_source.type: 'external' and immutable: false is an impossible combination, and the write-path migration corrects it rather than preserving it. The invariant is stated once, and the write path enforces it every time it touches a document.
Deleting the old field was never on the table
immutable is in the public API response. People have scripts that read it and automation that branches on it. Removing it is a breaking change for every one of them, and in self-hosted software you cannot survey who those people are.
So we deprecated it and kept returning it, forever as far as any user needs to care. We announced that in the documentation of every endpoint that returns a rule, as a deprecation warning in the OpenAPI schema, and as a custom response header naming the replacement field. The stated deprecation period was at least 24 months, which I would describe as an opening bid rather than a deadline.
The subtle part is that immutable did not keep its meaning. It used to mean "this rule is prebuilt and you cannot edit it". After the feature shipped it means "this rule is prebuilt", and you can edit it. Anyone reading the field to decide whether to show an edit button now behaves differently even though the value never changed. We could not prevent that, only document it.
I have done the opposite elsewhere. In hosted software, when the readers and writers are all mine and I can watch the rollout, the cheaper answer is often to define the new field forward-only, write it on new records, and never backfill. Same class of problem, different constraint, different correct answer. What decides it is whether you can see the installations.
All of this was groundwork for a three-way merge between the version Elastic shipped, the version the customer has, and the new version Elastic is publishing (#191439). That is a separate story and a harder one, but none of it could start until a rule could say, on any installation, where it came from and whether its owner had changed it.
My work here ended in December 2024 and the feature shipped across Kibana 8.16 to 9.0. It is all public, so if details have moved since, trust the code. I do not expect the shape of the answer to have moved. Write the read path first so the API is consistent from the day the feature ships, and let writes do the persistence whenever they happen to happen. The old field stays, answering the one question it can still answer, for as long as anyone might be reading it.