This topic we shall study about REST API.
what is an API?
API stands for Application Programming Interface. It is a set of tools for building an application. With the help of API it will be come easy for the programmer to build an application and distribute its services. With the help of API 2 different components can communicate with each other without knowing about each other.
REST API stands for REpresentational State Transfer API. It is based on HTTP. Now a day this is one of the most popular ways to communicate between multiple applications or Microservices because it uses less bandwidth.
As REST is based on HTTP hence it supports HTTP methods i.e. get, put, delete and post.
get() It will get the input from the user in a read only way.
put() It will help in creating new resource.
delete() It will help to delete a resource
post() It is used to update or create a new resource.
A “resource” can be anything with a name and can be stored. For example, in a login page, username and password are both resources.
To run any webserver, you need a server. Generally, we use Tomcat server.
Now coming back to our System Design, how does REST API is used in system design.
As I previously explained, as it uses less bandwidth, it can be used to communicate between multiple Microservices.
Generally the communication between client and database happens as shown below:
client ———-> API ———-> Database
So a client will communicate with a DB with the help of API. In API we write the logic to retrieve the data from DB and send it to the client. We send the data to the client using JSON format. Here API used will be REST API.
Usually a REST API will look like below:
The above API tells that I need question 2 and its answer. The response can be as shown below:
“question” : “what is the website name”,
“answer” : “prodevelopertutorial.com”
Below are some of the characteristic of REST API:
1. Uniform Interface:
As REST API is used to connect and transfer data between multiple devices, ways of accessing the API should be unique. REST uses Uniform Resource Identifier (URI) to map the resources and communicate between multiple components. The drawback it degrades the performance. But, since the information is transferred in a standard way, instead of specific to application needs, it can be considered as negligible.
2. Client Server:
RESI API is a client server based architecture. Here server does all the computational task and deliver result to the client. Multiple clients can be connected to a central server. This separation of user interface from the data computation will improve scalability and performance.
A REST HTTP request will send all the data required for the server to give the response. Once the request is completed, server will not remember the details of the request. Because of this property visibility, reliability and scalability will be increased.
Visibility is increased because, the server will look at the single point and does not have to search the data from other request.
In case of system crash, as we not saving and operating on the previous data, once the system is back up, new request with the latest data can be computed. Hence reliability will be increased.
As we are not saving any resources, server can free the resources once the request has been completed and give the space for other requests. Thus eliminating the need to manage the resource usage.
To improve the performance of application, cache is important. Cache is saving the data for later use. Cache constraints can be added to each resource. If the response from the server is cacheable, then the client will save the data for further use.
5. Layered System
Internet is consisting of multiple layers like client cache, server connector, server cache etc. A request can go through multiple layers to get the result. As REST API supports layered system, it is possible to communicate between legacy service to new services. We can encapsulate the legacy system into a layer and make them talk with the new system by simplifying the functionality.
How to create a REST API?
A REST API can be created by using JAVA, Node.js.
How to transfer data using REST API?
The data can be transferred using JSON, XML, plain text.