Akashic Help

Events and codecs

Events are stored forever, and your domain model will keep changing. Akashic keeps the two apart with two models for every event:

  • The domain event is what deciders produce and evolvers consume. It is free to change with the code.

  • The persisted event is the shape on disk. It only ever changes in backward-compatible ways.

A Codec joins them. It has a Serializer, which turns the persisted model into bytes, and a Converter, which maps between the persisted and domain models. The converter is the single place where old data meets current code, so the rest of the domain never deals with history.

Converter.toPersisted

Serializer.serialize

Serializer.deserialize

Converter.toDomain

Domain event

Persisted event

Bytes

Persisted event

Domain event

Use Protobuf for the persisted model

Protobuf suits stored events well. Fields are identified by number, so renaming a property changes nothing on disk. Unknown fields are skipped and missing fields take defaults, so old and new code can read each other's events. The encoding is compact. With kotlinx.serialization the schema is plain Kotlin:

plugins { kotlin("plugin.serialization") } dependencies { implementation("org.jetbrains.kotlinx:kotlinx-serialization-protobuf:1.10.0") }

Write the persisted model as mirror classes, named here with a P suffix. Give every field an explicit @ProtoNumber. Model the sealed hierarchy as a oneof: a wrapper holding one @ProtoOneOf property whose type is a sealed interface with one subclass per event.

