O'Reilly logo

RESTful Web Services Cookbook by Subbu Allamaraju

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 10. Conditional Requests

Conditional requests in HTTP help address two problems. For GET requests, conditional requests help clients and caches validate that a cached representation can still be considered fresh. For unsafe requests such as PUT, POST, and DELETE, conditional requests provide concurrency control.

Not supporting conditional GET requests reduces performance, but in the face of concurrency, not making unsafe requests such as POST, PUT, and DELETE conditional may affect the integrity of the application. In the absence of adequate concurrency control checks, the server is susceptible to lost updates and stale deletes. When a client submits a request to modify or delete a resource, it does so based on what it thinks is the current state of the resource. But under concurrent conditions, the current state of the resource is not static. Either the server, through some backend means, or other clients may have updated or deleted the resource.

Concurrency control ensures the correct processing of data under concurrent operations by clients. There are two ways to implement concurrency control:

Pessimistic concurrency control

In this model, the client gets a lock, obtains the current state of the resource, makes modifications, and then releases the lock. During this process, the server prevents other clients from acquiring a lock on the same resource. Relational databases operate in this manner.

Optimistic concurrency control

In this model, the client first gets a token. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required