Messaging Patterns
The Outbox Pattern for Reliable Messaging
Guarantee at-least-once event delivery without two-phase commit by writing events to the database in the same transaction as your business data.
The dual-write problem: if you write to your database and then publish to a message broker, a crash between the two leaves them inconsistent. The outbox pattern solves this by writing messages to an outbox table in the same local transaction as the business data. A relay process reads the outbox and publishes to the broker, marking messages as sent. Atomicity is guaranteed by the database; the broker gets at-least-once delivery.
CREATE TABLE outbox (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
aggregate TEXT NOT NULL,
event_type TEXT NOT NULL,
payload JSONB NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
sent_at TIMESTAMPTZ
);Continue reading
- →
Dependency Injection and IoC Containers
Invert control of dependency creation to keep classes decoupled from their collaborators and trivially testable.
- →
The Observer Pattern and Event-Driven Design
Decouple publishers from subscribers by letting objects register interest in events without the producer knowing who is listening.
- →
The Strategy Pattern: Encapsulating Algorithms
Define a family of algorithms, encapsulate each one, and make them interchangeable — open for extension, closed for modification.