Summary of idempotent

  • Idempotence is originally a mathematical concept, even if the formula f(x)=f(f(x)) can be a mathematical property. In the programming field, it means that for the same system, using the same conditions, a request and repeated multiple requests have the same impact on system resources.

  • Idempotence is a very important concept in the design of distributed system. The interface with this property is always designed with such a concept: when the interface is called, the abnormal and repeated attempts will always cause the loss that the system cannot bear, so this phenomenon must be prevented.

  • There are many ways to realize idempotent, but request token-based mechanism is widely applicable at present. The core idea is to generate a unique certificate, or token, for each operation. A token can only be executed once at each stage of the operation, and once executed successfully, the execution result is saved. Repeat request, return the same result (error), etc. Refer to A Brief Discussion on Idempotence [1]

Idempotent processing implementation

Join the rely on

<dependency>
    <groupId>com.pig4cloud.plugin</groupId>
    <artifactId>idempotent-spring-boot-starter</artifactId>
< version > 0.0.1 < / version ></dependency>
Copy the code

Configure Redis links

  • By default, no configuration is required. The theory is to support all configurations of Redisson-spring-boot-starter [2]
spring:
  redis:
    host: 127.0. 01.
    port: 6379
Copy the code

interface

@Idempotent(key = "#key", expireTime = 10, info = "Do not repeat inquiries")
@GetMapping("/test")
public String test(String key) {
    return "success";
}
Copy the code

test

  • 10 separate thread requests

  • Only 1 out of 10 requests will be successful


  • Check the background abnormal errors, 9 abnormal errors meet the expectation


Idempotent annotation

  • Key: A unique identifier for an idempotent operation, using spring EL expressions to reference method parameters with #. Can be empty, take the current URL + ARgs as the unique identifier of the request

  • Default: 1 The expiration date must be longer than the program execution time, otherwise the request may still come in

  • TimeUnit: timeUnit default: s (seconds)

  • Info: indicates an idempotent failure message, which can be customized

  • DelKey: indicates whether to delete the key after the service is complete. True: deletes the key. False: does not delete the key

Idempotent processing design principles

Process Design Reference [3]

  • 1. Before the request starts, the query result is displayed based on the key. An error is reported

  • 2. After the request is complete, delete the key directly. Whether the key exists or not is configurable

  • 3. ExpireTime expiration time, which prevents a request from being blocked. If the expiration time is longer than the service execution time, evaluate the expiration time.

  • 4. This scheme directly cuts to the interface request level.

  • 5. The expiration time must be longer than the service execution time. Otherwise, service request 1 is still in execution, and the front end is not masked, or the user jumps to the page and returns to request 2 again.

  • 6. DelKey = false is recommended. Even after services are executed, the key is not deleted and the expireTime time is forcibly locked. Prevent the occurrence of 5.

  • 7. Implementation thread: If the same request IP address and interface with the same parameters is made multiple times in expireTime, only one request can be successfully made.

  • 8 page mask, database level of the only index, first query and then add, and other processing methods should be dealt with.

  • 9. This note is only used for idempotent, not for locking, 100 concurrent pressure test, there will be problems, in this scenario is not meaningful, the actual user will not be 1s or 3s manual sent 50 or 100 repeated requests, or weak network has 100 repeated requests;

conclusion

  • pig-mesh/pig

  • pig-mesh/idempotent-spring-boot-starter

The resources

[1]

Reference the idempotence shallowly discussed: https://www.jianshu.com/p/475589f5cd7b


[2]

redisson-spring-boot-starter: https://github.com/redisson/redisson/tree/master/redisson-spring-boot-starter


[3]

Process design reference: https://github.com/it4alla/idempotent