Running Distributed Transactions in Kubernetes

Eduardo Patrocinio
2 min readJan 3, 2018

--

In his article at http://www.grahamlea.com/2016/08/distributed-transactions-microservices-icebergs/, Graham Lea vehemently advise not to run distributed transactions as a micro service application. He goes through a series of steps and solutions to mitigate the risk of implementing distributed transactions using microservices architecture.

Since the article publication, container orchestration layer has evolved considerably. Kubernetes now provides resilience capabilities with ReplicaSet and StatefulSet.

Are these features enough to allow implementing a distributed transaction using microservice?

In this article, I will describe a simple banking application. Then, I will implement a lot of disruption into the system, to test the ability of keep the atomicity of the transaction, then go to the step to correct (or at least minimize) the gaps for a microservice-based distributed transaction system

The application

The application consists of a simple banking system, with the following initial components:

For the banking system:

  • An account microservice (one instance for each account)
  • A balance microservice
  • A transfer microservice
  • An accountSystem micro-service (to retrieve information from all accounts)

For the validation layer:

  • A balance validator microservice

Each account will start with a balance of $100, and the goal is that the overall balance of all accounts is the number of account x $100, after many inter-account transfers and disruptions to the system.

For now, we are not going to implement any security, but it certainly need to be implement in any banking system

Take 1 — Initial microservice

In this step, we will create the following microservices:

  • account
  • balance
  • accountSystem
  • balanceValidator

as described in the following diagram:

The goal is to ensure the balanceValidator reports an overall balance of $200 for 2 accounts (Account1, Account2).

The accountSystem will have a list of all accounts available.

Here is the algorithm for the balanceValidator

sum = 0

accounts = accountBalance.getAccounts()

For each account in accounts

sum += balance.getBalance(account)

assert (sum = 100*accounts.size())

You can find the source code at the following repository: https://github.com/patrocinio/microbank

You can get the application by following these steps:

  • Clone the source code:
    git clone https://github.com/patrocinio/microbank.git
  • Go to the repository directory:
    cd scripts
  • Run the following script to deploy the microbank:
    ./deployMicrobank.sh
  • Finally run the following script to deploy the microbank management components:
    ./deployMicrobankManager.sh
  • Run this script to validate the overall account balances:
    ./validateBalance.sh
  • You will see a result like this:

patro:scripts edu$ ./validateBalance.sh
Context “mycluster.icp-context” set.
Namespace set to microbank-manager
Deleting job
Error from server (NotFound): jobs.batch “validate-balance” not found
Creating job
job “validate-balance” created
Pod: validate-balance-bb30h

> node-api@ start /usr/src/app
> node server.js

Sum and expected balances match: 200
Succeeded

In the next article, I will talk about the different kinds of disruptions we will bring to the system.

--

--

Eduardo Patrocinio
Eduardo Patrocinio

Written by Eduardo Patrocinio

Principal Solutions Architect, Strategic Accounts, AWS

No responses yet