Event Driven Microservice for dummies

Part-2

Shafin Hasnat
3 min readMay 6, 2022

Read Part-1

Introduction

So, in the first part, we built the auth service. We can now generate an OTP and login with that OTP. Now we will build mail service. Where we will send the generated OTP via email. This email service is loosely coupled with the auth service via a event bus.

What we gonna do is, we will generate an event when user hits /send_otp endpoint. Our email service will listen for the event. Upon event, our email service will retrieve the OTP and put an email sending function in a task queue with the OTP. And user will have the OTP in their email.

We will use Kafka here for message broker and Redis queue (RQ) for task queue.

Provisioning Kafka and Zookeeper in docker-compose

In this stage, we will add kafka and zookeeper in our docker-compose.yml in the VM.

Let’s make it up and running with

sudo docker-compose up -d

Command.

So now we have kafka running, and we are gonna use it as event bus of our microservice. Kafka is available in 9093 port of our vm.

Create event from auth service

So what we gonna do now is, when user hits /send_otp endpoint with an email, we will generate an event to kafka. It’s a few lines of code-

Add environment variable to the docker-compose.yml in the codebase.

Initiate kafka in our auth service. Go to __init__.py-

We will produce event from auth service when otp is generated. Go to src/user/api.py-

So, as soon as user hits a post request /send_otp endpoint with an email, that email goes to ‘otp_topic’ in kafka. We can consume the message and check directly from kafka-

For this, just ssh to the vm and go inside the kafka container and run the commands-

$ cd opt/bitnami/kafka/bin
$ kafka-console-consumer.sh --bootstrap-server kafka:9092 --topic otp_topic --from-beginning

Now it’s time to create mail service.

Mail service

Here we will consume email from kafka and send it in an task queue. make a directory called mail_service.

Let’s start with requirements.txt

Now Dockerfile

Now we will write a function for sending email. Make a file func.py and edit it like this-

Now we will use send_mail function when an event is generated. But first, we have to consume the message from kafka.

Create app.py

So, if we run app.py, it will listen for otp_topic. As soon as we hit /send_otp from auth_service, we will see the otp generated from redis.

Now it’s time to provision RQ.

In app.py-

That’s pretty much it. Now re-run app.py and start rq worker with this command-

rq worker --url redis://127.0.0.1:6379 default

Now we can see our mail service in action.

So, we gonna build and push mail_service and deploy in web.

docker build -t dockeruser/mail-service:latest .
docker push dockeruser/mail-service:latest

Deploy

ssh into the vm, open docker-compose.yml in vi editor and add the lines-

Now apply the changes and make up using-

sudo docker-compose up -d

command.

So, our mail_service is now up and running.

That’s all for this part. By now, we have a microservice of 2 individual services running. In future parts, we will continue from this part.

--

--