RESTful Services
REpresentational State Transfer a.k.a. REST is a resource-based architectural style used for developing web services. The web services that are built on REST architecture are known as RESTful web services. These web services are lightweight, highly scalable and maintainable. REST based systems use URIs for resource identification and use HyperText Transfer Protocol (HTTP) for communication.
Let’s take a look at 6 architectural constraints of REST API one by one.
- Uniform Interface
This suggests that there should be a uniform way of interacting with servers regardless of the type of the application. RESTful services use HTTP methods and requests to achieve this. In RESTful services, URIs are used to access resources and HTTP methods (GET, POST, PUT, DELETE etc.) are used to perform actions on a selected resource.
2. Stateless
In REST, the server does not maintain the client’s state. The client must send a self-descriptive message that contains sufficient information to perform that particular action. Statelessness of RESTful services increases the availability of the system.
3. Cacheable
In REST, we can cache responses on the client-side. It allows the client to use cached data for any subsequent request. Caching can help to improve availability and performance of the system.
4. Client-Server
REST applications should follow client-server architecture. Client is completely separated from the server and HTTP stack is used as the platform for communication between the two.
5. Layered System
REST applications have a layered architecture and each layer does not know anything about other layers.
6. Code on Demand
This is an optional constraint. According to this, Servers can transfer executable code to the client.
Now, let’s take a look at some of the best practices we can follow when creating RESTful Services.
- When creating REST APIs, it is good to use nouns over verbs for endpoint names.
- The nouns used for endpoints should be in plural form.
- Avoid using the GET method to alter the state.
- Always use HTTP status codes for errors.
- Version the API
Why is REST so popular?
- Scalability
Because of the separation between client and server, REST based systems are highly scalable. This is one of the biggest reasons why REST is widely used in the industry.
2. Flexibility and portability
Front-end and back-end can be hosted on different servers, which is a significant management advantage
3. Independence
In REST, the way how client and server is separated makes it easy for developments across a project to take place independently
In a future article, we will take a look at the HTTP methods and status codes used by RESTful services.