Events Aggregator: Distributed Systems Dissection
Blueprint for a 13-service Go event aggregation system built to survive duplicate crawls, dual-write inconsistency, cache stampedes, and cascading downstream timeouts.
Most event aggregators fail quietly: two worker replicas scrape the same ticketer at the same second, a database commit succeeds while the Kafka publish fails, or a cold cache key triggers a thundering herd against Postgres. kenya-events-aggregator was built in Go (zero JS) to eliminate each of those failure modes with small, inspectable primitives.
A distributed system is only as honest as its failure path. If you cannot point to the exact line of code that prevents a split-brain write or a cache stampede, the guarantee does not exist.
Design note, kenya-events-aggregator
1. System Topology
Traffic enters through Nginx (least_conn upstream balancing with unbuffered SSE/WebSocket proxying), fans out across stateless API replicas, and separates write paths (PgBouncer + Postgres Primary) from read paths (WAL-streaming Read Replica + Redis cache-aside).
2. Sharded Crawling & Mutual Exclusion
With 10 live Kenyan ticketer adapters running across worker-1 and worker-2, naive cron schedules cause duplicate HTTP hits. Each ticketer domain is mapped onto a Consistent Hash Ring (32 virtual nodes per worker) in resilience.go, paired with a UUID-owned Redis SETNX + atomic Lua release lock in redis.go.
3. Transactional Outbox vs. Dual Writes
Publishing directly to Kafka after a Postgres insert creates a classic dual-write race: if the process crashes between the commit and the broker ack, downstream consumers drift permanently. Instead, postgres.go writes both the deduplicated event (pg_trgm similarity > 0.78) and an outbox_events row inside a single ACID transaction.
| Failure Mode | Primitive | Guarantee |
|---|---|---|
| Duplicate domain crawls | 32-vnode Hash Ring + Lua Lock | Single active owner per ticketer |
| Postgres / Kafka split-brain | ACID Transactional Outbox | At-least-once relay to events.discovered |
| Cache stampede | Singleflight + TTL jitter | 1 DB query per cold key burst |
| SSRF on poster mirroring | ValidateExternalURL |
Blocks loopback, RFC1918, link-local |
4. Running Locally
Everything boots in a single Compose stack with pre-provisioned Prometheus and Grafana dashboards:
docker compose up -d --build curl http://localhost:8080/readyz curl "http://localhost:8080/api/v1/events?county=Nairobi&limit=10"
Related: Software Engineering Dossier · Blueprint & Cloud Film