Monday, June 16, 2025
HomeJavaWhat are Idempotent and Protected strategies of HTTP and REST

What are Idempotent and Protected strategies of HTTP and REST [Interview Question]


In case you are studying REST and need to perceive the distinction between the Idempotent and secure methodology then you’ve come to the correct place. Earlier, I’ve shared the finest books and programs to study RESTful Internet services and on this article, I’ll reply this in style REST Interview query. With the intention to effectively work with REST and RESTful internet service, good information of HTTP is basically useful. Although REST appears straightforward, designing a uniform and constant RESTful API is a tricky job. One of many tough duties is selecting proper the HTTP methodology for the correct job like when to make use of PUT vs POST. As soon as you understand the that means and objective of various HTTP strategies, it helps to decide on the correct methodology for the correct job. 
You’ll be able to divide HTTP strategies into two essential classes secure and idempotent. Protected strategies are HTTP strategies that don’t modify the useful resource like a GET request is secure as a result of it does not modify the useful resource you’re requesting like knowledge of a Guide.

One other secure HTTP methodology is HEAD, which does not change the useful resource illustration on the Server, however all different HTTP strategies like POST, PUTor DELETE are non-safe.

Coming to idempotent strategies, they’re HTTP strategies that may be known as a number of instances and they’re going to produce the identical consequence. They’re thought-about the secure choice to replace a useful resource on the Server.

Some examples of idempotent HTTP strategies are GET, PUT, and PATCH. Regardless of what number of instances you name them, they’ll produce the identical consequence with the identical URI. 

This basic information of HTTP protocol goes a great distance in making a production-grade REST API that may stand the check of time. 

What are Protected Strategies in HTTP and REST?

These are HTTP strategies that do not change the useful resource on the server-side. For instance, utilizing a GET or a HEAD request on a useful resource URL ought to NEVER change the useful resource. Protected strategies could be cached and prefetched with none repercussions or side-effects to the useful resource. Right here is an instance of a secure methodology

GET /order/123 HTTP/1.1

This may retrieve the order with orderId 123. Regardless of what number of instances you execute this methodology, the order within the server won’t be modified or impacted. 

That is why the GET methodology is a secure methodology.  Let’s have a look at a few extra particulars about Idempotent and secure strategies in HTTP and RESTFul internet companies.

What are Idempotent Strategies in HTTP and REST?

These are strategies which are secure from a number of calls i.e. they produce the identical consequence no matter what number of instances you name them. They modify the useful resource in Server each time you name them however the finish result’s at all times the identical. Maths is an effective place to clarify idempotent strategies, think about the next instance:

int i = 30; // idempotent

i++; // not idempotent

Right here the project operation is idempotent, irrespective of what number of instances you execute this assertion, i will at all times be 4. The second instance shouldn’t be idempotent. Executing this 10 instances will lead to a distinct final result as when operating 5 instances. Since each examples are altering the worth of i, each are non-safe strategies.

Idempotency is a vital factor whereas constructing a fault-tolerant RESTful API. Idempotency can be the rationale why must you use PUT over POST to replace a useful resource in REST.

For instance, suppose a consumer desires to replace a useful resource by way of POST. Since POST shouldn’t be an idempotent methodology, calling it a number of instances might lead to incorrect updates.

In the true world, it is quietly doubtless that your POST request might timeout, what is going to occur to the useful resource that. Is the useful resource truly up to date? Does the timeout occur throughout sending the request to the server, or the response to the consumer?

Can we safely retry once more, or do we have to work out first what has occurred with the useful resource? Through the use of idempotent strategies like PUT, you do not have to reply this query, however we are able to safely resend the request till we truly get a response again from the server.

Abstract

Here’s a good overview of which HTTP strategies are secure and Idempotent:

  • GET is each Protected and Idempotent.
  • HEAD can be each secure and idempotent.
  • OPTIONS can be secure and idempotent.
  • PUT shouldn’t be secure however idempotent.
  • DELETE shouldn’t be secure however idempotent.
  • POST is neither secure nor idempotent.
  • PATCH can be neither secure nor idempotent.

Here’s a slide that explains which strategies of the HTTP protocol are secure and that are Idempotent

Difference between HTTP Safe and Idempotent methods REST

That is all in regards to the secure and idempotent strategies in HTTP and REST. Bear in mind, secure strategies do not change the illustration of the useful resource within the Server e.g. GET methodology won’t change the content material of the web page your accessing. They’re the read-only strategies of the object-oriented programming world.

Equally, idempotent strategies won’t throw totally different outcomes even should you name them a number of instances. They’re secure for updating sources on Server. They are going to at all times return the identical consequence except you modify the URL. 



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments