A stateless REST APIs stands for Representational State Transfer is an architectural style of API that does not store any state(info) between requests. Meaning each client's request contains all the necessary information for the server to fulfill the request and the server does not store any information about previous request from the client(like browsers, mobile apps, or other servers). It makes the modern internet fast, reliable, and incredibly scalable.
Example:
GET /users/1234
Authorization(Request header): Bearer jkgjapoieh32hihaej23t4h
This retrieves user's information with ID 1234 once the request is verified with all required information.
- Stateless
- The server does not remember or handles previous requests
- The server does not store session data
- The server handles every request independently
- Every request includes authentication details
- JWT(JSON Web Tokens) are used commonly
- Flow
- During login -> Server generates and returns the JWT in the response
- The client(like browsers and mobile apps) stores the token
- The client sends each request with the token(usually in the request header)
- The server verifies the token sent in request and processes the response
The Trade-off:
- The main downside of using JWT tokens is that it increases overhethe packets of data sent over the network are slightly largerad. Because user is sending authentication token and context with every request, the packets of data sent over the network are slightly larger, however the benefits of being able to scale application to million of users outweighs the cost of few extra kilobytes of network request data.
No comments:
Post a Comment