Notificaciones de Dominio

Registro de notificaciones

Escrito por Darjus Kazakevic · revisado el

Publish your events as a feed, so nobody needs to read your database.

Other bounded contexts (parts of the system with their own model) need to know what happened here. The easy answer, a read replica or a view on your tables, makes your schema their contract. After that, you cannot change a column without breaking them.

Domain Notifications publish your domain events instead, as an ordered, append-only feed, often served over HTTP. Each consumer stores its own position and asks for events after it. The publisher keeps no per-consumer state and needs no broker. Each entry carries the fact and its ids, not the whole model.

En esencia — Expose a context's domain events as an ordered, append-only feed that consumers read from their own saved position, instead of reading your tables.

Cuándo recurrir a él

Recurre a él cuando

  • Other contexts ask for a replica or a view on your tables
  • Consumers must catch up at their own pace, without a broker
  • New consumers need to replay history from the start

Reconsidéralo cuando

  • Consumers need push within milliseconds, not polling
  • Reactions inside one context: dispatch them in-process
  • You already run Kafka, which gives the same log with less code

The moving parts

The notification log
An append-only table of events with an increasing position number. Rows are never updated. Old rows are archived only when no consumer still needs them.
The consumer's cursor
Each consumer stores the last position it processed and asks for ?after=<position>. A new consumer starts at 0 and catches up. A slow one falls behind without affecting anyone else.
Thin events
An entry carries what happened and the ids involved, such as “order 42 placed, total 30.00 EUR”. A consumer that needs more calls your published API.
Pages and caching
The feed is served in pages, often JSON or Atom over HTTP. A full page of old events never changes, so it can be cached for a long time by a CDN or proxy.

Applying it well

Publish the fact, not the aggregate
Carry ids and what happened, not the full model. A fat payload makes your schema the contract by another route. A consumer that needs more can ask through the published API.
Let the consumer keep the cursor
The feed is append-only and ordered, and each reader stores how far it got. The publisher keeps no subscriptions or acks, so a slow or brand-new reader costs it nothing.
Write the entry in the same transaction
Insert the notification row in the transaction that changes the aggregate, as with an outbox. A log written afterwards can miss events after a crash, and consumers cannot see the gap.

Errores habituales

  • Putting the whole aggregate in each entry, so every consumer depends on your model.
  • A plain sequence as the position. A row that commits late gets a lower number than rows already read, and is skipped. Serialize log writes, for example with one advisory lock.
  • Saving the cursor before handling the events, so a crash skips them for good.
  • Editing or deleting old entries, which breaks consumers that replay.