The REST protocol and ACID

The ACID properties were defined in Chapter 11, Storing and Retrieving Objects via Shelve. These properties can be summarized as Atomic, Consistent, Isolated, and Durable. These are the essential features of a transaction that consists of multiple database operations. These properties don't automatically become part of the REST protocol. We must consider how HTTP works when we also need to ensure that the ACID properties are met.

Each HTTP request is atomic; therefore, we should avoid designing an application that makes a sequence of related POST requests and hopes that the individual steps are processed as a single, atomic update. Instead, we should look for a way to bundle all of the information into a single request to achieve a simpler, atomic transaction.

Additionally, we have to be aware that requests will often be interleaved from a variety of clients; therefore, we don't have a tidy way to handle isolation among interleaved sequences of requests. If we have a properly multilayered design, we should delegate the durability to a separate persistence module. In order to achieve the ACID properties, a common technique is to define bodies for the POST, PUT, or DELETE requests that contain all the relevant information. By providing a single composite object, the application can perform all of the operations in an atomic request. These larger objects become documents that might contain several items that are part of a complex transaction.

When looking at our blog and post relationships, we see that we might want to handle two kinds of HTTP POST requests to create a new Blog instance. The two requests are as follows:

  • A blog with only a title and no additional post entries: We can easily implement the ACID properties for this, as it's only a single object.
  • A composite object that is a blog plus a collection of post entries: We need to serialize the blog and all of the relevant Post instances. This needs to be sent as a single POST request. We can then implement the ACID properties by creating the blog, the related posts, and returning a single 201 Created status when the entire collection of objects have been made durable. This may involve a complex multi-statement transaction in the database that supports the RESTful web server.

Let's look at how we can choose a representation out of JSON, XML, or YAML.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset