Scalable Go service that consumes raw usage events from Kafka, deduplicates via Redis, and aggregates them into billing-ready records using the transactional outbox pattern for exactly-once semantics.
Kafka (usage-events)
│
▼
┌───────────┐ ┌───────────┐ ┌────────────────┐
│ Consumer │────▶│ Dedup │────▶│ Aggregator │
│ Group │ │ (Redis) │ │ (Outbox TX) │
└───────────┘ └───────────┘ └────────────────┘
│ │
▼ ▼
┌───────────┐ ┌────────────────────┐
│ DLQ │ │ PostgreSQL │
│ (Kafka) │ │ usage_events │
└───────────┘ │ billing_records │
│ outbox │
└────────────────────┘
│
Reconciler (cron)
Design choices:
- Transactional outbox pattern — raw event insert, billing upsert, and outbox write happen in a single DB transaction for exactly-once billing semantics.
- Redis-backed idempotency keys with configurable TTL for duplicate detection before hitting the database.
- Dead-letter queue with Kafka + DB persistence for poisoned messages, with structured logging for automated alerts.
- Reconciliation job periodically cross-checks raw event counts against aggregated totals to detect drift.
# Start everything
docker compose up --build
# Publish a test event
echo '{"event_id":"test-1","user_id":"user-1","event_type":"api_call","quantity":1,"timestamp":1700000000000}' | \
kcat -P -b localhost:9092 -t usage-events
# Check health
curl http://localhost:8081/healthz
# Check metrics
curl http://localhost:2112/metricsPrerequisites: Go 1.22+
make build # Build binary
make test # Run all tests with race detection
make run # Build and run locally
make docker # docker compose up --build
make load-test # Publish 5K events/sec for 30s via vegeta/attack.shcmd/pipeline/ Entry point with graceful shutdown + in-flight draining
internal/
consumer/ Sarama consumer group (dedup → aggregate → DLQ)
dedup/ Redis SET NX idempotency keys
aggregator/ Validates and persists via transactional outbox
dlq/ Dead-letter queue (Kafka + DB)
reconciler/ Periodic drift detection (raw vs aggregated)
outbox/ Transactional outbox writes
storage/ PostgreSQL: events, billing records, outbox, DLQ
health/ /healthz and /readyz endpoints
config/ Environment-based configuration
metrics/ Prometheus counters, histograms, gauges
model/ Domain types + validation
migrations/ PostgreSQL schema (4 tables)
vegeta/ Load test script
make test # table-driven unit tests + race detectorKey test areas:
- Aggregator: valid events, validation failures, store errors (table-driven)
- Dedup: new events, repeated events, independent IDs (table-driven)
- Integration tests use
testcontainers-gofor real Redis + PostgreSQL
# Requires kcat (formerly kafkacat)
./vegeta/attack.sh 5000 30s # 5K events/sec for 30 seconds
# Monitor throughput
watch -n1 'curl -s localhost:2112/metrics | grep billing_events'- Prometheus metrics on
:2112/metrics— consumed, deduplicated, aggregated, DLQ, process duration, reconciliation drift - Health endpoints on
:8081—/healthz(liveness),/readyz(readiness) - Structured JSON logging via
log/slog