@Serializable data class AccountEventP(@ProtoOneOf val event: AccountEventOneOfP) @Serializable sealed interface AccountEventOneOfP { @Serializable data class Opened(@ProtoNumber(1) val value: AccountOpenedP) : AccountEventOneOfP @Serializable data class Deposited(@ProtoNumber(2) val value: AccountDepositedP) : AccountEventOneOfP @Serializable data class Withdrawn(@ProtoNumber(3) val value: AccountWithdrawnP) : AccountEventOneOfP // Field 4 held AccountFlaggedP, which the domain retired. Its number is never reused. @Serializable data class Flagged(@ProtoNumber(4) val value: AccountFlaggedP) : AccountEventOneOfP @Serializable data class TransferSent(@ProtoNumber(5) val value: AccountTransferSentP) : AccountEventOneOfP @Serializable data class IdempotencyApplied(@ProtoNumber(6) val value: CommandIdempotencyP) : AccountEventOneOfP } @Serializable data class AccountOpenedP(@ProtoNumber(1) val ownerName: String) @Serializable data class AccountDepositedP( @ProtoNumber(1) val amount: Long, // Added after the first release. Events stored before then decode with the default. @ProtoNumber(2) val reference: String? = null, ) @Serializable data class AccountWithdrawnP(@ProtoNumber(1) val amount: Long) @Serializable data class AccountFlaggedP(@ProtoNumber(1) val reason: String = "") @Serializable data class AccountTransferSentP( @ProtoNumber(1) val toAccountId: String, @ProtoNumber(2) val amount: Long, )

One serializer covers every persisted model:

/** Protobuf bytes for any `@Serializable` persisted model. */ class ProtoSerializer<T>(private val serializer: KSerializer<T>) : Serializer<T> { override fun serialize(value: T): ByteArray = ProtoBuf.encodeToByteArray(serializer, value) override fun deserialize(bytes: ByteArray): T = ProtoBuf.decodeFromByteArray(serializer, bytes) }

Convert between the two models

object AccountEventConverter : Converter<AccountEvent, AccountEventP> { override fun toPersisted(value: AccountEvent): AccountEventP = AccountEventP( when (value) { is AccountEvent.Opened -> AccountEventOneOfP.Opened(AccountOpenedP(value.owner)) is AccountEvent.Deposited -> AccountEventOneOfP.Deposited(AccountDepositedP(value.amount, value.reference)) is AccountEvent.Withdrawn -> AccountEventOneOfP.Withdrawn(AccountWithdrawnP(value.amount)) is AccountEvent.TransferSent -> AccountEventOneOfP.TransferSent( AccountTransferSentP(value.toAccountId, value.amount)) is AccountEvent.IdempotencyApplied -> AccountEventOneOfP.IdempotencyApplied(value.idempotency.toPersisted()) AccountEvent.Retired -> throw IllegalArgumentException( "Retired events are read from history, never written") }) override fun toDomain(value: AccountEventP): AccountEvent = when (val persisted = value.event) { is AccountEventOneOfP.Opened -> AccountEvent.Opened(owner = persisted.value.ownerName) is AccountEventOneOfP.Deposited -> AccountEvent.Deposited(persisted.value.amount, persisted.value.reference) is AccountEventOneOfP.Withdrawn -> AccountEvent.Withdrawn(persisted.value.amount) is AccountEventOneOfP.Flagged -> AccountEvent.Retired is AccountEventOneOfP.TransferSent -> AccountEvent.TransferSent(persisted.value.toAccountId, persisted.value.amount) is AccountEventOneOfP.IdempotencyApplied -> AccountEvent.IdempotencyApplied(persisted.value.toDomain()) } } val AccountEventCodec = CodecComposed(ProtoSerializer(AccountEventP.serializer()), AccountEventConverter)

Both when expressions are exhaustive over a sealed type. Adding an event to either model fails compilation until the converter handles it.

Evolving events

Every stored event must always convert to exactly one domain event. Stream versions and subscription checkpoints advance once per stored record, so a converter never drops an event or splits one in two.

Rename something in the domain

Change the domain class and the converter. The persisted model stays as it is. In the sample, the domain calls the field owner while storage still calls it ownerName.

Add a field

Add it to the persisted class with a new field number and a default. Events stored earlier decode with the default, and the converter decides what that means for the domain. AccountDepositedP.reference was added this way.

Retire an event

An event the domain no longer cares about still sits in its streams and still holds a version. Keep its persisted class and field number, and convert it to a single no-op domain event that the evolver ignores explicitly. AccountFlaggedP converts to AccountEvent.Retired.

Keep the retired event's stored name in the type registry too. Subscriptions skip records whose name is not registered, which would hide the event from snapshot maintenance:

val AccountEventTypes = EventTypeNames.ForAggregate( aggregate = "Account", events = mapOf( AccountEvent.Opened::class to "AccountOpened", AccountEvent.Deposited::class to "AccountDeposited", AccountEvent.Withdrawn::class to "AccountWithdrawn", AccountEvent.TransferSent::class to "AccountTransferSent", AccountEvent.IdempotencyApplied::class to "AccountIdempotencyApplied", // Keeps the name its events were stored under, so subscriptions still deliver them. AccountEvent.Retired::class to "AccountFlagged", ))

Change an event's meaning

Add a new persisted variant with a new field number and a new event name, and start writing that. Keep reading the old variant, converting it to whatever the current domain model needs.

Rules that keep stored data readable

  • Never reuse or renumber a @ProtoNumber.

  • Never change a field's type.

  • Never rename a stored event name in EventTypeNames.

  • Give every added field a default.

  • Bytes that cannot be decoded are an error. Replay and subscriptions stop on them; they are never skipped.

Test the codec

Converters are plain functions, so they are cheap to test. Keep a test for every evolution you make. The old shape of a class can live in the test:

class AccountCodecTest { @Test fun `every written event survives a round trip`() { val events = listOf( AccountEvent.Opened("Ada"), AccountEvent.Deposited(50, reference = "salary"), AccountEvent.Deposited(50, reference = null), AccountEvent.Withdrawn(20), AccountEvent.TransferSent("acc-2", 30), ) events.forEach { event -> assertThat(AccountEventCodec.decode(AccountEventCodec.encode(event))).isEqualTo(event) } } // The shape of a deposit before `reference` existed, kept to prove old bytes still decode. @Serializable data class AccountDepositedV1P(@ProtoNumber(1) val amount: Long) @Test fun `a deposit stored before references existed decodes without one`() { val storedDeposit = ProtoBuf.encodeToByteArray(AccountDepositedV1P.serializer(), AccountDepositedV1P(75)) val persisted = ProtoBuf.decodeFromByteArray(AccountDepositedP.serializer(), storedDeposit) val event = AccountEventConverter.toDomain(AccountEventP(AccountEventOneOfP.Deposited(persisted))) assertThat(event).isEqualTo(AccountEvent.Deposited(75, reference = null)) } @Test fun `a retired event still decodes to a domain event`() { val stored = AccountEventCodec.serializer.serialize( AccountEventP(AccountEventOneOfP.Flagged(AccountFlaggedP("manual review")))) assertThat(AccountEventCodec.decode(stored)).isEqualTo(AccountEvent.Retired) } @Test fun `state survives a snapshot round trip`() { val state = AccountState("acc-1", owner = "Ada", balance = 80) val codec = AccountStateSchemaCurrent.stateCodec assertThat(codec.decode(codec.encode(state))).isEqualTo(state) } }
10 November 2026