System Design Tutorial: REST API

This tutorial 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 distribut its services. With the help of API 2 different componenets 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 days 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 communiate with a DB with the help of API. In API we write the logic to retrive 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:
https://prodevelopertutorial.com/api/q/question/2
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 transffer 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 transffered 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 seperation of user interface from the data computation will improve scalability and performance.
3. Stateless:
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 eleminating the need to manage the resource usage.
4. Cacheable
To improve the preformace 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 sarever 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 layerd system,  it is possible to commpunicate between legacy service to new services. We can encapsulate the legasy 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 transffer data using REST API?
The data can be transffered using JSON, XML, plain text.
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *