1. Architectural Overview: Log-Based Brokers vs Traditional Queues
Unlike destructive AMQP queues that delete a message once acknowledged, a Log-Based Broker (Apache Kafka, Redpanda, AWS Kinesis) models a topic as a set of append-only, totally ordered partitions.
flowchart LR
P["Producer"] -->|"hash(key) mod N"| Part0["Partition 0: [0, 1, 2, 3]"]
P -->|"hash(key) mod N"| Part1["Partition 1: [0, 1, 2]"]
Part0 --> CG1["Consumer Group A (Worker 1)"]
Part1 --> CG2["Consumer Group A (Worker 2)"]
Part0 --> Audit["Consumer Group B (Audit Replay @ Offset 0)"]
Part1 --> Audit
Key Architectural Invariants
- Per-Key Ordering via Deterministic Hashing: Global ordering across a distributed cluster requires coordination bottlenecks, whereas per-key ordering only requires routing
fnv1aHash(key) % numPartitions. All events foraccount-99land in the exact same partition in strict sequential offset order. - Exclusive Partition Ownership Within a Consumer Group: Within a single
ConsumerGroup, each partition is assigned to at most one consumer instance (generationId++on join/leave rebalance). This guarantees order-preserving parallel consumption without distributed locks. - Non-Destructive Reads & Offset Replay: Because consumers track progress via
committedOffsets[partition] = offset + 1, multiple independent services (e.g.,billing-serviceandfraud-detector) consume the same stream concurrently, and any consumer canseek(partition, 0)to replay historical events.
2. Verification & Running the Lab
bash
node demo.js
node test.js