Junior — Middle
Can you share your experience using Apache Kafka in projects?
sobes.tech AI
Answer from AI
In projects with Node.js, I used Apache Kafka to organize reliable and scalable message transfer between microservices. The main scenarios included:
- Asynchronous event processing, such as logging or order processing.
- Integration with external systems through producers and consumers.
- Ensuring fault tolerance by storing messages in topics.
To work with Kafka in Node.js, I usually use libraries like kafkajs or node-rdkafka. Here's an example of a simple producer with kafkajs:
const { Kafka } = require('kafkajs')
const kafka = new Kafka({ clientId: 'my-app', brokers: ['kafka1:9092'] })
const producer = kafka.producer()
async function sendMessage() {
await producer.connect()
await producer.send({
topic: 'test-topic',
messages: [ { value: 'Hello Kafka' } ],
})
await producer.disconnect()
}
sendMessage().catch(console.error)
Experience has shown that it is important to properly configure error handling and session management to avoid message loss and ensure high availability.