SlideShare una empresa de Scribd logo
1 de 229
Representational State Transfer
(REST) architectural style
Prepared by Vinod Wilson – Architect – Crestron Electronics
What is a Hypermedia system?
• An extension to hypertext that supports linking
graphics, sound, and video elements in
addition to text elements. The World Wide
Web is a partial hypermedia system since is
supports graphical hyperlinks and links to
sound and video files. New hypermedia
systems under development will allow objects
in computer videos to be hyperlinked.
What is a Software Architecture?
A software architecture is an
abstraction of the run-time elements
of a software system during some phase
of its operation. A system may be
composed of many levels of abstraction
and many phases of operation, each
with its own software architecture.
Software Architecture
• At the heart of software architecture is the
principle of abstraction: hiding some of the
details of a system through encapsulation in
order to better identify and sustain its
properties
• A complex system will contain many levels of
abstraction, each with its own architecture.
Software Architecture
• An architecture represents an abstraction of system
behavior at that level, such that architectural elements
are delineated by the abstract interfaces they provide
to other elements at that level
Software Architecture
• Within each element may be found another
architecture, defining the system of sub-
elements that implement the behavior
represented by the parent element’s abstract
interface.
• This recursion of architectures continues down
to the most basic system elements: those that
cannot be decomposed into less abstract
elements.
Architecture of transitions between
phases
• In addition to levels of architecture,
• A software system will often have multiple
operational phases
• start-up
• initialization
• normal processing
• re-initialization
• Shutdown
• Each operational phase has its own architecture
Architecture of transitions
between phases- Example
Architecture of transitions
between phases- Example
• A configuration file will be treated as a data
element during the start-up phase
• But won’t be considered an architectural
element during normal processing.
• since at that point the information it
contained will have already been distributed
throughout the system
Architecture of transitions
between phases
• An overall description of a system architecture
must be capable of describing not only the
operational behavior of the system’s
architecture during each phase, but also the
architecture of transitions between phases.
Architectural design and source
code structural design
• Though closely related, they are separate
design activities.
Elements
• A software architecture is defined by a
configuration of architectural elements—
components, connectors, and data—constrained in
their relationships in order to achieve a desired set
of architectural properties.
• As an illustration, consider what happens to a
building if its blueprints and design plans are
burned. Does the building immediately collapse?
• No, since the properties by which the walls sustain
the weight of the roof remain intact.
Elements classification
• Processing elements (Or Components)
• Processing elements are those that perform
transformations on data,
• Data elements
• Data elements are those that contain the
information that is used and transformed
• Connecting elements (Connectors)
• connecting elements are the glue that holds the
different pieces of the architecture together.
Components
• A component is an abstract unit of software
instructions and internal state that provides a
transformation of data via its interface.
• Example transformations include
• loading into memory from secondary storage,
• performing some calculation,
• translating to a different format
• format, encapsulation with other data,
• a component is defined by its interface and
the services it provides to other components,
rather than by its implementation behind the
interface
Connectors
• A connector is an abstract mechanism that
mediates communication, coordination, or
cooperation among components.
• Connectors enable communication between
components by transferring data elements
from one interface to another without
changing the data.
• Internally, a connector may consist of a
subsystem of components that transform the
data for transfer, perform the transfer, and
then reverse the transformation for delivery
Data
• A datum is an element of information that is
transferred from a component, or received by
a component, via a connector.
• A datum is an element of information that is
transferred from a component, or received by
a component, via a connector.
Configurations
• A configuration is the structure of
architectural relationships among
components, connectors, and data during a
period of system run-time.
• Strictly speaking, one might think of a
configuration as being equivalent to a set of
specific constraints on component interaction.
Properties
• The set of architectural properties of a software
architecture includes all properties that derive from the
selection and arrangement of components, connectors,
and data within the system.
• Properties are induced by the set of constraints within
an architecture.
• For example, the uniform pipe-and-filter style obtains
the
• qualities of reusability of components and
configurability of the application by applying generality
to its component interfaces — constraining the
components to a single interface type
• Properties induced - reusable and configurable
components in uniform pipe-and-filter style
The goal of architectural design
• The goal of architectural design is to create an
architecture with a set of architectural
properties that form a superset of the system
requirements.
Architectural style
• An architectural style is a coordinated set of
architectural constraints that restricts the
roles/features of architectural elements and
the allowed relationships among those
elements within any architecture that
conforms to that style.
• An architectural style encapsulates important
decisions about the architectural elements and
emphasizes important constraints on the
elements and their relationships
Architectural style
• A good designer should select a style that
matches the needs of the particular problem
being solved
Views
Views
• An architectural viewpoint is often
application-specific and varies widely based
on the application domain.
• architectural viewpoints that address a
variety of issues
• state and control approaches
• data representation
• transaction life cycle,
• security safeguards,
• peak demand and graceful degradation.
• Etc…
Three important views in
software architecture
• Processing View
• A process view emphasizes the data flow through the
components and some aspects of the connections
among the components with respect to the data
• Data View
• A data view emphasizes the processing flow, with less
emphasis on the connectors
• Connection views.
• A connection view emphasizes the relationship
between components and the state of communication.
Network-based Application
Architectures
• Network-based vs. Distributed
• A distributed system is one that looks to its
users like an ordinary centralized system, but
runs on multiple, independent CPUs.
• In contrast, network-based systems are those
capable of operation across a network,
• Application Software vs. Networking Software
• For example
• A hypermedia application must be concerned with the location
of information pages, performing requests, and rendering data
streams.
• This is in contrast to a networking abstraction, where the goal is
to move bits from one location to another without regard to why
those bits are being moved
Architectural Properties of Key
Interest
• Performance
• Network Performance
• User-perceived Performance
• Network Efficiency
• Scalability
• Simplicity
• Modifiability
• Evolvability
• Extensibility
• Customizability
• Configurability
• Reusability
• Visibility
• Portability
• Reliability
Performance
• Network Performance
• Throughput is the rate at which information,
including both application data and communication
overhead, is transferred between components.
• Bandwidth is a measure of the maximum available
throughput over a given network link. Usable
bandwidth refers to that portion of bandwidth
which is actually available to the application.
• Styles impact network performance by their
influence on the number of interactions per user
action and the granularity of data elements.
Performance
• User-perceived Performance
• User-perceived performance differs from network
performance in that the performance of an action is
measured in terms of its impact on the user in front
of an application rather than the rate at which the
network moves information.
• The primary measures for user perceived
performance are latency and completion time.
• Latency is the time period between initial stimulus
and the first indication of a response.
Performance
• Latency occurs at several points in the processing of a
network-based application action:
• 1) the time needed for the application to recognize the event
that initiated the action;
• 2) the time required to setup the interactions between
components;
• 3) the time required to transmit each
interaction to the components;
• 4) the time required to process each interaction on those components;
• 5) the time required to complete sufficient transfer and
processing of the result of the interactions before the application
is able to begin rendering a usable result
• all five points can be impacted by the architectural
style
Network Efficiency
• An interesting observation about network-based
applications is that the best application
performance is obtained by not using the network.
• This essentially means that the most efficient
architectural styles for a network-based
application are those that can effectively minimize
use of the network when it is possible to do so.
• through reuse of prior interactions (caching)
• reduction of the frequency of network interactions in
relation to user actions (replicated data and
disconnected operation),
• by removing the need for some interactions by moving
the processing of data closer to the source of the data
(mobile code).
Network Efficiency
• The benefits of a style under local conditions
may become drawbacks when faced with
global conditions.
• the properties of a style must be framed in relation
to the interaction distance: within a single process,
across processes on a single host, inside a local-area
network (LAN), or spread across a wide-area
network (WAN)
Scalability
• Scalability refers to the ability of the architecture
to support large numbers of components, or
interactions among components, within an active
configuration.
• Scalability can be improved by simplifying
components, by distributing services across
many components (decentralizing the
interactions), and by controlling
interactions and configurations as a result of
monitoring.
• Styles influence these factors by determining the location of
application state, the extent of distribution, and the coupling
between components.
Simplicity
• The primary means by which architectural
styles induce simplicity is by applying the
principle of separation of concerns to the
allocation of functionality within components.
• If functionality can be allocated such
that the individual components are
substantially less complex, then they
will be easier to understand and
implement
Modifiability
• Modifiability is about the ease with which a change can be made to an
application architecture.
• Modifiability can be further broken down into
• Evolvability
• Evolvability represents the degree to which a component implementation can be
changed without negatively impacting other components
• Extensibility
• Extensibility is defined as the ability to add functionality to a system
• Customizability
• Customizability refers to the ability to temporarily specialize the behavior of an
architectural element, such that it can then perform an unusual service
• Configurability
• Configurability is related to both extensibility and reusability in that it refers to
postdeployment modification of components, or configurations of components, such
that they are capable of using a new service or data element type
• Reusability
• Reusability is a property of an application architecture if its components, connectors,
or data elements can be reused, without modification, in other applications
Network-based Architectural
Styles
• Data-flow Styles
• Pipe and Filter (PF)
• Uniform Pipe and Filter (UPF)
• Replication Styles
• Replicated Repository (RR)
• Cache ($)
• Hierarchical Styles
• Client-Server (CS)
• Layered System (LS) and Layered-Client-Server (LCS)
• Client-Stateless-Server (CSS)
• Client-Cache-Stateless-Server (C$SS)
• Layered-Client-Cache-Stateless-Server (LC$SS)
• Remote Session (RS)
• Remote Data Access (RDA)
• Mobile Code Styles
• Virtual Machine (VM)
• Remote Evaluation (REV)
• Code on Demand (COD)
• Layered-Code-on-Demand-Client-Cache-Stateless-Server (LCODC$SS)
• Mobile Agent (MA)
• Peer-to-Peer Styles
• Event-based Integration (EBI)
• C2
• Distributed Objects
• Brokered Distributed Objects
Data-flow Styles
• Pipe and Filter (PF)
• In a pipe and filter style, each component (filter)
reads streams of data on its inputs and produces
streams of data on its outputs, usually while
applying a transformation to the input streams and
processing them incrementally so that output begins
before the input is completely consumed
• The constraint is that a filter must be completely
independent of other filters (zero coupling): it must
not share state, control thread, or identity with the
other filters on its upstream and downstream
interfaces
Pipe and Filter (PF)
• Advantages properties
• PF allows the designer to understand the overall input/output
of the system as a simple composition of the behaviors of the
individual filters (simplicity).
• PF supports reuse: any two filters can be hooked together,
provided they agree on the data that is being transmitted
between them (reusability).
• PF systems can be easily maintained and enhanced: new
filters can be added to existing systems (extensibility) and
old filters can be replaced by improved ones (evolvability).
• they permit certain kinds of specialized analysis
(verifiability), such as throughput and deadlock analysis
• they naturally support concurrent execution (user-perceived
performance).
Pipe and Filter (PF)
• Disadvantages
• propagation delay is added through long pipelines,
batch sequential processing occurs if a filter cannot
incrementally process its inputs, and no
interactivity is allowed
• A filter cannot interact with its environment
because it cannot know that any particular output
stream shares a controller with any particular input
stream
Uniform Pipe and Filter (UPF)
• The uniform pipe and filter style adds the constraint
that all filters must have the same interface
• The primary example of this style is found in the Unix
operating system, where filter processes have an
interface consisting of one input data stream of
characters (stdin) and two output data streams of
characters (stdout and stderr)
• Advantages
• Restricting the interface allows independently developed filters
to be arranged at will to form new applications.
• Disadvantages
• A disadvantage of the uniform interface is that it may reduce
network performance if the data needs to be converted to or
from its natural format.
Replication Styles
• Replicated Repository (RR)
• Systems based on the replicated repository style [6]
improve the accessibility of data and scalability of
services by having more than one process provide
the same service. These decentralized servers
interact to provide clients the illusion that there is
just one, centralized service.
Cache ($)
• A variant of replicated repository is found in the cache
style: replication of the result of an individual request
such that it may be reused by later requests.
• This form of replication is most often found in cases
where the potential data set far exceeds the capacity of
any one client, as in the WWW [20], or where complete
access to the repository is unnecessary
• Caching provides slightly less improvement than the
replicated repository style in terms of user-perceived
performance, since more requests will miss the cache
and only recently accessed data will be available for
disconnected operation
Hierarchical Styles
• Client-Server (CS)
• A server component, offering a set of services, listens
for requests upon those services.
• A client component, desiring that a service be
performed, sends a request to the server via a
connector
• The server either rejects or performs the request and
sends a response back to the client.
• Separation of concerns is the principle behind the
client-server constraints.
• A proper separation of functionality should simplify the
server component in order to improve scalability.
Client-Server (CS)
• Tiers vs Layers
• These terms are often (regrettably) used
interchangeably. However they really are distinct
and have definite meanings. The basic difference is
that Tiers are physical, while Layers are logical.
Layered System (LS) and
Layered-Client-Server (LCS)
• A layered system is organized hierarchically,
each layer providing services to the layer
above it and using services of the layer below
it.
• Architectures based on layered-client-server
are referred to as two-tiered, three-tiered, or
multi-tiered architectures in the information
systems literature
Layered System (LS) and
Layered-Client-Server (LCS)
• The Two Tier Architecture
• The Three Tier Architecture
• The Multi or n-Tier Architecture
Layered System (LS) and
Layered-Client-Server (LCS)
• Layered systems reduce coupling across multiple layers
by hiding the inner layers
• from all except the adjacent outer layer, thus improving
evolvability and reusability.
• The primary disadvantage of layered systems is that
they add overhead and latency to the processing of
data, reducing user-perceived performance
• Layered-client-server adds proxy and gateway
components to the client-server style.
• A gateway component appears to be a normal server to
clients or proxies that request its services, but is in fact
forwarding those requests, with possible translation, to
its “inner-layer” servers
Client-Stateless-Server (CSS)
• The client-stateless-server style derives from client-
server with the additional constraint that no session
state is allowed on the server component
• Each request from client to server must contain all of
the information necessary to understand the request,
and cannot take advantage of any stored context on the
server
• Session state is kept entirely on the client.
• The disadvantage of client-stateless-server is that it
may decrease network performance by increasing the
repetitive data (per-interaction overhead) sent in a
series of requests, since that data cannot be left on the
server in a shared context.
Client-Cache-Stateless-Server
(C$SS)
• The client-cache-stateless-server style derives
from the client-stateless-server and cache
styles via the addition of cache components.
• The advantage of adding cache components is
that they have the potential to partially or
completely eliminate some interactions,
improving efficiency and user-perceived
performance.
Layered-Client-Cache-
Stateless-Server (LC$SS)
• The layered-client-cache-stateless-server style
derives from both layered-client-server and
client-cache-stateless-server through the
addition of proxy and/or gateway components.
• An example system that uses an LC$SS style is
the Internet domain name system (DNS).
Remote Session (RS)
• The remote session style is a variant of client-
server that attempts to minimize the complexity,
or maximize the reuse, of the client components
rather than the server component.
• Each client initiates a session on the server and
then invokes a series of services on the server,
finally exiting the session.
• Application state is kept entirely on the server.
• This style is typically used when it is desired to
access a remote service using a generic client
(e.g., TELNET [106]) or via an interface that
mimics a generic client (e.g.,FTP [107]).
Mobile Code Styles
• Mobile code styles use mobility in order to
dynamically change the distance between the
processing and source of data or destination of
results
• Code on Demand (COD)
• In the code-on-demand style [50], a client
component has access to a set of resources, but not
the know-how on how to process them..
• It sends a request to a remote server for the code
representing that know-how, receives that code,
and executes it locally.
Code on Demand (COD) Style
• The advantages of code-on-demand include the
ability to add features to a deployed client.
• which provides for improved extensibility and
configurability, and better user perceived
performance and efficiency when the code can
adapt its actions to the client’s environment and
interact with the user locally rather than through
remote interactions.
• Example : Javascript being downloaded from the
server based on the request from a webbrowser.
Layered-Code-on-Demand-Client-
Cache-Stateless-Server (LCODC$SS)
• Guess What?
Architectural Style Evaluation
Summary
Representational State
Transfer (REST)
• Thinking in Resources
• Resources are the fundamental building blocks of
web-based systems, to the extent that the Web is
often referred to as being “resource-oriented.”
• A resource is anything we expose to the Web, from a
document or video clip to a business process or
device.
• From a consumer’s point of view, a resource is
anything with which that consumer interacts while
progressing toward some goal.
• Almost anything can be modeled as a resource and
then made available for manipulation over the
network.
Resources and Identifiers
• To use a resource we need both to be able to
identify it on the network and to have some means
of manipulating it.
• The Web provides the Uniform Resource Identifier,
or URI, for just these purposes.
• A URI uniquely identifies a web resource, and at
the same time makes it addressable, or capable of
being manipulated using an application protocol
such as HTTP (which is the predominant protocol
on the Web).
• A resource’s URI distinguishes it from any other
resource, and it’s through its URI that interactions
with that resource take place.
Resources and Identifiers
• The relationship between URIs and resources is
many-to-one
• A URI identifies only one resource, but a
resource can have more than one URI.
• That is, a resource can be identified in more
than one way, much as humans can have
multiple email addresses or telephone
numbers.
Multiple URIs for a resource
Terms used on the Web to
refer to identifiers
Resource Representations
• Resources must have at least one identifier to
be addressable on the Web, and each
identifier is associated with one or more
representations.
• A representation is a transformation or a view
of a resource’s state at an instant in time.
• This view is encoded in one or more
transferable formats, such as XHTML, Atom,
XML, JSON, plain text, comma-separated
values, MP3, or JPEG.
Resource Representations
• (Actual Resource) Vs (Resource Information)
• For real-world resources, such as goods in a
warehouse, we can distinguish between the actual
object and the logical “information” resource
encapsulated by an application or service.
• It’s the information resource that is made available
to interested parties through projecting its
representations onto the Web.
Resource Representations
• Access to a resource is always mediated by
way of its representations. That is, web
components exchange representations.
• That is, web components exchange
representations; they never access the
underlying resource directly—the Web does not
support pointers!
• URIs relate, connect, and associate
representations with their resources on the
Web.
Resource Representations
Example of a resource and its
representations
Resource Representations
Computer-to-computer communication
using the Web
Representation Formats and
URIs
• There is a misconception that different
resource representations should each have
their own URI—a notion that has been
popularized by the Rails framework
• URIs should be opaque to consumers. Only the
issuer of the URI knows how to interpret and
map it to a resource. Using extensions such as
.xml, .html, or .json is a historical convention
that stems from the time when web servers
simply mapped URIs directly to files.
Representation Formats and URIs
Multiple resource representations addressed
by a single URI
Representation Formats and
URIs
• Using content negotiation, consumers can
negotiate for specific representation formats
from a service.
• They do so by populating the HTTP Accept
request header with a list of media types
they’re prepared to process.
• However, it is ultimately up to the owner of a
resource to decide what constitutes a good
representation of that resource in the context
of the current interaction, and hence which
format should be returned.
The Art of Communication
• On the Web, resources provide the subjects and
objects with which we want to interact, but how
do we act on them?
• The answer is that we need verbs, and on the Web
these verbs are provided by HTTP methods.
• The term uniform interface is used to describe
how a (small) number of verbs with well-defined
and widely accepted semantics are sufficient to
meet the requirements of most distributed
applications. A collection of verbs is used for
communication between systems.
The Art of Communication
• HTTP VERBS—GET, POST, PUT, DELETE, OPTIONS,
HEAD, TRACE, CONNECT, and PATCH—forms a
sufficiently general-purpose protocol to support a
wide range of solutions.
• In addition to verbs, HTTP also defines a collection
of response codes, such as 200 OK, 201 Created,
and 404 Not Found, that coordinate the
interactions instigated by the use of the verbs.
• Taken together, verbs and status codes provide a
general framework for operating on resources over
the network
The Art of Communication
Using HTTP to “GET” the
representation of a resource
From the Web Architecture to
the REST Architectural Style
• REST describes the Web as a distributed
hypermedia application whose linked
resources communicate by exchanging
representations of resource state.
Hypermedia
• Hypermedia as the engine of application state
• The idea is simple, and yet very powerful. A
distributed application makes forward progress by
transitioning from one state to another, just like a
state machine.
• The difference from traditional state machines,
however, is that the possible states and the
transitions between them are not known in
advance.
• Instead, as the application reaches a new state,
the next possible transitions are discovered. It’s
like a treasure hunt.
Hypermedia
Example of hypermedia as the engine for application
state in action
Introducing Restbucks: How to
GET a Coffee, Web Style
• Restbucks: A Little Coffee Shop with Global
Ambitions
• we’ll frame our problems and web-based solutions
in terms of a coffee shop called Restbucks, which
grows from modest beginnings to become a global
enterprise
Actors and Conversations
• Restbucks has actors such as
• Customers
• Cashiers
• Baristas (Coffee Maker)
• Managers
• suppliers
• All must interact to keep the coffee flowing.
XML-based exchange between
a customer and a waiter
Boundaries
• In Restbucks, we draw boundaries around the
actors involved in the system to encapsulate
implementation details and emphasize the
interactions between systems
• When we order a coffee, we don’t usually care
about the mechanics of the supply chain or the
details of the shop’s internal coffee-making
processes
• Composition of functionality and the introduction
of façades with which we can interact are common
practices in system design, and web-based systems
are no different in that respect.
Boundaries help decompose
groups of interactions
The Menu
Sample catalog of products offered
by Restbucks
Sample Interactions
A simple interaction between a
customer and a barista
Customers will have to wait?
A cashier helps the barista
The
interactions
(or protocol)
between the
cashier and
the barista
remain hidden
from
customers.
•By implementing this
scheme, Restbucks
decouples ordering
and payment from
the coffee
preparation.
•In turn, it is possible
for Restbucks to
abstract the inner
workings of the shop
through a façade.
While the customer
gets the same good
coffee, Restbucks is
free to innovate and
evolve its
implementation
A cashier helps the barista
• Restbucks decouples ordering and payment from
the coffee preparation
• In turn, it is possible for Restbucks to abstract the
inner workings of the shop through a façade.
• While the customer gets the same good coffee,
Restbucks is free to innovate and evolve its
implementation behind the scenes.
• Decoupling payments and drink preparation allows
Restbucks to optimize available resources.
Restbucks Formats
• All Restbucks resources are represented by
XML documents defined in the
http://restbucks.com namespace and
identified on the Web as the media types
application/xml and
application/vnd.restbucks+xml for standard
XML processing and Restbucks-specific
processing, respectively.
A Restbucks order resource
represented in XML format
HTTP
Header
HTTP
Request
Content
Acknowledging the order with
a custom XML format The order details are confirmed in
the reply and some additional
information is contained, such as the
payment amount and currency, a
timestamp for when the order was
received, and a <link> element that
identifies another resource with
which the customer is expected to
interact to make a payment.
Modeling Protocols and State
Transitions
A Simple Service Architecture
• HTTP remote procedure call architecture
Web Integration - URI
Templates
• Intuitive URIs
• As an example, let’s take Restbucks, which exposes
ordering information through URI-addressable
resources, such as
http://restbucks.com/order/1234.
• To a web developer, it should be intuitive that
changing the number after the final / character in
the URI will probably result in another resource
representation being returned for a different order.
From Intuitive URIs to URI
Templates
• An example of a URI template that describes valid URIs
for the service is
http://restbucks.com/order/{order_id}.
• The markup in curly braces, {order_id}, is an invitation
to Restbucks customers to “fill in the gaps” with
sensible values.
• By substituting those parameters, customers address
different coffee orders hosted at Restbucks.
• For example, the
http://restbucks.com/order/{year}/{month}/{day}
template supports accessing all of the orders for a given
date, allowing consumers to vary the year, month, and
day variables to access audit information.
Using URI Templates
• In practice, we prefer URI templates as a
means of internal documentation for services,
rather than as contract metadata. We find
that URI templates are fine as a shorthand
notation for communication within the context
of a system, but as a mechanism for describing
contracts, we think they risk introducing tight
coupling.
• We can put URI templates into practice
immediately, staring with the most basic HTTP
integration option: URI tunneling.
URI Tunneling
• We have numerous options for how to convey
our order to a cashier, and on the Web, URI
tunneling is the simplest.
• URI tunneling uses URIs as a means of
transferring information across system
boundaries by encoding the information within
the URI itself
URI Tunnelling
Mapping method calls to URIs
URI Tunnelling
HTTP request/response for URI
tunneling
Extracting business objects
from a URI
URI-tunneling client
CRUD Web Services
• Set of verbs required to fully support the
Create, Read, Update, Delete (CRUD) pattern
for manipulating resources across the network.
Modeling Orders As Resources
• Orders are created when a customer makes a
purchase.
• Orders are frequently read, particularly when
their preparation status is inquired.
• Under certain conditions, it may be possible
for orders to be updated (e.g., in cases where
customers change their minds or add
specialties to their drinks).
• Finally, if an order is still pending, a customer
may be allowed to cancel it (or delete it).
Possible states for an order
CRUD ordering service high-
level architecture
Building CRUD Services
The ordering service contract overview
Creating a Resource with POST
Creating an order via POST
Creating a coffee order via
POST
Response to successful order
creation
A malformed order request
Response to a malformed
order request
Reading Resource State with
GET
Requesting an order via GET
Request
Response
Rereading an order with GET
Requesting an order that
doesn’t exist via GET
Request Response
Ordering service indicates
unexpected failure with a 500
response
Updating a Resource with PUT
• Use POST to create a resource identified by a
service-generated URI.
• Use POST to append a resource to a collection
identified by a service-generated URI.
• Use PUT to create or overwrite a resource
identified by a URI computed by the client.
PUT request and responses
Updating an order
Successful update with a 200
response
Successful update with a 204
response
Whether 200 is used in preference to 204 is largely
an aesthetic choice. However, 200 with a response
body is more descriptive and actively confirms the
server-side state, while 204 is more efficient since it
returns no representation and indicates that the
server has accepted the request representation
verbatim.
Order has already been served
as a take-away
Implementing update with PUT
WCF ServiceContract for updating an order with PUT
Marking up an order for use
with WCF
WCF implementation for
updating an order
Removing a Resource with DELETE
DELETE request and responses
Removing an order with
DELETE
Request
Response - Order
successfully removed
Response - The requested
order doesn’t exist
Order has
been archived
Order has been archived
Using HttpListener to delete
an order
Safety and Idempotency
• GET is special since it has the properties of
being both safe and idempotent.
• PUT and DELETE are both idempotent, but
neither is safe
• While POST is neither safe nor idempotent.
Aligning Resource State
• In a distributed application, it’s often the case that
several consumers might interact with a single resource,
with each consumer oblivious to changes made by the
others. As well as these consumer-driven changes,
internal service behaviors can also lead to a resource’s
state changing without consumers knowing. In both
cases, a consumer’s understanding of resource state can
become misaligned with the service’s resource state.
Without some way of realigning expectations, changes
requested by a consumer based on an out-of-date
understanding of resource state can have undesired
effects, from repeating computationally expensive
requests to overwriting and losing another consumer’s
changes.
Aligning Resource State
• HTTP provides a simple but powerful mechanism for aligning
resource state expectations (and preventing race conditions) in
the form of entity tags and conditional request headers
• An entity tag value, or ETag, is an opaque string token that a
server associates with a resource to uniquely identify the state of
the resource over its lifetime.
• When the resource changes—that is, when one or more of its
headers, or its entity body, changes—the entity tag changes
accordingly, highlighting that state has been modified.
• ETag s are used to compare entities from the same resource. By
supplying an entity tag value in a conditional request header—
either an If-Match or an If-None-Match request header—a
consumer can require the server to test a precondition related to
the current resource state before applying the method supplied in
the request.
Aligning Resource State
• ETags are also used for cache control purposes
• To illustrate how ETags can be used to align
resource state in a multiconsumer scenario,
imagine a situation in which a party of two
consumers places an order for a single coffee.
Shortly after placing the order, the first
consumer decides it wants whole milk instead
of skim milk. Around the same time, the
second consumer decides it, too, would like a
coffee. Neither consumer consults the other
before trying to amend the order.
Consumer GETs the order
• To begin, both consumers GET the current
state of the order independently of each
other.
• Consumer GETs the order
Service generates a response
with an ETag header
How Etag values are computed?
• The service computes the entity tag and
supplies it as a quoted string in the ETag
header prior to returning a response.
• Entity tag values can be based on anything
that uniquely identifies an entity:
• a version number associated with a resource in
persistent storage
• one or more file attributes
• a checksum of the entity headers and body
Etag usage
• When a consumer receives a response containing
an ETag, it can (and should) use the value in any
subsequent requests it directs to the same
resource.
• Such requests are called “conditional requests”
• By supplying the received entity tag as the value of
an If-Match or If-None-Match conditional header,
the consumer can instruct the service to process its
request only if the precondition in the conditional
header holds true.
“If-Match” request header
• An If-Match request header instructs the service to
apply the consumer’s request only if the resource to
which the request is directed hasn’t changed since the
consumer last retrieved a representation of it.
• The service determines whether the resource has
changed by comparing the resource’s current entity tag
value with the value supplied in the If-Match header.
• If the values are equal, the resource hasn’t changed.
• The service then applies the method supplied in the
request and returns a 2xx response.
• If the entity tag values don’t match, the server
concludes that the resource has changed since the
consumer last accessed it, and responds with 412
Precondition Failed.
The first consumer conditionally
PUTs an updated order
The conditional PUT succeeds
• Because the order hadn’t been modified since the
first consumer last saw it, the PUT succeeds, as
shown
• Notice that while the response doesn’t include an
entity body, it does include an updated ETag
header. This new entity tag value reflects the new
state of the order resource held on the server (the
result of the successful PUT).
The second consumer
conditionally PUTs an
updated order
• Oblivious to the change that has just taken place, the second
consumer attempts to add its order, as shown
• This request again uses a conditional PUT, but with an entity tag value
that is now out of date (as a result of the first consumer’s
modification).
The response indicates a
precondition has failed
• The service determines that the second consumer
is trying to modify the order based on an out-of-
date understanding of resource state, and so
rejects the request, as shown
• When a consumer receives a 412 Precondition
Failed status code, the correct thing to do is to
GET a fresh representation of the current state of
the resource, and then use the ETag header value
supplied in this response to retry the original
request, which is what the second consumer does
in this case.
The response indicates a
precondition has failed
• Having done a fresh GET, the consumer sees
that the original order had been modified. The
second consumer is now in a position to PUT a
revised order that reflects both its and the
first consumer’s wishes.
“If-None-Match” Header
• Our example used the If-Match header to prevent the second
consumer from overwriting the first consumer’s changes. Besides
If-Match, consumers can also use If-None-Match
• An If-None-Match header instructs the service to process the
request only if the associated resource has changed since the
consumer last accessed it.
• The primary use of If-None-Match is to save valuable computing
resources on the service side. For example, it may be far cheaper
for a service to compare ETag values than to perform computation
to generate a representation.
• If-None-Match is mainly used with conditional GETs, whereas If-
Match is typically used with the other request methods, where
race conditions between multiple consumers can lead to
unpredictable side effects unless properly coordinated.
Conditional requests – additional
information
• Both If-Match and If-None-Match allow the use of a
wildcard character, *, instead of a normal entity tag value
• An If-None-Match conditional request that takes a wildcard
entity tag value instructs the service to apply the request
method only if the resource doesn’t currently exist.
• Wildcard If-None-Match requests help to prevent race
conditions in situations where multiple consumers compete
to PUT a new resource to a well-known URI.
• In contrast, an If-Match conditional request containing a
wildcard value instructs the service to apply the request
only if the resource does exist.
• Wildcard If-Match requests are useful in situations where
the consumer wishes to modify an existing resource using a
PUT, but only if the resource hasn’t already been deleted.
Conditional requests – additional
information
• As well as ETag and its associated If-Match and If-None-
Match headers, HTTP supports a timestamp-based Last-
Modified header and its two associated conditional
headers: If-Modified-Since and If-Unmodified-Since.
• These timestamp-based conditional headers act in
exactly the same way as the If-Match and If-None-Match
headers, but the conditional mechanism they implement
is accurate only to the nearest second—the limit of the
timestamp format used by HTTP.
• Because timestamps are often cheaper than hashes, If-
Modified-Since and If-Unmodified-Since may be
preferable in solutions where resources don’t change
more often than once per second.
Consuming CRUD Services
• .NET client code for order update via PUT
An XML-serializable order
Consuming Services
Automatically with WADL
• Although the patterns for writing clients in
.NET and Java are easy to understand and
implement, we can save ourselves effort—and,
in some cases, generate code automatically—
using service metadata.
• Web Application Description Language(WADL)
• A static description can be used to advertise the
addresses and representation formats of the
resources the service hosts
Ordering service WADL
example
• A WADL contract is an XML document that
describes a set of resources with URI
templates, permitted operations, and request-
response representations. As you’d expect,
WADL also supports the HTTP fault model and
supports the description of multiple formats
for resource representations.
Ordering service WADL
example
Ordering service WADL
example
WADL
• While it’s helpful that we can read and write
WADL by hand (at least in simple cases), the
point of WADL is to help tooling automate as
much service plumbing as possible.
• An opensource .NET WADL tool is available in
the below URI
• http://wadl.codeplex.com/
CRUD Is Good, but It’s Not
Great
• It’s clear that using HTTP as a CRUD protocol
can be a viable, robust, and easily
implemented solution for some problem
domains
Hypermedia Services
• When browsing the Web, we’re used to
navigating between pages by clicking links or
completing and submitting forms. Although we
may not realize it, these interlinked pages
describe a protocol—a series of steps we take
to achieve a goal, whether that’s buying
books, searching for information, creating a
blog post, or even ordering a coffee. This is
the very essence of hypermedia: by transiting
links between resources, we change the state
of an application.
Hypermedia As the Engine of
Application State
• The phrase hypermedia as the engine of
application state, sometimes abbreviated to
HATEOAS, was coined to describe a core tenet
of the REST architectural style. In this book,
we tend to refer to the hypermedia tenet or
just hypermedia. Put simply, the tenet says
that hypermedia systems transform application
state.
What is application state?
• If we think of an application as being
computerized behavior that achieves a goal,
we can describe an application protocol as the
set of legal interactions necessary to realize
that behavior. Application state is a snapshot
of an execution of such an application
protocol. The protocol lays out the interaction
rules; application state is a snapshot of the
entire system at a particular instant.
Hypermedia As the Engine of
Application State
• A hypermedia system is characterized by the
transfer of links in the resource
representations exchanged by the participants
in an application protocol. Such links advertise
other resources participating in the
application protocol. The links are often
enhanced with semantic markup to give
domain meanings to the resources they
identify.
Hypermedia As the Engine of
Application State
• For example, in a consumer-service
interaction,
• the consumer submits an initial request to the entry
point of the service.
• The service handles the request and responds with a
resource representation populated with links.
• The consumer chooses one of these links to
transition to the next step in the interaction. Over
the course of several such interactions, the
consumer progresses toward its goal.
• In other words, the distributed application’s state
changes.
Hypermedia As the Engine of
Application State
• Transformation of application state is the
result of the systemic behavior of the whole:
• The service
• The consumer
• The exchange of hypermedia-enabled resource
representations
• The advertisement and selection of links
• On each interaction, the service and consumer
exchange representations of resource state,
not application state.
Hypermedia As the Engine of
Application State
• A transferred representation includes links
that reflect the state of the application. These
links advertise legitimate application state
transitions
• But the application state isn’t recorded
explicitly in the representation received by
the consumer; it’s inferred by the consumer
based on the state of all the resources—
potentially distributed across many services—
with which the consumer is currently
interacting.
Hypermedia As the Engine of
Application State
• The current state of a resource is a
combination of:
• The values of information items belonging to that
resource
• Links to related resources
• Links that represent a transition to a possible future
state of the current resource
• The results of evaluating any business rules that
relate the resource to other local resources
Hypermedia As the Engine of
Application State
• the state of a resource is partly dependent on
the state of other local resources
• The state of a sales order, for example, is
partly a function of
• the state of a local copy of an associated purchase
order;
• changes to the purchase order will affect the state
of the sales order the next time the business rules
governing the state of the sales order are evaluated
(i.e., the next time a representation of the sales
order is generated).
Hypermedia As the Engine of
Application State
• Importantly, the rules that control the state of
a resource are internal to the service that
governs the resource: they’re not made
available to consumers
• In other words, resource state is a function of
a private ruleset that only the resource owner
knows about: those rules don’t leak into the
external representation.
Hypermedia As the Engine of
Application State
• Business rules that relate a resource to other
resources should refer only to locally owned
resources, however. This allows us to identify and
prevent circular dependencies, whereby the state
of resource A is partly a function of the state of
resource B, which in turn is partly a function of the
state of resource A, and so on. We can always
arrange locally owned resources so as to prevent
circular dependencies; we can’t do the same if the
associated resources are governed by another
service. If you need to relate the state of a
resource to a third-party resource, we recommend
making a local copy of the third-party resource
using the Atom-based state alignment mechanisms
Domain application
protocol(DAP)
• A service enforces a protocol—a domain
application protocol, or DAP—by advertising
legitimate interactions with relevant resources
• When a consumer follows links embedded in
resource representations and subsequently
interacts with the linked resources, the
application’s overall state changes
Domain application
protocol(DAP)
• Domain application protocols (DAPs) specify
the legal interactions between a consumer and
a set of resources involved in a business
process. DAPs sit atop HTTP and narrow
HTTP’s broad application protocol to support
specific business goals. As we shall see,
services implement DAPs by adding
hypermedia links to resource representations.
These links highlight other resources with
which a consumer can interact to make
progress through a business transaction.
DAP
Resources plus hypermedia
describe protocols
Hypermedia As the Engine of
Application State
• Consumers in a hypermedia system cause state transitions by visiting and
manipulating resource state.
• Interestingly, the application state changes that result from a consumer
driving a hypermedia system resemble the execution of a business process.
• This suggests that our services can advertise workflows using hypermedia.
• Hypermedia makes it easy to implement business protocols in ways that
reduce coupling between services and consumers.
• Rather than understand a specific URI structure, a consumer need only
understand the semantic or business context in which a link appears.
• This reduces an application’s dependency on static metadata such as URI
templates or Web Application Description Language (WADL). As a
consequence, services gain a great deal of freedom to evolve without
having to worry whether existing consumers will break.
Hypermedia As the Engine of
Application State
• Services should ensure that any changes they
introduce do not violate contracts with
existing consumers. While it’s fine for a
service to make structural changes to the
relationships between its resources, semantic
changes to the DAP or to the media types and
link relations used may change the contract
and break existing consumers.
Hypermedia As the Engine of
Application State
• For computer-to-computer interactions, we
advertise protocol information by embedding links
in representations, much as we do with the human
Web. To describe a link’s purpose, we annotate it.
Annotations indicate what the linked resource
means to the current resource: “status of your
coffee order,” “payment,” and so on. For
annotations we can use microformats or Semantic
Web technologies, or we can design our own
application-specific formats. We call such
annotated links hypermedia controls, reflecting
their enhanced capabilities over raw URIs.
What about Forms?
• Forms are hypermedia controls too. Though we
use links exclusively throughout the remainder
of this chapter, forms can also be used to
guide a consumer’s interaction with linked
resources. At the HTTP level, POSTing a
representation to a URI in a link is equivalent
to submitting a form.
Hypermedia Formats
• Hypermedia Dead Ends
• Despite the success of hypermedia formats on the
Web, today’s distributed applications typically use
nonhypermedia formats such as plain XML to
integrate systems. Although XML is easy to use as a
data interchange format, and despite its near
ubiquity, it is utterly oblivious to the Web.
XML lacks hypermedia controls
XML lacks hypermedia controls
• There’s nothing intrinsically wrong with this order representation when
considered in isolation. After all, it conveys the current state of the order
well enough. But it fails to provide any context; that is, it doesn’t indicate
the current state of the business process, or how to advance it. Informally,
we know we need to pay for a drink once ordered, but the representation
of Example 5-1 doesn’t indicate how to make that payment.
• The use of plain XML leaves the consumer without a guide—a protocol—for
successfully completing the business transaction it has initiated. Because
there are no hypermedia controls in the order representation, the
consumer must rely on out-of-band information to determine what to do
next. From a loose coupling point of view, that’s a poor design decision.
Aspects of the service’s implementation leak through mechanisms such as
URI templates into the consumer’s implementation, making change
difficult and risky.
• We can, of course, communicate protocol information to the developers of
a consumer application using written documentation, or static contracts
such as Web Services Description Language (WSDL), WADL, or URI
templates. But, as we’ll see, the Web and hypermedia enable us to do
better.
URI Templates and Coupling
• Restbucks could share URI templates with its consumers. For
example, it could document and share the template
http://restbucks.com/payment/{order_id}
• If Restbucks chose to publish URI templates to consumers, it
would then be bound to honor those templates for the long term,
• Risk breaking existing consumer applications. Publishing URI
template details outside a service’s boundary exposes too much
information about the service’s implementation.
• If the implementation of the ordering and payment services were
to change, perhaps as a result of outsourcing the payment
capability to a third party, there’d be an increased risk that
consumers built to the (now defunct)
http://restbucks.com/payment/{order_id} template would break.
• Since that kind of business change happens frequently, Restbucks
ought to encapsulate its implementation details as far as possible.
URI Templates and Coupling
• Generally, it’s better to expose only stable URIs.
• These stable URIs act as entry points to services, after which hypermedia
takes over.
• For example, the entry point to the Restbucks ordering service is
http://restbucks.com/order. Interacting with the resource at that URI
generates further resource representations, each of which contains
hypermedia links to yet more resources involved in the ordering business
process.
• This doesn’t mean URI templates are a bad idea.
• In fact, they are an excellent metadata format. But as with all good things,
we must learn how to use them in moderation.
• We believe URI templates are an excellent means for documenting service
design. Restbucks’ implementations all embrace URI templates for internal
documentation and implementation purposes.
• However, those same implementations only ever share completed (opaque)
URIs with consumers. The templates remain a private design and
development detail.
Selecting a Hypermedia Format
• Standard hypermedia formats
• Encoding an order in XHTML
Domain-specific hypermedia
formats
• A coffee order with a custom hypermedia
link
Domain-specific hypermedia
formats
• A coffee order with hypermedia
A media type for Restbucks
• The media type declaration used in the Content-
Type header for interactions with Restbucks is
application/vnd.restbucks+xml.
• Breaking it down, the media type name tells us
that the payload of the HTTP request or response
is to be treated as part of an application-specific
interaction. The vnd.restbucks part of the media
type name declares that the media type is vendor-
specific (vnd), and that the owner is restbucks.
The +xml part declares XML is used for the
document formatting.
Why application/xml doesn’t
help?
• By treating XML hypermedia formats as plain
XML, we skip many of their benefits
• The protocol information (the <link> elements)
will appear as odd-looking business
information.
Atom – Media Type
• In the Restbucks application domain, we assume
that consumers who understand the
application/vnd.restbucks+xml media type are
capable of dealing with everything defined by it.
• However, it occasionally happens that some
consumers want to handle only a subset of the
representation formats defined in a media type.
• While there is no standard solution to this issue on
the Web, there is a popular convention defined by
the Atom community.
• The application/atom+xml media type defines both
the feed and the entry resource representation
formats.
Atom Publishing Protocol
• While the vast majority of consumers can handle
both, there is a small subset wishing only to deal
with standalone entries. In recognition of this
need, Atom Publishing Protocol (AtomPub) added a
type parameter to the media type value (resulting
in Content-Type headers such as
application/atom+xml;type=entry).
• With such a value for the ContentType header, it is
now possible to include “entry” resource
representations as payloads in HTTP requests or
responses without requiring that the processing
software agents have a complete understanding of
all the Atom-defined formats.
Contracts
• Contracts are a composition of media types
and protocols
Hypermedia Protocols
The Restbucks Domain Application
Protocol
• Possible HTTP requests for the Restbucks
ordering service
The Restbucks Domain
Application Protocol
• Modeling Restbucks’ business processes as a DAP
and then representing that protocol as a state
machine in this manner is a useful way of capturing
a business process at design time.
• In a resource-oriented distributed application, an
application protocol can be thought of as a
function of one or more resource life cycles and
the business rules that connect these resources.
• Because of its resource-centric nature, the
Restbucks service does not host an application
protocol state machine.
The Restbucks Domain
Application Protocol
• In other words, there’s no workflow or business
logic for the application protocol as such
• Rather, the service governs the life cycles of the
orders and payments participating in the
application protocol
• Any workflows in the service implementation
relate to resource life cycles, not the application
protocol life cycle.
• While we’ve been explicit in modeling the business
process as an application protocol state machine,
we’ve been diligent in implementing it wholly in
terms of resource state machines.
The Restbucks Domain
Application Protocol
• State transitions for the order resource
• From this diagram we can derive the DAP:
Advertising Protocols with
Hypermedia
• Driving the Restbucks ordering protocol
through the happy path
Advertising Protocols with
Hypermedia
• Responses contain links to valid next steps in
the interaction
Hypermedia order
representation
Hypermedia order
representation
• This order representation shows the different URIs and associated rel
values consumers can use to advance the DAP. The semantic markup
definitions are shared widely with consumers as part of the media type
specification, and are as follows:
• payment
• The linked resource allows the consumer to begin paying for the order. Initiating
payment involves PUTting an appropriate resource representation to the
specified URI, as defined in the Restbucks media type.
• self
• The uri value can be used to GET the latest resource representation of the
order.
• update
• Consumers can change the order using a POST to transfer a representation to
the linked resource.
• cancel
• This is the uri to be used to DELETE the order resource should the consumer wish
to cancel the order.
A payment resource
representation
Payment response contains links to
order and receipt resources
Receipt representation with a
link to the order resource
The updated order resource
representation after payment
has been accepted
Order resource representation
after the barista has prepared it
Representations of completed
orders have no links
• Receipt
• Customers can DELETE the linked resource, thereby
completing the order.
Building the Ordering Service
in .NET
• The Restbucks .NET Hypermedia Framework
• Separating network, hypermedia, and
business logic
Declarative Domain-Specific
Language (DSL)
• The hypermedia framework manages resource
state transitions by embedding appropriate
hypermedia controls into a response based on
both the state of the resource targeted by the
request and the state of any associated
resources.
• To express this protocol, we have devised a
simple, declarative Domain-Specific Language
(DSL). Scripts written with the Restbucks DSL
choreograph numerous business actions and
externalize application state to consumers.
Building the Ordering Service
in .NET
• Remember that each workflow in the service
implementation represents resource state
machines, not a single, overarching
application protocol state machine. By
implementing the service solely in terms of
resource states, we avoid having to save
protocol instance state (application state) on
the server, which allows us to scale the service
horizontally.
The Restbucks .NET
hypermedia framework
Each state machine
analyzes incoming
HTTP requests and,
depending on the
state of the resource
to which the request
relates, dispatches
the payload to an
appropriate .NET
method in the
business logic layer.
The method deals
with XML documents
as the input and
output, and is
unaware of the HTTP
and hypermedia
details of the
interaction.
On the response
path, the
hypermedia
framework receives
XML documents
from the business
layer and augments
them with any
hypermedia
constructs declared
in the state machine
DSL description. The
resultant payload is
then passed to the
underlying HTTP
infrastructure for
delivery to the
consumer, as we see
in Figure
DSL
• The DSL used for the Restbucks hypermedia
framework is a state machine description
markup language. This language includes
constructs representing hypermedia controls,
which are injected into representations as XML
<dap:link> elements.
• While the primary focus of the DSL is to
describe a resource’s state transitions, it also
includes those DAP-specific transitions that
relate to the particular resource.
Representing a resource state machine
and the supported DAP transitions using
the Restbucks hypermedia DSL
Payment state machine
Delivery - The Ready state of
the order resource
Receipt state machine
Scaling Out
• Caching
• Caching is the ability to store copies of frequently
accessed data in several places along the request-
response path.
• When a consumer requests a resource representation,
the request goes through a cache or a series of caches
toward the service hosting the resource.
• If any of the caches along the request path has a fresh
copy of the requested representation, it uses that copy
to satisfy the request.
• If none of the caches can satisfy the request, the
request travels all the way to the service (or origin
server as it is formally known).
Caching
• Origin servers control the caching behavior of
the representations they issue
• Using HTTP headers, an origin server indicates
whether a response can be cached, and if so,
by whom, and for how long.
• Caches along the response path can take a
copy of a response, but only if the caching
metadata allows them to do so.
• The caches can then use these copies to
satisfy subsequent requests.
Caching
• Cached copies of a resource representation can be used
to satisfy subsequent requests so long as they remain
fresh
• A cached representation remains fresh for a specific
period of time, which is called its freshness lifetime.
• When the age of a cached object exceeds its freshness
lifetime, the object is said to be stale.
• Caches will often add an Age response header to a
cached response.
• The Age header indicates how many seconds have
passed since the representation was generated at the
origin server.
Caching
• A stale representation must be revalidated with the
origin server before it can be used to satisfy any further
requests
• If the revalidation reveals that the stale representation
is in fact still valid, the cached copy can be reused.
• If, however, the resource has changed since the stale
representation was first issued, the cached copy must
be invalidated and replaced.
• Representations can become invalid during their
freshness lifetime without the cache knowing.
• Unless the consumer specifically asks for a revalidation
or a new copy from the origin server, the cache will
continue to use these invalid (but fresh) representations
until they become stale.
Benefits of Caching
• Optimizing the network using caching improves the overall quality-of-
service characteristics of a distributed application. Caching significantly
benefits four areas of systems operation, allowing us to:
• Reduce bandwidth
• By reducing the number of network hops required to retrieve a representation,
caching reduces network traffic and conserves bandwidth.
• Reduce latency
• Because caches store copies of frequently accessed information nearer to where
the information is used, caching reduces the time it takes to satisfy a request.
• Reduce load on servers
• Because they are able to serve a percentage of requests from their own stores,
caches reduce the number of requests that reach an origin server.
• Hide network failures
• Caches can continue to serve cached content even if the origin server that
issued the content is currently unavailable or committed to an expensive
processing task that prevents it from generating a response. In this way, caches
provide fault tolerance by masking intermittent failures and delays from
consumers.
Caching and the Statelessness
Constraint
• Caching helps mitigate the consequences of
applying the statelessness constraint.[62] It
reduces the amount of data sent over the
network by storing representations closer to
where they are needed, and it reduces the
load on origin servers by having caches satisfy
repeated requests for the same data.
Making Content Cacheable
• Given that caches are designed around the
retrieval of resource representations, it shouldn’t
come as a surprise to learn that they mostly (but
not exclusively) work with GET requests.
• Responses to GET requests are cacheable by
default.
• Responses to POST requests are not cacheable by
default, but can be made cacheable if either an
Expires header, or a Cache-Control header with a
directive that explicitly allows caching, is added to
the response.
• Responses to PUT and DELETE requests are not
cacheable at all.
Response Headers Used for
Caching
• There are two main HTTP response headers that we can use to
control caching behavior:
• Expires
• The Expires HTTP header specifies an absolute expiry time for a cached
representation. Beyond that time, a cached representation is considered
stale and must be revalidated with the origin server. A service can indicate
that a representation has already expired by including an Expires value
equal to the Date header value (the representation expires now), or a
value of 0. To indicate that a representation never expires, a service can
include a time up to one year in the future.
• Cache-Control
• The Cache-Control header can be used in both requests and responses to
control the caching behavior of the response. The header value comprises
one or more comma-separated directives. These directives determine
whether a response is cacheable, and if so, by whom, and for how long.
•
Response Headers Used for
Caching
• If we can determine an absolute expiry time for a
cached response, we should use an Expires header.
• If it’s more appropriate to indicate how long the
response can be considered fresh once it has left
the origin server, we should use a Cache-Control
header, adding a max-age or s-maxage directive to
specify a relative Time to Live (TTL).
• Cacheable responses (whether to a GET or to a
POST request) should also include a validator—
either an ETag or a Last-Modified header:
A response with an absolute
expiry time
A response with a relative
expiry time
Using Caching Directives in
Responses
• Cache-Control directives serve three functions
when used in a response. Some make normally
uncacheable responses cacheable. Others
make normally cacheable responses
uncacheable. Finally, there are some Cache-
Control directives that do not affect the
cacheability of a response at all; rather, they
determine the freshness of an already
cacheable response. An individual directive
can serve one or more of these functions.
Using Caching Directives in
Responses
• max-age=<delta-seconds>
• This directive controls both cacheability and
freshness. It makes a response capable of being
cached by local and shared caches (proxies and
reverse proxies), as well as specifying a freshness
lifetime in seconds. A max-age value overrides any
Expiry value supplied in a response.
• s-maxage=<delta-seconds>
• Like max-age, this directive serves two functions: it
makes responses cacheable, but only by shared
caches, and it specifies a freshness lifetime in
seconds.
Using Caching Directives in
Responses
• public
• This directive makes a response capable of being cached by
local and shared caches, but doesn’t determine a freshness
value. Importantly, public takes precedence over
authorization headers. Normally, if a request includes an
Authorization header, the response cannot be cached. If,
however, the response includes a public directive, it can be
cached. You should exercise care, however, when making
responses that require authorization cacheable.
• private
• This directive makes a response capable of being cached by
local caches only (i.e., within the consumer implementation).
At the same time, it prevents normally cacheable responses
from being cached by shared caches. private doesn’t
determine a freshness value.
Using Caching Directives in
Responses
• must-revalidate
• This directive makes normally uncacheable responses
cacheable, but requires caches to revalidate stale
responses with the origin server. Only if the stale
response is successfully validated with the origin
server can the cached content be used to satisfy the
request. must-revalidate is enormously useful in
balancing consistency with reduced bandwidth and
computing resource consumption. While it forces a
revalidation request to travel all the way to the origin
server, an efficient validation mechanism on the server
side will prevent the core service logic from being
invoked for a large percentage of requests—all for the
cost of a measly 304 Not Modified response.
Using Caching Directives in
Responses
• proxy-revalidate
• This directive is similar to must-revalidate, but it only applies to shared
caches.
• no-cache
• This directive requires caches to revalidate a cached response with the
origin server with every request. If the request is successfully validated
with the origin server, the cached content can be used to satisfy the
request. The directive only works for responses that have been made
cacheable using another header or directive (i.e., it doesn’t make
uncacheable responses cacheable). Unfortunately, different caches behave
in different ways with regard to no-cache: some caches treat no-cache as
an instruction to not cache a response (as per an old draft of HTTP 1.1);
some treat it correctly, as a requirement to always revalidate a cached
response.
• no-store
• This directive makes normally cacheable content uncacheable by all
caches.
Making content cacheable by
local caches only
Caching authorized responses
Minimizing traffic for accessing
local, consistent, cached
representations
In revalidating each request
with the server, the cache
will pass on the contents of
the Authorization header
supplied by the consumer. If
the origin server replies 401
Unauthorized, the cache
will refuse to release the
cached representation. The
combination public, max-
age=0 differs from must-
revalidate in that it allows
caching of responses to
requests that contain
Authorization headers.
Implementing Caching in .NET
• Using the WinINet local cache from consumer
code
A simple web server
GetMenu(…) satisfies an HTTP
GET request
Response to a GET request for
the Restbucks menu
Adding a Cache-Control header
to the response
The response now includes a
Cache-Control header
The menu representation is
cached throughout the Web

