Kafka
Apache Kafka provides high-throughput event streaming with log retention for replay and analytics.
Why Kafka
Section titled “Why Kafka”| Strength | Benefit |
|---|---|
| High throughput | Millions of events/second |
| Log retention | Events persist for replay |
| Partitioning | Horizontal scaling |
| Ecosystem | Kafka Streams, Connect, ksqlDB |
| Ordering | Per-partition ordering guarantees |
Trade-offs
Section titled “Trade-offs”| Concern | Consideration |
|---|---|
| Operational complexity | ZooKeeper (or KRaft) coordination |
| Resource heavy | More memory/disk than AMQP |
| Latency | Batching adds milliseconds |
Configuration
Section titled “Configuration”[bus]backend = "kafka"
[bus.kafka]brokers = ["localhost:9092"]topic_prefix = "angzarr"consumer_group = "angzarr-handlers"Environment Variables
Section titled “Environment Variables”export KAFKA_BROKERS="localhost:9092"export KAFKA_TOPIC_PREFIX="angzarr"export BUS_BACKEND="kafka"Topic Layout
Section titled “Topic Layout”Events are partitioned by aggregate root for ordering:
Topic: angzarr.events.player Partition 0: [player-001 events, player-004 events, ...] Partition 1: [player-002 events, player-005 events, ...] Partition 2: [player-003 events, player-006 events, ...]
Topic: angzarr.events.hand Partition 0: [hand-001 events, ...] ...Partitioning Strategy
Section titled “Partitioning Strategy”Events partition by hash(domain + root) % partitions. All events for the same aggregate land in the same partition, preserving order.
Consumer Groups
Section titled “Consumer Groups”Handlers join consumer groups for load balancing:
Consumer Group: player-projector Consumer 1 ← Partition 0, 1 Consumer 2 ← Partition 2, 3
Consumer Group: output-projector Consumer 1 ← Partition 0, 1, 2, 3Each partition is consumed by exactly one consumer per group.
Retention
Section titled “Retention”Configure retention for replay capabilities:
# Topic-level configurationretention.ms=604800000 # 7 daysretention.bytes=-1 # Unlimited by sizeEvents remain available for replay within the retention window.
Helm Deployment
Section titled “Helm Deployment”bus: backend: kafka
kafka: enabled: true brokers: - kafka-0.kafka.messaging.svc.cluster.local:9092 - kafka-1.kafka.messaging.svc.cluster.local:9092 topicPrefix: angzarrTesting
Section titled “Testing”# Run Kafka tests (requires testcontainers)cargo test --test bus_kafka --features kafka
# Requires podman socketsystemctl --user start podman.socketMonitoring
Section titled “Monitoring”Key metrics to monitor:
| Metric | Concern |
|---|---|
| Consumer lag | Processing falling behind |
| Under-replicated partitions | Replication issues |
| Request latency | Broker performance |
| Disk usage | Retention capacity |
When to Use Kafka
Section titled “When to Use Kafka”- High volume — Millions of events/second
- Event replay — Analytics, debugging, new projectors
- Cross-team sharing — Multiple teams consuming events
- Stream processing — Kafka Streams, ksqlDB