Middle+
How was Kafka used in your project?
sobes.tech AI
Answer from AI
In my project, Kafka was used as a distributed messaging system to ensure reliable and scalable data transfer between microservices. For example, services published events about user actions (registration, purchases) to Kafka topics, and other services — subscribers — processed these events asynchronously, which helped to unload main business processes and increase fault tolerance.
To work with Kafka in Go, the sarama client was used, which allowed creating producers and consumers with the necessary configuration, including partition management and error handling.
Example of a simple producer in Go using sarama:
package main
import (
"log"
"github.com/Shopify/sarama"
)
func main() {
producer, err := sarama.NewSyncProducer([]string{"localhost:9092"}, nil)
if err != nil {
log.Fatal("Failed to start producer:", err)
}
defer producer.Close()
msg := &sarama.ProducerMessage{
Topic: "user-events",
Value: sarama.StringEncoder("User registered: user123"),
}
partition, offset, err := producer.SendMessage(msg)
if err != nil {
log.Println("Failed to send message:", err)
} else {
log.Printf("Message sent to partition %d at offset %d\n", partition, offset)
}
}