Idempotent Personal Understanding and application

Source: Internet
Author: User
Tags abs arithmetic socket

[Original link: http://www.smithfox.com/?e=16 reprint please keep this statement, thank you]

The explanation of idempotent on most networks is similar to the following:

"Idempotent" means the same result can always be obtained when the same method is called repeatedly using the same parameters. For example, the result of a GET request access to the same resource is the same. "

I think this explanation is very wrong, the power of the emphasis on the external through the interface to the internal impact of the system, the outside world how to see the system and idempotent is not related. In the above explanation, System.getcpuload (), the two calls return can be the same. But because it is a read-only interface, it has no effect on the internal state of the system, so this function is idempotent.

First look at what is idempotent, and if you are not interested, you can skip this algebraic interpretation of the concept directly:)

Idempotent (idempotence) is derived from the concept of higher algebra.

The definitions are as follows (added to their understanding):

Monocular operation, X is any number within a set, F is the operator if it satisfies f (x) =f (f (x)), then we call the F operation to be idempotent (idempotent)

In real numbers, for example, an absolute value operation is an example: ABS (a) =abs (ABS (a))

Binocular operation, X is an arbitrary number in a set, F is an operator if it satisfies f (x,x) =x, the premise of the F operation is that two parameters are the same as x, then we also call the F operation as having idempotent

For example, in a real number, a function that asks for the maximum value of two numbers: Max (x,x) = X, and the Boolean algebra, the logical operation "and", "or" are also idempotent operations, because they conform to and (0,0) = 0, and (1) = 0, or (=) = 1

The use of idempotent in software development requires some deeper understanding. My understanding is as follows:

Mathematics deals with arithmetic and numerical values, which are often handled by objects and functions in program development. But we can not simply understand that the arithmetic in the power of mathematics is the function, and the value is the Object!!

For example, a person object has two attributes weight and age, but all function can only operate on one of these properties. So from this level we can understand that the function is only idempotent to an attribute of the object that the function operates on, rather than to an entire object that has an operational power.

person {
 private int weight;
 private int age;
 is idempotent function public
 void setage (int v) {
     this.age = v; 
 }
 Not idempotent function public
 void Increaseage () {
     this.age++;
 } 
 is idempotent function public
 void setweight (int v) {
     this.weight=v+10;//intentionally added 10 Catty!!
 }
}

It is also important to clarify that the concepts expressed by idempotent are concerned with mathematical calculations and numerical values, and do not mention the security of the numerical values.

For example, the Setage function of the person above, there are two kinds of case is not idempotent concern, but the development of the program must be concerned about:

1. Two threads simultaneously call

2. Because age is impossible to decrement from business, if the previous call was set to 30 years old, the latter call became 10 years old or more outrageous-1 years old

So in restful design, idempotent and security are measured as two different indicators of post,put,get,delete operations:

Important Method security. idempotent.
GET Is Is
DELETE Whether Is
PUT Whether Is
POST Whether Whether

Idempotent is the system's interface to the external commitment (rather than implementation), the commitment as long as the invocation of the interface succeeds, the external multiple calls to the system's impact is consistent. An interface declared as Idempotent will assume that an external call failure is the norm, and that there must be retries after the failure. Just as the cache has the basic implementation paradigm of the cache, Idempotent also has its own fixed external call paradigm cache implementation paradigm:

Value GetValue (key) {
    value = Getvaluefromcache (key);

    if (value = = null) {
        value = readfrompersistence (key);
        Savevalueintocache (Key,value);
    }

    return value;
}
idempotent external Call Paradigm
Client.age = +;

while (some exit conditions) {
    try{
        if (socket.setpersonage (person,client.age) = = FAILED) {
	        int newage = Socket.getpersonage ();
	        Dealing with conflict issues: Because age is only likely to become larger, the age of the client is updated to the server-side of the older
	        if (newage>30) {
			client.age = newage;
			break;
		} else{
			//Unable to resolve the conflict, try again
		}
        } else return;
    } catch (Exception) {
        //Network exception occurred, try again
    }
}

The internal implementation of the Idempotent interface needs to be internally protected, typically with a version mechanism similar to optimistic locking. The focus of the release is on the time.

[Original link: http://www.smithfox.com/?e=16 reprint please keep this statement, thank you]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.