<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Peter Williams &#187; REST</title>
	<atom:link href="http://barelyenough.org/blog/tag/rest/feed/" rel="self" type="application/rss+xml" />
	<link>http://barelyenough.org</link>
	<description>… and there is much to be learned</description>
	<lastBuildDate>Mon, 23 Jan 2012 18:32:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Vertical Slicing</title>
		<link>http://barelyenough.org/blog/2010/07/vertical-slicing/</link>
		<comments>http://barelyenough.org/blog/2010/07/vertical-slicing/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 02:57:33 +0000</pubDate>
		<dc:creator>Peter Williams</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[REST]]></category>

		<guid isPermaLink="false">http://barelyenough.org/?p=466</guid>
		<description><![CDATA[I am a fan of polylithic architectures. Such architectures have many advantages related to enhancing evolvability and maintainability. When you decide to create a system composed of small pieces how do you decide what functionality goes into which component? Principles The goal is to sub-divide the application into multiple highly cohesive components which are weakly [...]]]></description>
			<content:encoded><![CDATA[<p>I am a fan of <a href='http://barelyenough.org/blog/2009/09/small-pieces/'>polylithic architectures</a>. Such architectures have many advantages related to enhancing evolvability and maintainability. When you decide to create a system composed of small pieces how do you decide what functionality goes into which component?</p>
<h2 id='principles'>Principles</h2>
<p>The goal is to sub-divide the application into multiple <a href='http://en.wikipedia.org/wiki/Cohesion_(computer_science)'>highly cohesive</a> components which are weakly <a href='http://github.com/jimweirich/presentation_connascence/raw/master/Connascence.key.pdf'>connascence</a> with each other. To achieve the desired cohesion it will be necessary to align the component boundaries with natural fissure points in the application.</p>
<p>The strategy should allow for the production of a arbitrary number of components. A component that was of a manageable size yesterday could easily become too large tomorrow. In that situation the over-sized component will need to be sub-divided. Applying the same strategy repeated will result in a system that is more easily understood.</p>
<p>We want to minimize redundancy in the components. Redundancy results in more code with must be understood and maintained. More importantly redundancy usually introduces <a href='http://onestepback.org/articles/connascence/conalgorithm.html'>connascence of algorithm</a>, making changes more error prone and expensive. In a perfect world, any particular behavior would be implemented in exactly one component.</p>
<p>We want to isolate changes to the system. When implementing a new feature it is desirable to change as few components as possible. Each additional component that must be changed raise the complexity of the change. The componentization strategy should minimize the number of components involved in the average change to the system.</p>
<p>With those metrics in mind lets explore the two most common approaches and see how they compare with each other. Those two patterns of componentization are horizontal slicing and vertical slicing.</p>
<h2 id='horizontal_slicing'>Horizontal slicing</h2>
<p>In this approach the component boundaries are derived from that implementation domain. The implementation is divided into a set of stacked layers in such a way that a layer initiates communication with the layers below it. This results in a standard layered architectures. By implementing each layer in a separate component you can achieve the horizontal slicing. This style of componentization strategy results in the very common <a href='http://en.wikipedia.org/wiki/Multitier_architecture'>n-tier architecture pattern</a>.</p>
<p>For example, an application that has a business logic and a presentation layer the application would be divided into two components. A business logic component and a presentation component.</p>
<h2 id='vertical_slicing'>Vertical slicing</h2>
<p>In this approach the component boundaries are derived from the application domain. Related domain concepts are grouped together into components. Individual components communicate with any other components as needed.</p>
<p>This approach is also quite common but is usually thought of a lot less formally. It is more common for this type of segmentation to develop incidentally. For example, because separate teams developed the parts independently, and then integrated them later. Any time you integrate separate applications you have vertical componentization.</p>
<h2 id='the_score'>The Score</h2>
<p>Against the metrics we laid out earlier, vertical slicing does much better than horizontal.</p>
<table>
<thead>
<tr>
<th />
<th>Horizontal slicing</th>
<th>Vertical slicing</th>
</tr>
</thead>
<tbody>
<tr>
<td style='text-align: left;'>Cohesion</td>
<td style='text-align: left;'>high</td>
<td style='text-align: left;'>high</td>
</tr>
<tr>
<td style='text-align: left;'>Repeatability</td>
<td style='text-align: left;'>low</td>
<td style='text-align: left;'>high</td>
</tr>
<tr>
<td style='text-align: left;'>DRYness</td>
<td style='text-align: left;'>low</td>
<td style='text-align: left;'>high</td>
</tr>
<tr>
<td style='text-align: left;'>Change isolation</td>
<td style='text-align: left;'>low</td>
<td style='text-align: left;'>high</td>
</tr>
</tbody>
</table>
<h3 id='cohesion'>Cohesion</h3>
<p>Horizontal slicing has high cohesion. Each of the components can represent the a logically cohesive part of the implementation.</p>
<p>Vertical slicing also has high cohesion. Each component represents highly cohesive part of the application domain.</p>
<h3 id='repeatability'>Repeatability</h3>
<p>Vertical slicing provides a mechanism for reapply the subdivision pattern an arbitrary number of times. If any component gets too large to manage it can be divided into multiple components based on the application domain concepts. This same process can be repeated from the initial division of a monolithic application until components of the desired size have been achieved.</p>
<p>Horizontal slicing is less repeatable. The more tiers the harder it is to maintain cohesiveness. In practice it is very rare to see an tiered architecture with more than 4 tiers, and 3 tiers is much more common.</p>
<h3 id='dryness'><a href='http://en.wikipedia.org/wiki/Don%27t_repeat_yourself'>DRYness</a></h3>
<p>Horizontal slicing tends to result in some repetition. Certain behaviors will have to be repeated a each layer. For example, data validation rules. You will need those in the presentation layer to provide good error messages and in the business logic layer to prevent bad data being persisted.</p>
<p>Vertical slicing allows you to reduce the connascence of algorithm because any single user activity is implemented in exactly one component. Components usually do end up communicating to each other, however, they do so in a way that does not require in the same algorithms be implemented in multiple components. For any one bit of data or behavior, one component will its authoritative source.</p>
<h3 id='change_isolation'>Change isolation</h3>
<p>Vertical scaling tends to allow new features to be implemented by changing only one component. The component changed is the one which already contains features cohesive with the new one.</p>
<p>Horizontal slicing, on the other hand, tends to require changes in every layer. The new feature will require additions to the presentation layer, the business logic layer and the persistence layer. Having to work in every layer increase the cognitive load required to achieve the desired result.</p>
<h2 id='conclusion'>Conclusion</h2>
<p>Vertical slicing provides significant advantages. The high cohesion, dryness, and change isolation combine to drastically reduces the risks and cost of change. That is turn allow better/faster maintenance and evolution of the system. The repeatability allows you to retain these benefits even while adding functionality over time. Each time a component gets too large you can divide it until you have reach a application size that is human scaled.</p>
<p>Having a large number of components operate as a system does result in a good deal of communication between the components. It important to pay attention to the design of the APIs. Poor API design can introduce excessive coupling which will eat up most of the advantages described above. <a href='http://barelyenough.org/blog/2007/05/hypermedia-as-the-engine-of-application-state/'>Hypermedia</a> &#8211; or more precisely, following the REST architectural style &#8211; is the best way i know to reduce coupling between the components.</p>
]]></content:encoded>
			<wfw:commentRss>http://barelyenough.org/blog/2010/07/vertical-slicing/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>What are links</title>
		<link>http://barelyenough.org/blog/2010/01/what-are-links/</link>
		<comments>http://barelyenough.org/blog/2010/01/what-are-links/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 05:54:10 +0000</pubDate>
		<dc:creator>Peter Williams</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[hypertext]]></category>
		<category><![CDATA[REST]]></category>

		<guid isPermaLink="false">http://barelyenough.org/?p=422</guid>
		<description><![CDATA[When designing hypertext formats is it better to provide links for every available action or to provided links to related resources and let the client use the protocol interface to achieve particular actions on those related resources? I have leaned in both directions at various times. I have never fully convinced myself either. To make [...]]]></description>
			<content:encoded><![CDATA[<p>When designing hypertext formats is it better to provide links for every available action or to provided links to related resources and let the client use the protocol interface to achieve particular actions on those related resources?</p>
<p>I have leaned in both directions at various times. I have never fully convinced myself either.</p>
<p>To make the issues a bit clearer let me use and example lifted from the <a href='http://www.subbu.org/blog/2010/01/hypertext-is-the-transaction-engine'>article that got me thinking about this most recently</a>.<sup id='fnref:1'><a href='#fn:1' rel='footnote'>1</a></sup></p>
<pre><code>&lt;cart&gt;
  &lt;!-- some stuff here --&gt;
  &lt;link rel=&quot;http://ex.org/rel/abort&quot;
        href=&quot;http://ex.org/cart/cancel;token=987654321&quot;/&gt;
  &lt;link rel=&quot;http://ex.org/rel/add-more&quot;
        href=&quot;http://ex.org/cart/add;token=987654321&quot;/&gt;
  &lt;link rel=&quot;http://ex.org/rel/buy&quot;
        href=&quot;http://ex.org/cart/buy;token=987654321&quot;/&gt;
&lt;/cart&gt;</code></pre>
<p>I place this example in the &#8220;links for every action&#8221; camp. Each of the links in the example describes exactly one action.</p>
<p>An alternate approach might look something like this.</p>
<pre><code>&lt;cart&gt;
  &lt;!-- some stuff here --&gt;
  &lt;link rel=&quot;http://ex.org/rel/line-items&quot;
        href=&quot;http://ex.org/cart/line-items;token=987654321&quot;/&gt;
  &lt;link rel=&quot;http://ex.org/rel/new-order&quot;
        href=&quot;http://ex.org/orders?cart=987654321&quot;/&gt;
&lt;/cart&gt;</code></pre>
<p>From a client perceptive these are a bit different.</p>
<dl>
<dt>Abandoning cart</dt>
<dd>A client that wants to abandon a cart in the first example would make a DELETE or POST &#8211; it&#8217;s a bit hard to tell which from the example &#8211; request to the href of the <code>http://ex.org/rel/abort</code> link. In the second example a similar client would just DELETE the cart resource.</dd>
<dt>Adding item</dt>
<dd>When adding an item in the first example the client would post a www-form-urlencoded document containing the URI of the item to add and the quantity to the href of the <code>http://ex.org/rel/add-more</code> link. In the second example, the same document gets posted to href of the <code>http://ex.org/rel/line-items</code> link.</dd>
<dt>Placing order</dt>
<dd>In the first example the client would make a POST request to the href of the <code>http://ex.org/rel/buy</code> link. In the second example the client would POST a www-form-urlencoded document containing the cart URI and some payment information to the href of the <code>http://ex.org/rel/order</code> link.</dd>
</dl>
<h3 id='differences'>Differences</h3>
<p>Obviously the to approaches result in quite similar markup. The same behavior is encoded in both. In the first example the links are action oriented. All actions that can be taken on an item are explicitly stated using a link. In the second approach the links are data oriented rather than action oriented. Rather than having separate links to retrieve the current line items and to add a new line item the <code>http://ex.org/rel/items</code> link provide both actions using the GET and POST HTTP methods respectively.</p>
<p>The first approach it better at expressing what actions are allowable at any given point in time. For example, once the purchase process has been initiated it does not make sense to abort a cart. So if you GET a cart after POSTing to the <code>http://ex.org/rel/buy</code> link the representation would not have the <code>http://ex.org/rel/abort</code> link.</p>
<p>The second is more concise because it, at least potentially, provides access to more than one action per link based on the standard HTTP methods. You don&#8217;t need to provide a separate abort link because DELETEing the cart is sufficient. You don&#8217;t need to provide separate get line items and add line item links because a single link that can handle GET and POST requests will work.</p>
<p>The first approach is a bit more flexible with regard to implementation details. If you need for some reason to have different URIs for the retrieve line item request than the add line item request you could easily achieve it. The second example makes that impossible.</p>
<h3 id='conclusion'>Conclusion</h3>
<p>I am still not entirely convinced but i am leaning toward the more flexible, verbose and explicit approach of a link for every actions.<sup id='fnref:2'><a href='#fn:2' rel='footnote'>2</a></sup> Having links represent actions rather than resources feels a bit odd, but i think it provides more of the benefits we hope to get from a RESTful architecture.</p>
<div class='footnotes'>
<hr />
<ol>
<li id='fn:1'>
<p>I am <a href='http://barelyenough.org/blog/2010/01/unobtrusive-link-info/'>still not a fan of the <code>link</code> element</a>. This example is a good one in every other regard.</p>
<p><a href='#fnref:1' rev='footnote'>&#8617;</a></li>
<li id='fn:2'>
<p>That counts as at least the third vacillation i have had on this topic. I was leaning the other direction before writing this.</p>
<p><a href='#fnref:2' rev='footnote'>&#8617;</a></li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://barelyenough.org/blog/2010/01/what-are-links/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unobtrusive link info</title>
		<link>http://barelyenough.org/blog/2010/01/unobtrusive-link-info/</link>
		<comments>http://barelyenough.org/blog/2010/01/unobtrusive-link-info/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 05:19:14 +0000</pubDate>
		<dc:creator>Peter Williams</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://barelyenough.org/?p=416</guid>
		<description><![CDATA[Mr Amundsen&#8217;s recent post regarding the design of &#8220;semantic machine media types&#8221; got me thinking about media type design. One of the commonly encouraged practices, particularly on the REST discuss group, is the use of link elements. I really dislike this idea. It sets my teeth on edge because it treats links &#8211; which are [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://amundsen.com/blog/archives/1023'>Mr Amundsen&#8217;s</a> recent post regarding the design of &#8220;semantic machine media types&#8221; got me thinking about media type design. One of the commonly encouraged practices, particularly on the <a href='http://tech.groups.yahoo.com/group/rest-discuss'>REST discuss group</a>, is the use of <a href='http://tools.ietf.org/html/rfc4287#section-4.2.7'><code>link</code> elements</a>.</p>
<p>I really dislike this idea. It sets my teeth on edge because it treats links &#8211; which are possibly the most important bits of data in existence &#8211; as second class citizens. It is easiest to show what i mean with a bit of extrapolation:</p>
<pre><code>&lt;complexElement rel=&quot;entry&quot;&gt;
  &lt;string rel=&quot;id&quot;&gt;234132&lt;/string&gt;
  &lt;string rel=&quot;displayName&quot;&gt;Peter Williams&lt;/string&gt;
  &lt;complexElement rel=&quot;name&quot;&gt;
    &lt;string rel=&quot;familyName&quot;&gt;Williams&lt;/string&gt;
    &lt;string rel=&quot;givenName&quot;&gt;Peter&lt;/string&gt;
  &lt;/complexElement&gt;
  &lt;complexElement rel=&quot;emails&quot;&gt;
    &lt;link rel=&quot;email&quot; href=&quot;mailto:pezra@barleyenough.org&quot;/&gt;
    &lt;string rel=&quot;type&quot;&gt;personal&lt;/string&gt;
  &lt;/complexElement&gt;
&lt;/complexElement&gt;</code></pre>
<p>That is what a <a href='http://portablecontacts.net/'>portable contact</a> might look like if we treated all data the way <code>link</code> elements work. That example looks pretty ugly to me, as i suspect it does to most people. It is ugly because very important information regarding the role of elements is relegated to a subsidiarity position in favor of fairly unimportant information about its type. However, <code>link</code> elements do to links exactly what my example does to all the data. The effect is that properties whose values happen to be independently addressable resources are obfuscated.</p>
<p>The <a href='http://en.wikipedia.org/wiki/Revealed_preference'>revealed preference</a> of the world is against <code>link</code> elements. Just look at pretty much any format that embeds application specific semantics. As far as i know, there is not a single widely used format that actually represents its links as <code>link</code> elements. Even <a href='http://tools.ietf.org/html/rfc4287'>atom</a> uses properly named elements for most its links. The <code>link</code> element it defines is largely relegated to the back water of extensibility.</p>
<p>One benefit that <code>link</code> elements have, or at least could have if they where more widely used, is the facilitation of standard link processing tools. Fortunately, we do not have to give up the expressiveness and clarity of <a href='http://c2.com/cgi/wiki?IntentionRevealingNames'>intention revealing names</a> to achieve this result. Rather than obscuring the links we could just treat them as normal data. The additional information needed to support standard tools could be added in a relatively unobtrusive way. Consider the following:</p>
<pre><code>&lt;entry xmlns:link=&quot;http://unobtrusive-generic-linking.org/&quot;&gt;
  ...
  &lt;emails&gt;
    &lt;value link:hrefDisposition=&quot;elementContent&quot;
           link:rel=&quot;foo&quot;&gt;
      mailto:pezra@barelyenough.org&lt;/value&gt;
    &lt;type&gt;personal&lt;/type&gt;
  &lt;emails&gt;
&lt;/entry&gt;</code></pre>
<p>This is idea is similar to <a href='http://www.w3.org/TR/xlink'>xLink</a> but more flexible and simpler to use.</p>
<p>You could expand the idea to JSON with relative ease. Consider the following expansion of the portable contacts json format tagged with some unobtrusive link info.</p>
<pre><code>{&quot;entry&quot;: [
  {&quot;id&quot;: &quot;42&quot;,
  &quot;emails&quot;:
    [{&quot;address&quot;   : &quot;mailto:pezra@barelyenough.org&quot;,
      &quot;type&quot;      : &quot;personal&quot;,
      &quot;_linkInfo&quot; : {&quot;hrefDisposition&quot; : &quot;address&quot;,
                     &quot;rel&quot; : &quot;foo&quot;}}]}]}</code></pre>
<p>Unobtrusive link info makes links visible to and usable by generic link processing tools while protecting the use of intention revealing names that format designers, and users, want. This is important because it allows new formats to reuse &#8220;standard&#8221; link semantics more easily and uniformly.</p>
]]></content:encoded>
			<wfw:commentRss>http://barelyenough.org/blog/2010/01/unobtrusive-link-info/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>In defense of link storage</title>
		<link>http://barelyenough.org/blog/2009/11/in-defense-of-link-storage/</link>
		<comments>http://barelyenough.org/blog/2009/11/in-defense-of-link-storage/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 04:46:00 +0000</pubDate>
		<dc:creator>Peter Williams</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[REST]]></category>

		<guid isPermaLink="false">http://barelyenough.org/?p=399</guid>
		<description><![CDATA[It seems that more and more are beginning to grasp the hypermedia constraint of REST. This is an unmitigated Good Thing. However, once you get hypermedia the idea of a client persisting links that it has found starts to seem a little odd. For example, Kirk Wylie describes clients that store links as &#8220;not well [...]]]></description>
			<content:encoded><![CDATA[<p>It seems that more and more are beginning to grasp the <a href='http://www.infoq.com/articles/mark-baker-hypermedia'>hypermedia constraint</a> of REST. This is an unmitigated Good Thing. However, once you get hypermedia the idea of a client persisting links that it has found starts to seem a little odd. For example, Kirk Wylie describes clients that store links as &#8220;not well behaved&#8221; in his <a href='http://www.infoq.com/presentations/restful-financial-systems-integration'>excellent presentation on REST in financial systems</a>. Even on the rest-discuss mailing list there is <a href='http://tech.groups.yahoo.com/group/rest-discuss/message/13516'>no consensus on the matter</a>.</p>
<p>The idea of an application as a set of states (read: representations) with transitions (read: links) to other states seems to go against the idea of storing links. Transitions from one application state to another are surely transient. Any change in the application state, either by this client or some completely unrelated client, could easily invalidate those transitions. In that context a client that stored links for later use would surely be doomed dereferencing dead links for the rest of it&#8217;s days.</p>
<p>Further, the idea that clients might store links is a frightening specter for maintainers of services. If clients store links, and you prefer not to break those clients, you must continue supporting any links you have ever included in any representation in perpetuity. Talk about limiting your design freedom. Such a strict requirement would surely raise the cost of maintaining the service over time.</p>
<h2 id='reality_sets_in'>Reality sets in</h2>
<p>Those are scary thoughts. Some of these issues are even real. But end the end it doesn&#8217;t matter. Almost all non-trivial systems are going to require that URIs be stored in places other than the origin server. Sometimes these stored URIs will merely be caches. Other times they will be data that cannot be recalculated mechanically.</p>
<p>For example, say you have an order taking system and an inventory system. When placing an order the user goes to the web site, searches for &#8220;coffee&#8221;, selects the third item in the results and places an order 1 of that item. An order is a set of line items each of which references a product. Once payment is received the order system is going to need to be able to tell the shipping department which items from inventory to send to the customer.</p>
<p>The inventory system has, of course, a URI for every type of product that is for sale. So the simplest and most effective way for an order to reference a product is to use that the inventory URI for that product. URIs are called universal resource <em>identifiers</em> for a reason, we might as well use them as such.</p>
<p>In this example, we have a situation where the product references in the order are not merely caches of URIs. Many things may change the ordering of search results &#8211; a new product being added, an old one being discontinued, even a small change to a description of some product. So at any moment the the third item in the search results for &#8220;coffee&#8221; might be different. Once the user has made their selection no automata can reliably retrace those steps.</p>
<p>The implications of this are significant. The inventory must continue to support the product URIs used in orders until such time as the order system would never care to dereference those URIs again. If a month from now the user comes back and wants to see their order history, those product URIs had better still work.</p>
<p>Fortunately, HTTP provides us with a ready solution. Behold the awesomeness that is <a href='http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2'>HTTP redirection</a>. HTTP redirection is your best friend when it comes to gracefully changing REST/HTTP applications. Clients get what they need &#8211; URIs continue to work as identifiers indefinitely &#8211; and servers get what they need &#8211; a lot of freedom to change the names and dispositions of resources.</p>
<h2 id='transient_links'>Transient links</h2>
<p>We are still faced with this issue of the transient nature of links. Certainly, many links encode transitions which may be transient. The client has no general way of distinguishing between links which represent transiently available state transitions, and those that represent more permanent transitions.</p>
<p>In our example, immediately after creating an order, it probably provides some links to pay for the order. After the user has provided payment those transition would no longer be valid. However, the link to the inventory product is a more permanent part of the order resource.</p>
<p>The only tractable way i see to deal with this issue is to document the lifespan the various link found in a representation. Once the client implementer understand the semantics of the links they well often be able to infer the likely lifespan of the links without further input. However, guidance can be provided in situations where precision is required or the lifespan is ambiguous. A transient link is, by definition, an option part of the representation so documenting the conditions that cause it to be present is likely to be required anyway.</p>
<h2 id='best_practices_server'>Best practices: Server</h2>
<p>REST/HTTP application developers should assume that clients will store links and dereference them after indeterminate periods of time. When resources are relocated or renamed requests to the resource&#8217;s obsolete URI should be redirected to the canonical URI using a <code>301 Moved<br />
Permanently</code> response.</p>
<p>For links whose validity has a bounded lifespan the documentation of the representations (the media type) should explicitly layout that the link is transient and optional. If possible the documentation should also describe the conditions of the links existence.</p>
<p>Remind client developers early and often that client <em>must</em> follow any and all redirects from the server.</p>
<h2 id='best_practices_client'>Best practices: Client</h2>
<p>Clients should follow redirects. Fastidiously.</p>
<p>Clients should update it&#8217;s internal storage upon receiving a <code>301<br />
Moved Permanently</code> response by replace the URI it requested with newly provided location.</p>
<p>Client developers should be aware of transient links in the representations being dealt with. Either do not store these URIs or ensure that attempts so use these URIs handle failure in ways that make sense for the application.</p>
<p>Believe and follow the redirections the server sends to you. Seriously.</p>
]]></content:encoded>
			<wfw:commentRss>http://barelyenough.org/blog/2009/11/in-defense-of-link-storage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Small pieces</title>
		<link>http://barelyenough.org/blog/2009/09/small-pieces/</link>
		<comments>http://barelyenough.org/blog/2009/09/small-pieces/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 13:47:52 +0000</pubDate>
		<dc:creator>Peter Williams</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[REST]]></category>

		<guid isPermaLink="false">http://barelyenough.org/?p=397</guid>
		<description><![CDATA[The idea of creating a functioning application by loosely connecting many small pieces has been around for a long time. Certainly since early in the development of Unix, and probably even before. It has survived because it is such a powerful approach. This idea is at the very core of the architecture of the web. [...]]]></description>
			<content:encoded><![CDATA[<p>The idea of creating a functioning application by loosely connecting many small pieces has been around for a long time. Certainly since early in the development of Unix, and probably even before. It has survived because it is such a powerful approach.</p>
<p>This idea is at the very core of the architecture of the <a href='http://smallpieces.com'>web</a>. However, achieving such compartmentalization has been difficult for business application development. The recent advancement of REST into the mainstream is bringing this mentality within the reach of many development teams.</p>
<p>The small pieces approach totally dominates the lower levels of software development in the form of object oriented programing. Each &#8220;object&#8221; is a small self-contained piece, and a large number of the small pieces are joined together to provide the functionality of the application.</p>
<p>At the application level, however, this approach does not enjoy the same ubiquity. It is much more common to see a monolithic approach. There is one giant application that does everything for everyone. The path of least resistance for any single new feature is to implement it in the existing application structure. There is some overhead in creating a new application, so in the short term multiple small applications seems more costly.</p>
<p>Unfortunately, while it is easier to add any particular feature to an existing application doing so means you give up all the advantages of small pieces loosely joined. And, the advantages of a small application approach are significant.</p>
<p>Perhaps the single largest advantage is that smaller applications are easier to understand. To effectively improve or maintain software it is necessary to understand it. The <a href='http://onestepback.org/articles/connascence'>connascence</a> of one bit of code and everything else is often unclear. This lack of clarity means that the risk of any particular change having unintentional impacts increases with the size of the code base. Building more smaller applications is an effective way to manage such risks.</p>
<p>Another advantage of this approach is that it makes using novel implementation techniques more workable. If you have a large monolithic application and you get a new requirement that might benefit from a different language, framework or runtime you are pretty much out of luck. In a compartmentalized architecture each application can have it&#8217;s own technology stack. If you new a set of features that that might benefit from the concurrency of Erlang you can use for that component without impacting the other components in any way.</p>
<p>Have several small applications often turns intractably large efforts in to several smaller tasks. For example, consider upgrading the framework on which your application(s) are built to a new version. The new version has features and improvements that would be highly beneficial but target version is incompatible with the version you are currently running in some minor ways.</p>
<p>Such an upgrade will necessarily touch most of the application. Its risk profile will be very broad. The benefits of the upgrade will rarely be directly visible to the business so the priority of such work is always rather low. The cost and risk of such work is often so large, and the perceived benefit so small, that such work is put off until support is being terminated for the version currently in use.</p>
<p>In a compartmentalized system the any single component can be upgraded much more quickly, and at lower risk. The benefits of upgrading match the effort required much more closely in such situation. Many times the effort required is often so low there is no discussion required, upgrades become normal refactoring tasks.</p>
<p>Experimentation is also quite a bit more manageable in a compartmentalized architecture. The smaller size of the components makes implementing new ideas faster and more approachable. This lowers the cost of implementing new ideas and recovering if the idea does not pan out.</p>
<p>When experimenting it is often not immediately clear if a new idea really is an improvement. Sometimes developers and users need to work with it a for a while to form a reasoned opinion. In compartmentalized systems experiments can be designed to impact a small portion of the total application. This allows small experiments to can soak for a while until the team is ready to call the results. If idea worked the practice can be expanded to the rest of the components, if not it is only a small portion of the code base that needs to be cleaned up.</p>
<p>It is worth noting that this approach will effectively true your large application into a distributed system of small applications. This is distributed applications are a little scary, and for good reason. Before you embark on this path you should have a plan for how to integrate the parts into a whole. For most business application <a href='http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm'>REST</a>/HTTP is a very good technology for integrating applications.</p>
<p>There are many other situations where the small pieces approach&#8217;s conversion of large tasks into small ones is an advantage. There are also situations where it causes more overall work. In my experience, though, the chunking of tasks is well worth the small additional overhead. It is much easier to manage many small semi-independent development efforts than a few large ones.</p>
]]></content:encoded>
			<wfw:commentRss>http://barelyenough.org/blog/2009/09/small-pieces/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Announcing Resourceful</title>
		<link>http://barelyenough.org/blog/2008/06/announcing-resourceful/</link>
		<comments>http://barelyenough.org/blog/2008/06/announcing-resourceful/#comments</comments>
		<pubDate>Mon, 30 Jun 2008 17:48:18 +0000</pubDate>
		<dc:creator>Peter Williams</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[Resourceful]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://pezra.barelyenough.org/blog/?p=356</guid>
		<description><![CDATA[Resourceful has its initial (0.2) release today. Resourceful is a sophisticated HTTP client library for Ruby. It will (when it is complete, at least) provide an simple API for fully utilizing the amazing goodness that is HTTP. It is already tasty, though. The 0.2 release provides fully compliant HTTP caching a framework for implementing cache [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://resourceful.rubyforge.org/'>Resourceful</a> has its <a href='http://github.com/paul/resourceful/commits/rel_0.2'>initial (0.2) release</a> <a href='http://www.theamazingrando.com/blog/index.php/2008/06/30/announcing-resourceful/'>today</a>.</p>
<p>Resourceful is a sophisticated HTTP client library for Ruby. It will (when it is complete, at least) provide an simple API for fully utilizing the amazing goodness that is HTTP.</p>
<p>It is already tasty, though. The 0.2 release provides</p>
<ul>
<li>fully compliant HTTP caching</li>
<li>a framework for implementing cache managers (memory based cache manager provided)</li>
<li>fully compliant transparent redirection handling (with hooks for overriding the default behavior)</li>
<li>plugable HTTP authentication handling (Basic provided)</li>
</ul>
<h3 id='introduction'>Introduction</h3>
<p>The API is strongly influenced by our successful experiences with REST. Each URI is represented by a <code>Resource</code> object. The <code>Resource</code> objects act as a proxy for the conceptual resource. <code>Resources</code> expose the basic set of HTTP verbs: get, put, post, delete. For example to get a representation of some resource you do this</p>
<pre><code>require &#39;resourceful&#39;
http = Resourceful::HttpAccessor.new
resp = http.resource(&#39;http://rubyforge.org&#39;).get
puts resp.body</code></pre>
<p>If you want to post a form you do this</p>
<pre><code>require &#39;resourceful&#39;
http = Resourceful::HttpAccessor.new
resp = http.resource(&#39;http://rubyforge.org&#39;).post(&quot;name=Peter&amp;hobbies=programming,diy&quot;, :content_type =&gt; &quot;application/x-www-form-urlencoded&quot;)
puts resp.code</code></pre>
<p>All non-2xx responses are either handled transparently (e.g. by following redirects) or the method will raise a <code>UnsuccessfulHttpRequestError</code>.</p>
<h3 id='conclusion'>Conclusion</h3>
<p>If you need a decent HTTP library in Ruby come on over and check it out. If you see something you want, or want fixed, feel free to branch the <a href='http://github.com/paul/resourceful/tree/master'>git repo</a> and do it. <a href='http://www.theamazingrando.com/blog/'>Paul</a> and I would love some more contributors and will welcome you with open arms.</p>
]]></content:encoded>
			<wfw:commentRss>http://barelyenough.org/blog/2008/06/announcing-resourceful/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>REST/HTTP Service Versioning (Response to Jean-Jacques Dubray)</title>
		<link>http://barelyenough.org/blog/2008/05/resthttp-service-versioning-reponse-to-jean-jacques-dubray/</link>
		<comments>http://barelyenough.org/blog/2008/05/resthttp-service-versioning-reponse-to-jean-jacques-dubray/#comments</comments>
		<pubDate>Mon, 19 May 2008 05:54:39 +0000</pubDate>
		<dc:creator>Peter Williams</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[rest-versioning]]></category>

		<guid isPermaLink="false">http://pezra.barelyenough.org/blog/?p=341</guid>
		<description><![CDATA[Jean-Jacques Dubray takes issue with my approach of using content negotiation to manage service versioning in HTTP. I actually hesitate to respond to Mr. Dubray because the overall tone of his piece is rather off putting. On the other hand, he raises a couple of interesting questions which I have been really looking for and [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://www.ebpml.org/blog/89.htm'>Jean-Jacques Dubray takes issue</a> with <a href='http://pezra.barelyenough.org/blog/2008/05/versioning-rest-web-services/'>my approach of using content negotiation to manage service versioning</a> in HTTP. I actually hesitate to respond to Mr. Dubray because the overall tone of his piece is rather off putting. On the other hand, he raises a couple of interesting questions which I have been really looking for and excuse to talk about. So I will give it a go.</p>
<h3 id='handling_obsolescent_service_providers'>Handling obsolescent service providers</h3>
<p>Mr. Dubray asks how we deal with version skew between the client and server.</p>
<blockquote>
<p>Backwards compatibility is when the consumer comes in with a &#8220;newer&#8221; request version than the service provider can provide. This is common when a consumer uses different providers for the same type of service. So ultimately, you need to provide some room to define the version of both the consumer and the version of the service provider that it is targeting. Your mechanism only supports &#8220;one version&#8221;.</p>
</blockquote>
<p>Not true, the versioning mechanism I describe easily handles multiple versions. First, lets be clear, a service provider cannot provide capabilities that where not conceived of until after it was written. So Mr. Dubray must be interested in is the ability of a single consumer to successfully communicate with multiple versions of the service provider. I agree with him that this is an absolutely vital feature of any versioning mechanism.</p>
<p>Fortunately, content negotiation deals with this issue quite handily. I left this out of the original post for simplicities sake but it well worth talking about. HTTP allows user agents &#8211; or service consumers, if you prefer &#8211; to specify more than one acceptable response format. For example, the following is a perfectly legal HTTP conversation.</p>
<pre><code>===&gt;
GET /accounts/42
Accept: application/vnd.myapp-v2+xml, application/vnd.myapp-v1+xml;q=0.8

&lt;===
200 OK
Content-Type: application/vnd.myapp-v1+xml

&lt;account&gt;
  &lt;name&gt;Inigo Montoya&lt;/name&gt;
&lt;/account&gt;</code></pre>
<p>The <a href='http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html'>Accept header field</a> in the request indicates that the consumer can operate using either version 1 or 2 of the API but it prefers version 2. Accept headers can include any number of MIME media types along with preference indicators (the q=number part). This allows consumers to inform the server of all acceptable dialects of the API with which it can work. In the example, the server obviously did not support version 2 of the API and therefore responded using version 1.</p>
<h3 id='resource_deprecation'>Resource deprecation</h3>
<p>Further along Mr. Dubray asks this question,</p>
<blockquote>
<p>Another flaw of your versioning strategy is that URIs are by default part of the versioning strategy. I have often pointed out that &#8220;Query-by-examples&#8221; are encoded by members of the REST community (MORCs) in a URI syntax, for instance:</p>
</blockquote>
<blockquote>
<pre><code>/customer/{id}/PurchaseOrders ...</code></pre>
</blockquote>
<blockquote>
<p>Peter, how do you express that a particular QBE belongs to one version and not to the other?</p>
</blockquote>
<p>I don&#8217;t. The set of purchase orders associated with a particular customer is not version specific. The customer has agreed to purchase the same things regardless of which version of the service you are talking to.</p>
<p>Perhaps the question Mr. Dubray is really trying to ask is, what happens if you want to deprecate such resource?</p>
<p>(One reason to do so might be that the purchase order collections become too big to reasonably render in a single response. There are other, better ways to solve that particular problem but it is a nice concrete use case for resource deprecation.)</p>
<p>Resource deprecations is easily handled in REST using media types to handle versioning. First some ground rules, user agents should <em>never</em> be constructing such a URI. Doing so should be a gross violation of the HATEOAS constraint of REST. Rather they would be extracting that URI from the representation of the customer provided by the server. In such a case, an HTTP conversation getting the purchase orders for a customer might look like this.</p>
<pre><code>===&gt;
GET /customer/42
Accept: application/vnd.myapp-v1+xml
&lt;===
200 OK
Content-Type: application/vnd.myapp-v1+xml

&lt;customer&gt;
  &lt;purchase-orders href=&quot;http://service.example/customer/42/purchase-orders&quot;/&gt;
&lt;/customer&gt;

===&gt;
GET /customer/42/purchase-orders
Accept: application/vnd.myapp-v1+xml
&lt;===
200 OK
Content-Type: application/vnd.myapp-v1+xml

&lt;purchase-orders&gt;
  ...
&lt;/purchase-orders&gt;</code></pre>
<p>At version 2 of the API we deprecate the all-purchase-orders-for-customer resource &#8211; removing all references to it in the customer representations &#8211; and replace it with a purchases-order-by-month-by-customer resource. A similar HTTP conversation with a client capable of handling version 2 of the API would look like this.</p>
<pre><code>===&gt;
GET /customer/42
Accept: application/vnd.myapp-v2+xml
&lt;===
200 OK
Content-Type: application/vnd.myapp-v2+xml

&lt;customer&gt;
  &lt;purchase-orders-by-month href-template=&quot;http://service.example/customer/42/purchase-orders?in_month={xmlschema-gYearMonth}&quot;/&gt;
&lt;/customer&gt;

===&gt;
GET /customer/42/purchase-orders?in_month=2008-05
Accept: application/vnd.myapp-v2+xml
&lt;===
200 OK
Content-Type: application/vnd.myapp-v2+xml

&lt;purchase-orders&gt;
  ...
&lt;/purchase-orders&gt;</code></pre>
<p>Notice that in version 2 of the API the all-purchase-orders-for-customer resource is no longer exposed in any way. As a human you might guess that it still exists, and indeed it would need to in order to handle requests to version 1 of the API. However, a version 2 consumer will never make a request to that resource because it is not mentioned in the version 2 representations. Indeed, any requests for the all-purchase-orders-for-customer by a version 2 consumer would be met with a <code>406 Not Acceptable</code> response because it is not part of the version 2 API.</p>
<h3 id='wrap_up'>Wrap up</h3>
<p>Toward the end Mr. Dubray gets into full rant mode with these bits,</p>
<blockquote>
<p>You will soon start realizing that resources do have a state that is independent of the &#8220;application&#8221; since by definition a resource can participate in multiple &#8220;applications&#8221;. This is the essence of SOA, i.e. the essence of reuse.</p>
</blockquote>
<p>Resources certainly may participate in multiple &#8220;applications&#8221;. There is nothing in the REST principles that prevent that. I don&#8217;t really claim to be an SOA expert. I just make systems work using REST principles. So far I have not found a problem reusing my resources in multiple applications. In fact, REST seems to excel at that very thing.</p>
<blockquote>
<p>At least, some of the MORCs <span>Member Of the REST Community</span> could have the courtesy to acknowledge that they are indeed building a programming model on top of REST, that this programming model needs clear semantics and that these semantics are not intrinsically part of REST (nor always RESTful).</p>
</blockquote>
<p>I, for one, will readily acknowledge that we have built, and are continuing to build, programming models on top of REST. REST is merely a set of principles, articulated as constraints, that facilitate the creation of useful network based architectures. I would be very surprised if many in the REST community would disagree with me. These programming models do, for the most part, adhere to the REST principles.</p>
<p>Building REST/HTTP web services is certainly not fully understood yet. That does not make it special, hardly any sort of system design or architecture is fully understood. However, REST seems, to me at least, to be a better fit for today&#8217;s applications and technologies than any of the alternatives.</p>
<hr />
<h4 id='related_posts'>Related Posts</h4>
<p>If you&#8217;re interested in REST/HTTP service versioning be sure not to miss the <a href='/blog/tag/rest-versioning/'>rest of the series.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://barelyenough.org/blog/2008/05/resthttp-service-versioning-reponse-to-jean-jacques-dubray/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Versioning REST Web Services (Tricks and Tips)</title>
		<link>http://barelyenough.org/blog/2008/05/versioning-rest-web-services-tricks-and-tips/</link>
		<comments>http://barelyenough.org/blog/2008/05/versioning-rest-web-services-tricks-and-tips/#comments</comments>
		<pubDate>Tue, 13 May 2008 20:47:56 +0000</pubDate>
		<dc:creator>Peter Williams</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[rest-versioning]]></category>

		<guid isPermaLink="false">http://pezra.barelyenough.org/blog/?p=336</guid>
		<description><![CDATA[In my previous post on this subject I described an approach to versioning the API of a REST/HTTP web service. This approach has significant advantages over the approach that is currently most common (i.e. embedding a version token in the URL). However, it does have some downsides. This post is an attempt to outline those [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href='http://pezra.barelyenough.org/blog/2008/05/versioning-rest-web-services/'>previous post on this subject</a> I described an approach to versioning the API of a REST/HTTP web service. This approach has significant advantages over the approach that is currently most common (i.e. embedding a version token in the URL). However, it does have some downsides. This post is an attempt to outline those and to present some ways to mitigate the negative impacts.</p>
<h3 id='nonstandard_mime_media_types'>Nonstandard MIME media types</h3>
<p>Using content negotiation to manage versions requires, by definition, the introduction of nonstandard media types. There is really no way around this. I personally don&#8217;t feel this is a Bad Thing. The new, nonstandard, media types do a much better job describing the sort of media the client is requesting. It does, however, mean that browsers &#8211; and perhaps some HTTP tools &#8211; will work less well with the web service.</p>
<p>The browser not working is a pretty big issue. They are almost certainly not the target consumer of the services, but having the browser not work raises the level of effort for exploring the API. If you have created a cool new service you want as few barriers to entry as possible. Personally, I always use <a href='#curl'>curl</a> when I am exploring but I know several people who would prefer to use a browser.</p>
<p>Unfortunately, I don&#8217;t really have a great general solution for browsers. That being said, in many situations a much can be done to make life better. For example, if the resources in question do not have HTML representations you could serve the current preferred format with a generic content type that browsers can render &#8211; e.g. <code>text/plain</code> or <code>application/xml</code> &#8211; to browsers.</p>
<h3 id='curl_'>Curl <a id='curl' /></h3>
<p>One advantage of having the version token directly in the URL is that it makes it <em>really</em> easy to use <a href='http://curl.haxx.se/'>curl</a> against the service. By default curl makes requests with the Accept header field set to <code>*/*</code>. For a reasonably designed service this would result in a response in the current preferred format. If you want to change to Accept header you need to invoke curl like this</p>
<pre><code>curl --header &#39;Accept: application/vnd.foo.myformat-v1+xml&#39; http://api.example/hello-world</code></pre>
<p>That is not too horrible, really. It is a bit much to type all the time, but I have curl rc files for all the formats I deal with on a daily basis. If your service is implemented in Rails there is an even easier way. With Rails you give each format you support a short name that may be used as an &#8220;extension&#8221; for URLs. For example, if we define the short name for <code>application/vnd.foo.myformat-v1+xml</code> to be <code>mf1</code> we can say this</p>
<pre><code>curl http://api.example/hello-world.mf1</code></pre>
<p>That is equivalent, from the point of view of a Rails based service, to the previous example. I imagine similar functionality could be implemented in most web frameworks. This effectively puts you back to having the version embedded in the URL, which is convenient for debugging and exploration. (It is still unsuitable for production use, though, for all the same reasons as other approaches to embedding the version in the URL.)</p>
<h3 id='nonobviousness'>Nonobviousness</h3>
<p>Another potential downside of using content negotiated versioning is that the various versions my be less discoverable, compared to a version-in-the-URL approach. I am not entirely sure this is true &#8211; after all there is a version token in the media type &#8211; but if it is true it would be a Good Thing.</p>
<p>Do you really want people &#8220;discovering&#8221; a version of the API that was deprecated a year ago? I think it might be better, in either approach, to use version tokens that are not readily guessable. Obviously, previous versions of and API will be documented and remain accessible, but raising some barriers to entry on depreciated parts of a system seems appropriate to me.</p>
<h3 id='unfamiliarity'>Unfamiliarity</h3>
<p>This may be the biggest issue of all. People are just not very familiar, and therefore comfortable, with content negotiation. This in spite of the fact that it has been a fundamental part of HTTP since forever. I think this features obscurity is waning now, though, because it is such a powerful feature.</p>
<p>Two years ago <a href='http://www.loudthinking.com/arc/000572.html'>Rails got content negotiation support</a>. (That link seems to be broken at the moment. You can see part of the post I am talking about by going <a href='http://www.loudthinking.com/arc/2006_04.html'>here</a> and searching for &#8220;The Accept header&#8221;.) As frameworks like Rails keep adding and improving their support for this powerful feature the community of developers will become more familiar and comfortable with it. What is needed now is more education in the community on how best to utilize this feature.</p>
<hr />
<h4 id='related_posts'>Related Posts</h4>
<p>If you&#8217;re interested in REST/HTTP service versioning be sure not to miss the <a href='/blog/tag/rest-versioning/'>rest of the series.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://barelyenough.org/blog/2008/05/versioning-rest-web-services-tricks-and-tips/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Versioning REST Web Services</title>
		<link>http://barelyenough.org/blog/2008/05/versioning-rest-web-services/</link>
		<comments>http://barelyenough.org/blog/2008/05/versioning-rest-web-services/#comments</comments>
		<pubDate>Mon, 12 May 2008 05:59:37 +0000</pubDate>
		<dc:creator>Peter Williams</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[rest-versioning]]></category>

		<guid isPermaLink="false">http://pezra.barelyenough.org/blog/?p=335</guid>
		<description><![CDATA[Managing changes to APIs is hard. That is no surprise to anyone who has ever maintained an API of any sort. Web services, being a special case of API, are susceptible to many of the difficulties around versioning as other types of APIs. For HTTP based REST style web services the combination of resources and [...]]]></description>
			<content:encoded><![CDATA[<p>Managing changes to APIs is hard. That is no surprise to anyone who has ever maintained an API of any sort. Web services, being a special case of API, are susceptible to many of the difficulties around versioning as other types of APIs. For HTTP based REST style web services the combination of resources and content negotiation can be used to mitigate most of the issues surrounding API versioning.</p>
<p>Let&#8217;s assume you have a REST/HTTP web service that has some account resources. Say you can make a request like this:</p>
<pre><code>===&gt;
GET /accounts/3 HTTP/1.1
Accept: application/vnd.mycompany.myapp+xml
&lt;===
HTTP/1.1 200 OK
Content-Type: application/vnd.mycompany.myapp+xml

&lt;account&gt;
  &lt;name&gt;Inigo Montoya&lt;/name&gt;
&lt;/account&gt;</code></pre>
<p>First, you probably noticed that my example uses a <a href='http://tools.ietf.org/html/rfc4288#section-3.2'>vendor MIME media type</a> to describe the representation. Using a more generic MIME media type like <code>application/xml</code> is much more common, at least in my experience. Using generic media types is perfectly legal but a bit silly. You are not really asking for any old XML document, but rather an XML document that has a quite specific schema. Aside from my idealistic rantings, using a specific media type has some strong practical benefits which are at the core of this post.</p>
<h2 id='backwards_compatible_changes'>Backwards compatible changes</h2>
<p>Often changes will need to be made to expose new behavior of the system that do not negatively impact correctly implemented clients. Say, for example, you want to start tracking email address for accounts. If the <code>application/vnd.mycompany.myapp+xml</code> format documentation is clear that elements that are not recognized should be ignored you can simply add a email element to the account representation.</p>
<pre><code>&lt;account&gt;
  &lt;name&gt;Inigo Montoya&lt;/name&gt;
  &lt;email-address&gt;mailto:prepare-to-die@youkilledmyfather.example&lt;/email-address&gt;
&lt;/account&gt;</code></pre>
<p>Any client that was created before the addition of the email element will simply ignore it&#8217;s presence. Problem solved.</p>
<h2 id='incompatible_changes'>Incompatible changes</h2>
<p>Unfortunately, not all changes can be implemented in a way that is backwards compatible. For example, a couple of months after adding email to accounts the sales team sign a deal for 1 bazillion dollars. But the new customer demands that each account be allowed to have more than one email address. After thinking for a while, you decide that the best way to expose that is by changing the account representation as follows.</p>
<pre><code>&lt;account&gt;
  &lt;name&gt;Inigo Montoya&lt;/name&gt;
  &lt;email-addresses&gt;
    &lt;email-address priority=&#39;1&#39;&gt;mailto:prepare-to-die@youkilledmyfather.example&lt;/email-address&gt;
    &lt;email-address priority=&#39;2&#39;&gt;mailto:vengeance@youkilledmyfather.example&lt;/email-address&gt;
  &lt;email-address&gt;
&lt;/account&gt;</code></pre>
<p>That, of course, will break any clients that are expecting the old format &#8211; so pretty much all of them. This is a place where we can bring content negotiation to bare. You can simply define a new media type &#8211; say <code>application/vnd.mycompany.myapp-v2+xml</code> &#8211; and associate new multi-email format with it. Clients can then request whichever format they want. Older clients don&#8217;t know the new media type so they get served the older single email format.</p>
<pre><code>===&gt;
GET /accounts/3 HTTP/1.1
Accept: application/vnd.mycompany.myapp+xml
&lt;===
HTTP/1.1 200 OK
Content-Type: application/vnd.mycompany.myapp+xml

&lt;account&gt;
  &lt;name&gt;Inigo Montoya&lt;/name&gt;
  &lt;email-address&gt;mailto:prepare-to-die@youkilledmyfather.example&lt;/email-address&gt;
&lt;/account&gt;</code></pre>
<p>Newer clients do know the new media type so they can have access to the new functionality.</p>
<pre><code>===&gt;
GET /accounts/3 HTTP/1.1
Accept: application/vnd.mycompany.myapp-v2+xml
&lt;===
HTTP/1.1 200 OK
Content-Type: application/vnd.mycompany.myapp-v2+xml

&lt;account&gt;
  &lt;name&gt;Inigo Montoya&lt;/name&gt;
  &lt;email-addresses&gt;
    &lt;email-address priority=&#39;1&#39;&gt;mailto:prepare-to-die@youkilledmyfather.example&lt;/email-address&gt;
    &lt;email-address priority=&#39;2&#39;&gt;mailto:vengeance@youkilledmyfather.example&lt;/email-address&gt;
  &lt;email-address&gt;
&lt;/account&gt;</code></pre>
<p>Everyone gets what they need. Easy as pie.</p>
<h2 id='alternate_approaches'>Alternate approaches</h2>
<p>The most commonly proposed approach for versioning REST/HTTP web service interfaces today seems to be to mutilate the URIs by inserting a version. For example,</p>
<pre><code>http://foo.example/api/v1/accounts/3</code></pre>
<p>I really hate this approach as it implies that an account in one version of the API is really a different resource than the account in a different version of the API.</p>
<p>It also forces clients into a nasty choice, either support multiple versions of the API simultaneously or break one of the core constrains of REST. For example, say a client exists for the v1 API that saves references (URIs that include the version indicator) to accounts. Some time later the client is updated to support the new version of the API. In this situation the The client can support both versions of the API simultaneously because all the previously stored URIs point at the old version of the API or it has to mung all the URIs it has stored to the point at the new API. Munging all the URIS breaks the <a href='http://pezra.barelyenough.org/blog/2007/05/hypermedia-as-the-engine-of-application-state/'>HATEOAS</a> constraint of REST and supporting multiple versions of the API is a maintenance nightmare.</p>
<h2 id='conclusion'>Conclusion</h2>
<p>Changes to REST/HTTP web service interfaces come it three basic flavors, changes to the properties associated with a type of resource, additions of new types of resources and deprecation of obsolete types of resources. If you are following the <a href='http://pezra.barelyenough.org/blog/2007/05/hypermedia-as-the-engine-of-application-state/'>HATEOAS</a> constraint of REST the approach described here can be used to safely handle all three scenarios.</p>
<p>This approach does lead to media types being created, but media types are cheap so we can &#8211; and should &#8211; have as many as we need. Used properly, content negotiation can be used to solve the problems related to versioning a REST/HTTP web service interface.</p>
<hr />
<h4 id='related_posts'>Related Posts</h4>
<p>If you&#8217;re interested in REST/HTTP service versioning be sure not to miss the <a href='/blog/tag/rest-versioning/'>rest of the series.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://barelyenough.org/blog/2008/05/versioning-rest-web-services/feed/</wfw:commentRss>
		<slash:comments>55</slash:comments>
		</item>
		<item>
		<title>RESTful Service Discovery and Description</title>
		<link>http://barelyenough.org/blog/2008/01/restful-service-discovery-and-description/</link>
		<comments>http://barelyenough.org/blog/2008/01/restful-service-discovery-and-description/#comments</comments>
		<pubDate>Tue, 22 Jan 2008 21:00:30 +0000</pubDate>
		<dc:creator>Peter Williams</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[web services]]></category>

		<guid isPermaLink="false">http://pezra.barelyenough.org/blog/2008/01/restful-service-discovery-and-description/</guid>
		<description><![CDATA[There has been a great deal of discussion regarding RESTful web service description languages this week. The debate is great for the community but I think Steve Vinoski has it basically right never once — not even once — have I seen anyone develop a consuming application without relying on some form of human-oriented documentation [...]]]></description>
			<content:encoded><![CDATA[<p>There has been a great deal of discussion <a href='http://www.tbray.org/ongoing/When/200x/2008/01/18/Service-Doc-as-Interface-Def'>regarding</a> <a href='http://www.25hoursaday.com/weblog/2008/01/17/MythRESTfulWebServicesDontNeedAnInterfaceDefinitionLanguage.aspx'>RESTful</a> <a href='http://home.badc.rl.ac.uk/lawrence/blog/2008/01/22/whither_service_descriptions'>web</a> <a href='http://www.mnot.net/blog/2008/01/21/wadl_watching'>service</a> <a href='http://tomayko.com/weblog/2008/01/18/ws-vs-rest-sucks-nowadays'>description</a> languages this week. The debate is great for the community but I think <a href='http://steve.vinoski.net/blog/2008/01/16/idls-vs-human-documentation/'>Steve Vinoski has it basically right</a></p>
<blockquote>
<p>never once — not even once — have I seen anyone develop a consuming application without relying on some form of human-oriented documentation for the service being consumed</p>
</blockquote>
<p>When you start writing an application that makes use of some services you are not writing some sort of generic web services consumer. You are writing a consumer of one very specific web service and the semantics of a service, as with everything else, turn out to be a lot more complicated, subtle and interesting than the syntax.</p>
<p>Human-oriented documentation necessary because only human can understand the really interesting parts of a service description. Based on my experience, it also seems to be sufficient. Sure we could all jump on the full fledged service description language band wagon but I don&#8217;t think that service consumers would get much, if any, value out of it.<sup id='6354b2fa5c2dba65b06b5acf4059bf582b9d3808fnref:1'><a href='#6354b2fa5c2dba65b06b5acf4059bf582b9d3808fn:1' rel='footnote'>1</a></sup></p>
<h2 id='discoverability'>Discoverability</h2>
<p>Discoverability is the most important capability that interface definition languages bring to the table. However, most service description languages provide discoverability almost as a side effect, rather than it being their primary purpose.</p>
<p>I think it would be better to promote discoverability by working on a more focused capabilities publishing mechanism. To that end, I want to describe what my team has done on this front. It is not entirely suitable general use, but useful standards often emerge from extracting the common parts of many bespoke solutions.</p>
<p>First I want to be clear about the terminology I am using just to make sure we all understand one another</p>
<dl>
<dt>service</dt>
<dd>A cohesive set of resources that are exposed via HTTP.</dd>
<dt>resource</dt>
<dd>An single entity which exposes a RESTful interface via HTTP.</dd>
<dt>service provider</dt>
<dd>A process or set of processes that implement one or more services.</dd>
<dt>container</dt>
<dd>Another name for a service provider.</dd>
</dl>
<h4 id='background'>Background</h4>
<p>We needed the ability to discover the actual URIs of resources at runtime from very early in our project because of our basic architecture. Our system is composed of at least four services<sup id='6354b2fa5c2dba65b06b5acf4059bf582b9d3808fnref:2'><a href='#6354b2fa5c2dba65b06b5acf4059bf582b9d3808fn:2' rel='footnote'>2</a></sup>. The containers that provide these services may be deploy in various (and arbitrary) ways. Maintaining the list of top level resources of other services in configuration files became unmanageable long before we every actually deployed the system in production.</p>
<p>We need a way that any component in the system could discover the URIs of resources exposed by other components in the system. We handled this by providing a system wide registry of all the services that are available and a description resource for each service that provides link to the resources contained with that service.</p>
<h3 id='service_description'>Service Description</h3>
<p>Containers that provide a service are responsible for exposing a &#8220;service description&#8221; for that service.</p>
<p>A service description is a resource that provides links to all the top level resources in service. Currently we support just one type of representation (format) for service description, a JSON format that looks like this</p>
<pre><code>{
  &quot;_type&quot;:        &quot;ServiceDescriptor&quot;,
  &quot;service_type&quot;: &quot;http://mydomain.example/services/something-interesting&quot;,
  &quot;resources&quot;: [
    {
      &quot;_type&quot;: &quot;ResourceDescriptor&quot;,
      &quot;name&quot;:  &quot;OpenIdProvider&quot;,
      &quot;href&quot;:  &quot;http://core.ssbe.example/openid&quot;
    },
    {
      &quot;_type&quot;: &quot;ResourceDescriptor&quot;,
      &quot;name&quot;:  &quot;AllAccounts&quot;,
      &quot;href&quot;:  &quot;http://core.ssbe.example/accounts&quot;
    }
  ]
}</code></pre>
<p><code>service_type</code> is the globally unique name for the type service that is being described. It should be a URI that is owned by the creator of the service. Each top level resource that is exposed as part of this service has a resource descriptor in the <code>resources</code> set.</p>
<p>If you wanted know about all the accounts of the system you would</p>
<p>1. <code>GET</code> the service descriptor resource 2. iterate over the resources collection until you found the <code>AllAccounts</code> resource descriptor 3. <code>GET</code> the URI found in the <code>href</code> pair of the resource descriptor (<code>http://core.ssbe.example/accounts</code> in this example)</p>
<p>One important thing to note is that each resource is really exactly one resource, and not a type of resource. If you are looking for a particular account you have to get the AllAccounts collection and find the account you are looking for in that set.</p>
<h3 id='capabilities'>Capabilities</h3>
<p>The Capabilities resource is the only well known entry point for our system. If a program wants to interact with our system it always starts with the capabilities service and the works it&#8217;s way down, using the links in documents, to the resource it actually cares about.</p>
<p>The JSON representation we support looks like</p>
<pre><code>{
  &quot;_type&quot;: &quot;SystemCapabilities&quot;,
  &quot;services&quot;: [
    {
      &quot;_type&quot;:        &quot;ServiceDescriptor&quot;,
      &quot;href&quot;:         &quot;http://alarm.ssbe.example/service_descriptors/escalations&quot;,
      &quot;service_type&quot;: &quot;http://mydomain.example/services/something-interesting&quot;
    }
  ]
}</code></pre>
<p>To discover the URI of a particular top level resource a consumer must</p>
<p>1. <code>GET</code> the capabilities document 2. iterate though the objects in <code>services</code> until if it finds the one of the correct <code>service_type</code> 3. <code>GET</code> the full service descriptor using the URI in the <code>href</code> pair 4. iterate of the resources until it finds the one with the correct name 5. extract the URI from it&#8217;s <code>href</code> pair</p>
<p>Services are registered with the capabilities resource, by <code>POST</code>ing a service description to it, when the containers that provide those services are started.</p>
<h3 id='issues'>Issues</h3>
<h4 id='no_supported_methods_or_format_information'>No supported methods or format information</h4>
<p>This approach only provides a way to discover the URIs of top level resources. It makes no attempt to describe the representations (formats) or methods those resources support. That sort of thing would not be hard add but so far I have had absolutely not need for it. That information is provided by the human-oriented documentation and since it does not change in each deployment there is no need for it included in the dynamic resource discovery mechanism.</p>
<h4 id='nontop_level_resources_are_not_represented'>Non-top level resources are not represented</h4>
<p>Resources that are not top level &#8211; by which I mean resources that not listed in a service description document &#8211; are not represented at all. This is a feature, really, but it makes extending this format to include method and data format information less compelling becayse only a relatively minor subset of the resources in the system are surfaced in the service descriptions.</p>
<h4 id='encourages_large_representations'>Encourages large representations</h4>
<p>The fact that only singleton resources are supported can lead to top level documents that are excessively large. In fact, we have already had to deal with this issue. We have basically punted on the issue but I think the correct approach would be to introduce a ResourceTypeDescriptor that would operate much like a ResourceDescriptor except that the link would be a <a href='http://bitworking.org/projects/URI-Templates/'>URI template</a> rather than a concrete URI.</p>
<div class='footnotes'>
<hr />
<ol>
<li id='6354b2fa5c2dba65b06b5acf4059bf582b9d3808fn:1'>
<p>On the other hand service providers might get some value. Something like WADL does give you a way to declaratively define a suite of regression tests. On the other hand, you might be better off using a tool specifically built for that purpose.</p>
<p><a href='#6354b2fa5c2dba65b06b5acf4059bf582b9d3808fnref:1' rev='footnote'>&#8617;</a></li>
<li id='6354b2fa5c2dba65b06b5acf4059bf582b9d3808fn:2'>
<p>That is the base number of services. Additionally functionality is added to the system in the form of additional services so the actually number of services varies based on what you need the system to do.</p>
<p><a href='#6354b2fa5c2dba65b06b5acf4059bf582b9d3808fnref:2' rev='footnote'>&#8617;</a></li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://barelyenough.org/blog/2008/01/restful-service-discovery-and-description/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

