Why avoid microservice

Sunil Singh
3 min readAug 20, 2023

--

Developing microservices seems easy, but it comes with a lot of cost. The hardest part of microservices is communication and achieving transactions across services.If you don't require it, avoid it altogether. Lets take below simple example

Let's consider a simple case here. We are printing invoices, and we have to put them in sequence with a few metadata and report this invoice to some system at the end of the month.Let's consider each invoice having the below format:

This is three invoices generated.

How monolith service will work

Table structure in monolith

As the invoice number or ID is auto-generated, we are guaranteed to get a unique invoice number always. In case the user updates metadata, same will get reflected in invoices.Below apis are necessary to work in invoice

1)CreateUser()
2)UpdateUser().Example: change phone number
3)CreateInvoice() →make code transactional so that everything rolls back in case of failure
4)Update Invoices.

Things will run smoothly in below design for monolith

Lets break this use case in three services.

Service 1: Sequencer Service .This service will generate the sequence of invoices.

Service 2:User service to store all the metadata related to invoice number.

Service 3:Invoice Service .This service will generate the invoice and store all the metadata.

Sequencer Service:
We will assume the system has a very low load to keep things simple .Here we will not auto generate the key and assume logic is simple to auto-increment the sequence number.

Table structure of sequencer

Now, whenever we get a request for a new invoice number, we will automatically enter the Invoice number and serve the request.

User service:
User service table

We will have create/update api for the same

Invoice Service

Invoice service will have following api
CreateApi
updateApi

All Three service structures

Lets try to create one invoice through create Api and see what all can wrong and how to fix

CreateInvoiceApi

GetInvoiceNumberFromSeqService()
TryTogetValidCustomerDetailsFromUserService()
CreateInvoice i.e. persist in db

What can go wrong .

Consider that we have executed statement one(GetInvoiceNumberFromSeqService()) and then our invoice service
went down .

Q1)How will we pick the transaction from there, as the invoice number is already generated but transation not commited?

Q2) Consider invoice generated succesfully and customer came updated his number in User service .How we will show this change in invoice service.

Solution
we will talk about saga pattern, where consistency can be acheived across microservices .All microservices have to be implemented with idempotency and retry logic and talk to each other in sense of event else it will fail.

--

--