Idempotency

The Devprime platform offers an automatic idempotency approach that allows you to control duplicate commands in HTTP/gRPC requests and Stream services (Kafka, RabbitMQ).

The idempotency strategy is widely applied to avoid, for example, the charge of a duplicate payment received in the API. To better visualize, we’re talking about the request with a POST/PUT/PATCH command or an event that arrives duplicated by Stream.

Some Stream services occasionally send duplicate messages to ensure high delivery speeds, and this forces applications to control idempotency.

Devprime offers a very flexible approach to service configuration that can be configured to start automatically, by 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 the “flow” parameter as the “backend”, enabling the feature without the need to pass a foreign key when consuming the API.

To better visualize the scenario in practice, it is essential to follow the sequence of images where we demonstrate the entire process in the Log. Automatic Logging is generated by the native Servability approach.

Making the first post in the Ordering API
When you look at the log, view the idempotency information as active in that request, initiating duplicate protection.

Devprime Capacities Idempotency

Make a second post in the Request API identical to the previous one
Right now we are repeating the scenario, only now we will have another response in the control of idempotency.

When analyzing the API result, we see the duplicate request being rejected
![Idempotency of Devprime resources]@@@@5@@@@@

When you look at the Log in the idempotency section you will find the “Reject”
![Devprime Capabilities idempotency]@@@@6@@@

Two-step idempotency setup
Setup is a simple and customizable process

Enable Idempotency on the “Devprime_App” key by setting the “Enable” value to “true”.

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 Redis persistence on the “State” key for idempotency automatic transaction control information.

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 idempotency manually
In the desired scenarios, you can manually configure idempotency support.

1
2
3
4
5
6
7
8
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’ve just checked out a powerful feature present in the Devprime Stack. Use the Devprime platform to accelerate your application modernization and digital platform development journey.

More details on deidempotency scenarios
Implementing idempotency in microservices
Idempotency in microservices and REST APIs
Implementing idempotency in microservices

Last modified January 10, 2024 (967dcac3)