Más contenido relacionado

La actualidad más candente

REST and RESTful Web Services
REST and RESTful Web ServicesREST and RESTful Web Services
REST and RESTful Web ServicesKasun Madusanke
 
200211 Fielding Apachecon
200211 Fielding Apachecon200211 Fielding Apachecon
200211 Fielding ApacheconDaniel Parker
 
Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web servicesnbuddharaju
 
Rest & RESTful WebServices
Rest & RESTful WebServicesRest & RESTful WebServices
Rest & RESTful WebServicesPrateek Tandon
 
IRJET- Rest API for E-Commerce Site
IRJET- Rest API for E-Commerce SiteIRJET- Rest API for E-Commerce Site
IRJET- Rest API for E-Commerce SiteIRJET Journal
 
Restful web services by Sreeni Inturi
Restful web services by Sreeni InturiRestful web services by Sreeni Inturi
Restful web services by Sreeni InturiSreeni I
 
Restful webservice
Restful webserviceRestful webservice
Restful webserviceDong Ngoc
 
RabbitMQ Implementation as Message Broker in Distributed Application with RES...
RabbitMQ Implementation as Message Broker in Distributed Application with RES...RabbitMQ Implementation as Message Broker in Distributed Application with RES...
RabbitMQ Implementation as Message Broker in Distributed Application with RES...IJCSIS Research Publications
 
