Asynchronous Processing with Go using Kafka and MongoDB

In my previous blog post "My First Go Microservice using MongoDB and Docker Multi-Stage Builds", I created a Go microservice sample which exposes a REST http endpoint and saves the data received from an HTTP POST to a MongoDB database.

In this example, I decoupled the saving of data to MongoDB and created another microservice to handle this. I also added Kafka to serve as the messaging layer so the microservices can work on its own concerns asynchronously.

In case you have time to watch, I recorded a walkthrough of this blog post in the video below :)

Here is the high-level architecture of this simple asynchronous processing example wtih 2 microservices.

Microservice 1 - is a REST microservice which receives data from a /POST http call to it. After receiving the request, it retrieves the data from the http request and saves it to Kafka. After saving, it responds to the caller with the same data sent via /POST

Microservice 2 - is a microservice which subscribes to a topic in Kafka where Microservice 1 saves the data. Once a message is consumed by the microservice, it then saves the data to MongoDB.

Before you proceed, we need a few things to be able to run these microservices:

  1. Download Kafka - I used version kafka_2.11-1.1.0
  2. Install librdkafka - Unfortunately, this library should be present in the target system
  3. Install the Kafka Go Client by Confluent
  4. Run MongoDB. You can check my previous blog post about this where I used a MongoDB docker image.

Let's get rolling!

Start Kafka first, you need Zookeeper running before you run the Kafka server. Here's how

$ cd /<download path>/kafka_2.11-1.1.0
$ bin/ config/

Then run Kafka - I am using port 9092 to connect to Kafka. If you need to change the port, just configure it in config/ If you are just a beginner like me, I suggest to just use default ports for now.

$ bin/ config/

After running Kafka, we need MongoDB. To make it simple, just use this docker-compose.yml.

version: '3'
    image: mongo
      - "27017:27017"
      - "mongodata:/data/db"
      - network1



Run the MongoDB docker container using Docker Compose

docker-compose up

Here is the relevant code of Microservice 1. I just modified my previous example to save to Kafka rather than MongoDB.


func jobsPostHandler(w http.ResponseWriter, r *http.Request) {

	//Retrieve body from http request
	b, err := ioutil.ReadAll(r.Body)
	defer r.Body.Close()
	if err != nil {

	//Save data into Job struct
	var _job Job
	err = json.Unmarshal(b, &_job)
	if err != nil {
		http.Error(w, err.Error(), 500)


	//Convert job struct into json
	jsonString, err := json.Marshal(_job)
	if err != nil {
		http.Error(w, err.Error(), 500)

	//Set content-type http header
	w.Header().Set("content-type", "application/json")

	//Send back data as response


func saveJobToKafka(job Job) {

	fmt.Println("save to kafka")

	jsonString, err := json.Marshal(job)

	jobString := string(jsonString)

	p, err := kafka.NewProducer(&kafka.ConfigMap{"bootstrap.servers": "localhost:9092"})
	if err != nil {

	// Produce messages to topic (asynchronously)
	topic := "jobs-topic1"
	for _, word := range []string{string(jobString)} {
			TopicPartition: kafka.TopicPartition{Topic: &topic, Partition: kafka.PartitionAny},
			Value:          []byte(word),
		}, nil)

Here is the code of Microservice 2. What is important in this code is the consumption from Kafka, the saving part I already discussed in my previous blog post. Here are the important parts of the code which consumes the data from Kafka.


func main() {

	//Create MongoDB session
	session := initialiseMongo()
	mongoStore.session = session



func receiveFromKafka() {

	fmt.Println("Start receiving from Kafka")
	c, err := kafka.NewConsumer(&kafka.ConfigMap{
		"bootstrap.servers": "localhost:9092",
		"":          "group-id-1",
		"auto.offset.reset": "earliest",

	if err != nil {

	c.SubscribeTopics([]string{"jobs-topic1"}, nil)

	for {
		msg, err := c.ReadMessage(-1)

		if err == nil {
			fmt.Printf("Received from Kafka %s: %s\n", msg.TopicPartition, string(msg.Value))
			job := string(msg.Value)
		} else {
			fmt.Printf("Consumer error: %v (%v)\n", err, msg)



func saveJobToMongo(jobString string) {

	fmt.Println("Save to MongoDB")
	col := mongoStore.session.DB(database).C(collection)

	//Save data into Job struct
	var _job Job
	b := []byte(jobString)
	err := json.Unmarshal(b, &_job)
	if err != nil {

	//Insert job into MongoDB
	errMongo := col.Insert(_job)
	if errMongo != nil {

	fmt.Printf("Saved to MongoDB : %s", jobString)


Let's get down to the demo, run Microservice 1. Make sure Kafka is running.

$ go run rest-kafka-sample.go

I used Postman to send data to Microservice 1

Here is the log you will see in Microservice 1. Once you see this, it means data has been received from Postman and saved to Kafka

Since we are not running Microservice 2 yet, the data saved by Microservice 1 will just be in Kafka. Let's consume it and save to MongoDB by running Microservice 2.

$ go run kafka-mongo-sample.go

Now you'll see that Microservice 2 consumes the data and saves it to MongoDB

Check if data is saved in MongoDB. If it is there, we're good!

Complete source code can be found here

Shameless plug! If you like this blog post, please follow me in Twitter @donvito. I tweet about Docker, Kubernetes, GoLang, Cloud, DevOps, Agile and Startups. Would love to connect in GitHub and LinkedIn



Melvin Vivas

Melvin Vivas

Father of 2 wonderful kids, loves programming, wannabee gopher, currently working at an international bank. Bloggin' about Golang, Docker, and Kubernetes.

Read More