Another New Year is coming, I take some time to look back on my accumulated knowledge, I am lucky to have access to and work with the microservices system and have been taught a lot by my predecessors, so I would like to note down one of the technologies that are used very commonly in Microservices systems today is Kafka.
In the framework of the article, I will not talk about concepts but would like to share the steps of kafka's Pattern pub — sub application to build a push email feature when a user successfully submits an order.
I divided into 2 services here including: order and noti with the main roles respectively as follows:
- order-service (order): manage user orders
- noti-service (noti): push notifications such as email, sms to users
When the user successfully pays the order, the order will push (pub) a mess (mess content you can define) into Kafka and continue to process other requests from the user. Sending confirmation email will be noti get (sub) mess from Kafka and send. The division of services highlights the advantages such as ease of scalability and maintainability of microservices, unfortunately, a broken note will not affect the user's purchasing feature. Not stopping at noti, you can completely develop new services such as reward, shipment, ...
1. Producer
I use kafkajs package to setup producer in order-service and consumer in noti-service.
- kafkajs: kafka.js.org
After recording the user's successful order, orderInfo will be pushed into Kafka via the sendMessage function defined as above.
2. Consumer
noti-service will take the mess that was pushed into kafka previously from the order-service, build the email and send to the user the confirmed order information.
- nodemailer: npmjs.com/package/nodemailer
That's it, finally I tested the full flow from successful purchase -> send email and received the results. Tadaaaa.
At this point, I hope my little knowledge of kafka can be shared with everyone about how to apply it in practice in microservices. I would like to leave the link of the full source code of the project here.
- Source code: github.com/lequocduyquang/micro-kafka
The article certainly has many shortcomings, I hope everyone can contribute and share so that I can learn and improve. Thank you so much to everyone who took the time to read my post.
If everyone finds the article interesting, please give me 1 subscribe or 1 github star. Wishing everyone the very best for the new year.
3. Reference source:
- Confluent: confluent.io/blog/getting-started-with-kafk..
- Nodemailer: nodemailer.com/smtp/testing