Old events arrive later: revisions and asynchronous change ordering

Full snapshots and deltas on MySQL 8.4/InnoDB: revision scope, equal-number conflicts, an atomic decision journal and recovery after a gap.

A conceptual illustration of an accepted layer and late changes.
A conceptual illustration of an accepted layer and late changes.

Imagine a workshop card containing a material name and the number of prepared parts. Its source created three successive states, but the handler received messages in the order 3, 1, 2. Writing every payload would leave readers with the second state. Successfully delivering every message does not guarantee the right result: the final arrival was already obsolete.

We need an acceptance contract. What are we ordering, does each message contain the full state, and what happens when the same number carries different content? The example runs in the existing Symfony Demo on MySQL 8.4 through native PDO, using InnoDB tables. The transitions below are checked against the decision journal and projection rows.

A number belongs to a defined scope

In this model one authoritative source issues positive integer revisions for the tuple site, object_id, locale. A card on another site or in another language has its own sequence. Revision 8 of one object says nothing about revision 4 of another. This is a local order for changes to a particular state.

A delivery identifier answers a different question: which transport attempt are we seeing? Stable operation identity helps prevent repeating one effect. A handler lease generation concerns permission to work. None replaces the domain revision. An old event remains old when resent with a new delivery ID and a newly authorized handler.

Arrival time is unsuitable too. Retries, delays and parallel execution change it independently of editing order. If independent publishers can assign one number to different states, the source needs a decision first: one sequencer, coordinated versioning or another explicit protocol. Selecting whichever message arrived last only hides the contradiction.

Full state allows intermediate numbers to be skipped

The main event completely replaces a card: revision, label, total. State three already includes the result of earlier changes. An empty projection can accept it immediately without rebuilding states one and two. This is a property of the agreed payload that must be established before selecting an algorithm.

Delivery Content Expected decision Visible revision
3 Clay, 30 applied 3
1 Clay, 10 stale 3
2 Clay, 20 stale 3
3 Clay, 30 duplicate 3
3 Clay, 31 conflict 3
4 Clay, 40 applied 4

The projection stores the accepted revision with its state. Older messages return an explicit stale decision without overwriting data. A newer full state can advance the boundary. Separate controls start revision 1 for a second card, another site and the English locale: the first card's boundary does not extend to its neighbors.

A rising visible revision does not prove every event was processed. Missing messages may contain required audit information or separate business effects. Full replacement allows skipping them only when building this particular state. It does not authorize losing a payment, notification or another independent action.

Equal numbers require content comparison

Repeating revision three with the same state should change nothing. Revision three with a different part count indicates a contract conflict. Arbitrary overwriting makes the result depend on delivery order; silent success hides a publisher problem. The example distinguishes duplicate from conflict while retaining the accepted state.

Comparing raw JSON strings is inconvenient: key order can change without changing data. Here Snapshot::fromArray() accepts an exact schema and types, and canonicalState() always serializes label followed by total. Revision is compared separately. Reordered input keys therefore do not create a false conflict. This canonicalization belongs to a small teaching schema; it is not a general JSON standard.

A string instead of an integer, an extra field, an unsupported locale, invalid UTF-8 or an out-of-range value is rejected before writing. A larger model would need rules for nulls, decimal values, collections and text normalization. Equal numbers alone do not establish equal domain content.

Comparison and writing share one boundary

Reading a revision, comparing it in PHP and then writing unconditionally leaves room for another handler. Both may read an old value before the slower one overwrites a newer state. Comparison must belong to the same atomic decision as mutation.

The store uses an InnoDB transaction: reading and locking the rows needed for the decision, changing the projection and appending the journal entry share one commit or rollback boundary. Connections use PDO MySQL, without a SQL translation layer. Locking errors, including native MySQL errors 1205 and 1213, propagate to the caller rather than becoming stale or successful application; the store rolls back the transaction it owns.

InnoDB uses REPEATABLE READ isolation and row locks, rather than one database-wide writer lock. A separate migrator executes DDL before business transactions; the runtime account has no CREATE, ALTER or DROP privileges. The MySQL implicit-commit documentation explains why schema creation must not be presented as part of atomic event acceptance. This is neither an atomic schema upgrade nor an automatic data transfer from an old SQLite file. The sequential scenario checks state transitions, not competing-process behavior, broker behavior or performance.

Partial changes have a different contract

Now replace full cards with counter changes. Start at revision 1, total 10. Event two adds 5 and event three adds 7. Applying the third first and declaring revision 3 current produces 17 instead of the expected 22. Rejecting event two as stale then makes the loss permanent.