Soap and restful webservice
Soap and restful webserviceSoap and restful webservice
Soap and restful webserviceDong Ngoc
 
Restful web-services
Restful web-servicesRestful web-services
Restful web-servicesrporwal
 

La actualidad más candente (20)

REST & RESTful Web Services
REST & RESTful Web ServicesREST & RESTful Web Services
REST & RESTful Web Services
 
REST and RESTful Web Services
REST and RESTful Web ServicesREST and RESTful Web Services
REST and RESTful Web Services
 
Web Services in the Real World
Web Services in the Real WorldWeb Services in the Real World
Web Services in the Real World
 
200211 Fielding Apachecon
200211 Fielding Apachecon200211 Fielding Apachecon
200211 Fielding Apachecon
 
Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web services
 
Rest & RESTful WebServices
Rest & RESTful WebServicesRest & RESTful WebServices
Rest & RESTful WebServices
 
ReSTful API Final
ReSTful API FinalReSTful API Final
ReSTful API Final
 
JSON and REST
JSON and RESTJSON and REST
JSON and REST
 
IRJET- Rest API for E-Commerce Site
IRJET- Rest API for E-Commerce SiteIRJET- Rest API for E-Commerce Site
IRJET- Rest API for E-Commerce Site
 
Rest web services
Rest web servicesRest web services
Rest web services
 
