In event-driven systems, historical events stored in Topics are immutable facts. You never UPDATE past events in place. Keel provides built-in schema versioning and read-time upcasting so new consumers can replay historical events while staying strictly typed against the latest schema.
When to use each strategy
| Change type | Recommended strategy | Built-in Keel support |
|---|---|---|
| Adding an optional field with a default | Additive schema evolution | k.optional(...) or Schema.optionalWith |
| Renaming a field, changing types or units (for example, float to cents), or backfilling a required field | Chained read-time upcasters (v1 to v2 to v3) |
schemaVersion and upcasters on Topic (SCHEMA_VERSION_HEADER) |
| Breaking semantic change between services | Expand-Contract with a new subject (v2) and dual-publish or translator consumer |
Subscribe to v1 via durableStream and publish to v2 |
1. Additive changes (non-breaking)
When you only add optional fields, no version bump or upcaster is needed:
export const OrderPlacedTopic = k.topic(
"acme.orders.placed",
k.object({
orderId: k.string(),
totalCents: k.int(),
// Added later — older events without `couponCode` still decode cleanly
couponCode: k.optional(k.string()),
})
)
Old consumers ignore unknown fields, and new consumers handle couponCode ?? undefined for historical events.
2. Chained read-time upcasting (schemaVersion and upcasters)
For structural changes such as renaming fields, converting floating-point dollars to integer cents, or adding required fields with backfilled defaults, keep the historical log immutable. Declare a chain of small pure upcasters from v1 to v2 to v3.
When reading or writing versioned events on a Topic, Keel runs three steps:
- Stamps the current
schemaVersionon every new publish using thex-keel-schema-versionheader (SCHEMA_VERSION_HEADER). - Treats legacy events written without a version header as
v1. - Runs matching upcaster steps in order from
fromtotobeforeSchema.decodeUnknownEffect. Every consumer reading the topic (durableStreamandliveStream) receives only the latest domain type.
import { k } from "@chimp-labs/keel/sdk"
export const OrderPlacedTopic = k.topic(
"acme.orders.placed",
k.object({
orderId: k.string(),
totalCents: k.int(),
currency: k.string(),
}),
{
schemaVersion: 3,
upcasters: [
{ from: 1, to: 2, upcast: (e) => ({ ...e, currency: "BRL" }) },
{
from: 2,
to: 3,
upcast: ({ total, ...rest }) => ({
...rest,
totalCents: Math.round(total * 100),
}),
},
],
}
)
[!TIP] Pass upcasters as an array (
EventUpcasterStep[]) for single-event topics, or as a record keyed by_tagortype(with an optional"*"fallback) for union schemas. Keel checks at construction time that every step moves forward (to > from >= 1) with no duplicatefromversions, which stops infinite loops.
3. Breaking semantic changes with Expand-Contract
Use Expand-Contract when an event’s business meaning changes and no 1:1 upcast exists (for example, splitting one coarse event into two domain events or migrating a topic shared by independent teams):
- Introduce a new topic subject (
orders.placed.v2) with the new payload schema. - During the migration window, either dual-publish from the producer or run a lightweight translator consumer over
OrderPlacedV1TopicusingdurableStreamthat transforms and publishes toOrderPlacedV2Topic. - Once all downstream consumers move to
orders.placed.v2, retire thev1topic and translator.