Saga Patterns by Example 
Published: 2020-08-11 15:49:26

## Moving from Monolithic application to microservices

A very common scenario nowadays is migrating Monolith applications to a new Microservice Architecture. 

We are not going to focus on explaining these architectures in this article. Instead, we are focusing on a very specific issue when it comes to dealing with Microservices: Data Consistency.

With MicroServices we are usually going to follow a **One Database per service** pattern. Imagine business transactions spanning across multiple services, meaning one transaction completion or failure will impact subsequent transactions. We need to maintain **data consistency**. 

We can’t use ACID local transactions, we need to solve this issue differently.

## Saga

We’ll need **Saga: Sequence of local transactions.**
Each local transaction will

1. Trigger the associated action (This will generally be a DB update, but can also be something like sending an email to a customer etc..)
2. Publish a message/event to trigger the next local transaction

If the local transaction fails, there will be a series of compensating transactions to undo preceding local transactions. Undoing things doesn’t necessarily mean deleting data in case the action is a DB transaction, but rather updating some state to **FAILED**.

Enough theory, let’s get to a specific business which we will use to explore the specifics of what we described and to understand Saga.

## Enrollment Credit Card 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vdc1g72h0lfbqrxvi2gy.png)

Let’s imagine a Bank has an _Enrollment to New Credit Card_ process. Whenever a customer wants to ask for a new Credit Card, they will enter a bunch of data through some Forms. 

* This data will get to a Card Service that will create the data needed to create Digital Credit Card on the fly (in the meantime the actual physical card gets processed and sent)

* For this purpose it will first check the provided customer’s data is accurate and can be validated  through the Identity Service (maybe this service will use a third party service like Equifax to check the customer’s identity)
 
* Then, the Calculation Service will determine the credit limit for the new credit card based on internal calculations and other factors

* Once everything is successfully completed the Card Service can finally return all the data for the new Digital Credit Card to the customer that was computed in the beginning.

## Choreography Pattern

Saga comes into 2 different flavors. First, we’ll explore the simplest and most direct one: The Choreography pattern. 

### Use Case 

The best way to describe this is to show the diagrams of the sequences of transactions happening just after the customer has entered all the required data in the form.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gvbylcc03bfwgvfk2pue.png)

The input data will get to the Card Service that will create a new record in the Card DB. Note we have a status value, which will play a key role here. For now it’s set with **VERIFYING** value.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/w5axtn9djqa5j20kw0j2.png)

Once the record is successfully persisted, a new event will be streamed by Card Service and consumed by Identity Service. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/k514k36mli2bqfc7xw3h.png)

Identity service will consume the event, then it will execute its internal logic and persist a new record in the Identity DB. The **isVerified** attribute will be a boolean stating if the identity was indeed verified or not.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qckb6aaaf3cgd00z488i.png)

Once the record is successfully persisted, a new event will be streamed and will be consumed by Card Service and Calculation Service.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mgt5b8inos0y0vban8q4.png)

The event consumed by Card Service will actually trigger an update of the record persisted in the first step. The status value will be updated to **IDENTITY_VERIFIED**. We are notifying the main Service that the identity was verified. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ovmcplkoiargx3or87av.png)

The event will also be consumed by Calculation Service. This one will execute some internal logic so that the goal is to calculate the amount Limit for the potential new Credit Card and persist this new data to a new record in the Amount DB.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ss6ij6uugfhu0qsyscoe.png)

Once the record is successfully persisted a new event with the amount limit data will be streamed to be consumed by the Card Service.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/83nio8pjma142g0kt36i.png)

Finally, once the event is consumed by the Card Service we will again update the initial record with its final state. The **amountLimit** value will be updated and the status will change to **VERIFIED**. This status value means the new Credit Card Data is ready to be returned to the customer so they can start using their new Digital Credit Card.

### Implementation details

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/r8qsopwbkb1wfn7ee7fx.png)

Let’s imagine how we would implement this in the cloud, more specifically with Azure. We are showing  just a small part of the implementation as we can extrapolate this to the other services.

As we can see from the diagram above, the services are implemented as Spring boot services. Whenever a new request gets to the Card Service, a new record is persisted in a Cosmos DB Card container. 

