Asynchronous messaging principles — stack- and broker-agnostic. Use when designing or reviewing message-driven code: commands and events, their contracts, consumers and handlers, retries and dead letters, ordering, duplicates and idempotency, concurrent writes from consumers, or when a message is lost, applied twice, applied out of order or retried forever. It encodes: commands vs events, contracts as a published language with the version in the topic, every message assumed duplicated and out...
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Messaging?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/jorgecostamacia-messaging-code-harness)
---
name: messaging
description: >-
Asynchronous messaging principles — stack- and broker-agnostic. Use when
designing or reviewing message-driven code: commands and events, their
contracts, consumers and handlers, retries and dead letters, ordering,
duplicates and idempotency, concurrent writes from consumers, or when a
message is lost, applied twice, applied out of order or retried forever.
It encodes: commands vs events, contracts as a published language with the
version in the topic, every message assumed duplicated and out of order,
the version travelling with the data and a strict newer-wins guard,
optimistic concurrency on the write, retries owned by the broker (never a
loop in the use case), and malformed messages parked, not retried. The
library and broker specifics live in each stack's skill.
---
# Messaging (principles)
Message-driven systems fail in ways synchronous ones don't: messages arrive twice, late, out of order, or never succeed. These rules make that the normal case instead of a bug. Sections marked **[canon]** state the standard (Hohpe & Woolf's *Enterprise Integration Patterns*, Evans); **[our convention]** marks house decisions. The bus library and the broker's mechanics are each stack skill's.
## Commands and events — [canon: Hohpe & Woolf]
- A **command** asks one service to do something (`PedidosExtract`). It has one handler and an imperative name, and the service can refuse it.
- An **event** states that something happened (`PedidosLoaded`): any number of subscribers, named in the past tense, nobody can veto it.
- A service **owns** the commands it accepts and the events it publishes; it never publishes another service's events.
## Contracts are the published language — [canon: Evans' Published Language; the route convention is ours]
- The **service's own contract** declares its messages and is released on its own. It **has its own version, apart from the service's**: each takes its SemVer from its own changes, and the contract's major moves only when a message breaks, or when the transport needs new topics (a new partition count in Kafka, see the transport skill). Consumers depend on the contract, never on the service.
- **The contract's major is in the topic/route name** (`<service>.v6.<module>.<action>.event`). A breaking change is a new major: new topics, consumers move when they upgrade, nothing breaks in flight. A minor or patch never moves a topic — keep changes additive. A new major is the person's decision: when a change needs one, say so and wait; never start one on your own.
- **A field crosses a chain of services producer-first, and nullable.** A consumer ignores what it does not know and uses a default for what it does not receive. So a new *optional* field is additive (a minor). The contract is released first, then the producer takes it, and each consumer follows at its own pace, in either order. A consumer can even map the column before the contract has the field; the column stays empty until the field arrives. A new *required* field cannot be additive: it lands in the next major, when the consumer moves topic. The store change (a column) goes in before the service that reads it.
- Routes are names, not code. Derive them from one base for each module, in kebab case, with `.command` or `.event` suffixes. Then you can find a topic, its consumer group and its dead letter from each other.
## Assume duplicates and disorder — [canon: at-least-once delivery, idempotent receiver; the version guard is our convention]
Write every consumer as if each message could arrive **twice, late, or before an older one**: a redelivery after a crash, a retry, a reload, two partitions at the same time. Do not rely on broker ordering: it holds only within a partition, and only until something retries.
- **The version travels with the data.** Each message carries the instant (or sequence) of the state it describes, and the consumer stores it with what it writes.
- **Apply only what is strictly newer** (`stored < incoming`). An equal version is a redelivery — a no-op. An older one is stale — dropped.
- **A newer message always moves the stored version**, even when it changes nothing else. If not, the row looks older than it is, and a stale message that arrives later wins. The identity of the message that wrote it (its correlation id) moves with the version, so the trail always points at the last writer.
- **Publish only on a real change**, so identical messages do not ripple downstream. Let the event carry the **previous state** next to the new one (`null` when the row is new). Then a consumer can tell what changed without a second read.
- **One instant per operation**: an operation takes its version once, at its start, and stamps it on all that it writes and publishes.
## Concurrent writes — [our convention]
Two consumers may handle messages for the same row at the same time. **Optimistic concurrency guards the write**: a save over a row that changed after the read fails. The infrastructure retries the message, and the guard decides again against the current row. A race that surfaces as a duplicate-key failure on insert resolves the same way **when the write is idempotent**: the version travels with the data, so the retry becomes an update or a no-op. When the write is not idempotent, a duplicate key is a business fact ("already exists") that no retry changes. Park the message; do not retry it.
## Retries belong to the broker — [canon: redelivery and the dead letter; the ladder is ours]
- **No retry loop in the use case.** A failure — a conflict, a timeout, a race — escapes the handler. The messaging infrastructure retries the message with a **backoff ladder** (minutes to a day). So a dependency that is down for an hour costs only delay.
- Retries are **durable**: requeued or scheduled outside the process, so a restart loses none.
- **Never retry a malformed message.** The infrastructure excludes a validation failure from retries and **parks** it in a dead-letter destination at once. A retry can only fail the same way. Parked messages are kept, readable and replayable; nothing is silently dropped.
- Park a message that the consumer cannot even read (corrupt envelope) apart from one that failed in the handler.
## Handlers — [our convention]
- A **consumer** is the service that receives a message; its **handler** is the adapter that the message reaches (a stack may call the one for an event a subscriber).
- A handler is a **thin driving adapter**: unpack the message, call one use case, send or publish what it returns, log its line. No rule lives there.
- A handler that sends commands or publishes events does it **after** the use case succeeded; a failed use case sends and publishes nothing.
- The correlation id of the incoming message travels on everything it causes.