REST (Representational State Transfer) Architecture
What is REST?
REST is a software architectural style for designing web services. It defines a set of rules for how web applications communicate over the internet.
REST is resource-based: everything on the server is considered a resource, such as:
- A file
- An image
- A row in a database table
Each resource can have multiple representations, like JSON, XML, HTML, or plain text.
Example: A user record in a database can be accessed in JSON or XML format depending on the client request.
Architectural Constraints of REST
REST recommends six main constraints that make web services scalable, reliable, and maintainable:
1. Uniform Interface
Resources are accessed in a consistent way using URLs. Clients interact with resources through standard methods, decoupling client and server.
2. Statelessness
Each client request must contain all the information needed for the server to process it. The server does not store session state, making the system scalable.
3. Client-Server
Clients (UI) and servers (data and business logic) are separated, allowing independent evolution of both.
4. Cacheability
Responses must be explicitly marked as cacheable or non-cacheable. Caching improves performance and reduces server load.
5. Layered System
Clients cannot usually tell whether they are connected directly to the server or through intermediaries like proxies. Layers improve scalability and security.
6. Code on Demand (Optional)
Servers can provide executable code (like JavaScript) to extend client functionality. This is optional but adds flexibility.
Advantages of REST Constraints
- Scalability: Statelessness and layers make horizontal scaling easier.
- Simplicity: Clear rules make APIs easier to design and use.
- Modifiability: Client and server evolve independently.
- Reliability: Statelessness and caching improve fault tolerance.
- Portability: Standard interfaces like HTTP and URLs.
- Visibility: Standard communication simplifies monitoring and debugging.
HTTP Verbs in REST
REST uses HTTP methods to perform operations on resources:
| HTTP Method | Operation | Description |
|---|---|---|
| GET | READ | Fetch a resource from the server |
| POST | CREATE | Add a new resource to the server |
| PUT | UPDATE | Modify an existing resource |
| DELETE | DELETE | Remove a resource from the server |
GET /users/1 # Fetch user with ID 1
POST /users # Create a new user
PUT /users/1 # Update user with ID 1
DELETE /users/1 # Delete user with ID 1
Conclusion
REST is a standardized approach to building web services that treats everything as a resource, uses stateless communication, supports caching and layers, and performs CRUD operations via HTTP methods. It is simple, scalable, and highly compatible with web technologies.