Saturday, April 18, 2026

Microservices Distributed Transactions & Failure Handling

 

🚀 Introduction

In a monolithic system, handling transactions is simple:

  • One database
  • One transaction
  • Either commit or rollback

But in microservices, things break apart:

  • Each service has its own database
  • No global transaction manager
  • Network failures are common

👉 This creates the Distributed Transaction Problem


❗ Why Distributed Transactions Are Hard

Imagine an e-commerce flow:

Order Service → Inventory Service → Payment Service

What if:

  • Order is created ✅
  • Inventory reserved ✅
  • Payment fails ❌

Now your system is inconsistent.

Traditional ACID transactions don’t work well here because:

  • Services are independent
  • Databases are separate
  • Network failures are unavoidable

🧠 Approach 1: Two-Phase Commit (2PC)

🔹 How it works

Phase 1: Prepare
→ All services say “ready?”

Phase 2: Commit / Rollback
→ If all agree → commit
→ If any fail → rollback

⚠️ Problems with 2PC

  • Slow (blocking)
  • Single point of failure (coordinator)
  • Poor scalability
  • Locks resources for long time

👉 That’s why modern systems avoid 2PC


✅ Approach 2: Saga Pattern (Recommended)

💡 Core Idea

Break one big transaction into multiple small local transactions

T1 → T2 → T3 → T4

If something fails:

C3 → C2 → C1 (compensating transactions)

👉 Instead of rollback, we undo using business logic


🔄 Saga Flow (Text Diagram)

✅ Success Flow

[Order Service] → create order (PENDING)

[Inventory Service] → reserve stock

[Payment Service] → charge customer

[Order Service] → mark CONFIRMED

❌ Failure Flow

[Order Service] → created

[Inventory Service] → reserved

[Payment Service] → FAILED ❌

[Inventory Service] → release stock

[Order Service] → cancel order

👉 Each step has a compensation step


🧭 Types of Saga

1️⃣ Choreography (Event-driven)

No central controller. Services react to events.

OrderCreated → Inventory Service
StockReserved → Payment Service
PaymentDone → Order Service

✔ Pros:

  • Loose coupling
  • Simple for small systems

❌ Cons:

  • Hard to debug
  • Event chaos in large systems

2️⃣ Orchestration (Central Controller)

A central service controls everything.

Orchestrator:
→ Call Order Service
→ Call Inventory Service
→ Call Payment Service

✔ Pros:

  • Clear flow
  • Easier debugging

❌ Cons:

  • Central dependency

⚠️ Failure Handling Strategies

Handling failures is the core challenge in distributed systems.

🔁 1. Retry Mechanism

  • Retry temporary failures
  • Use exponential backoff
Retry → Retry → Fail → Compensation

🔄 2. Compensating Transactions

  • Undo previous steps

Example:

  • Refund payment
  • Release inventory

👉 This is the heart of Saga


🧱 3. Idempotency

Ensure repeated operations don’t break data:

Charge $100 → Retry → Should NOT charge again

📬 4. Message Queues

Use Kafka / RabbitMQ:

Service Down ❌
Message Stored ✅
Processed Later ✅

📦 5. Transactional Outbox Pattern

Ensure DB update + event publish are atomic:

DB Commit + Event Save → Then Publish

Prevents:

  • Lost messages
  • Inconsistent states

🔍 6. Observability

  • Logs
  • Tracing
  • Metrics

👉 Helps debug failures across services


⚖️ Trade-offs

Feature2PCSaga
Consistency            Strong            Eventual
Performance            Slow            Fast
Scalability            Poor            High
Complexity            Low            High
Failure Handling            Automatic rollback            Manual compensation

🧠 Key Insight

👉 In microservices, you don’t aim for perfect consistency

You aim for:

Eventual Consistency + Resilience


🏁 Conclusion

Distributed transactions are unavoidable in microservices, but traditional solutions like 2PC don’t scale.

The Saga pattern is the industry standard because:

  • It embraces failure
  • It works asynchronously
  • It scales well

But it comes with a cost:
👉 You must design failure handling explicitly


✍️ Final Thought

“In distributed systems, failure is not an exception — it’s the default.”

Thursday, March 26, 2026

Deploying Website to Azure Blob Storage


Azure Blob storage is great for hosting static websites. 

1. Create Storage Account

Data for the site will be stored in a container. But we are not going to create a container just yet. 


First, we are going to configure static sites with Azure Blob Containers. 

2. Create Static website

Inside Storage Account find the Static website

And now you can see the web container has been created


3. Upload Site Content to Blob Storage


First, we need to build the application

3.1 Build the Application First



There are multiple tools available to upload bulk data to Blob Container

  • Azure Copy, which is CLI tool 
  • Azure Storage Explore

3.2 Upload using Azure Copy

First, you need to download AzCopy

azcopy copy "source" "destination"

Source is 
"./app/dist/*" <-- All content of this dist folder

How to get  the destination 

First, we need to get a Shared Access Token so the copy tool has the necessary permissions to upload files







Select all the permissions




For this, we are going to need the Blob SAS URL





The destination is 
In the destination, we need to specify that we also have subfolders. For that we need to put -- recursive



3.3 Upload using Azure Storage Explorer

First, download the Storage Explorer.









Then, need to connect to Azure Storage Account
You can connect to a subscription or can connect to a storage account directly.
Here we are going to connect to the Storage Account directly.


To get the Connection String, go to the Azure Portal

Get the Connection String to key1 or key2


Then connect




























 Netlify

What is Netlify? 

An all-in-one platform that provides hosting and serverless backend services for web applications and static websites.


Features of Netlify

  • Continuous Deployment
  • Deploy Previews
  • Serverless Functions
  • Split Testing
  • Custom Domains

URL: https://www.netlify.com/

Deploy Previews

A preview of a deployment can be seen (by configuring from the site settings).


Split Testing



Serverless Functions






Monday, March 23, 2026

Messaging Concepts

 What is Message

RPC (Remote Procedure Calls) are unreliable. Reasons are as below


1. Remote Procedure No Server (Server is down)


2. Remote Procedure Call No Response from Server


3. Remote Procedure Call Too Many Calls. The server has handled all these requests synchronously. But nowadays we have so many asynchronous libraries to mitigate this. But still depends on the load on the server. 


Messaging

So to get around some of these problems, we can use messaging. So with messaging what we do is we impose a middleman, and that middleman is a message queue, or more generally a message broker


client can drop a message onto the queue, and depending on the message broker, we can guarantee that we know that message has been delivered or not. The server asynchronously picks up the message from the queue, and the server can then process that message. Now, depending on the type of processing, we may or may not care whether that message has been processed or not. We may or may not care synchronously whether that message has been processed or not. So we may have this fire‑and‑forget scenario, I'm just going to send out a message and let some server at some point deal with that message. And at this point as well, the message broker can guarantee that the message will be delivered. So if the server takes the message off the queue and halfway through processing blows up and hasn't yet acknowledged the message, that message will get pushed back onto the queue, and at some point it will be re-delivered. So message brokers allow redelivery of messages. So we have this idea of guaranteed delivery, where we know a server will be able to process this message at some point. 

Introduction to RabbitMQ

Where does RabbitMQ fit?
- AMQP
- Many client libraries
- Java, Python, C#, etc.


Concepts in RabbitMQ


What are Exchanges ? 
What Queues are ?
How we can use Exchanges and Queues together to publish and consume messages ? 
So once we have Exchanges and Queues, we have to bind these together. How bindings work ?
How messages are delivered and how they are consumed ?


Publishers/Producers talk to Exchanges, Consumers talk to Queues, Publishers publish messages to Exchanges, and Consumers consume those messages from Queues.


How we bind queues to exchanges and how we exchange these messages ?


The Exchanges are then bound to Queues, and as we can see, we can have multiple Queues bound to a given Exchange.

How this binding works and how we can bind these queues to an exchange ?


Publisher publishes into an exchange depending on the binding and some routing information in the published message. Those messages will end up on a Queue somewhere or maybe more than one Queue. And the Consumer will then consume the messages from those Queues. And Consumers can consume from many Queues if they need to.

Exchanges

