46.1 Real-Time Event Processing Topology
Sources Kafka Topics Consumers
┌──────────┐ ┌──────────────────────────┐ ┌──────────────┐
│ External │─────▶│ events.submitted │─────▶│ Validator │
│ feed A │ │ │ │ Service │
└──────────┘ ├──────────────────────────┤ └──────┬───────┘
│ events.validated │─────▶│ Enrichment │
┌──────────┐ ├──────────────────────────┤ │ Service │
│ System │─────▶│ records.new │ └──────┬───────┘
│ B feed │ ├──────────────────────────┤ │
└──────────┘ │ anomalies.detected │─────▶│ Alerting/Agent│
├──────────────────────────┤ │ Service │
┌──────────┐ │ outcomes.received │ └──────────────┘
│ System C │─────▶│ │
│ feed │ └──────────────────────────┘
└──────────┘
46.2 Kafka Core Concepts
| Concept | Description | Interview Relevance |
|---|---|---|
| Topics | Logical channels for messages | One per event type |
| Partitions | Parallel processing units | Controls throughput & ordering |
| Consumer Groups | Multiple consumers sharing work | Horizontal scaling |
| Replication | Data durability (RF=3) | No message loss |
| Exactly-once | Idempotent producers + transactions | Critical for financial/ordered data |
| Schema Registry | Schema versioning (Avro/Protobuf) | Backward compatibility |
| Kafka Connect | Source/sink connectors | Database CDC, S3 sink |
| Kafka Streams/ksqlDB | Stream processing | Real-time aggregations |
46.3 Kafka vs. Kinesis
| Dimension | Kafka (MSK) | Kinesis |
|---|---|---|
| Management | Semi-managed | Fully managed |
| Throughput | 1M+ msg/sec | Depends on shard count |
| Retention | Configurable (days–forever) | 7 days (extendable) |
| Consumer model | Pull (consumer-driven) | Push & pull |
| Cost | Broker-based (fixed) | Shard-based (pay per) |
| Ecosystem | Kafka Connect, Streams, ksqlDB | Lambda integration |
| Best for | High volume, complex routing | AWS-native, simpler setups |
46.4 Design Guarantees for Financial/Ordered Event Streams
- Exactly-once processing: idempotent producers + transactional consumers.
- Ordering: partition by the natural entity key (e.g.,
order_id/claim_id) so all events for that entity process in order. - Schema evolution: Avro + Schema Registry, backward-compatible changes only (never remove/rename a required field without a version bump and dual-write period).
- Dead-letter queue: failed messages route to a DLQ for investigation — never silently dropped.
- Monitoring: consumer lag (alert if it exceeds a threshold, e.g. 1000 messages), throughput per topic, error rate per consumer group.
46.5 Pipeline Selection Matrix
| Requirement | Real-Time (Kafka/Kinesis) | Batch (Spark/EMR) | Micro-Batch (Spark Streaming) |
|---|---|---|---|
| Latency | Milliseconds | Hours | Seconds–minutes |
| Volume | Event-by-event | Large datasets | Medium datasets |
| Cost | Higher (always running) | Lower (on-demand) | Medium |
| AI integration | Inference per event | Model training | Feature computation |
Feeding AI agents with real-time data:
Kafka Topic: events.stream
│
├── Consumer: Risk Scorer
│ → Calls inference endpoint per event
│ → Publishes to events.risk_scores topic
│
├── Consumer: NLP/Enrichment Pipeline
│ → Processes unstructured text as it arrives
│ → Updates vector DB for RAG
│
└── Consumer: Agent Trigger Service
→ High-risk events trigger an agentic workflow
→ Agent combines real-time context + historical data
46.6 Q&A
Q: How would you design a real-time event processing pipeline using Kafka guaranteeing no data loss or duplication for financial data? Topics per event stage (submitted → validated → processed → responded); one consumer group per downstream service for independent horizontal scaling; idempotent producers with enable.idempotence=true plus transactional writes for exactly-once semantics; partition key set to the business entity ID to preserve per-entity ordering; Avro schemas registered in Schema Registry with backward-compatibility enforcement; DLQ topic for poison messages; replication factor 3 with min.insync.replicas=2 for durability; monitor consumer lag continuously as the primary health signal.
Q: Kafka or Kinesis for a new AWS-native streaming pipeline? Kinesis if the team wants a fully managed, AWS-native service with tight Lambda integration and lower operational overhead, and throughput needs are moderate/shard-scalable. MSK (Kafka) if you need very high throughput, complex routing/stream-processing (ksqlDB, Kafka Streams), long or indefinite retention, or portability across clouds. For high-volume, multi-consumer-group architectures with CDC ingestion, Kafka’s ecosystem (Connect, Streams) usually wins.