Prompt
How do I build an event-driven system that can survive consumer outages?
Latest observation
To build an event-driven system that survives consumer outages, design for durable event storage, replayability, idempotent consumers, and loose coupling. The core idea is: producers should not depend on consumers being online.
1) Use a durable event broker
Choose a broker that persists events until consumers can read them later:
- Kafka
- Pulsar
- RabbitMQ with durable queues
- cloud equivalents like SNS/SQS, Pub/Sub, Event Hubs
Key requirements:
- Persistent storage
- Acknowledgements
- At-least-once delivery
- Consumer offset tracking / queue retention
2) Decouple producers from consumers
Producers should:
- write events to the broker
- not call consumer services synchronously
- not wait for downstream processing
This prevents consumer outages from blocking the rest of the system.
3) Make consumers idempotent
Outages often lead to retries and duplicate deliveries. Consumers must safely process the same event more than once.
Common patterns:
- Store processed event IDs in a deduplication table
- Use natural idempotency keys
- Upsert instead of insert
- Make state transitions conditional
Example:
- If event
order-123-createdis processed twice, the second attempt should have no effect.
4) Use acknowledgements correctly
Only acknowledge an event after it has been fully processed and committed.
General rule:
- read event
- process it
- persist result
- ack event
If you ack too early, an outage or crash can cause data loss.
5) Implement retry with backoff
When downstream dependencies fail:
- retry transient failures
- use exponential backoff with jitter
- cap retries to avoid overload
Avoid tight retry loops that amplify outages.
6) Add a dead-letter queue (DLQ)
If an event keeps failing:
- move it to a DLQ after max retries
- alert operators
- allow manual inspection and reprocessing
This prevents one bad event from blocking the stream.
7) Separate consumer state from broker state
Consumers should persist their progress independently:
- database checkpoints
- offset commits after successful work
- transactional outbox/inbox patterns where needed
This helps recovery after consumer restarts.
8) Use the Outbox pattern for reliable publishing
If a service updates its database and emits an event, do both in a single transaction using an outbox table:
- write business data
- write event to outbox
- background publisher sends outbox events to broker
This prevents losing events if the service crashes between DB commit and message publish.
9) Control backpressure
If consumers go down, queues grow. Plan for it:
- size retention to cover expected outage windows
- autoscale consumers when backlog grows
- limit producer throughput if needed
- monitor lag and queue depth
10) Design for replay
Assume consumers will need to rebuild state:
- keep events long enough
- version events carefully
- make event schemas backward compatible
- support reprocessing from a checkpoint or from the beginning
11) Monitor the right signals
Track:
- consumer lag
- queue depth
- retry counts
- DLQ volume
- processing latency
- error rates
- stale offsets/checkpoints
Alerts should trigger before the backlog becomes unrecoverable.
12) Avoid brittle assumptions
Don’t assume:
- events are delivered exactly once
- consumers process in order globally
- consumers are always available
- downstream services never fail
Design for the opposite.
A practical reference architecture
A robust setup often looks like this:
- Producer service writes to a local DB and an outbox table in one transaction
- Outbox relay publishes events to Kafka/Pub/Sub
- Consumers read from the broker
- Each consumer:
- processes idempotently
- stores progress/checkpoints
- retries transient failures
- sends poison events to a DLQ
- Monitoring watches lag, retries, and DLQ size
If you want the shortest rule set
Use this checklist:
- Durable broker
- At-least-once delivery
- Idempotent consumers
- Ack after successful processing
- Retries with backoff
- DLQ for poison messages
- Outbox pattern for publishing
- Replayable events and schema versioning
- Monitoring for lag and backlog
If you want, I can also sketch:
- a Kafka-based architecture,
- a cloud-native version using SQS/PubSub, or
- a sample implementation with code.