RESTful Architecture

The title is definitely making some readers think why is someone again writing on this topic. What inspired me to write again after a long time is the confusion between "RESTful Web Services" and "XML over HTTP". I came accross this confusion while working on a framework of mine and later I will mention how it interested me. So I would like to clear the confusion, as per my understanding, on RESTful Architecture.

First lets see how Dr. Roy Thomas Fielding in his PhD dissertation defines REST,

REST is defined by four interface constraints: identification of resources; manipulation of resources through representations; self-descriptive messages; and, hypermedia as the engine of application state.

In this writeup I would like illustrate how Dr. Fielding illustrates this and my (as a scholar of REST) understanding on the matter.

As the REST wikipedia page mentions at the center of the RESTful architecture is "Resource" and I fully agree with it. So I would like start by defining what is a resource. Dr. Fielding defines it as,

...any concept that might be the target of an author's hypertext reference must fit within the definition of a resource. A resource is a conceptual mapping to a set of entities, not the entity that corresponds to the mapping at any particular point in time.
Now lets pick a common concept to build the idea of resources. Lets take a book sales aggregator service (similar to Google Checkout); it sells books written by one or many authors and published by one or more publishing house and sold at different prices by stores and are in different state (new or old) and different descriptions to go with them.
The obvious resources here are Book, Store, Author and Publisher and I would have BookState be a resource as well, it is comprised of price and description. Cardinality would be Book has many Stores, Authors and Publishers; Store has many BookStates. I would also like to make BookExcerpt a weak entity of Book. So according to Dr. Fielding's definition and my understanding of resource all the objects in OOP (in italic) are resources.

Before going into the nature of representation lets see what Dr. Fielding has to say about constituents of a resource -

The values in the set may be resource representations and/or resource identifiers. A resource can map to the empty set, which allows references to be made to a concept before any realization of that concept exists. ....... The only thing that is required to be static for a resource is the semantics of the mapping, since the semantics is what distinguishes one resource from another.

The first part of the statement actually signifies an advantage of RESTful architecture and that one may develop individual resource components independently. The second part represents a constraint on availability of definition of how resources are inter-related . So in our example the cardinal semantics between the resources/objects should be static. Now that we know about resources next thing to visit is the representation of resources and Dr. Fielding's take on this is,

A representation consists of data, metadata describing the data, and, on occasion, metadata to describe the metadata (usually for the purpose of verifying message integrity). Metadata is in the form of name-value pairs, where the name corresponds to a standard that defines the value's structure and semantics. Response messages may include both representation metadata and resource metadata: information about the resource that is not specific to the supplied representation.
Control data defines the purpose of a message between components, such as the action being requested or the meaning of a response. It is also used to parameterize requests and override the default behavior of some connecting elements. For example, cache behavior can be modified by control data included in the request or response message.

The representation of a resource in different media type format (referred to as data, while the media type itself is a metadata) is just part of the architecture and not architecture itself. The messages should be self-descriptive, the representation format for the resource should respect other resources as they are and the manipulation of all resources and their relations to other resources should be done through transition of states. So if I want to change the cover image of a Book resource I should be able to do it using a PUT command of HTTP.

Now here is where the confusion lies - lets say I am using Java on the server to respond to the requests. If I am using Jersey+JAX-RS stack and application/xml as media type, there is a good chance that I am also using JAXB (not that its mandatory, one can use their on converters, known Producer and Consumer) for converting your resource object to XML; I will see that Book is converted to a XML which contains the representations of authors and other related resources and sub-resources within them. This creates multiple problems, e.g.,

  • Why do I need to get all entity sets of a resource and its sub or related resources, while I do not need them? Which will cause an additional and not-required payload to be submitted over network.
  • How do I manipulate any related resources of book without actually transmitting the whole book, e.g. author(s)?
  • How do I sent different set of control bytes for different resources linked within the Book?

IMHO, here is where the principal of REST is violated, unless the answers to the above like questions are positive. So if we are to design a RESTful API we should consider not only the representation part but also the manipulation and saying so, IMHO, using different HTTP commands (or equivalent for other protocols) is not sufficient but actually designing the resource semantics such that it can be manipulated partly and totally (depending on requirements and other factors) is a must as well.

Another important component of the architecture, which is not mentioned in the definition of REST (probably because it was reason of REST coming to existence) is Uniform Interface. The only thing it does not mention explicitly mentioned as a constraint is that the representation format also should be uniform or conform to established standards, so that its readable in a uniform manner by the clients, e.g. how resources are linked - in HTML it should be <link>, in XML it should be XLink etc.

