Skip to content

Publish events as they are stored, so they project the same when replayed - #558

Draft
erikrozendaal wants to merge 3 commits into
masterfrom
improvement/publish-stored-events
Draft

erikrozendaal wants to merge 3 commits into
masterfrom
improvement/publish-stored-events

Conversation

@erikrozendaal

@erikrozendaal erikrozendaal commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Builds on #559, which simplifies how events are deserialized.

A projection should be the same whether its events are projected right after they are stored or during a replay. It was not, because projectors got the event objects the aggregate applied, while a replay deserializes the events from their JSON: times lose their sub-millisecond precision, symbols become strings and values are only cast to their attribute type when deserializing. So a projector could store other values live than after a replay, or behave differently when it reads, for example, a hash key.

The event stores now publish the events as they are stored, so projectors and workflows see the same values live and when replaying. The aggregate still keeps the values it applied, so a new verify_event_serialization setting lets specs fail on events that change when they are stored, pointing out the domain code to fix.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved serialization issues can produce different live, replay, and dry-run behavior, while verification can mutate timestamps and a new test fails on supported Ruby 3.3.

6 open issues
What changed in this PR

This PR aims to make live event handling match replay behavior by publishing serialized-and-deserialized events, while leaving the aggregate’s applied values unchanged.

Changes:

  • Normalize published events in the real and fake event stores.
  • Add optional serialization verification to detect changes in event values.
  • Add regression specs and document the new behavior and setting.
File Description
spec/​lib/​sequent/​test/​test_spec.rb Tests fake-store normalization and verification.
spec/​lib/​sequent/​core/​event_store_spec.rb Tests publication consistency and serialization errors.
lib/​sequent/​test/​command_handler_helpers.rb Normalizes events published by the fake store.
lib/​sequent/​core/​event_store.rb Normalizes events before publication.
lib/​sequent/​core/​event_serialization.rb Adds event round-tripping and value verification.
lib/​sequent/​configuration.rb Adds the disabled-by-default verification setting.
docs/​docs/​concepts/​event.md Explains publication behavior and verification.
docs/​docs/​concepts/​configuration.md Documents the new setting.
CHANGELOG.md Announces the behavior change and verification option.

Review effort: Balanced

Comment thread lib/sequent/core/event_serialization.rb Resolved
Comment thread lib/sequent/core/event_store.rb Resolved
Comment thread lib/sequent/core/event_serialization.rb Resolved
Comment thread lib/sequent/core/event_serialization.rb Resolved
Comment thread lib/sequent/core/event_serialization.rb Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# The events table keeps the aggregate_id, sequence_number and created_at in columns, from which they are loaded:
# the aggregate_id as a lowercase UUID, and the created_at in the time zone of the database session.
def loaded_event_json(event_json)
created_at = event_json['created_at'] && Time.iso8601(event_json['created_at'])
created_at = event_json['created_at'] && Time.iso8601(event_json['created_at'])
event_json.merge(
'aggregate_id' => event_json['aggregate_id']&.downcase,
'created_at' => created_at && convert_timestamp(created_at).iso8601(Sequent.configuration.time_precision),
Comment on lines +49 to +50
serialized = Oj.strict_load(Oj.dump(serialize_event(event)))
stored = deserialize_event_json(serialized['event_type'], loaded_event_json(serialized['event_json']))
def serialize_events(events)
events.map { |event| [event.class.name, Sequent::Core::Oj.dump(event)] }
events.map { |event| Sequent::Core::Oj.dump(serialize_event(event)) }
end
…ayed

The event store published the events the aggregate applied, while a
replay deserializes them from the JSON in the event store. Times were
truncated to the time_precision, symbols became strings and attributes
were cast to their type only when replaying, so a projector could store
other values live than after a replay. The event stores, including the
FakeEventStore of the test helpers, now publish the events as they are
deserialized from the JSON they store.

The aggregate keeps the values it applied. The new
verify_event_serialization setting fails storing an event that changes
other than by truncating its times, so specs can point out the domain
code that creates values it does not store.
…as loaded

The EventStore and the FakeEventStore each serialized and deserialized
events themselves, and the round trip for publishing added a third
way. They now share Helpers::EventSerialization, which deserializes
events like a replay does, and publish the events using its
stored_event. The EventStore also applies what loading the events
from their columns does: the aggregate_id as a lowercase UUID and
created_at in the time zone of the database session. A dry run
publishes and records the events the same way.

Verifying the serialization no longer converts the times of the event
to UTC in place, only accepts actual times as truncated times, and
reports a hash that changes its class, like one with indifferent
access, or its values.
@erikrozendaal
erikrozendaal force-pushed the improvement/publish-stored-events branch from 14fa8c1 to 287eaae Compare November 4, 2026 11:57
@erikrozendaal
erikrozendaal changed the base branch from master to improvement/remove-text-json-columns November 4, 2026 11:57
Serializing an event is dumping it with its class name next to it, and
each event store stores that in its own way, so serialize_event only
wrapped it to be unwrapped again. The event stores parse their JSON
before deserializing it, so deserialize_event_json only takes the
parsed JSON.
Base automatically changed from improvement/remove-text-json-columns to master November 4, 2026 17:01

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants