Some REST Design Patterns (and Anti-Patterns)



Similar documents
Techniques for Composing REST services

Session 6 Patterns and best practices in SOA/REST

REST vs. SOAP: Making the Right Architectural Decision

Understanding Service-Orientation Part II: The Principles

BPMN for REST. Cesare Pautasso Faculty of Informatics, USI Lugano, Switzerland

Designing RESTful Web Applications

The Hyper-Text Transfer Protocol (HTTP)

Modeling RESTful Conversations with Extended BPMN Choreography Diagrams

SOA and Virtualization Technologies (ENCS 691K Chapter 2)

vcloud Air Platform Programmer's Guide

Distribution and Integration Technologies

Getting Started with Service- Oriented Architecture (SOA) Terminology

SOACertifiedProfessional.Braindumps.S90-03A.v by.JANET.100q. Exam Code: S90-03A. Exam Name: SOA Design & Architecture

September 2009 Cloud Storage for Cloud Computing

Service Computing: Basics Monica Scannapieco

A Comparison of Service-oriented, Resource-oriented, and Object-oriented Architecture Styles

A Quick Introduction to SOA

reference: HTTP: The Definitive Guide by David Gourley and Brian Totty (O Reilly, 2002)

Building Web-based Infrastructures for Smart Meters

A standards-based approach to application integration

02267: Software Development of Web Services

How To Understand A Services-Oriented Architecture

A Comparison of Service-oriented, Resource-oriented, and Object-oriented Architecture Styles

Nuance Mobile Developer Program. HTTP Services for Nuance Mobile Developer Program Clients

Oracle Service Bus: - When to use, where to use and when not to use

Definition of SOA. Capgemini University Technology Services School Capgemini - All rights reserved November 2006 SOA for Software Architects/ 2

The Service Revolution software engineering without programming languages

Towards Introducing and Implementation of SOA Design Antipatterns

JOURNAL OF OBJECT TECHNOLOGY

Developers Integration Lab (DIL) System Architecture, Version 1.0

Satisfying business needs while maintaining the

SOA Success is Not a Matter of Luck

Architectural Patterns (3)

Service-Oriented Architectures

DARIAH Storage API A Basic Storage Service API on Bit Preservation Level

B6: GET /started/with/ HTTP Analysis

Introduction to Service Oriented Architectures (SOA)

Service Virtualization: Managing Change in a Service-Oriented Architecture

Developing a Service Oriented Process Management System for University Quality Assurance

CONTENT of this CHAPTER

EVALUATION. WA1844 WebSphere Process Server 7.0 Programming Using WebSphere Integration COPY. Developer

WIRIS quizzes web services Getting started with PHP and Java

Project #2. CSE 123b Communications Software. HTTP Messages. HTTP Basics. HTTP Request. HTTP Request. Spring Four parts

Service-Oriented Architecture: Analysis, the Keys to Success!

HTTP Protocol. Bartosz Walter

Web-Based Hazus-MH. A Conceptual Approach. Mourad Bouhafs, AVP ATKINS Atlanta mourad.bouhafs@atkinsglobal.com

CSCI 5828 Spring 2010 Foundations of Software Engineering. - Arpit Sud

02267: Software Development of Web Services

Method of Unified Communications and Collaboration Service in Open Service Platform based on RESTful Web Services

Web Services Software Architecture

AN APPROACH TO DEVELOPING BUSINESS PROCESSES WITH WEB SERVICES IN GRID

Process Execution Engine

Service-oriented architecture in e-commerce applications

Fairsail REST API: Guide for Developers

E-Business Suite Oracle SOA Suite Integration Options

Application layer Web 2.0

This project was supported by Grant No DB-BX-K105 awarded by the Bureau of Justice, Office of Justice Programs in collaboration with the U.S.

Internet Technologies. World Wide Web (WWW) Proxy Server Network Address Translator (NAT)

Design REST Services with CXF JAX- RS implementation: best practices and lessons learned

Service Oriented Architectures

An Oracle White Paper November Oracle Primavera P6 EPPM Integrations with Web Services and Events

Automating Rich Internet Application Development for Enterprise Web 2.0 and SOA

Contents. 2 Alfresco API Version 1.0

Lightweight Data Integration using the WebComposition Data Grid Service

Domain Name System (DNS)

Internet Technologies Internet Protocols and Services

Internationalization and Web Services

OCCI and Security Operations in OpenStack - Overview

Service-Oriented Architecture and Software Engineering

HireDesk API V1.0 Developer s Guide

What is the NXTware Evolution Server Peter Marquez, Product Marketing ecube Systems

Service Oriented Architecture 1 COMPILED BY BJ

Microsoft SOA Roadmap

REST API Development. B. Mason Netapp E-Series

SOA Myth or Reality??

Air Force SOA Enterprise Service Bus Study Using Business Process Management Workflow Orchestration for C4I Systems Integration

WP4: Cloud Hosting Chapter Object Storage Generic Enabler

Cloud Elements! Marketing Hub Provisioning and Usage Guide!

Scientific versus Business Workflows

Enterprise Application Designs In Relation to ERP and SOA

OAuth 2.0 Developers Guide. Ping Identity, Inc th Street, Suite 100, Denver, CO

Delivery Context in Internet Protocols. Larry Masinter Adobe Systems Incorporated

Computer Networks. Lecture 7: Application layer: FTP and HTTP. Marcin Bieńkowski. Institute of Computer Science University of Wrocław

Request for Comments: August 2006

Who are We Specialized. Recognized. Preferred. The right partner makes all the difference.

1 What Are Web Services?

Transcription:

Some REST Design Patterns (and Anti-Patterns) Cesare Pautasso Faculty of Informatics University of Lugano, Switzerland c.pautasso@ieee.org http://www.pautasso.info

Abstract The REST architectural style is simple to define, but understanding how to apply it to design concrete REST services in support of SOA can be more complex. The goal of this talk is to present the main design elements of a RESTful architecture and introduce a pattern-based design methodology for REST services. A selection of REST-inspired SOA design patterns taken from the upcoming "SOA with REST" book will be explained and further discussed to share useful solutions to recurring design problems and to also the foundational building blocks that comprise the REST framework from a patterns perspective. We will conclude by introducing some common SOA anti-patterns particularly relevant to the design of REST services in order to point out that not all current Web services that claim to be RESTful are indeed truly so. 2009 - Cesare Pautasso 2

Acknowledgements The following distinguished individuals have contributed to the the patterns and reviewed some of the material presented in this talk: Raj Balasubramanian Benjamin Carlyle Thomas Erl Stefan Tilkov Erik Wilde Herbjorn Wilhelmsen Jim Webber And all the participants, sheperds and sheeps of the SOA Patterns Workshop 2009 - Cesare Pautasso 3

About Cesare Pautasso Assistant Professor at the Faculty of Informatics, University of Lugano, Switzerland (since Sept 2007) Research Projects: SOSOA Self- Organizing Service Oriented Architectures CLAVOS Continuous Lifelong Analysis and Verification of Open Services BPEL for REST Researcher at IBM Zurich Research Lab (2007) Post- Doc at ETH Zürich Software: JOpera: Process Support for more than Web services http://www.jopera.org/ Ph.D. at ETH Zürich, Switzerland (2004) Representations: http://www.pautasso.info/ (Web) http://twitter.com/pautasso/ (Twitter Feed) 2009 - Cesare Pautasso 4

REST Patterns - Outline Design Methodology Simple Doodle Service Example & Demo SOA Design Patterns Entity Endpoint Uniform Contract Endpoint Redirection Content Negotiation Idempotent Capability AntiPatterns Tunneling everything through GET Tunneling everything through POST 2009 - Cesare Pautasso 5

Design Methodology for REST 1. Identify resources to be exposed as services (e.g., yearly risk report, book catalog, purchase order, open bugs, polls and votes) 2. Model relationships (e.g., containment, reference, state transitions) between resources with hyperlinks that can be followed to get more details (or perform state transitions) 3. Define nice URIs to address the resources 4. Understand what it means to do a GET, POST, PUT, DELETE for each resource (and whether it is allowed or not) 5. Design and document resource representations 6. Implement and deploy on Web server 7. Test with a Web browser /loan /balance /client /book /order /soap GET PUT? POST DELETE 2009 - Cesare Pautasso 6

Design Space M Representations (Variable) 4 Methods (Fixed) N Resources (Variable) 2009 - Cesare Pautasso 7

Simple Doodle API Example Design 1. Resources: polls and votes 2. Containment Relationship: poll {id1} vote {id4} /poll /poll/{id} /poll/{id}/vote /poll/{id}/vote/{id} DELETE POST PUT GET? {id2} {id3} {id5} 3. URIs embed IDs of child instance resources 4. POST on the container is used to create child resources 5. PUT/DELETE for updating and removing child resources 2009 - Cesare Pautasso 8

Simple Doodle API Example 1. Creating a poll (transfer the state of a new poll on the Doodle service) /poll /poll/090331x /poll/090331x/vote POST /poll <options>a,b,c</options> 201 Created Location: /poll/090331x GET /poll/090331x 200 OK <options>a,b,c</options> <votes href= /vote /> 2. Reading a poll (transfer the state of the poll from the Doodle service) 2009 - Cesare Pautasso 9

Simple Doodle API Example Participating in a poll by creating a new vote sub- /poll /poll/090331x /poll/090331x/vote /poll/090331x/vote/1 resource POST /poll/090331x/vote <name>c. Pautasso</name> <choice>b</choice> 201 Created Location: /poll/090331x/vote/1 GET /poll/090331x 200 OK <options>a,b,c</options> <votes><vote id= 1 > <name>c. Pautasso</name> <choice>b</choice> </vote></votes> 2009 - Cesare Pautasso 10

Simple Doodle API Example Existing votes can be updated (access control headers not shown) /poll /poll/090331x /poll/090331x/vote /poll/090331x/vote/1 PUT /poll/090331x/vote/1 <name>c. Pautasso</name> <choice>c</choice> 200 OK GET /poll/090331x 200 OK <options>a,b,c</options> <votes><vote id= /1 > <name>c. Pautasso</name> <choice>c</choice> </vote></votes> 2009 - Cesare Pautasso 11

Simple Doodle API Example Polls can be deleted once a decision has been made /poll /poll/090331x /poll/090331x/vote /poll/090331x/vote/1 DELETE /poll/090331x 200 OK GET /poll/090331x 404 Not Found 2009 - Cesare Pautasso 12

Design Patterns Content Negotiation M Representations (Variable) 4 Methods (Fixed) Entity Endpoint Endpoint Redirect N Resources (Variable) Uniform Contract Idempotent Capability 2009 - Cesare Pautasso 13

Pattern: Uniform Contract Provider US calculaterate() Consumer Provider CH Provider IT retrievetaxrate() figureoutratefortax() How can consumers take advantage of multiple evolving service endpoints? Problem: Accessing similar services requires consumers to access capabilities expressed in service- specific contracts. The consumer needs to be kept up to date with respect to many evolving individual contracts. 2009 - Cesare Pautasso 14

Pattern: Uniform Contract GET www.irs.gov/rate Provider US GET www.admin.ch/tax/rate Consumer Provider CH Provider IT GET www.tesoro.it/tasse/iva Solution: Standardize a uniform contract across alternative service endpoints that is abstracted from the specific capabilities of individual services. Benefits: Service Abstraction, Loose Coupling, Reusability, Discoverability, Composability. 2009 - Cesare Pautasso 15

Example Uniform Contract CRUD CREATE READ UPDATE DELETE REST POST GET PUT DELETE Create a sub resource Retrieve the current state of the resource Initialize or update the state of a resource at the given URI Clear a resource, after the URI is no longer valid 2009 - Cesare Pautasso 16

POST vs. GET GET is a read-only operation. It can be repeated without affecting the state of the resource (idempotent) and can be cached. Note: this does not mean that the same representation will be returned every time. POST is a read-write operation and may change the state of the resource and provoke side effects on the server. Web browsers warn you when refreshing a page generated with POST 2009 - Cesare Pautasso 17

POST vs. PUT What is the right way of creating resources (initialize their state)? PUT /resource/{id} 201 Created Problem: How to ensure resource {id} is unique? (Resources can be created by multiple clients concurrently) Solution 1: let the client choose a unique id (e.g., GUID) POST /resource 301 Moved Permanently Location: /resource/{id} Solution 2: let the server compute the unique id Problem: Duplicate instances may be created if requests are repeated due to unreliable communication 2009 - Cesare Pautasso 18

Pattern: Endpoint Redirection Stale Reference Consumer Redirect Service Endpoint Consumer Old Endpoint New Endpoint How can consumers of a service endpoint adapt when service inventories are restructured? Problem: Service inventories may change over time for business or technical reasons. It may not be possible to replace all references to old endpoints simultaneously. Solution: Automatically refer service consumers that access the stale endpoint identifier to the current identifier. 2009 - Cesare Pautasso 19

Endpoint Redirection with HTTP GET /old 301 Moved Permanently Location: /new GET /new 200 OK /old /new HTTP natively supports the Endpoint redirection pattern using a combination of 3xx status codes and standard headers: 301 Moved Permanently 307 Temporary Redirect Location: /newuri Tip: Redirection responses can be chained. Warning: do not create redirection loops! 2009 - Cesare Pautasso 20

Design Patterns Content Negotiation M Representations (Variable) 4 Methods (Fixed) Entity Endpoint Endpoint Redirect N Resources (Variable) Uniform Contract Idempotent Capability 2009 - Cesare Pautasso 21

Pattern: Entity Endpoint Consumer X Z Provider Endpoint A B C X Y Z Business Entities How can entities be positioned as reusable enterprise resources? Problem: A service with a single endpoint is too coarse-grained when its capabilities need to be invoked on its data entities. A consumer needs to work with two identifiers: a global one for the service and a local one for the entity managed by the service. Entity identifiers cannot be reused and shared among multiple services 2009 - Cesare Pautasso 22

Pattern: Entity Endpoint Consumer Provider Entity Endpoints X Y Z A B C Solution: expose each entitity as individual lightweight endpoints of the service they reside in Benefits: Global addressability of service entities 2009 - Cesare Pautasso 23

URI - Uniform Resource Identifier Internet Standard for resource naming and identification (originally from 1994, revised until 2005) Examples: http://tools.ietf.org/html/rfc3986 URI Scheme Authority Path https://www.google.ch/search?q=rest&start=10#1 Query Fragment REST does not advocate the use of nice URIs In most HTTP stacks URIs cannot have arbitrary length (4Kb) 2009 - Cesare Pautasso 24

What is a nice URI? A RESTful service is much more than just a set of nice URIs http://map.search.ch/lugano http://maps.google.com/lugano http://maps.google.com/maps?f=q&hl=en&q=lugano, +switzerland&layer=&ie=utf8&z=12&om=1&iwloc=addr 2009 - Cesare Pautasso 25

URI Design Guidelines Prefer Nouns to Verbs Keep your URIs short If possible follow a positional parameterpassing scheme for algorithmic resource query strings (instead of the key=value&p=v encoding) Some use URI postfixes to specify the content type Do not change URIs Use redirection if you really need to change them GET /book?isbn=24&action=delete DELETE /book/24 Note: REST URIs are opaque identifiers that are meant to be discovered by following hyperlinks and not constructed by the client This may break the abstraction Warning: URI Templates introduce coupling between client and server 2009 - Cesare Pautasso 26

Pattern: Content Negotiation Consumer? Service New Consumer How can services support different consumers without changing their contract? Problem: Service consumers may change their requirements in a way that is not backwards compatible. A service may have to support both old and new consumers without having to introduce a specific capability for each kind of consumer. 2009 - Cesare Pautasso 27

Pattern: Content Negotiation Consumer New Consumer Service Solution: specific content and data representation formats to be accepted or returned by a service capability is negotiated at runtime as part of its invocation. The service contract refers to multiple standardized media types. Benefits: Loose Coupling, Increased Interoperability, Increased Organizational Agility 2009 - Cesare Pautasso 28

Content Negotiation in HTTP Negotiating the message format does not require to send more messages (the added flexibility comes for free) GET /resource Accept: text/html, application/xml, application/json 1. The client lists the set of understood formats (MIME types) 200 OK Content-Type: application/json 2. The server chooses the most appropriate one for the reply (status 406 if none can be found) 2009 - Cesare Pautasso 29

Advanced Content Negotiation Quality factors allow the client to indicate the relative degree of preference for each representation (or media-range). Media/Type; q=x If a media type has a quality value q=0, then content with this parameter is not acceptable for the client. Accept: text/html, text/*; q=0.1 The client prefers to receive HTML (but any other text format will do with lower priority) Accept: application/xhtml+xml; q=0.9, text/html; q=0.5, text/plain; q=0.1 The client prefers to receive XHTML, or HTML if this is not available and will use Plain Text as a fall back 2009 - Cesare Pautasso 30

Forced Content Negotiation The generic URI supports content negotiation GET /resource Accept: text/html, application/xml, application/json The specific URI points to a specific representation format using the postfix (extension) GET /resource.html GET /resource.xml GET /resource.json Warning: This is a conventional practice, not a standard. What happens if the resource cannot be represented in the requested format? 2009 - Cesare Pautasso 31

Multi-Dimensional Negotiation Content Negotiation is very flexible and can be performed based on different dimensions (each with a specific pair of HTTP headers). Request Header Accept: Accept-Language: Accept-Charset: Accept-Encoding: Example Values application/xml, application/json en, fr, de, es iso-8859-5, unicode-1-1 compress, gzip Response Header Content-Type: Content-Language: Charset parameter fo the Content-Type header Content-Encoding: 2009 - Cesare Pautasso 32

Pattern: Idempotent Capability Client Service Client Service Timeout Timeout!!! How can a service consumer recover from lost messages after network disruption or server failure within a service cluster? Problem: Service oriented architectures are distributed systems. Failures (such as the loss of messages) may occur during service capability invocation. A lost request should be retried, but a lost response may cause unintended side-effects if retried automatically. 2009 - Cesare Pautasso 33

Pattern: Idempotent Capability Client ESB ESB Service Timeout Solution: use an ESB, with support for reliable messaging. Problem: do we always need this? Are there some messages more critical than others? 2009 - Cesare Pautasso 34

Pattern: Idempotent Capability Client Service Simpler Solution: if possible use idempotent service capabilities, whereby services provide a guarantee that capability invocations are safe to repeat in the case of failures that could lead to a response message being lost 2009 - Cesare Pautasso 35

Idempotent vs. Unsafe Idempotent requests can be processed multiple times without side-effects GET /book PUT /order/x DELETE /order/y If something goes wrong (server down, server internal error), the request can be simply replayed until the server is back up again Safe requests are idempotent requests which do not modify the state of the server (can be cached) GET /book Unsafe requests modify the state of the server and cannot be repeated without additional (unwanted) effects: Withdraw(200$) //unsafe Deposit(200$) //unsafe Unsafe requests require special handling in case of exceptional situations (e.g., state reconciliation) POST /order/x/payment In some cases the API can be redesigned to use idempotent operations: B = GetBalance() //safe B = B + 200$ //local SetBalance(B) //idempotent 2009 - Cesare Pautasso 36

Dealing with Concurrency GET /balance 200 OK ETag: 26 PUT /balance ETag: 26 200 OK ETag: 27 /balance Breaking down the API into a set of idempotent requests helps to deal with temporary failures. But what about if another client concurrently modifies the state of the resource we are about to update? Do we need to create an explicit /balance/lock resource? (Pessimistic Locking) Or is there an optimistic solution? 2009 - Cesare Pautasso 37

Dealing with Concurrency /balance GET /balance 200 OK ETag: 26 PUT /balance ETag: 26 PUT /balance ETag: 26 200 OK ETag: 27 409 Conflict The 409 status code can be used to inform a client that his request would render the state of the resource inconsistent 2009 - Cesare Pautasso 38

Antipatterns - REST vs. HTTP REST HTTP RPC RESTful HTTP 2009 - Cesare Pautasso 39

Antipatterns HTTP as a tunnel Tunnel through one HTTP Method GET /api?method=addcustomer&name=pautasso GET /api?method=deletecustomer&id=42 GET /api?method=getcustomername&id=42 GET /api?method=findcustomers&name=pautasso* Everything through GET Advantage: Easy to test from a Browser address bar (the action is represented in the resource URI) Problem: GET should only be used for read-only (= idempotent and safe) requests. What happens if you bookmark one of those links? Limitation: Requests can only send up to approx. 4KB of data (414 Request-URI Too Long) 2009 - Cesare Pautasso 40

Antipatterns HTTP as a tunnel Tunnel through one HTTP Method Everything through POST Advantage: Can upload/download an arbitrary amount of data (this is what SOAP or XML-RPC do) Problem: POST is not idempotent and is unsafe (cannot cache and should only be used for dangerous requests) POST /service/endpoint <soap:envelope> <soap:body> <findcustomers> <name>pautasso*</name> </findcustomers> </soap:body> </soap:envelope> Is this a resource? 2009 - Cesare Pautasso 41

REST Design Patterns 1. Uniform Contract 2. Entity Endpoint 3. Entity Linking* 4. Content Negotiation 5. Distributed Response Caching* 6. Endpoint Redirection 7. Idempotent Capability 8. Message-based State Deferral* 9. Message-based Logic Deferral* 10.Consumer-Processed Composition* *Not Included in this talk 2009 - Cesare Pautasso 42

References R. Fielding, Architectural Styles and the Design of Networkbased Software Architectures, PhD Thesis, University of California, Irvine, 2000 C. Pautasso, O. Zimmermann, F. Leymann, RESTful Web Services vs. Big Web Services: Making the Right Architectural Decision, Proc. of the 17th International World Wide Web Conference (WWW2008), Bejing, China, April 2008 C. Pautasso, BPEL for REST, Proc. of the 7 th International Conference on Business Process Management (BPM 2008), Milano, Italy, September 2008 C. Pautasso, Composing RESTful Services with JOpera, In: Proc. of the International Conference on Software Composition (SC2009), July 2009, Zurich, Switzerland. 43

Conclusion Raj Balasubramanian, Applying the SOA composition Benjamin principle Carlyle, to Thomas Erl, REST gives interesting results Cesare Pautasso, Thanks to hyperlinks, REST SOA brings with a REST, new (more dynamic and loosely coupled) Prentice Hall, twist to SOA composition to appear in 2010 Composing RESTful services helps to build mashups, but is different A RESTful API is the perfect abstraction for publishing the state of a workflow 2009 - Cesare Pautasso 44

ECOWS 2010 8 th European Conference on Web Services Cyprus http://www.cs.ucy.ac.cy/ecows10 http://www.twitter.com/ecows2010 2009-2010 - Cesare Pautasso, Erik Wilde 127