Last Updated: November 21, 2025
Apache Kafka
Distributed streaming platform
Core Concepts
| Item | Description |
|---|---|
Topic
|
Stream of records |
Producer
|
Publishes messages |
Consumer
|
Reads messages |
Partition
|
Ordered log for topic |
Offset
|
Position in partition |
Consumer Group
|
Load balancing consumers |
Broker
|
Kafka server |
Producer & Consumer
// Producer
const { Kafka } = require('kafkajs')
const kafka = new Kafka({
clientId: 'my-app',
brokers: ['localhost:9092']
})
const producer = kafka.producer()
await producer.connect()
await producer.send({
topic: 'user-events',
messages: [
{ key: 'user1', value: JSON.stringify({ action: 'signup' }) }
],
})
// Consumer
const consumer = kafka.consumer({ groupId: 'my-group' })
await consumer.connect()
await consumer.subscribe({ topic: 'user-events' })
await consumer.run({
eachMessage: async ({ topic, partition, message }) => {
console.log({
value: message.value.toString(),
})
},
})
Common Patterns
| Item | Description |
|---|---|
Event Sourcing
|
Store all state changes |
CQRS
|
Separate read/write models |
Log Aggregation
|
Collect logs from services |
Stream Processing
|
Real-time data processing |
CDC
|
Capture database changes |
Best Practices
- Use appropriate partition keys
- Set retention policies per topic
- Monitor consumer lag
- Use consumer groups for scalability
💡 Pro Tips
Quick Reference
Kafka guarantees order within a partition only