Rest surekha
Rest surekhaRest surekha
Rest surekha
 
Restful web services by Sreeni Inturi
Restful web services by Sreeni InturiRestful web services by Sreeni Inturi
Restful web services by Sreeni Inturi
 
Restful webservice
Restful webserviceRestful webservice
Restful webservice
 
RabbitMQ Implementation as Message Broker in Distributed Application with RES...
RabbitMQ Implementation as Message Broker in Distributed Application with RES...RabbitMQ Implementation as Message Broker in Distributed Application with RES...
RabbitMQ Implementation as Message Broker in Distributed Application with RES...
 
Web Programming
Web ProgrammingWeb Programming
Web Programming
 
Rest http basics
Rest http basicsRest http basics
Rest http basics
 
Rest Webservice
Rest WebserviceRest Webservice
Rest Webservice
 
Design patternsforiot
Design patternsforiotDesign patternsforiot
Design patternsforiot
 
Soap and restful webservice
Soap and restful webserviceSoap and restful webservice
Soap and restful webservice
 
Restful web-services
Restful web-servicesRestful web-services
Restful web-services
 

Similar a Representational state transfer (rest) architectural style1.1

Cp7101 design and management of computer networks -network
Cp7101 design and management of computer networks -networkCp7101 design and management of computer networks -network
Cp7101 design and management of computer networks -networkDr Geetha Mohan
 
Diksha sda presentation
Diksha sda presentationDiksha sda presentation
Diksha sda presentationdikshagupta111
 
architectural design
 architectural design architectural design
architectural designPreeti Mishra
 
Chapter 2_Software Architecture.ppt
Chapter 2_Software Architecture.pptChapter 2_Software Architecture.ppt
Chapter 2_Software Architecture.pptRushikeshChikane1
 
Chapter 2_Software Architecture.ppt
Chapter 2_Software Architecture.pptChapter 2_Software Architecture.ppt
Chapter 2_Software Architecture.pptRushikeshChikane2
 
Cp7101 design and management of computer networks-requirements analysis
Cp7101 design and management of computer networks-requirements analysisCp7101 design and management of computer networks-requirements analysis
Cp7101 design and management of computer networks-requirements analysisDr Geetha Mohan
 
Design Engineering and Design concepts
Design Engineering and Design conceptsDesign Engineering and Design concepts
Design Engineering and Design conceptsJigyasaAgrawal7
 
network design 6.pptx
network design 6.pptxnetwork design 6.pptx
network design 6.pptxaida alsamawi
 
