Purpose
Design reliable, scalable Kafka-based event streaming architectures with proper partitioning, ordering, and error handling strategies.
When to Use
Use this skill when the task involves:
- Designing Kafka topics and partition strategies
- Implementing producers or consumers
- Setting up consumer groups for scaling
- Handling failures with retries and DLQs
- Configuring exactly-once or at-least-once semantics
- Integrating with Schema Registry (Avro, Protobuf)
Constraints
- Enable idempotent producer (
enable.idempotence=true) by default
- Use Schema Registry for message contracts in production
- Design partition keys based on ordering requirements
- Implement dead letter queues for poison messages
- Set appropriate retention based on replay needs
- Use transactional producers for exactly-once across topics
- Monitor consumer lag as a key health metric
Execution Steps
- Identify events — List domain events with their schemas
- Design topics — Name conventions, partition count, replication factor
- Define partition keys — Choose keys that ensure ordering where needed
- Implement producers — Idempotent with proper serializers
- Build consumers — Consumer groups, offset management, error handling
- Add DLQ — Configure dead letter topic for failed messages
- Set up monitoring — Consumer lag alerts, throughput dashboards
Examples
Input
Design a Kafka topic for order events that maintains per-customer ordering
Output
// Topic Configuration
const topicConfig = {
topic: 'orders.events.v1',
partitions: 12, // Scale based on throughput needs
replicationFactor: 3,
config: {
'retention.ms': 7 * 24 * 60 * 60 * 1000, // 7 days
'cleanup.policy': 'delete',
'min.insync.replicas': 2,
},
};
// Producer with idempotency
import { Kafka, Partitioners } from 'kafkajs';
const kafka = new Kafka({
clientId: 'order-service',
brokers: ['kafka:9092'],
});
const producer = kafka.producer({
idempotent: true,
maxInFlightRequests: 5,
createPartitioner: Partitioners.DefaultPartitioner,
});
interface OrderEvent {
orderId: string;
customerId: string;
type: 'CREATED' | 'UPDATED' | 'CANCELLED';
payload: Record<string, unknown>;
timestamp: string;
}
async function publishOrderEvent(event: OrderEvent): Promise<void> {
await producer.send({
topic: 'orders.events.v1',
messages: [
{
key: event.customerId, // Partition by customerId for ordering
value: JSON.stringify(event),
headers: {
'event-type': event.type,
'correlation-id': event.orderId,
},
},
],
});
}
// Consumer with error handling
const consumer = kafka.consumer({
groupId: 'order-processor',
maxWaitTimeInMs: 100,
retry: { retries: 3 },
});
await consumer.subscribe({ topic: 'orders.events.v1', fromBeginning: false });
await consumer.run({
eachMessage: async ({ topic, partition, message }) => {
try {
const event = JSON.parse(message.value?.toString() || '{}');
await processOrderEvent(event);
} catch (error) {
// Send to DLQ after retries exhausted
await sendToDLQ('orders.events.v1.dlq', message, error);
}
},
});
Related Skills
nest-backend-service-builder — Integrate Kafka with NestJS
database-schema-designer — Outbox pattern for reliability
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: kafka-stream-designer3description: Design Kafka topics, partitions, consumer groups, producers with idempotency, retry strategies, dead letter queues, exactly-once semantics, and schema registry integration Use when this capability is needed.4---56# Purpose78Design reliable, scalable Kafka-based event streaming architectures with proper partitioning, ordering, and error handling strategies.910## When to Use1112Use this skill when the task involves:1314- Designing Kafka topics and partition strategies15- Implementing producers or consumers16- Setting up consumer groups for scaling17- Handling failures with retries and DLQs18- Configuring exactly-once or at-least-once semantics19- Integrating with Schema Registry (Avro, Protobuf)2021## Constraints2223- Enable **idempotent producer** (`enable.idempotence=true`) by default24- Use **Schema Registry** for message contracts in production25- Design **partition keys** based on ordering requirements26- Implement **dead letter queues** for poison messages27- Set appropriate **retention** based on replay needs28- Use **transactional producers** for exactly-once across topics29- Monitor **consumer lag** as a key health metric3031## Execution Steps32331. **Identify events** — List domain events with their schemas342. **Design topics** — Name conventions, partition count, replication factor353. **Define partition keys** — Choose keys that ensure ordering where needed364. **Implement producers** — Idempotent with proper serializers375. **Build consumers** — Consumer groups, offset management, error handling386. **Add DLQ** — Configure dead letter topic for failed messages397. **Set up monitoring** — Consumer lag alerts, throughput dashboards4041## Examples4243### Input4445> Design a Kafka topic for order events that maintains per-customer ordering4647### Output4849```typescript50// Topic Configuration51const topicConfig = {52 topic: 'orders.events.v1',53 partitions: 12, // Scale based on throughput needs54 replicationFactor: 3,55 config: {56 'retention.ms': 7 * 24 * 60 * 60 * 1000, // 7 days57 'cleanup.policy': 'delete',58 'min.insync.replicas': 2,59 },60};6162// Producer with idempotency63import { Kafka, Partitioners } from 'kafkajs';6465const kafka = new Kafka({66 clientId: 'order-service',67 brokers: ['kafka:9092'],68});6970const producer = kafka.producer({71 idempotent: true,72 maxInFlightRequests: 5,73 createPartitioner: Partitioners.DefaultPartitioner,74});7576interface OrderEvent {77 orderId: string;78 customerId: string;79 type: 'CREATED' | 'UPDATED' | 'CANCELLED';80 payload: Record<string, unknown>;81 timestamp: string;82}8384async function publishOrderEvent(event: OrderEvent): Promise<void> {85 await producer.send({86 topic: 'orders.events.v1',87 messages: [88 {89 key: event.customerId, // Partition by customerId for ordering90 value: JSON.stringify(event),91 headers: {92 'event-type': event.type,93 'correlation-id': event.orderId,94 },95 },96 ],97 });98}99100// Consumer with error handling101const consumer = kafka.consumer({102 groupId: 'order-processor',103 maxWaitTimeInMs: 100,104 retry: { retries: 3 },105});106107await consumer.subscribe({ topic: 'orders.events.v1', fromBeginning: false });108109await consumer.run({110 eachMessage: async ({ topic, partition, message }) => {111 try {112 const event = JSON.parse(message.value?.toString() || '{}');113 await processOrderEvent(event);114 } catch (error) {115 // Send to DLQ after retries exhausted116 await sendToDLQ('orders.events.v1.dlq', message, error);117 }118 },119});120```121122## Related Skills123124- `nest-backend-service-builder` — Integrate Kafka with NestJS125- `database-schema-designer` — Outbox pattern for reliability126127---128> Converted and distributed by [TomeVault](https://tomevault.io/claim/phatpham9) — claim your Tome and manage your conversions.129<!-- tomevault:4.0:skill_md:2026-04-12 -->