There are different types of Exchanges
  • Topic Exchange
  • Fanout Exchange
  • Direct Exchange
  • Headers Exchange
  • Dead Letter Exchange
Each of these exchanges deals with messages differently. 
So Topic Exchanges, for example, allow us to route messages depending on some topic information. 
Direct Exchanges deliver a message directly to a Queue.
Fanout Exchanges can deliver messages to multiple Queues and have multiple processes for those messages.


Queues

A queue is an ordered collection of messages, and consumers read messages from queues. And a given consumer can process multiple queues if necessary. So we're not limited to a single queue per consumer. And essentially with the consumer when it reads from a queue, it doesn't necessarily know what sort of exchange delivered messages to that queue. All the consumer cares about is that it can get a message from a queue and somehow process that message. 
So what an exchange is, it can have different behaviors and different exchange types of different behavior. Queues can have different behavior as well. And with queues, we can define different properties on different queues. So queues have a name that we can use. Queues can be marked as Durable. So a Durable Queue is a queue that will survive a service restart.
Queues can have other properties as well. So for example, we can mark a queue as exclusive. We can define the delete semantics on the queue, and there are other arguments we can use when we create the queue.



Binding

So if queues are used by consumers and exchanges are used by publishers and we've said that the consumer doesn't really care about the exchange, how does RabbitMQ know which messages that are published to an exchange go onto which queues to be consumed by a consumer? Well, that's done via something called a binding. So queues are bound to exchanges. And when we do the binding, we specify three things.
  • Consists of exchange
  • Routing Key
  • Queue

We can specify the exchange to bind to, the queue we want to bind to that exchange, and something called a Routing Key. And the routing key defines which messages that come into that exchange get posted to this queue. So not all messages that go to an exchange get passed on to all the queues that are bound to that exchange. The messages that are passed on to a specific queue are defined via the routing key. And different exchanges will behave differently with regard to this routing key. For example, a fanout exchange ignores the routing key, and it sends all messages to all queues that are bound to that exchange. Queue is bound to an Exchange via a Routing Key. fanout exchange ignores the routing key.
Because of the separation between exchanges and queues and because of this abstraction of the routing key, we get a lot of flexibility in the way that messages can be processed by RabbitMQ and, therefore, the way that we can process those messages within our application.






Tuesday, March 28, 2023

What is Backstage ?

 Backstage is constructed out of three parts

1. Core

2. App

3. Plugins


What is Storybook ?

 Storybook is the most popular tool for UI component development and documentation.

Each story allows you to demonstrate a specific component variation to verify appearance and behaviour.

Previously, you'd have to spin up the app, navigate to a page, and contort the UI into the correct state. This is a massive waste of time and bogs down front-end development. With Storybook, you can skip all those steps and jump straight to working on a UI component in a specific state.

Storybook makes it easy to work on one component in one state (aka a story) at a time.

Wednesday, February 22, 2023

Azure AD

 Azure AD is Microsoft’s cloud-based identity and access management service. It's

- Lets you manage internal and external users

- Allows you to manage devices

- Allows you to manage first party and third party applications

When you log into Azure Portal (using username and password) that particular account is authenticated by Azure AD. That particular account may have zero or more Subscriptions. Azure Portal is just a website that is protected by Azure AD. Azure AD is free and you don't need a subscription to create an Azure AD. 


Modern Authentication

- WS-* (WS-Federation is under WS-*) and SAML

- OAuth (is a standard for delegation)

I am allowing "A" to do "B" on my behalf. At no point does A get my password. B is a set of permission.

Eg:- I (Anuruddha on Twitter), am allowing website www.somesite.com(A) to read my profile. And in exchange A gets to know who I am, and offers me some functionality. There is no standards

- OpenID Connect

Add strict standards so OAuth can be used as an authentication protocol. The identity layer builds on top of OAuth 2.0. 

OpenID Connect Flows

- Implicit (If you are using JS SPA)

- PKCE (For Mobile apps, for redirects)

- AuthCode (Build of Token. ID Token, Access Token, Refresh Token - to renew the Access Token silently)

- Client Credentials (Pass Client ID and you get Access Token back, no refresh token.)