Architecture Design in Software Engineering
Architecture Design in Software EngineeringArchitecture Design in Software Engineering
Architecture Design in Software Engineeringcricket2ime
 
Architectural design1
Architectural design1Architectural design1
Architectural design1Zahid Hussain
 
Architectural design1
Architectural design1Architectural design1
Architectural design1Zahid Hussain
 
unit 5 Architectural design
 unit 5 Architectural design unit 5 Architectural design
unit 5 Architectural designdevika g
 
Systems Engineering Architecture - Vitech.pptx
Systems Engineering Architecture - Vitech.pptxSystems Engineering Architecture - Vitech.pptx
Systems Engineering Architecture - Vitech.pptxBarananKamal
 
Software design, software engineering
Software design, software engineeringSoftware design, software engineering
Software design, software engineeringRupesh Vaishnav
 
16 & 2 marks in i unit for PG PAWSN
16 & 2 marks in i unit for PG PAWSN16 & 2 marks in i unit for PG PAWSN
16 & 2 marks in i unit for PG PAWSNDhaya kanthavel
 
Software Architecture
Software ArchitectureSoftware Architecture
Software ArchitectureAhmed Misbah
 
Unit_4_Software_Design.pptx
Unit_4_Software_Design.pptxUnit_4_Software_Design.pptx
Unit_4_Software_Design.pptxtaxegap762
 

Similar a Representational state transfer (rest) architectural style1.1 (20)

Cp7101 design and management of computer networks -network
Cp7101 design and management of computer networks -networkCp7101 design and management of computer networks -network
Cp7101 design and management of computer networks -network
 
Diksha sda presentation
Diksha sda presentationDiksha sda presentation
Diksha sda presentation
 
architectural design
 architectural design architectural design
architectural design
 
Chapter 2_Software Architecture.ppt
Chapter 2_Software Architecture.pptChapter 2_Software Architecture.ppt
Chapter 2_Software Architecture.ppt
 
Chapter 2_Software Architecture.ppt
Chapter 2_Software Architecture.pptChapter 2_Software Architecture.ppt
Chapter 2_Software Architecture.ppt
 
Cp7101 design and management of computer networks-requirements analysis
Cp7101 design and management of computer networks-requirements analysisCp7101 design and management of computer networks-requirements analysis
Cp7101 design and management of computer networks-requirements analysis
 
Design Engineering and Design concepts
Design Engineering and Design conceptsDesign Engineering and Design concepts
Design Engineering and Design concepts
 
DSchap-02.ppt
DSchap-02.pptDSchap-02.ppt
DSchap-02.ppt
 
network design 6.pptx
network design 6.pptxnetwork design 6.pptx
network design 6.pptx
 
Architecture Design in Software Engineering
Architecture Design in Software EngineeringArchitecture Design in Software Engineering
Architecture Design in Software Engineering
 
Architectural design1
Architectural design1Architectural design1
Architectural design1
 
Architectural design1
Architectural design1Architectural design1
Architectural design1
 
Software Design - SDLC Model
Software Design - SDLC ModelSoftware Design - SDLC Model
Software Design - SDLC Model
 
unit 5 Architectural design
 unit 5 Architectural design unit 5 Architectural design
unit 5 Architectural design
 
Systems Engineering Architecture - Vitech.pptx
Systems Engineering Architecture - Vitech.pptxSystems Engineering Architecture - Vitech.pptx
Systems Engineering Architecture - Vitech.pptx
 
Software design, software engineering
Software design, software engineeringSoftware design, software engineering
Software design, software engineering
 
16 & 2 marks in i unit for PG PAWSN
16 & 2 marks in i unit for PG PAWSN16 & 2 marks in i unit for PG PAWSN
16 & 2 marks in i unit for PG PAWSN
 
Software Architecture
Software ArchitectureSoftware Architecture
Software Architecture
 
Unit_4_Software_Design.pptx
Unit_4_Software_Design.pptxUnit_4_Software_Design.pptx
Unit_4_Software_Design.pptx
 
Patterns
PatternsPatterns
Patterns
 

Más de Vinod Wilson

UI Design - Organizing the page
UI Design - Organizing the pageUI Design - Organizing the page
UI Design - Organizing the pageVinod Wilson
 
Service oriented architecture introduction
Service oriented architecture   introductionService oriented architecture   introduction
Service oriented architecture introductionVinod Wilson
 
Togaf – models for phase A
Togaf – models for phase ATogaf – models for phase A
Togaf – models for phase AVinod Wilson
 
The components of togaf architecture
The components of togaf architectureThe components of togaf architecture
The components of togaf architectureVinod Wilson
 
Togaf – architecture development method (adm)
Togaf – architecture development method (adm)Togaf – architecture development method (adm)
Togaf – architecture development method (adm)Vinod Wilson
 
Togaf 9 introduction
Togaf 9 introductionTogaf 9 introduction
Togaf 9 introductionVinod Wilson
 
D3 data visualization
D3 data visualizationD3 data visualization
D3 data visualizationVinod Wilson
 
Event driven architecture
Event driven architectureEvent driven architecture
Event driven architectureVinod Wilson
 
Domain driven design simplified
Domain driven design simplifiedDomain driven design simplified
Domain driven design simplifiedVinod Wilson
 
Developing saas application in azure
Developing saas application in azureDeveloping saas application in azure
Developing saas application in azureVinod Wilson
 
Iot cloud service v2.0
Iot cloud service v2.0Iot cloud service v2.0
Iot cloud service v2.0Vinod Wilson
 
IoT mobile app device cloud identity and security architecture
IoT mobile app device cloud identity and security architectureIoT mobile app device cloud identity and security architecture
IoT mobile app device cloud identity and security architectureVinod Wilson
 

Más de Vinod Wilson (15)

UI Design - Organizing the page
UI Design - Organizing the pageUI Design - Organizing the page
UI Design - Organizing the page
 
Service oriented architecture introduction
Service oriented architecture   introductionService oriented architecture   introduction
Service oriented architecture introduction
 
Togaf – models for phase A
Togaf – models for phase ATogaf – models for phase A
Togaf – models for phase A
 
The components of togaf architecture
The components of togaf architectureThe components of togaf architecture
The components of togaf architecture
 
Togaf – architecture development method (adm)
Togaf – architecture development method (adm)Togaf – architecture development method (adm)
Togaf – architecture development method (adm)
 
Togaf 9 introduction
Togaf 9 introductionTogaf 9 introduction
Togaf 9 introduction
 
Ssas mdx language
Ssas mdx languageSsas mdx language
Ssas mdx language
 
D3 data visualization
D3 data visualizationD3 data visualization
D3 data visualization
 
Unit testing
Unit testingUnit testing
Unit testing
 
Event driven architecture
Event driven architectureEvent driven architecture
Event driven architecture
 
Domain driven design simplified
Domain driven design simplifiedDomain driven design simplified
Domain driven design simplified
 
Data partitioning
Data partitioningData partitioning
Data partitioning
 
Developing saas application in azure
Developing saas application in azureDeveloping saas application in azure
Developing saas application in azure
 
Iot cloud service v2.0
Iot cloud service v2.0Iot cloud service v2.0
Iot cloud service v2.0
 
IoT mobile app device cloud identity and security architecture
IoT mobile app device cloud identity and security architectureIoT mobile app device cloud identity and security architecture
IoT mobile app device cloud identity and security architecture
 

Último

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 

