Keel by ChimpLabs
Jump to page or section Esc
Core Concepts

Event Versioning & Upcasting

Evolve immutable Topic schemas with additive fields, chained read-time upcasters, and Expand-Contract without consumer downtime.

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:

  1. Stamps the current schemaVersion on every new publish using the x-keel-schema-version header (SCHEMA_VERSION_HEADER).
  2. Treats legacy events written without a version header as v1.
  3. Runs matching upcaster steps in order from from to to before Schema.decodeUnknownEffect. Every consumer reading the topic (durableStream and liveStream) 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 _tag or type (with an optional "*" fallback) for union schemas. Keel checks at construction time that every step moves forward (to > from >= 1) with no duplicate from versions, 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):

  1. Introduce a new topic subject (orders.placed.v2) with the new payload schema.
  2. During the migration window, either dual-publish from the producer or run a lightweight translator consumer over OrderPlacedV1Topic using durableStream that transforms and publishes to OrderPlacedV2Topic.
  3. Once all downstream consumers move to orders.placed.v2, retire the v1 topic and translator.