The interesting part of this architecture comes when integrating Change Feed. We won’t get into too many details on how it works (the official [documentation](https://docs.microsoft.com/en-us/azure/cosmos-db/change-feed-functions) is great) but in a nutshell:

* Enabled by default
* It exposes the Cosmos DB logs to the end user so you can consume it and integrate it with other resources to be notified about any new insertion or update.
* Sorted: Comes in the order of modification
* Can be integrated with a number of [resources](https://docs.microsoft.com/en-us/azure/cosmos-db/change-feed-design-patterns)

The easiest way to consume the change feed is the integration with Azure Functions, which also comes in handy  with our purpose. 

Once a new record in the Card Container is persisted, the Change feed will trigger a new Azure function with an event containing the new record. The azure function will receive this event and will do the following:

* Deserialize the content so we get the **customerId**
* Find the record associated with the given “customerId” in the Input Container. Actually, the Change Feed event received will only contain the data persisted in the Card Container. This is not enough for the next step which is the Identity Service. We will need identity data that was part of the original request. In this case we are fetching this data by directly reading a given Input container, but it might be something else such as an HTTP request from another Service.
* HTTP request to the Identity Service 
* Wait for the Response and depending on the Response Code and Body, stream a new event through Event Hub to be consumed by the Card Service which will transition the Card Container record from **VERIFYING** to **IDENTITY_VERIFIED**.

The implementation of the Azure function that consumes the change feed is really simple. Here is a skeleton of this function implemented in Java.

```java
package com.function;

import com.azure.cosmos.*;
import com.azure.cosmos.models.CosmosItemResponse;
import com.azure.cosmos.models.PartitionKey;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;
import com.model.Input;
import org.json.JSONObject;

import java.util.Collections;

/**
 * Azure Function with Cosmo DB Card container Trigger from Change Feed.
 */
public class CardChangeFeedTrigger {

    private CosmosContainer container;

    private static final String databaseId = "Enrollments";
    private static final String containerId = "Input";

    @FunctionName("CardChangeFeedTrigger")
    public void run(
        @CosmosDBTrigger(name = "items",
        databaseName = "Enrollments", collectionName = "Card",
        createLeaseCollectionIfNotExists = true,
        connectionStringSetting = "AzureCosmosDBConnection") String[] items,
        final ExecutionContext context) {

        setupCosmoDB();

        for (String item : items) {            

            // Read customerId from Change Feed.
            JSONObject jsonObject = new JSONObject(item);
            String customerId = (String)jsonObject.get("customerId");


            // Read Input from Cosmo Container
            CosmosItemResponse<Input> input = container.readItem(customerId, new PartitionKey(customerId), Input.class);
            context.getLogger().info("New item received " + input.getItem());

            // Now we do an HTTP CALL to the Identity Service with data customerID and data from Input Model

            // Based on the Answer 
            // 1. "IDENTITY_VERIFIED" event back to CardService if HTTP call succeeded.
            // 1. "IDENTITY_FAILED" event back to CardService if HTTP call failed.
            // 2. "IDENTITY_NOT_VERIFIED" event back to CardService if Identity not verified.
        }
        
    }

    private void setupCosmoDB() {
        CosmosClient client = new CosmosClientBuilder()
                .endpoint(System.getenv("COSMO_DB_HOST"))
                .key(System.getenv("COSMO_DB_MASTER_KEY"))
                .preferredRegions(Collections.singletonList(System.getenv("REGION")))
                .consistencyLevel(ConsistencyLevel.EVENTUAL)
                .buildClient();
        CosmosDatabase database = client.getDatabase(databaseId);
        container = database.getContainer(containerId);
    }
}
```

### Advantages 

* Easiest way to implement SAGA
* Each transaction is independent
* It’s ideal when not dealing with many local transactions (e.g. no more than 4) 

### Disadvantages

* It is difficult to track and maintain this logic if several transactions and services are involved. In the implementation example we showed above let’s analyze how this would work:

* We introduce a new Azure Function to consume the change feed. In terms of ownership it could be tricky to define who should “own” this new function. Does it fall under the Card Service? Or under the Identity Service?

* Also, as we saw before, the event sent through Event Hub needs to be consumed by the Card Service meaning we’ll need to add a new logic in Card Service so it becomes a consumer of this message. Imagine having this same pattern with multiple services, N services would need to send events to Card Service, it would become difficult to maintain at some point.

## Orchestration Pattern

The second approach of SAGA comes with an Orchestrator. In this pattern, the coordination of all the transactions is fully abstracted into a separate service, an Orchestrator. We’ll use the same example as the one for Choreography pattern but we are adding a new Service called Verification Service.


### Implementation details

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/n33dsfir1cf2wmsvbsq8.png)


This Verification Service will actually internally check if the customer is eligible for a new Card and check for any outstanding loan or any potential red Flag.

We added this service so we have parallels and sequential operations:

* Once a new request is generated it will get to Card Service 
* Card Service will trigger the Orchestrator which will be in charge of coordinating the rest of the transactions
* We have 2 verifications that need to happen and they will both be triggered in parallel, the Verification and the Identity Service.
* Once both are successfully completed,  the orchestrator will trigger the Calculation Service to compute the **amountLimit**. 

We’ll use durable functions to implement the Orchestrator. 

### Durable functions

The [documentation] (https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-overview?tabs=csharp) on Durable functions is very useful, but in a nutshell:

* Extension of Azure functions
* Serverless State machines - used for Long-running orchestrations
* It simplifies complex transactions and coordination
* It maintains a local state
* It’s code only (No json schemas or config files like other orchestration systems)
* Currently available in **Python, JavaScript and C#**

The following image describes the structure of the project to create an orchestrator. We implemented the project in Javascript.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cgdzswss18dx5h8f2ggm.png)

Under the same project (that we usually create with VSCode) we’ll have 3 different kinds of Azure Functions to create a whole Orchestrator workflow. Each function has its own folder.

### Orchestrator Function

The EnrollmentCreditCardOrchestrator folder will have the code of the orchestrator function itself. As seen it only contains 2 files:

* function.json
* index.js

The json file is a config file and as seen it’s a very small file specifying the function defined in the index.js file is an Orchestration Function (for further details, please check the documentation which is great). 

The Orchestration function looks like the following:

```javascript
const df = require("durable-functions");

module.exports = df.orchestrator(function* (context) {
    const request = context.df.getInput();
    context.log("Received New enrollment request. person: " + (request.person ? JSON.stringify(request.person) : undefined) + 
    " || card: " + (request.card ? JSON.stringify(request.card) : undefined) + ".");

    // Verify the request has all the mandatory fields
    verifyRequest(request);

    const tasks = [];

    const verificationTask = context.df.callActivity("VerificationActivity", request.person)
    tasks.push(verificationTask);

    const identityTask = context.df.callActivity("IdentityActivity", request.person);
    tasks.push(identityTask);

    // Wait for verificationTask and identityTask to finish
    yield context.df.Task.all(tasks);

    context.log("Verification Task " + JSON.stringify(verificationTask.result));
    context.log("Identity Task " + JSON.stringify(identityTask.result));

    const calculationRequest = {
        "creditData": verificationTask.result,
        "identity": identityTask.result 
    }

    const calculationCreditCard = yield context.df.callActivity("CalculationActivity", calculationRequest);

    return calculationCreditCard;
});

function verifyRequest(request) {
    if (!request) {
        throw new Error("A request object is required.");
    }
    if (!request.person.id) {
        throw new Error("Gobernment ID is required.");
    }
    if (!request.person.firstName) {
        throw new Error("A first name is required.");
    }
    if (!request.person.lastName) {
        throw new Error("A last name is required.");
    }
    if (!request.person.workType) {
        throw new Error("A type of work is required.");
    }
    if (!request.person.workPlace) {
        throw new Error("A work place is required.");
    }
    if (!request.person.salary) {
        throw new Error("A salary is required.");
    }
    if (!request.card.type) {
        throw new Error("A Card type is required.");
    }
    if (!request.card.brand) {
        throw new Error("A Cart brand is required.");
    }
}
```

The way we call the actual services is through Activities and more precisely through the **context.df.callActitity** function which is another kind of function we’ll define later on. 

The context object is the one containing the request when triggering the orchestrator.

“VerificationActivity” and “IdentityActivity” are triggered in parallel, whereas “CalculationActivity” sequentially. How do we do this? 

* Verification and Identity activities are called without the **yield** word. This means they run in parallel.
* Line 18 specifies we want to wait for both activities to be completed before moving forward
* Once both successfully return with their respective payload, we actually call the Calculation activity synchronously, we get the result from it and then return the final result **calculationCreditCard** to the user who triggered the Orchestration.

### Activity Function 

As seen in the project structure screenshot, each activity function will have its own folder. Each folder will have its config file specifying the function in index.js is an activity function. 

The index.js will look something like the following:

```javascript
const axios = require("axios");
const identityURL = process.env["IDENTITY_SERVICE_URL"]
const CircularJSON = require('circular-json');

module.exports = async function (context, request) {
    try {
        const result = await axios.post(identityURL, request)
        context.log("body returned..." + CircularJSON.stringify(result))

        return result.data;
    } catch (err) {
        context.log(`Identity Service encountered an error: ${err}`);
        throw new Error(err);
    }
}
```

In this specific case we imagined the services are already implemented somewhere else so the only goal of the activity function would be to use some HTTP library (like axios in this case) to make the HTTP request to them. In the practice if we are dealing with a new brand service you can also implement the actual logic of this service in the Activity Function. 

The activity functions and orchestration functions live in different VMs. 

### HTTP Trigger Function

This is the last kind of function we need, the only purpose of this function is to actually expose the orchestrator as an HTTP endpoint so it can be called from any client easily.


```javascript
const df = require("durable-functions");

module.exports = async function (context, req) {
    const client = df.getClient(context);
    const instanceId = await client.startNew(req.params.functionName, undefined, req.body);

    context.log(`Started orchestration with ID = '${instanceId}'.`);

    return client.createCheckStatusResponse(context.bindingData.req, instanceId);
};
```

As a result, when triggering the orchestrator we’ll get a bunch of URLs as a result (Check documentation)


When calling the **statusQueryGetUri** we will get the current status of the orchestrator

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hdqk1g2zajsh0i9l7spn.png)

### Advantages

* Easy to maintain the whole flow in a single place
* Avoid cyclic dependencies, again everything happens in a single place.
* If designing a smart orchestrator, the complexity of adding new transactions remains linear.

### Disadvantages

* New Service to maintain: In distributed teams pattern, adding this new service can be tricky to decide which team should own it. It contains logic and domains from all the services.
* It’s also challenging to know a little bit of the logic of all the services, or at the least clearly understanding the return payloads from services and understanding how to react if something fails and how it affects all the services.