Último (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

Representational state transfer (rest) architectural style1.1

  • 1. Representational State Transfer (REST) architectural style Prepared by Vinod Wilson – Architect – Crestron Electronics
  • 2. What is a Hypermedia system? • An extension to hypertext that supports linking graphics, sound, and video elements in addition to text elements. The World Wide Web is a partial hypermedia system since is supports graphical hyperlinks and links to sound and video files. New hypermedia systems under development will allow objects in computer videos to be hyperlinked.
  • 3. What is a Software Architecture? A software architecture is an abstraction of the run-time elements of a software system during some phase of its operation. A system may be composed of many levels of abstraction and many phases of operation, each with its own software architecture.
  • 4. Software Architecture • At the heart of software architecture is the principle of abstraction: hiding some of the details of a system through encapsulation in order to better identify and sustain its properties • A complex system will contain many levels of abstraction, each with its own architecture.
  • 5. Software Architecture • An architecture represents an abstraction of system behavior at that level, such that architectural elements are delineated by the abstract interfaces they provide to other elements at that level
  • 6. Software Architecture • Within each element may be found another architecture, defining the system of sub- elements that implement the behavior represented by the parent element’s abstract interface. • This recursion of architectures continues down to the most basic system elements: those that cannot be decomposed into less abstract elements.
  • 7. Architecture of transitions between phases • In addition to levels of architecture, • A software system will often have multiple operational phases • start-up • initialization • normal processing • re-initialization • Shutdown • Each operational phase has its own architecture
  • 9. Architecture of transitions between phases- Example • A configuration file will be treated as a data element during the start-up phase • But won’t be considered an architectural element during normal processing. • since at that point the information it contained will have already been distributed throughout the system
  • 10. Architecture of transitions between phases • An overall description of a system architecture must be capable of describing not only the operational behavior of the system’s architecture during each phase, but also the architecture of transitions between phases.
  • 11. Architectural design and source code structural design • Though closely related, they are separate design activities.
  • 12. Elements • A software architecture is defined by a configuration of architectural elements— components, connectors, and data—constrained in their relationships in order to achieve a desired set of architectural properties. • As an illustration, consider what happens to a building if its blueprints and design plans are burned. Does the building immediately collapse? • No, since the properties by which the walls sustain the weight of the roof remain intact.
  • 13. Elements classification • Processing elements (Or Components) • Processing elements are those that perform transformations on data, • Data elements • Data elements are those that contain the information that is used and transformed • Connecting elements (Connectors) • connecting elements are the glue that holds the different pieces of the architecture together.
  • 14. Components • A component is an abstract unit of software instructions and internal state that provides a transformation of data via its interface. • Example transformations include • loading into memory from secondary storage, • performing some calculation, • translating to a different format • format, encapsulation with other data, • a component is defined by its interface and the services it provides to other components, rather than by its implementation behind the interface
  • 15. Connectors • A connector is an abstract mechanism that mediates communication, coordination, or cooperation among components. • Connectors enable communication between components by transferring data elements from one interface to another without changing the data. • Internally, a connector may consist of a subsystem of components that transform the data for transfer, perform the transfer, and then reverse the transformation for delivery
  • 16. Data • A datum is an element of information that is transferred from a component, or received by a component, via a connector. • A datum is an element of information that is transferred from a component, or received by a component, via a connector.
  • 17. Configurations • A configuration is the structure of architectural relationships among components, connectors, and data during a period of system run-time. • Strictly speaking, one might think of a configuration as being equivalent to a set of specific constraints on component interaction.
  • 18. Properties • The set of architectural properties of a software architecture includes all properties that derive from the selection and arrangement of components, connectors, and data within the system. • Properties are induced by the set of constraints within an architecture. • For example, the uniform pipe-and-filter style obtains the • qualities of reusability of components and configurability of the application by applying generality to its component interfaces — constraining the components to a single interface type • Properties induced - reusable and configurable components in uniform pipe-and-filter style
  • 19. The goal of architectural design • The goal of architectural design is to create an architecture with a set of architectural properties that form a superset of the system requirements.
  • 20. Architectural style • An architectural style is a coordinated set of architectural constraints that restricts the roles/features of architectural elements and the allowed relationships among those elements within any architecture that conforms to that style. • An architectural style encapsulates important decisions about the architectural elements and emphasizes important constraints on the elements and their relationships
  • 21. Architectural style • A good designer should select a style that matches the needs of the particular problem being solved
  • 22. Views
  • 23. Views • An architectural viewpoint is often application-specific and varies widely based on the application domain. • architectural viewpoints that address a variety of issues • state and control approaches • data representation • transaction life cycle, • security safeguards, • peak demand and graceful degradation. • Etc…
  • 24. Three important views in software architecture • Processing View • A process view emphasizes the data flow through the components and some aspects of the connections among the components with respect to the data • Data View • A data view emphasizes the processing flow, with less emphasis on the connectors • Connection views. • A connection view emphasizes the relationship between components and the state of communication.
  • 25. Network-based Application Architectures • Network-based vs. Distributed • A distributed system is one that looks to its users like an ordinary centralized system, but runs on multiple, independent CPUs. • In contrast, network-based systems are those capable of operation across a network, • Application Software vs. Networking Software • For example • A hypermedia application must be concerned with the location of information pages, performing requests, and rendering data streams. • This is in contrast to a networking abstraction, where the goal is to move bits from one location to another without regard to why those bits are being moved
  • 26. Architectural Properties of Key Interest • Performance • Network Performance • User-perceived Performance • Network Efficiency • Scalability • Simplicity • Modifiability • Evolvability • Extensibility • Customizability • Configurability • Reusability • Visibility • Portability • Reliability
  • 27. Performance • Network Performance • Throughput is the rate at which information, including both application data and communication overhead, is transferred between components. • Bandwidth is a measure of the maximum available throughput over a given network link. Usable bandwidth refers to that portion of bandwidth which is actually available to the application. • Styles impact network performance by their influence on the number of interactions per user action and the granularity of data elements.
  • 28. Performance • User-perceived Performance • User-perceived performance differs from network performance in that the performance of an action is measured in terms of its impact on the user in front of an application rather than the rate at which the network moves information. • The primary measures for user perceived performance are latency and completion time. • Latency is the time period between initial stimulus and the first indication of a response.
  • 29. Performance • Latency occurs at several points in the processing of a network-based application action: • 1) the time needed for the application to recognize the event that initiated the action; • 2) the time required to setup the interactions between components; • 3) the time required to transmit each interaction to the components; • 4) the time required to process each interaction on those components; • 5) the time required to complete sufficient transfer and processing of the result of the interactions before the application is able to begin rendering a usable result • all five points can be impacted by the architectural style
  • 30. Network Efficiency • An interesting observation about network-based applications is that the best application performance is obtained by not using the network. • This essentially means that the most efficient architectural styles for a network-based application are those that can effectively minimize use of the network when it is possible to do so. • through reuse of prior interactions (caching) • reduction of the frequency of network interactions in relation to user actions (replicated data and disconnected operation), • by removing the need for some interactions by moving the processing of data closer to the source of the data (mobile code).
  • 31. Network Efficiency • The benefits of a style under local conditions may become drawbacks when faced with global conditions. • the properties of a style must be framed in relation to the interaction distance: within a single process, across processes on a single host, inside a local-area network (LAN), or spread across a wide-area network (WAN)
  • 32. Scalability • Scalability refers to the ability of the architecture to support large numbers of components, or interactions among components, within an active configuration. • Scalability can be improved by simplifying components, by distributing services across many components (decentralizing the interactions), and by controlling interactions and configurations as a result of monitoring. • Styles influence these factors by determining the location of application state, the extent of distribution, and the coupling between components.
  • 33. Simplicity • The primary means by which architectural styles induce simplicity is by applying the principle of separation of concerns to the allocation of functionality within components. • If functionality can be allocated such that the individual components are substantially less complex, then they will be easier to understand and implement
  • 34. Modifiability • Modifiability is about the ease with which a change can be made to an application architecture. • Modifiability can be further broken down into • Evolvability • Evolvability represents the degree to which a component implementation can be changed without negatively impacting other components • Extensibility • Extensibility is defined as the ability to add functionality to a system • Customizability • Customizability refers to the ability to temporarily specialize the behavior of an architectural element, such that it can then perform an unusual service • Configurability • Configurability is related to both extensibility and reusability in that it refers to postdeployment modification of components, or configurations of components, such that they are capable of using a new service or data element type • Reusability • Reusability is a property of an application architecture if its components, connectors, or data elements can be reused, without modification, in other applications
  • 35. Network-based Architectural Styles • Data-flow Styles • Pipe and Filter (PF) • Uniform Pipe and Filter (UPF) • Replication Styles • Replicated Repository (RR) • Cache ($) • Hierarchical Styles • Client-Server (CS) • Layered System (LS) and Layered-Client-Server (LCS) • Client-Stateless-Server (CSS) • Client-Cache-Stateless-Server (C$SS) • Layered-Client-Cache-Stateless-Server (LC$SS) • Remote Session (RS) • Remote Data Access (RDA) • Mobile Code Styles • Virtual Machine (VM) • Remote Evaluation (REV) • Code on Demand (COD) • Layered-Code-on-Demand-Client-Cache-Stateless-Server (LCODC$SS) • Mobile Agent (MA) • Peer-to-Peer Styles • Event-based Integration (EBI) • C2 • Distributed Objects • Brokered Distributed Objects
  • 36. Data-flow Styles • Pipe and Filter (PF) • In a pipe and filter style, each component (filter) reads streams of data on its inputs and produces streams of data on its outputs, usually while applying a transformation to the input streams and processing them incrementally so that output begins before the input is completely consumed • The constraint is that a filter must be completely independent of other filters (zero coupling): it must not share state, control thread, or identity with the other filters on its upstream and downstream interfaces
  • 37. Pipe and Filter (PF) • Advantages properties • PF allows the designer to understand the overall input/output of the system as a simple composition of the behaviors of the individual filters (simplicity). • PF supports reuse: any two filters can be hooked together, provided they agree on the data that is being transmitted between them (reusability). • PF systems can be easily maintained and enhanced: new filters can be added to existing systems (extensibility) and old filters can be replaced by improved ones (evolvability). • they permit certain kinds of specialized analysis (verifiability), such as throughput and deadlock analysis • they naturally support concurrent execution (user-perceived performance).
  • 38. Pipe and Filter (PF) • Disadvantages • propagation delay is added through long pipelines, batch sequential processing occurs if a filter cannot incrementally process its inputs, and no interactivity is allowed • A filter cannot interact with its environment because it cannot know that any particular output stream shares a controller with any particular input stream
  • 39. Uniform Pipe and Filter (UPF) • The uniform pipe and filter style adds the constraint that all filters must have the same interface • The primary example of this style is found in the Unix operating system, where filter processes have an interface consisting of one input data stream of characters (stdin) and two output data streams of characters (stdout and stderr) • Advantages • Restricting the interface allows independently developed filters to be arranged at will to form new applications. • Disadvantages • A disadvantage of the uniform interface is that it may reduce network performance if the data needs to be converted to or from its natural format.
  • 40. Replication Styles • Replicated Repository (RR) • Systems based on the replicated repository style [6] improve the accessibility of data and scalability of services by having more than one process provide the same service. These decentralized servers interact to provide clients the illusion that there is just one, centralized service.
  • 41. Cache ($) • A variant of replicated repository is found in the cache style: replication of the result of an individual request such that it may be reused by later requests. • This form of replication is most often found in cases where the potential data set far exceeds the capacity of any one client, as in the WWW [20], or where complete access to the repository is unnecessary • Caching provides slightly less improvement than the replicated repository style in terms of user-perceived performance, since more requests will miss the cache and only recently accessed data will be available for disconnected operation
  • 42. Hierarchical Styles • Client-Server (CS) • A server component, offering a set of services, listens for requests upon those services. • A client component, desiring that a service be performed, sends a request to the server via a connector • The server either rejects or performs the request and sends a response back to the client. • Separation of concerns is the principle behind the client-server constraints. • A proper separation of functionality should simplify the server component in order to improve scalability.
  • 43. Client-Server (CS) • Tiers vs Layers • These terms are often (regrettably) used interchangeably. However they really are distinct and have definite meanings. The basic difference is that Tiers are physical, while Layers are logical.
  • 44. Layered System (LS) and Layered-Client-Server (LCS) • A layered system is organized hierarchically, each layer providing services to the layer above it and using services of the layer below it. • Architectures based on layered-client-server are referred to as two-tiered, three-tiered, or multi-tiered architectures in the information systems literature
  • 45. Layered System (LS) and Layered-Client-Server (LCS) • The Two Tier Architecture • The Three Tier Architecture • The Multi or n-Tier Architecture
  • 46. Layered System (LS) and Layered-Client-Server (LCS) • Layered systems reduce coupling across multiple layers by hiding the inner layers • from all except the adjacent outer layer, thus improving evolvability and reusability. • The primary disadvantage of layered systems is that they add overhead and latency to the processing of data, reducing user-perceived performance • Layered-client-server adds proxy and gateway components to the client-server style. • A gateway component appears to be a normal server to clients or proxies that request its services, but is in fact forwarding those requests, with possible translation, to its “inner-layer” servers
  • 47. Client-Stateless-Server (CSS) • The client-stateless-server style derives from client- server with the additional constraint that no session state is allowed on the server component • Each request from client to server must contain all of the information necessary to understand the request, and cannot take advantage of any stored context on the server • Session state is kept entirely on the client. • The disadvantage of client-stateless-server is that it may decrease network performance by increasing the repetitive data (per-interaction overhead) sent in a series of requests, since that data cannot be left on the server in a shared context.
  • 48. Client-Cache-Stateless-Server (C$SS) • The client-cache-stateless-server style derives from the client-stateless-server and cache styles via the addition of cache components. • The advantage of adding cache components is that they have the potential to partially or completely eliminate some interactions, improving efficiency and user-perceived performance.
  • 49. Layered-Client-Cache- Stateless-Server (LC$SS) • The layered-client-cache-stateless-server style derives from both layered-client-server and client-cache-stateless-server through the addition of proxy and/or gateway components. • An example system that uses an LC$SS style is the Internet domain name system (DNS).
  • 50. Remote Session (RS) • The remote session style is a variant of client- server that attempts to minimize the complexity, or maximize the reuse, of the client components rather than the server component. • Each client initiates a session on the server and then invokes a series of services on the server, finally exiting the session. • Application state is kept entirely on the server. • This style is typically used when it is desired to access a remote service using a generic client (e.g., TELNET [106]) or via an interface that mimics a generic client (e.g.,FTP [107]).
  • 51. Mobile Code Styles • Mobile code styles use mobility in order to dynamically change the distance between the processing and source of data or destination of results • Code on Demand (COD) • In the code-on-demand style [50], a client component has access to a set of resources, but not the know-how on how to process them.. • It sends a request to a remote server for the code representing that know-how, receives that code, and executes it locally.
  • 52. Code on Demand (COD) Style • The advantages of code-on-demand include the ability to add features to a deployed client. • which provides for improved extensibility and configurability, and better user perceived performance and efficiency when the code can adapt its actions to the client’s environment and interact with the user locally rather than through remote interactions. • Example : Javascript being downloaded from the server based on the request from a webbrowser.
  • 55. Representational State Transfer (REST) • Thinking in Resources • Resources are the fundamental building blocks of web-based systems, to the extent that the Web is often referred to as being “resource-oriented.” • A resource is anything we expose to the Web, from a document or video clip to a business process or device. • From a consumer’s point of view, a resource is anything with which that consumer interacts while progressing toward some goal. • Almost anything can be modeled as a resource and then made available for manipulation over the network.
  • 56. Resources and Identifiers • To use a resource we need both to be able to identify it on the network and to have some means of manipulating it. • The Web provides the Uniform Resource Identifier, or URI, for just these purposes. • A URI uniquely identifies a web resource, and at the same time makes it addressable, or capable of being manipulated using an application protocol such as HTTP (which is the predominant protocol on the Web). • A resource’s URI distinguishes it from any other resource, and it’s through its URI that interactions with that resource take place.
  • 57. Resources and Identifiers • The relationship between URIs and resources is many-to-one • A URI identifies only one resource, but a resource can have more than one URI. • That is, a resource can be identified in more than one way, much as humans can have multiple email addresses or telephone numbers.
  • 58. Multiple URIs for a resource
  • 59. Terms used on the Web to refer to identifiers
  • 60. Resource Representations • Resources must have at least one identifier to be addressable on the Web, and each identifier is associated with one or more representations. • A representation is a transformation or a view of a resource’s state at an instant in time. • This view is encoded in one or more transferable formats, such as XHTML, Atom, XML, JSON, plain text, comma-separated values, MP3, or JPEG.
  • 61. Resource Representations • (Actual Resource) Vs (Resource Information) • For real-world resources, such as goods in a warehouse, we can distinguish between the actual object and the logical “information” resource encapsulated by an application or service. • It’s the information resource that is made available to interested parties through projecting its representations onto the Web.
  • 62. Resource Representations • Access to a resource is always mediated by way of its representations. That is, web components exchange representations. • That is, web components exchange representations; they never access the underlying resource directly—the Web does not support pointers! • URIs relate, connect, and associate representations with their resources on the Web.
  • 63. Resource Representations Example of a resource and its representations
  • 65. Representation Formats and URIs • There is a misconception that different resource representations should each have their own URI—a notion that has been popularized by the Rails framework • URIs should be opaque to consumers. Only the issuer of the URI knows how to interpret and map it to a resource. Using extensions such as .xml, .html, or .json is a historical convention that stems from the time when web servers simply mapped URIs directly to files.
  • 66. Representation Formats and URIs Multiple resource representations addressed by a single URI
  • 67. Representation Formats and URIs • Using content negotiation, consumers can negotiate for specific representation formats from a service. • They do so by populating the HTTP Accept request header with a list of media types they’re prepared to process. • However, it is ultimately up to the owner of a resource to decide what constitutes a good representation of that resource in the context of the current interaction, and hence which format should be returned.
  • 68. The Art of Communication • On the Web, resources provide the subjects and objects with which we want to interact, but how do we act on them? • The answer is that we need verbs, and on the Web these verbs are provided by HTTP methods. • The term uniform interface is used to describe how a (small) number of verbs with well-defined and widely accepted semantics are sufficient to meet the requirements of most distributed applications. A collection of verbs is used for communication between systems.
  • 69. The Art of Communication • HTTP VERBS—GET, POST, PUT, DELETE, OPTIONS, HEAD, TRACE, CONNECT, and PATCH—forms a sufficiently general-purpose protocol to support a wide range of solutions. • In addition to verbs, HTTP also defines a collection of response codes, such as 200 OK, 201 Created, and 404 Not Found, that coordinate the interactions instigated by the use of the verbs. • Taken together, verbs and status codes provide a general framework for operating on resources over the network
  • 70. The Art of Communication Using HTTP to “GET” the representation of a resource
  • 71. From the Web Architecture to the REST Architectural Style • REST describes the Web as a distributed hypermedia application whose linked resources communicate by exchanging representations of resource state.
  • 72. Hypermedia • Hypermedia as the engine of application state • The idea is simple, and yet very powerful. A distributed application makes forward progress by transitioning from one state to another, just like a state machine. • The difference from traditional state machines, however, is that the possible states and the transitions between them are not known in advance. • Instead, as the application reaches a new state, the next possible transitions are discovered. It’s like a treasure hunt.
  • 73. Hypermedia Example of hypermedia as the engine for application state in action
  • 74. Introducing Restbucks: How to GET a Coffee, Web Style • Restbucks: A Little Coffee Shop with Global Ambitions • we’ll frame our problems and web-based solutions in terms of a coffee shop called Restbucks, which grows from modest beginnings to become a global enterprise
  • 75. Actors and Conversations • Restbucks has actors such as • Customers • Cashiers • Baristas (Coffee Maker) • Managers • suppliers • All must interact to keep the coffee flowing.
  • 76. XML-based exchange between a customer and a waiter
  • 77. Boundaries • In Restbucks, we draw boundaries around the actors involved in the system to encapsulate implementation details and emphasize the interactions between systems • When we order a coffee, we don’t usually care about the mechanics of the supply chain or the details of the shop’s internal coffee-making processes • Composition of functionality and the introduction of façades with which we can interact are common practices in system design, and web-based systems are no different in that respect.
  • 79. The Menu Sample catalog of products offered by Restbucks
  • 80. Sample Interactions A simple interaction between a customer and a barista
  • 82. A cashier helps the barista The interactions (or protocol) between the cashier and the barista remain hidden from customers. •By implementing this scheme, Restbucks decouples ordering and payment from the coffee preparation. •In turn, it is possible for Restbucks to abstract the inner workings of the shop through a façade. While the customer gets the same good coffee, Restbucks is free to innovate and evolve its implementation
  • 83. A cashier helps the barista • Restbucks decouples ordering and payment from the coffee preparation • In turn, it is possible for Restbucks to abstract the inner workings of the shop through a façade. • While the customer gets the same good coffee, Restbucks is free to innovate and evolve its implementation behind the scenes. • Decoupling payments and drink preparation allows Restbucks to optimize available resources.
  • 84. Restbucks Formats • All Restbucks resources are represented by XML documents defined in the http://restbucks.com namespace and identified on the Web as the media types application/xml and application/vnd.restbucks+xml for standard XML processing and Restbucks-specific processing, respectively.
  • 85. A Restbucks order resource represented in XML format HTTP Header HTTP Request Content
  • 86. Acknowledging the order with a custom XML format The order details are confirmed in the reply and some additional information is contained, such as the payment amount and currency, a timestamp for when the order was received, and a <link> element that identifies another resource with which the customer is expected to interact to make a payment.
  • 87. Modeling Protocols and State Transitions
  • 88. A Simple Service Architecture • HTTP remote procedure call architecture
  • 89. Web Integration - URI Templates • Intuitive URIs • As an example, let’s take Restbucks, which exposes ordering information through URI-addressable resources, such as http://restbucks.com/order/1234. • To a web developer, it should be intuitive that changing the number after the final / character in the URI will probably result in another resource representation being returned for a different order.
  • 90. From Intuitive URIs to URI Templates • An example of a URI template that describes valid URIs for the service is http://restbucks.com/order/{order_id}. • The markup in curly braces, {order_id}, is an invitation to Restbucks customers to “fill in the gaps” with sensible values. • By substituting those parameters, customers address different coffee orders hosted at Restbucks. • For example, the http://restbucks.com/order/{year}/{month}/{day} template supports accessing all of the orders for a given date, allowing consumers to vary the year, month, and day variables to access audit information.
  • 91. Using URI Templates • In practice, we prefer URI templates as a means of internal documentation for services, rather than as contract metadata. We find that URI templates are fine as a shorthand notation for communication within the context of a system, but as a mechanism for describing contracts, we think they risk introducing tight coupling. • We can put URI templates into practice immediately, staring with the most basic HTTP integration option: URI tunneling.
  • 92. URI Tunneling • We have numerous options for how to convey our order to a cashier, and on the Web, URI tunneling is the simplest. • URI tunneling uses URIs as a means of transferring information across system boundaries by encoding the information within the URI itself
  • 97. CRUD Web Services • Set of verbs required to fully support the Create, Read, Update, Delete (CRUD) pattern for manipulating resources across the network.
  • 98. Modeling Orders As Resources • Orders are created when a customer makes a purchase. • Orders are frequently read, particularly when their preparation status is inquired. • Under certain conditions, it may be possible for orders to be updated (e.g., in cases where customers change their minds or add specialties to their drinks). • Finally, if an order is still pending, a customer may be allowed to cancel it (or delete it).
  • 100. CRUD ordering service high- level architecture
  • 101. Building CRUD Services The ordering service contract overview
  • 102. Creating a Resource with POST Creating an order via POST
  • 103. Creating a coffee order via POST
  • 104. Response to successful order creation
  • 105. A malformed order request
  • 106. Response to a malformed order request
  • 108. Requesting an order via GET Request Response
  • 109. Rereading an order with GET
  • 110. Requesting an order that doesn’t exist via GET Request Response
  • 111. Ordering service indicates unexpected failure with a 500 response
  • 112. Updating a Resource with PUT • Use POST to create a resource identified by a service-generated URI. • Use POST to append a resource to a collection identified by a service-generated URI. • Use PUT to create or overwrite a resource identified by a URI computed by the client.
  • 113. PUT request and responses
  • 115. Successful update with a 200 response
  • 116. Successful update with a 204 response Whether 200 is used in preference to 204 is largely an aesthetic choice. However, 200 with a response body is more descriptive and actively confirms the server-side state, while 204 is more efficient since it returns no representation and indicates that the server has accepted the request representation verbatim.
  • 117. Order has already been served as a take-away
  • 118. Implementing update with PUT WCF ServiceContract for updating an order with PUT
  • 119. Marking up an order for use with WCF
  • 121. Removing a Resource with DELETE DELETE request and responses
  • 122. Removing an order with DELETE Request Response - Order successfully removed Response - The requested order doesn’t exist Order has been archived
  • 123. Order has been archived
  • 124. Using HttpListener to delete an order
  • 125. Safety and Idempotency • GET is special since it has the properties of being both safe and idempotent. • PUT and DELETE are both idempotent, but neither is safe • While POST is neither safe nor idempotent.
  • 126. Aligning Resource State • In a distributed application, it’s often the case that several consumers might interact with a single resource, with each consumer oblivious to changes made by the others. As well as these consumer-driven changes, internal service behaviors can also lead to a resource’s state changing without consumers knowing. In both cases, a consumer’s understanding of resource state can become misaligned with the service’s resource state. Without some way of realigning expectations, changes requested by a consumer based on an out-of-date understanding of resource state can have undesired effects, from repeating computationally expensive requests to overwriting and losing another consumer’s changes.
  • 127. Aligning Resource State • HTTP provides a simple but powerful mechanism for aligning resource state expectations (and preventing race conditions) in the form of entity tags and conditional request headers • An entity tag value, or ETag, is an opaque string token that a server associates with a resource to uniquely identify the state of the resource over its lifetime. • When the resource changes—that is, when one or more of its headers, or its entity body, changes—the entity tag changes accordingly, highlighting that state has been modified. • ETag s are used to compare entities from the same resource. By supplying an entity tag value in a conditional request header— either an If-Match or an If-None-Match request header—a consumer can require the server to test a precondition related to the current resource state before applying the method supplied in the request.
  • 128. Aligning Resource State • ETags are also used for cache control purposes • To illustrate how ETags can be used to align resource state in a multiconsumer scenario, imagine a situation in which a party of two consumers places an order for a single coffee. Shortly after placing the order, the first consumer decides it wants whole milk instead of skim milk. Around the same time, the second consumer decides it, too, would like a coffee. Neither consumer consults the other before trying to amend the order.
  • 129. Consumer GETs the order • To begin, both consumers GET the current state of the order independently of each other. • Consumer GETs the order
  • 130. Service generates a response with an ETag header
  • 131. How Etag values are computed? • The service computes the entity tag and supplies it as a quoted string in the ETag header prior to returning a response. • Entity tag values can be based on anything that uniquely identifies an entity: • a version number associated with a resource in persistent storage • one or more file attributes • a checksum of the entity headers and body
  • 132. Etag usage • When a consumer receives a response containing an ETag, it can (and should) use the value in any subsequent requests it directs to the same resource. • Such requests are called “conditional requests” • By supplying the received entity tag as the value of an If-Match or If-None-Match conditional header, the consumer can instruct the service to process its request only if the precondition in the conditional header holds true.
  • 133. “If-Match” request header • An If-Match request header instructs the service to apply the consumer’s request only if the resource to which the request is directed hasn’t changed since the consumer last retrieved a representation of it. • The service determines whether the resource has changed by comparing the resource’s current entity tag value with the value supplied in the If-Match header. • If the values are equal, the resource hasn’t changed. • The service then applies the method supplied in the request and returns a 2xx response. • If the entity tag values don’t match, the server concludes that the resource has changed since the consumer last accessed it, and responds with 412 Precondition Failed.
  • 134. The first consumer conditionally PUTs an updated order
  • 135. The conditional PUT succeeds • Because the order hadn’t been modified since the first consumer last saw it, the PUT succeeds, as shown • Notice that while the response doesn’t include an entity body, it does include an updated ETag header. This new entity tag value reflects the new state of the order resource held on the server (the result of the successful PUT).
  • 136. The second consumer conditionally PUTs an updated order • Oblivious to the change that has just taken place, the second consumer attempts to add its order, as shown • This request again uses a conditional PUT, but with an entity tag value that is now out of date (as a result of the first consumer’s modification).
  • 137. The response indicates a precondition has failed • The service determines that the second consumer is trying to modify the order based on an out-of- date understanding of resource state, and so rejects the request, as shown • When a consumer receives a 412 Precondition Failed status code, the correct thing to do is to GET a fresh representation of the current state of the resource, and then use the ETag header value supplied in this response to retry the original request, which is what the second consumer does in this case.
  • 138. The response indicates a precondition has failed • Having done a fresh GET, the consumer sees that the original order had been modified. The second consumer is now in a position to PUT a revised order that reflects both its and the first consumer’s wishes.
  • 139. “If-None-Match” Header • Our example used the If-Match header to prevent the second consumer from overwriting the first consumer’s changes. Besides If-Match, consumers can also use If-None-Match • An If-None-Match header instructs the service to process the request only if the associated resource has changed since the consumer last accessed it. • The primary use of If-None-Match is to save valuable computing resources on the service side. For example, it may be far cheaper for a service to compare ETag values than to perform computation to generate a representation. • If-None-Match is mainly used with conditional GETs, whereas If- Match is typically used with the other request methods, where race conditions between multiple consumers can lead to unpredictable side effects unless properly coordinated.
  • 140. Conditional requests – additional information • Both If-Match and If-None-Match allow the use of a wildcard character, *, instead of a normal entity tag value • An If-None-Match conditional request that takes a wildcard entity tag value instructs the service to apply the request method only if the resource doesn’t currently exist. • Wildcard If-None-Match requests help to prevent race conditions in situations where multiple consumers compete to PUT a new resource to a well-known URI. • In contrast, an If-Match conditional request containing a wildcard value instructs the service to apply the request only if the resource does exist. • Wildcard If-Match requests are useful in situations where the consumer wishes to modify an existing resource using a PUT, but only if the resource hasn’t already been deleted.
  • 141. Conditional requests – additional information • As well as ETag and its associated If-Match and If-None- Match headers, HTTP supports a timestamp-based Last- Modified header and its two associated conditional headers: If-Modified-Since and If-Unmodified-Since. • These timestamp-based conditional headers act in exactly the same way as the If-Match and If-None-Match headers, but the conditional mechanism they implement is accurate only to the nearest second—the limit of the timestamp format used by HTTP. • Because timestamps are often cheaper than hashes, If- Modified-Since and If-Unmodified-Since may be preferable in solutions where resources don’t change more often than once per second.
  • 142. Consuming CRUD Services • .NET client code for order update via PUT
  • 144. Consuming Services Automatically with WADL • Although the patterns for writing clients in .NET and Java are easy to understand and implement, we can save ourselves effort—and, in some cases, generate code automatically— using service metadata. • Web Application Description Language(WADL) • A static description can be used to advertise the addresses and representation formats of the resources the service hosts
  • 145. Ordering service WADL example • A WADL contract is an XML document that describes a set of resources with URI templates, permitted operations, and request- response representations. As you’d expect, WADL also supports the HTTP fault model and supports the description of multiple formats for resource representations.
  • 148. WADL • While it’s helpful that we can read and write WADL by hand (at least in simple cases), the point of WADL is to help tooling automate as much service plumbing as possible. • An opensource .NET WADL tool is available in the below URI • http://wadl.codeplex.com/
  • 149. CRUD Is Good, but It’s Not Great • It’s clear that using HTTP as a CRUD protocol can be a viable, robust, and easily implemented solution for some problem domains
  • 150. Hypermedia Services • When browsing the Web, we’re used to navigating between pages by clicking links or completing and submitting forms. Although we may not realize it, these interlinked pages describe a protocol—a series of steps we take to achieve a goal, whether that’s buying books, searching for information, creating a blog post, or even ordering a coffee. This is the very essence of hypermedia: by transiting links between resources, we change the state of an application.
  • 151. Hypermedia As the Engine of Application State • The phrase hypermedia as the engine of application state, sometimes abbreviated to HATEOAS, was coined to describe a core tenet of the REST architectural style. In this book, we tend to refer to the hypermedia tenet or just hypermedia. Put simply, the tenet says that hypermedia systems transform application state.
  • 152. What is application state? • If we think of an application as being computerized behavior that achieves a goal, we can describe an application protocol as the set of legal interactions necessary to realize that behavior. Application state is a snapshot of an execution of such an application protocol. The protocol lays out the interaction rules; application state is a snapshot of the entire system at a particular instant.
  • 153. Hypermedia As the Engine of Application State • A hypermedia system is characterized by the transfer of links in the resource representations exchanged by the participants in an application protocol. Such links advertise other resources participating in the application protocol. The links are often enhanced with semantic markup to give domain meanings to the resources they identify.
  • 154. Hypermedia As the Engine of Application State • For example, in a consumer-service interaction, • the consumer submits an initial request to the entry point of the service. • The service handles the request and responds with a resource representation populated with links. • The consumer chooses one of these links to transition to the next step in the interaction. Over the course of several such interactions, the consumer progresses toward its goal. • In other words, the distributed application’s state changes.
  • 155. Hypermedia As the Engine of Application State • Transformation of application state is the result of the systemic behavior of the whole: • The service • The consumer • The exchange of hypermedia-enabled resource representations • The advertisement and selection of links • On each interaction, the service and consumer exchange representations of resource state, not application state.
  • 156. Hypermedia As the Engine of Application State • A transferred representation includes links that reflect the state of the application. These links advertise legitimate application state transitions • But the application state isn’t recorded explicitly in the representation received by the consumer; it’s inferred by the consumer based on the state of all the resources— potentially distributed across many services— with which the consumer is currently interacting.
  • 157. Hypermedia As the Engine of Application State • The current state of a resource is a combination of: • The values of information items belonging to that resource • Links to related resources • Links that represent a transition to a possible future state of the current resource • The results of evaluating any business rules that relate the resource to other local resources
  • 158. Hypermedia As the Engine of Application State • the state of a resource is partly dependent on the state of other local resources • The state of a sales order, for example, is partly a function of • the state of a local copy of an associated purchase order; • changes to the purchase order will affect the state of the sales order the next time the business rules governing the state of the sales order are evaluated (i.e., the next time a representation of the sales order is generated).
  • 159. Hypermedia As the Engine of Application State • Importantly, the rules that control the state of a resource are internal to the service that governs the resource: they’re not made available to consumers • In other words, resource state is a function of a private ruleset that only the resource owner knows about: those rules don’t leak into the external representation.
  • 160. Hypermedia As the Engine of Application State • Business rules that relate a resource to other resources should refer only to locally owned resources, however. This allows us to identify and prevent circular dependencies, whereby the state of resource A is partly a function of the state of resource B, which in turn is partly a function of the state of resource A, and so on. We can always arrange locally owned resources so as to prevent circular dependencies; we can’t do the same if the associated resources are governed by another service. If you need to relate the state of a resource to a third-party resource, we recommend making a local copy of the third-party resource using the Atom-based state alignment mechanisms
  • 161. Domain application protocol(DAP) • A service enforces a protocol—a domain application protocol, or DAP—by advertising legitimate interactions with relevant resources • When a consumer follows links embedded in resource representations and subsequently interacts with the linked resources, the application’s overall state changes
  • 162. Domain application protocol(DAP) • Domain application protocols (DAPs) specify the legal interactions between a consumer and a set of resources involved in a business process. DAPs sit atop HTTP and narrow HTTP’s broad application protocol to support specific business goals. As we shall see, services implement DAPs by adding hypermedia links to resource representations. These links highlight other resources with which a consumer can interact to make progress through a business transaction.
  • 164. Hypermedia As the Engine of Application State • Consumers in a hypermedia system cause state transitions by visiting and manipulating resource state. • Interestingly, the application state changes that result from a consumer driving a hypermedia system resemble the execution of a business process. • This suggests that our services can advertise workflows using hypermedia. • Hypermedia makes it easy to implement business protocols in ways that reduce coupling between services and consumers. • Rather than understand a specific URI structure, a consumer need only understand the semantic or business context in which a link appears. • This reduces an application’s dependency on static metadata such as URI templates or Web Application Description Language (WADL). As a consequence, services gain a great deal of freedom to evolve without having to worry whether existing consumers will break.
  • 165. Hypermedia As the Engine of Application State • Services should ensure that any changes they introduce do not violate contracts with existing consumers. While it’s fine for a service to make structural changes to the relationships between its resources, semantic changes to the DAP or to the media types and link relations used may change the contract and break existing consumers.
  • 166. Hypermedia As the Engine of Application State • For computer-to-computer interactions, we advertise protocol information by embedding links in representations, much as we do with the human Web. To describe a link’s purpose, we annotate it. Annotations indicate what the linked resource means to the current resource: “status of your coffee order,” “payment,” and so on. For annotations we can use microformats or Semantic Web technologies, or we can design our own application-specific formats. We call such annotated links hypermedia controls, reflecting their enhanced capabilities over raw URIs.
  • 167. What about Forms? • Forms are hypermedia controls too. Though we use links exclusively throughout the remainder of this chapter, forms can also be used to guide a consumer’s interaction with linked resources. At the HTTP level, POSTing a representation to a URI in a link is equivalent to submitting a form.
  • 168. Hypermedia Formats • Hypermedia Dead Ends • Despite the success of hypermedia formats on the Web, today’s distributed applications typically use nonhypermedia formats such as plain XML to integrate systems. Although XML is easy to use as a data interchange format, and despite its near ubiquity, it is utterly oblivious to the Web.
  • 169. XML lacks hypermedia controls
  • 170. XML lacks hypermedia controls • There’s nothing intrinsically wrong with this order representation when considered in isolation. After all, it conveys the current state of the order well enough. But it fails to provide any context; that is, it doesn’t indicate the current state of the business process, or how to advance it. Informally, we know we need to pay for a drink once ordered, but the representation of Example 5-1 doesn’t indicate how to make that payment. • The use of plain XML leaves the consumer without a guide—a protocol—for successfully completing the business transaction it has initiated. Because there are no hypermedia controls in the order representation, the consumer must rely on out-of-band information to determine what to do next. From a loose coupling point of view, that’s a poor design decision. Aspects of the service’s implementation leak through mechanisms such as URI templates into the consumer’s implementation, making change difficult and risky. • We can, of course, communicate protocol information to the developers of a consumer application using written documentation, or static contracts such as Web Services Description Language (WSDL), WADL, or URI templates. But, as we’ll see, the Web and hypermedia enable us to do better.
  • 171. URI Templates and Coupling • Restbucks could share URI templates with its consumers. For example, it could document and share the template http://restbucks.com/payment/{order_id} • If Restbucks chose to publish URI templates to consumers, it would then be bound to honor those templates for the long term, • Risk breaking existing consumer applications. Publishing URI template details outside a service’s boundary exposes too much information about the service’s implementation. • If the implementation of the ordering and payment services were to change, perhaps as a result of outsourcing the payment capability to a third party, there’d be an increased risk that consumers built to the (now defunct) http://restbucks.com/payment/{order_id} template would break. • Since that kind of business change happens frequently, Restbucks ought to encapsulate its implementation details as far as possible.
  • 172. URI Templates and Coupling • Generally, it’s better to expose only stable URIs. • These stable URIs act as entry points to services, after which hypermedia takes over. • For example, the entry point to the Restbucks ordering service is http://restbucks.com/order. Interacting with the resource at that URI generates further resource representations, each of which contains hypermedia links to yet more resources involved in the ordering business process. • This doesn’t mean URI templates are a bad idea. • In fact, they are an excellent metadata format. But as with all good things, we must learn how to use them in moderation. • We believe URI templates are an excellent means for documenting service design. Restbucks’ implementations all embrace URI templates for internal documentation and implementation purposes. • However, those same implementations only ever share completed (opaque) URIs with consumers. The templates remain a private design and development detail.
  • 173. Selecting a Hypermedia Format • Standard hypermedia formats • Encoding an order in XHTML
  • 174. Domain-specific hypermedia formats • A coffee order with a custom hypermedia link
  • 175. Domain-specific hypermedia formats • A coffee order with hypermedia
  • 176. A media type for Restbucks • The media type declaration used in the Content- Type header for interactions with Restbucks is application/vnd.restbucks+xml. • Breaking it down, the media type name tells us that the payload of the HTTP request or response is to be treated as part of an application-specific interaction. The vnd.restbucks part of the media type name declares that the media type is vendor- specific (vnd), and that the owner is restbucks. The +xml part declares XML is used for the document formatting.
  • 177. Why application/xml doesn’t help? • By treating XML hypermedia formats as plain XML, we skip many of their benefits • The protocol information (the <link> elements) will appear as odd-looking business information.
  • 178. Atom – Media Type • In the Restbucks application domain, we assume that consumers who understand the application/vnd.restbucks+xml media type are capable of dealing with everything defined by it. • However, it occasionally happens that some consumers want to handle only a subset of the representation formats defined in a media type. • While there is no standard solution to this issue on the Web, there is a popular convention defined by the Atom community. • The application/atom+xml media type defines both the feed and the entry resource representation formats.
  • 179. Atom Publishing Protocol • While the vast majority of consumers can handle both, there is a small subset wishing only to deal with standalone entries. In recognition of this need, Atom Publishing Protocol (AtomPub) added a type parameter to the media type value (resulting in Content-Type headers such as application/atom+xml;type=entry). • With such a value for the ContentType header, it is now possible to include “entry” resource representations as payloads in HTTP requests or responses without requiring that the processing software agents have a complete understanding of all the Atom-defined formats.
  • 180. Contracts • Contracts are a composition of media types and protocols
  • 181. Hypermedia Protocols The Restbucks Domain Application Protocol • Possible HTTP requests for the Restbucks ordering service
  • 182. The Restbucks Domain Application Protocol • Modeling Restbucks’ business processes as a DAP and then representing that protocol as a state machine in this manner is a useful way of capturing a business process at design time. • In a resource-oriented distributed application, an application protocol can be thought of as a function of one or more resource life cycles and the business rules that connect these resources. • Because of its resource-centric nature, the Restbucks service does not host an application protocol state machine.
  • 183. The Restbucks Domain Application Protocol • In other words, there’s no workflow or business logic for the application protocol as such • Rather, the service governs the life cycles of the orders and payments participating in the application protocol • Any workflows in the service implementation relate to resource life cycles, not the application protocol life cycle. • While we’ve been explicit in modeling the business process as an application protocol state machine, we’ve been diligent in implementing it wholly in terms of resource state machines.
  • 184. The Restbucks Domain Application Protocol • State transitions for the order resource • From this diagram we can derive the DAP:
  • 185. Advertising Protocols with Hypermedia • Driving the Restbucks ordering protocol through the happy path
  • 186. Advertising Protocols with Hypermedia • Responses contain links to valid next steps in the interaction
  • 188. Hypermedia order representation • This order representation shows the different URIs and associated rel values consumers can use to advance the DAP. The semantic markup definitions are shared widely with consumers as part of the media type specification, and are as follows: • payment • The linked resource allows the consumer to begin paying for the order. Initiating payment involves PUTting an appropriate resource representation to the specified URI, as defined in the Restbucks media type. • self • The uri value can be used to GET the latest resource representation of the order. • update • Consumers can change the order using a POST to transfer a representation to the linked resource. • cancel • This is the uri to be used to DELETE the order resource should the consumer wish to cancel the order.
  • 190. Payment response contains links to order and receipt resources
  • 191. Receipt representation with a link to the order resource
  • 192. The updated order resource representation after payment has been accepted
  • 193. Order resource representation after the barista has prepared it
  • 194. Representations of completed orders have no links • Receipt • Customers can DELETE the linked resource, thereby completing the order.
  • 195. Building the Ordering Service in .NET • The Restbucks .NET Hypermedia Framework • Separating network, hypermedia, and business logic
  • 196. Declarative Domain-Specific Language (DSL) • The hypermedia framework manages resource state transitions by embedding appropriate hypermedia controls into a response based on both the state of the resource targeted by the request and the state of any associated resources. • To express this protocol, we have devised a simple, declarative Domain-Specific Language (DSL). Scripts written with the Restbucks DSL choreograph numerous business actions and externalize application state to consumers.
  • 197. Building the Ordering Service in .NET • Remember that each workflow in the service implementation represents resource state machines, not a single, overarching application protocol state machine. By implementing the service solely in terms of resource states, we avoid having to save protocol instance state (application state) on the server, which allows us to scale the service horizontally.
  • 198. The Restbucks .NET hypermedia framework Each state machine analyzes incoming HTTP requests and, depending on the state of the resource to which the request relates, dispatches the payload to an appropriate .NET method in the business logic layer. The method deals with XML documents as the input and output, and is unaware of the HTTP and hypermedia details of the interaction. On the response path, the hypermedia framework receives XML documents from the business layer and augments them with any hypermedia constructs declared in the state machine DSL description. The resultant payload is then passed to the underlying HTTP infrastructure for delivery to the consumer, as we see in Figure
  • 199. DSL • The DSL used for the Restbucks hypermedia framework is a state machine description markup language. This language includes constructs representing hypermedia controls, which are injected into representations as XML <dap:link> elements. • While the primary focus of the DSL is to describe a resource’s state transitions, it also includes those DAP-specific transitions that relate to the particular resource.
  • 200. Representing a resource state machine and the supported DAP transitions using the Restbucks hypermedia DSL
  • 202. Delivery - The Ready state of the order resource
  • 204. Scaling Out • Caching • Caching is the ability to store copies of frequently accessed data in several places along the request- response path. • When a consumer requests a resource representation, the request goes through a cache or a series of caches toward the service hosting the resource. • If any of the caches along the request path has a fresh copy of the requested representation, it uses that copy to satisfy the request. • If none of the caches can satisfy the request, the request travels all the way to the service (or origin server as it is formally known).
  • 205. Caching • Origin servers control the caching behavior of the representations they issue • Using HTTP headers, an origin server indicates whether a response can be cached, and if so, by whom, and for how long. • Caches along the response path can take a copy of a response, but only if the caching metadata allows them to do so. • The caches can then use these copies to satisfy subsequent requests.
  • 206. Caching • Cached copies of a resource representation can be used to satisfy subsequent requests so long as they remain fresh • A cached representation remains fresh for a specific period of time, which is called its freshness lifetime. • When the age of a cached object exceeds its freshness lifetime, the object is said to be stale. • Caches will often add an Age response header to a cached response. • The Age header indicates how many seconds have passed since the representation was generated at the origin server.
  • 207. Caching • A stale representation must be revalidated with the origin server before it can be used to satisfy any further requests • If the revalidation reveals that the stale representation is in fact still valid, the cached copy can be reused. • If, however, the resource has changed since the stale representation was first issued, the cached copy must be invalidated and replaced. • Representations can become invalid during their freshness lifetime without the cache knowing. • Unless the consumer specifically asks for a revalidation or a new copy from the origin server, the cache will continue to use these invalid (but fresh) representations until they become stale.
  • 208. Benefits of Caching • Optimizing the network using caching improves the overall quality-of- service characteristics of a distributed application. Caching significantly benefits four areas of systems operation, allowing us to: • Reduce bandwidth • By reducing the number of network hops required to retrieve a representation, caching reduces network traffic and conserves bandwidth. • Reduce latency • Because caches store copies of frequently accessed information nearer to where the information is used, caching reduces the time it takes to satisfy a request. • Reduce load on servers • Because they are able to serve a percentage of requests from their own stores, caches reduce the number of requests that reach an origin server. • Hide network failures • Caches can continue to serve cached content even if the origin server that issued the content is currently unavailable or committed to an expensive processing task that prevents it from generating a response. In this way, caches provide fault tolerance by masking intermittent failures and delays from consumers.
  • 209. Caching and the Statelessness Constraint • Caching helps mitigate the consequences of applying the statelessness constraint.[62] It reduces the amount of data sent over the network by storing representations closer to where they are needed, and it reduces the load on origin servers by having caches satisfy repeated requests for the same data.
  • 210. Making Content Cacheable • Given that caches are designed around the retrieval of resource representations, it shouldn’t come as a surprise to learn that they mostly (but not exclusively) work with GET requests. • Responses to GET requests are cacheable by default. • Responses to POST requests are not cacheable by default, but can be made cacheable if either an Expires header, or a Cache-Control header with a directive that explicitly allows caching, is added to the response. • Responses to PUT and DELETE requests are not cacheable at all.
  • 211. Response Headers Used for Caching • There are two main HTTP response headers that we can use to control caching behavior: • Expires • The Expires HTTP header specifies an absolute expiry time for a cached representation. Beyond that time, a cached representation is considered stale and must be revalidated with the origin server. A service can indicate that a representation has already expired by including an Expires value equal to the Date header value (the representation expires now), or a value of 0. To indicate that a representation never expires, a service can include a time up to one year in the future. • Cache-Control • The Cache-Control header can be used in both requests and responses to control the caching behavior of the response. The header value comprises one or more comma-separated directives. These directives determine whether a response is cacheable, and if so, by whom, and for how long. •
  • 212. Response Headers Used for Caching • If we can determine an absolute expiry time for a cached response, we should use an Expires header. • If it’s more appropriate to indicate how long the response can be considered fresh once it has left the origin server, we should use a Cache-Control header, adding a max-age or s-maxage directive to specify a relative Time to Live (TTL). • Cacheable responses (whether to a GET or to a POST request) should also include a validator— either an ETag or a Last-Modified header:
  • 213. A response with an absolute expiry time
  • 214. A response with a relative expiry time
  • 215. Using Caching Directives in Responses • Cache-Control directives serve three functions when used in a response. Some make normally uncacheable responses cacheable. Others make normally cacheable responses uncacheable. Finally, there are some Cache- Control directives that do not affect the cacheability of a response at all; rather, they determine the freshness of an already cacheable response. An individual directive can serve one or more of these functions.
  • 216. Using Caching Directives in Responses • max-age=<delta-seconds> • This directive controls both cacheability and freshness. It makes a response capable of being cached by local and shared caches (proxies and reverse proxies), as well as specifying a freshness lifetime in seconds. A max-age value overrides any Expiry value supplied in a response. • s-maxage=<delta-seconds> • Like max-age, this directive serves two functions: it makes responses cacheable, but only by shared caches, and it specifies a freshness lifetime in seconds.
  • 217. Using Caching Directives in Responses • public • This directive makes a response capable of being cached by local and shared caches, but doesn’t determine a freshness value. Importantly, public takes precedence over authorization headers. Normally, if a request includes an Authorization header, the response cannot be cached. If, however, the response includes a public directive, it can be cached. You should exercise care, however, when making responses that require authorization cacheable. • private • This directive makes a response capable of being cached by local caches only (i.e., within the consumer implementation). At the same time, it prevents normally cacheable responses from being cached by shared caches. private doesn’t determine a freshness value.
  • 218. Using Caching Directives in Responses • must-revalidate • This directive makes normally uncacheable responses cacheable, but requires caches to revalidate stale responses with the origin server. Only if the stale response is successfully validated with the origin server can the cached content be used to satisfy the request. must-revalidate is enormously useful in balancing consistency with reduced bandwidth and computing resource consumption. While it forces a revalidation request to travel all the way to the origin server, an efficient validation mechanism on the server side will prevent the core service logic from being invoked for a large percentage of requests—all for the cost of a measly 304 Not Modified response.
  • 219. Using Caching Directives in Responses • proxy-revalidate • This directive is similar to must-revalidate, but it only applies to shared caches. • no-cache • This directive requires caches to revalidate a cached response with the origin server with every request. If the request is successfully validated with the origin server, the cached content can be used to satisfy the request. The directive only works for responses that have been made cacheable using another header or directive (i.e., it doesn’t make uncacheable responses cacheable). Unfortunately, different caches behave in different ways with regard to no-cache: some caches treat no-cache as an instruction to not cache a response (as per an old draft of HTTP 1.1); some treat it correctly, as a requirement to always revalidate a cached response. • no-store • This directive makes normally cacheable content uncacheable by all caches.
  • 220. Making content cacheable by local caches only
  • 222. Minimizing traffic for accessing local, consistent, cached representations In revalidating each request with the server, the cache will pass on the contents of the Authorization header supplied by the consumer. If the origin server replies 401 Unauthorized, the cache will refuse to release the cached representation. The combination public, max- age=0 differs from must- revalidate in that it allows caching of responses to requests that contain Authorization headers.
  • 223. Implementing Caching in .NET • Using the WinINet local cache from consumer code
  • 224. A simple web server
  • 225. GetMenu(…) satisfies an HTTP GET request
  • 226. Response to a GET request for the Restbucks menu
  • 227. Adding a Cache-Control header to the response
  • 228. The response now includes a Cache-Control header
  • 229. The menu representation is cached throughout the Web