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 collection of a duplicate payment received in the API. To better visualize, we are talking about the request with a POST/PUT/PATCH command or an event that arrives duplicated by the Stream.

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

Devprime offers a very flexible approach to service configuration that can be configured to start automatically, per parameter sent by the consumer, and/or restricted to specific processes according to each microservice’s strategy.

The idempotency feature can be configured to work automatically using the “action” parameter as “auto” and the “flow” parameter as “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. Auto-Logging is generated by the native Servability approach.

Making the First Post to the Orders API
When you examine the log, view the idempotency information as active in that request, initiating duplicate protection.

Devprime Idempotency Capabilities

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

When analyzing the API result, we see the duplicate request decline
Devprime Resource Idempotency

When you look at the Log in the idempotency section you will find the “Reject”
Devprime Idempotency Capabilities

Two-step configuration of idempotency
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 in 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 idempower 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 Devprime Stack. Use the Devprime platform to accelerate the journey of application modernization and digital platform development.

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

Last modified August 20, 2024 (2f9802da)