Idempotency in microservices and REST APIs

Implementing Idempotency in REST APIs and Mycoservice based on DevPrime

Applications on the DevPrime platform already have an automatic Idempotency feature protecting services so that duplicate inclusions do not occur.

In this scenario we will use an obritatory key sent by the application that is using the API. To use the feature simply change the settings in the DevPrime-based microservice.

Creating a microservice to use in the example

We will use a microservice based on the DevPrime platform

Starting Idempotency setup

Setting the idempotency in the “DevPrime_App” key
Change the Idempotency setting in the “Enabled” option to “true” and “Flow” to “frontend”. We changed the “key” to “my-idempotency-key”. With this setting the API will require the Idempotency key in Headar.
code .\src\app\appsettings.json

1
2
3
4
5
6
7
8
9
    "Idempotency": {
      "Enable": "true",
      "Alias": "State2",
      "Duration": "86400",
      "Flow": "frontend",
      "key": "my-idempotency-key",
      "Scope": "all",
      "Action": "auto"
    }

Include a second persistence in the “DevPrime_State” key
In this example we will use a local Redis with a default password
code .\src\app\appsettings.json

1
2
3
4
5
6
7
8
9
{
  "enable": "true",
  "alias": "State2",
  "dbtype": "redis",
  "connection": "127.0.0.1:6379,password=LltF8Nx*yo",
  "timeout": "5",
  "retry": "2",
  "durationofbreak": "45"
}

Initiating automatic Idempotency in the microservice

Now it’s time to test the configuration. Before starting the microservice make sure that you have activated the containers and created the queue in RabbitMQ.

Run the app and post the Order API
We need to pass this scenario the Idempotency key by Head so you need to consume the API using curl, podtman, insomnia and many others.

Starting the microservice configured to use an Idempotency key
DevPrime Capabilities Idempotency Key`

A sample json code for use in the API. Modify your tests.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{
  "customerName": "Bill",
  "customerTaxID": "string",
  "itens": [
    {
      "description": "short",
      "amount": 1,
      "sku": "XNY1AC",
      "price": 100
    }
  ],
  "total": 100
}

In the first test we will not pass the idempotência key in the POST as an example performed in Postman.
DevPrime Capabilities Idempotency Key

When looking at the API returns we will have a request negative by the absence of the key that in the configuration with the “Flow” type “Backend” is mandatory and must be passed by header.
DevPrime Capabilities Idempotency Key

Now let’s add our “my-idempotency-key” key and a guid value.
DevPrime Capabilities Idempotency Key

After confirming the post in the previous step the Log already records success in the idempotência process.
DevPrime Capabilities Idempotency Key

When you try to perform a new Post with the same previous key the duplicate command is already automatically denied.
DevPrime Capabilities Idempotency Key

In this article you noted how easy it is to implement the Idempotency feature in microservices development with the DevPrime platform.

You have additional settings such as “Action” that configured as “manual” require you to activate on a specific method and/or “Scope” that can limit the performance on an Adapter such as “Web” or “Stream”

Implementing idempotency in microservices
Building an idempotent event-driven microservices

Last modified March 13, 2022 (4cc8d5e)