Representational state transfer (REST) is an architectural style for building distributed systems based on hypermedia. REST aims to leverage existing protocols and features of the modern web in order to create an architecture that is performant, scalable, simple, modular, and reliable. The core ideas behind REST include:
Client-Server Architecture
REST systems have a separation between the client and server. Clients initiate requests to servers, which process requests and return appropriate responses. This helps improve scalability by simplifying the client side while enabling more complexity on the server side.
Statelessness
REST requires that client session state be held only on the client, and each request contain all necessary information for processing. This constraint helps scalability since no client context needs to be stored on the server between requests.
Cacheability
REST responses should ideally be cacheable to improve performance. Resources should be uniquely addressable via URIs to enable caching of responses.
Uniform Interface
REST defines a uniform interface between clients and servers that helps decouple the architecture. This interface is constrained by:
- Resource identification through URIs
- Resource manipulation through representations
- Self-descriptive messages
- Hypermedia as the engine of application state
Layered System
REST allows clients and servers to be composed into hierarchical layers to improve scalability through intermediary components like proxies, gateways, and caches.
Code on Demand (Optional)
REST allows client functionality to be extended dynamically by downloading and executing code. This simplifies clients and improves extensibility.
Benefits of REST
Some key benefits of RESTful architectures include:
- Improved scalability and performance
- Reliability through statelessness
- Simplicity and modularity of components
- Flexibility through loose coupling
- Reusability through standardized interfaces
RESTful Principles
There are several guiding principles and best practices for building RESTful systems:
- Use HTTP methods explicitly – GET, POST, PUT, DELETE etc.
- Give resources unique URIs
- Return JSON/XML representations
- Use status codes meaningfully
- Make resources HATEOAS driven
- Use HTTP headers for caching, auth, content-type
Common REST Architectural Components
Some common components found in REST architectures include:
Component | Description |
---|---|
Client | Initiates requests and receives responses, e.g. browser, mobile app |
Resource | Data representation, e.g. document, image, service |
Representation | Serialized resource state, e.g. JSON, XML, PNG |
URI | Resource identifier, e.g. https://api.example.com/users/123 |
Message | HTTP request and response |
Server | Hosts resources and handles requests |
REST Maturity Model
There are various levels of RESTful maturity for APIs and systems:
Level 0 – HTTP as transport
Using HTTP only as a transport protocol without leveraging its features.
Level 1 – Resources
Exposing resources with proper URIs.
Level 2 – HTTP verbs
Using HTTP methods explicitly for operations on resources.
Level 3 – Hypermedia controls
Using hypermedia for driving application state transitions.
Most APIs strive for at least level 2 maturity with proper use of HTTP and resources. Level 3 hypermedia-driven systems are more flexible but complex to build.
REST vs SOAP Web Services
REST and SOAP represent two different architectural approaches for building web services:
REST | SOAP | |
---|---|---|
Architectural style | Resource-based | Method-based |
Protocol | HTTP | HTTP, SMTP, TCP, etc |
Message format | JSON, XML, HTML | XML |
Caching | Easy to cache | Not cacheable |
Security | SSL/TLS, OAuth | WS-Security |
In summary, REST emphasizes simplicity, scalability and loose coupling while SOAP focuses more on standardized schemas and stringent security.
Example REST APIs
Some examples of popular REST APIs include:
- Twitter API – Manage tweets, followers, search, etc.
- GitHub API – Programmatically access GitHub resources and functions.
- Stripe API – Integrate payments into an application.
- Slack API – Build bots and integrate with Slack teams.
- Twilio API – Add messaging, voice and video to apps.
Conclusion
REST is an architectural style that leverages principles like statelessness and uniform interfaces for building highly scalable, flexible and loosely coupled distributed systems. It makes efficient use of modern web protocols and is now the predominant approach for building web services and APIs. Understanding RESTful principles and constraints is essential for modern web development.