Idempotency

The DevPrime platform offers an automatic approach to Idempotency allowing you to control duplicate commands in both HTTP/gRPC requests and Stream services (Kafka, RabbitMQ).

The idempotency strategy is widely applied in scenarios, for example, charging a double payment. In the microservices scenario we are talking about a request for a POST/PUT/PATCH or even an event that arrives through the Stream.

Some Stream services eventually send duplicate messages to ensure high delivery speed, and this forces applications to control Idempotency.

DevPrime offers a very flexible approach to configuring the service that can be configured to start automatically, by a parameter sent by the consumer and/or restricted to specific processes according to the strategy of each microservice.

The Idempotency feature can be configured to work automatically using the “action” parameter as “auto” and “flow” as “backend” without the need to pass a foreign key.

To visualize in practice the scenario and the importance of idempotency follow the sequence of images where we demonstrate in the Log generated by the DevPrime Observability strategy.

Posting to the Order API
When you look at the log you will already have the idempotência information as active for this request
DevPrime Capabilities idempotency

Making a second post in the Order API identical to the previous one

The API result already reports the refusal of the duplicate request
DevPrime Capabilities idempotency

When analyzing the log in the section of the idempotência you will find the “Reject”
DevPrime Capabilities idempotency

Two-step Idempotency setup

Enable Idempotency in the “DevPrime_App” key

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

Add a second persistence Redis in the “State” key

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"
}

Activating manual Idempotency
Adding idempotency in the Order API

1
2
3
4
5
app.MapPost("/v1/order", async (HttpContext http, IOrderService Service, DevPrime.Web.ViewModels.Order.Order command) => await Dp(http).Pipeline(() =>
        {
            Dp(http).Idempotency.Enable = true;
            Service.Add(command.ToApplication());
        }));

You just checked out a powerful feature present in DevPrime Stack. Use Devprime to accelerate the journey of application modernization and digital platform development.

Implementing idempotency
Implementing idempotency in microservices
Idempotency in microservices and REST APIs
Implementing idempotency in microservices

Last modified March 16, 2022 (9c7f636)