To share , learn and express my views. (mostly technical)
HTTP Methods – Safety & Idempotency
I was trying to explain Idempotency and Safety to my colleagues. Initially, they had a hard time grasping the difference between safety and idempotency. I then used a mathematical example to explain these terms and it seems to have gone well with them.
Here’s how I explained…
The HTTP’s uniform interface have two important properties; Idempotency and Safety. i.e some HTTP methods are safe (GET & HEAD) and some are idempotent (PUT & DELETE). Safe methods (GET,HEAD) are automatically Idempotent but the reverse is not true. i.e PUT and DELETE are idempotent and not safe.
Safety means that the method will not have any effect on the server state. A GET and HEAD are readonly request and it is expected not to cause any state changes on the server. Idempotentcy means that the method can be invoked any number of times and the result will be same. e.g. DELETEing a resource any number of times results in same state i.e resource deleted state.
The following example from mathematics will further explain safety and idempotency:
- 8 * 1 * 1 * ….. = 8 (This is a safe operation, the result (8) will not change regardless of how many times we multiply 8 by 1)
- 8 * 0 * 0 * ….. = 0 (This is not a safe operation but it is Idempotent, the result changes (becomes 0) and remains so regardless of the number of time we multiply 8 by 0). Note, this is not safe because the 8 * 0 changes the result the first time.