Event-driven systems with Kafka and NATS
When services need to react to something without blocking the request that caused it, events are usually the right abstraction. The hard part isn't publishing a message — it's designing the contract, the delivery guarantees and the failure paths so the system stays predictable at 3am.
In this post I'll walk through how I think about event-driven communication in Go, and where I reach for Kafka versus NATS. These are patterns from production systems: ride-hailing dispatch, real-time messaging and multi-region platforms.
Why events at all
A synchronous call couples the caller to the callee's availability, latency and deployment schedule. Events remove that coupling: the producer records what happened and moves on. Consumers decide what to do with it, independently.
- Resilience — a slow consumer doesn't slow down the request path.
- Extensibility — new consumers can be added without changing the producer.
- Auditability — the event stream becomes a record of what actually happened.
Choosing between Kafka and NATS
They solve overlapping problems, but they optimise for different things. I use a simple rule of thumb:
- Kafka when I need replay, durable retention, ordered partitions and a long-lived log that multiple teams can read independently.
- NATS when I need low-latency request/reply or lightweight pub/sub inside a system I control end to end. JetStream adds persistence when needed.
// an event describes a fact that already happened type RideEvent struct { ID string `json:"id"` Type string `json:"type"` RiderID string `json:"rider_id"` Timestamp time.Time `json:"timestamp"` }
Three rules I try to keep
- Events are immutable facts, never commands disguised as facts.
- Consumers tolerate unknown fields — producers can add, never remove, without a version bump.
- Every event has an ID so handling can be idempotent.
Handling failures
Delivery is at-least-once, so duplicates are normal. Consumers must be idempotent: check the event ID against a short-lived store, then process. Poison messages should go to a dead-letter topic rather than blocking a partition forever.
"The queue is not the system. The system is the contract, the consumer behaviour and the path you take when things go wrong."— A note from my architecture journal
Key takeaways
- Use events to decouple, but design the contract like a public API.
- Kafka for replay and durable logs; NATS for fast, lightweight messaging.
- Assume at-least-once delivery and make consumers idempotent.
- Invest in lag and failure visibility from day one.