Consequently, Delta carries both its revision and baseRevision, and its new number must immediately follow the base. A delta applies only to that exact base. At state 1, receiving revision=3, baseRevision=2 returns needs_snapshot. Visible revision 1 and total 10 remain, while the row records the minimum required full-snapshot revision, 3.

This policy does not buffer payloads or wait indefinitely for missing history. A later delta 2 is also held from application while reconciliation is required. Recovery supplies an authoritative full snapshot at revision 3 or higher; the synthetic scenario uses total 22. The snapshot clears the gap marker, allowing delta 4 with base 3 to add 2.

When no projection exists, a delta also returns needs_snapshot without inventing initial state. The recovery coordinator must obtain a current full snapshot. Requesting reconciliation is not an applied change. The teaching command supplies a prepared synthetic snapshot; it implements no network fetch, publisher verification or reconciliation service.

A repeated delta is not always a duplicate

The store retains canonical content for the latest applied delta. The same revision, base and amount yield duplicate; a different amount yields conflict. An older revision yields stale. This is bounded memory of the latest acceptance, rather than a complete domain event history.

If the current revision arrived through a full snapshot, an equal delta yields covered_by_snapshot. We know the state already represents that revision, but one total cannot prove that an earlier delta was identical. Calling it a verified duplicate would overstate our knowledge. Likewise, a decision journal containing payload hashes helps explain processing but cannot reconstruct missing domain history.

Checking the useful property

The app:revision-projection command runs in the existing Demo using Symfony 8.1 and PHP 8.4.1+. The lab requires MySQL 8.4, the pdo_mysql extension and the shared guarded fixture; open the command help with bash fixtures/mysql_labs/run.sh php bin/console app:revision-projection --help. The synthetic scenario owns a temporary schema through App\Infrastructure\MysqlLab: the helper performs an explicit migration, after which the scenario uses DML access and reads the result through a separate native PDO connection to the same schema. A SQLite file path is no longer a storage parameter. Console describes command execution; this experiment does not connect a Messenger transport. Its idempotent handler documentation helps explain redelivery, while our domain contract determines the order of distinct revisions.

Actual local results

The MySQL checks cover the scenario's core invariants: deliveries 3→1→2 retain visible revisions 3/3/3 and totals 30/30/30. A gap at revision 3 with base 2 while current revision is 1 retains state 1/10 and minimum required snapshot revision 3; a later delta 2 does not lower that boundary. Authoritative snapshot 3/22 clears the gap marker, and delta 4 produces total 24. A separate native PDO connection reads the committed projection and journal from the same schema.

The recovery-boundary check requires a lower revision not to reduce the stored snapshot requirement; an explanation based on SQLite storage classes does not describe the MySQL implementation. Another check deliberately fails journal insertion through a trigger using native SIGNAL SQLSTATE '45000': the projection change rolls back and no successful decision row appears. The migrator creates the trigger during fixture setup, not in runtime code. The kernel command is still checked through Symfony’s native LazyCommand wrapper rather than assuming a concrete registry class.

These results concern a sequential lab check on actual MySQL 8.4 through native PDO. They are not performance measurements or proof of competing OS processes, broker delivery, distributed recovery, or crash and power-loss resilience. The scenario does not fetch snapshots from an external service; its MySQL TCP connection must not be confused with that kind of data retrieval.

Acceptance also needs an operational decision: equal-revision conflicts should reach publisher diagnostics, while snapshot requests should reach a bounded recovery process. Blindly retrying the same conflicting payload changes nothing. The example returns these decisions explicitly; error routing and its budget remain application responsibilities.

The practical choice begins with message semantics. Full replacement state can skip intermediate revisions. A delta needs an exact base or explicit recovery. Atomic acceptance protects known state, but creates no global order, publisher trust or exactly-once guarantee. Those boundaries let us handle a late event without turning the entire system into one queue.

The shared MySQL fixture and its files are introduced in the transaction chapter. Run commands from the single article_demo root; do not create a separate Symfony application for each article.

Check the current example against an owned disposable MySQL schema:

bash fixtures/mysql_labs/run.sh php vendor/bin/phpunit tests/RevisionProjection
  1. Data & Infrastructure An article has been withdrawn: how to remove it from search
  2. Databases The article was saved but the event was not sent: why use a transactional outbox
  3. Weather Importing weather data again: checkpoints and revisions instead of duplicates

A project by the author · Weather Solution200

After reading, a look outside.

Temperature, rain and wind in your city. Check the forecast and plan the hours ahead.

Check the forecast
Next article