BLOG / ARCHITECTURE

Event-driven architecture with Go and Kafka: when it pays off

Events instead of synchronous calls solve real problems — and create new ones. Here are the criteria we use to make that decision, illustrated by a payment platform, a billing system and a B2B marketplace.

“Let's do it with events” is the most expensive sentence in an architecture meeting. It adds a broker, duplicated data, eventual consistency and a whole class of failures that a plain HTTP call never produces. Sometimes it is the only way to solve the problem. Far more often it is a way to make life harder.

Three signs you actually need events

  1. A single fact has more than three consumers, and the list changes more than once a quarter
  2. Peak load is several times the average, and the queue works as a buffer rather than decoration
  3. Ordering and a full history of facts are a business requirement: settlements, charges, audit
  4. Consumers must survive the source being unavailable without losing data

In the bank's payment platform all four applied: a payment authorisation is consumed by anti-fraud, statements, notifications, EDI and analytics, and tax days push peaks to ×3. In the telecom billing system the third point was decisive — charges must be reproducible from the event history, or you have nothing to settle a subscriber dispute with.

The event contract matters more than the broker

An event is your service's public API, except you cannot ask the caller what they meant. So we fix the schema before the first line of code: an identifier, the time it occurred, a version and only the fields that describe the fact itself.

// An event is a fact, not a command: past-tense name,
// immutable fields, and a version as part of the contract.
type PaymentAuthorized struct {
	EventID     string    `json:"event_id"`
	OccurredAt  time.Time `json:"occurred_at"`
	Version     int       `json:"version"`
	PaymentID   string    `json:"payment_id"`
	AccountID   string    `json:"account_id"`
	AmountMinor int64     `json:"amount_minor"` // minor units, never float
	Currency    string    `json:"currency"`
}

// Publish inside the same transaction as the state change (outbox),
// otherwise you will eventually lose either the record or the event.
func (r *Repo) Authorize(ctx context.Context, p Payment) error {
	return r.tx(ctx, func(tx *sql.Tx) error {
		if err := savePayment(ctx, tx, p); err != nil {
			return err
		}
		return appendOutbox(ctx, tx, "payments.authorized.v1", PaymentAuthorized{
			EventID:     uuid.NewString(),
			OccurredAt:  time.Now().UTC(),
			Version:     1,
			PaymentID:   p.ID,
			AccountID:   p.AccountID,
			AmountMinor: p.AmountMinor,
			Currency:    p.Currency,
		})
	})
}

The outbox pattern is not decoration, it is a correctness requirement. Without it you get either a payment with no event or an event with no payment, and you will discover which at the worst possible moment. The partition key is part of the contract too: for payments we use the account identifier so that one client's operations are guaranteed to stay in order.

topic: payments.authorized.v1
partitions: 12          # ordering is guaranteed within a partition
replication.factor: 3
config:
  min.insync.replicas: 2
  retention.ms: 604800000   # 7 days: enough to replay after an incident
  cleanup.policy: delete
  max.message.bytes: 262144
consumers:
  - group: antifraud
    dlq: payments.authorized.v1.dlq
    max.poll.records: 500

Consumer idempotency deserves its own line. Kafka guarantees at-least-once delivery, which means every event will arrive twice sooner or later. The consumer must store processed event_id values and silently ignore repeats — otherwise a subscriber gets charged twice.

FIG. 1 — AN EVENT'S PATH FROM TRANSACTION TO CONSUMERS AND DLQ

When it is over-engineering

In Stoxy's warehouse analytics we deliberately skipped the broker in the early versions: the facts had a single consumer, load was predictable, and data was collected in hourly batches anyway. Kafka would have added three weeks of infrastructure work and zero product value — the queue appeared only at 140 warehouses, once there were more than a dozen connectors.

“Event-driven architecture pays off where the list of consumers is unknown in advance. If it is known and stable, you have simply replaced a function call with a distributed system.”

The cost is measurable too: observability (tracing across asynchronous boundaries), replay tooling, a DLQ and the work of triaging stuck messages, a schema registry and evolution rules. In our experience that is 15–20% added to the timeline plus a standing load on the operations team.

How we decide in practice

The rule is simple: start with synchronous calls and explicit service boundaries, and introduce an event once a third consumer of the fact appears or history becomes a requirement. Boundaries drawn correctly from the start let you move to events within a sprint. The reverse move — from a premature broker back to a sane system — costs quarters. What this looks like on a live project is described in our payment platform migration post-mortem.

ARCHITECTUREGOKAFKA
Telegram