So here is my checklist for building a RESTful system:

  • Identify resources and their identifier. Deduce the problem domain well to divide them to as many independent component as possible.
  • Choose the states well for transiting between resource states for the specific protocol. E.g. if protocol is HTTP you might map the methods as mentioned here; that is not enough, HTTP headers and response codes should be choosen appropriately; for example cache control, compression.
  • Resources should refer to individual resources by their URI; state transition for relation between resources might (not sure but IMHO must) be implemented as well.
  • The representation format should follow established norms. E.g. XML representation format should have a DTD or XSD as a metadata to the representation.

In context of our book sales aggregator example I want to elaborate my understanding of some of the above mentioned checkpoints. Firstly we have (tentatively) identified our resources; next I would want to create useful identifier for them. For Book it would be , for Author it would be email address, for Publisher it would be their name in same formatting as a blog or wiki page title and so on. In my XML representation of book, I would have a element encapsulating all authors, it would in fact be <authors>; and every <author> in would refer to the author resource using XLink. I would have the following for updating the author relation with book - /books/{ISBN}/authors/[slot/{index}]?. Now lets say I want to get the 1st Author for Book A, then my URI would be /books/A/authors/slot/0; the response message could be either a XML pointing to the Author's URI or (my choice) use HTTP response status code 302; also I would make use HTTP compression using the headers. IMHO rest of the checkpoints are trivial.

I would like to end this writeup by stating why I came up with this post . I am working on the framework stated at the beginning to make infrastructure code for common tasks, such as versioning, full text search, DAOs and representations of domain objects in various media type available out of the box. For versioning and representing domain objects I could not find a better way than REST. I am also working on another project which needs Web Services to interface with clients; my past experience with SOAP was sour enough for me to look into alternatives and I chose RESTful WS, but later realized it was nothing but RESTlike WS or XML/JSON over HTTP. Furthermore this writeup just expresses my understanding and in some case thoughts of RESTful architecture, so if you feel I have misunderstood or made any mistake, feel free to correct me :). If you are interested in more details of implement REST over HTTP please check this out.

10 comments:

  1. hoping to see more articles on REST .

    ReplyDelete
  2. nice writeup. hope to see more article on it.
    just one question. what particular problem you experienced using SOAP ..

    ReplyDelete
  3. @Fazle
    The problem is not with SOAP protocol itself, but more with the tools I had to use for SOAP and also the performance, in terms of payload, was not to my satisfaction.

    ReplyDelete
  4. Nice article, the checklist is very noticeable.

    ReplyDelete
  5. Hi, Imran! Great post. Always good to read about REST :-)

    Probably the most important lesson I've learned while learning about REST is that there's always something new to learn. You've now outlined what you've read about REST and tried to applied the principle to a set of book resources.

    For example, let's take the example of the constrained media type where you mention "that the representation format also should be uniform or conform to established standards". The concept of the constrained media type is open to wide interpretation.

    Let's take your book example, you define a media type for your book, let's say application/vnd.imyousuf.book+xml, and document what it means and add a schema and get it working perfectly with JAXB. This approach is possibly the least interoperable, since nobody else in the world will know what to do with that media type.

    Then again, you could use application/xml. This way, recipients will know that it's XML, but without prior knowledge to your schema and domain, it won't make any sense.

    You could use HTML, and use specific values for rel and class and id and document your usage of these, possibly including a profile attribute. This would make it possible to use any old browser to look at the books. The usefulness is still quite small, since no other software developer will use the same rel, class and id attributes that you do.

    A JSON format (or just declaring it as application/json) suffers from the same problem as XML, in that a consumer just gets a data structure, but without prior knowledge it has no idea how to use it.

    The best option is to find a media format that fits your domain, so that serendipity may thrve! I think this is really hard, and it's what I'm exploring at the moment. I think microformats, and perhaps RDFa can have a profound effect on how computers can consume HTML, without prior knowledge of your specific formats.

    -mogsie-

    ReplyDelete
  6. @mogsie
    Firstly, thank you and glad that you liked it...
    Secondly, I fully agree that there is scope to always learn where REST is concerned.
    Thirdly, the point that you elaborated was the same exact discussion that prompted me to read the theoretical part of the topic.
    Its only ironic that I failed to explain properly what I meant when I mentioned "representation format also should be uniform or conform to established standards". I actually wanted to stress that the format depends on domain while we could apply some constraints on them and thats why I mentioned that there should be namespaces and schema for XML nodes. In case of XML when linking resource lets use xlink namespace and schema instead of something custom, that was point for conforming to standards; and knowledge of schema and domain is a must, I could not find a methodology or format where those would not be required, be it XML (custom or standard), HTML or JSON.

    ReplyDelete
  7. Nice hope to see more similar articles

    ReplyDelete
  8. your site is very nice, i like it!! wellcome to my blog!
    http://www.tech24h.us/

    ReplyDelete
  9. Superb ! Your blog is incredible. I am delighted with it. Thanks for sharing with me.

    ReplyDelete

Search