REST API YT + IntervieBit Rest is the most common communication standard between computers over internet.
API: Application Programming Interface: a way for two computers talk to each other.
REST:
- json.md + http.md
- Representational State Transfer
- the common API standard used by most mobile and web applications to talk to servers is called REST API.
- An API that follows the REST standard is called a RESTful API.
- post is
not idempotent
because it creates a new resource every time it is called | Get, put, delete, head, options areidempotent
because they do not change the state of the server. | Link - core components of HTTP Request & Response 6. & 7.
- put-vs-post
- idempotent and safe HTTP methods | Safe methods are those that do not change any resources internally |
- payload size: not in get and delete, but in post and put. sending data in the body of the request.
- Not possible to maintain sessions in REST on the server-side. | REST is stateless, meaning that each request is independent and does not rely on any previous.
URI's
1. Uniform Resource Identifier:
> :////
URI = URL + URN
eg. https://example.com/api/v3/users
eg. https://example.com/api/v3/products
/users ✅ | /getUsers ❌
Client -> Server
POST /Procucts HTTP/1.1
Accept: 'application/json'
___________________________ ------------>
{
"name": "Product 1",
"price": 100,
"quantity": 10
}
<------------
HTTP/1.1 201 Created
GET /Products/1 HTTP/1.1
Accept: 'application/json'
__________________________
- Rest API is stateless, meaning that each request is independent and does not rely on any previous request. so easylly scalable and reliable.
To well behave REST API:
- if ther is to much of data, use pagination.
/products?limit=25&offset=50
- versioning of api is very importent.
/api/v1/products
- if ther is to much of data, use pagination.
- some best practices: Link
Also explore
- GraphQL
- SOAP
- gRPC
- AJAX: AJAX - Asynchronous javascript and XML 🤯. | AJAX is used for dynamic updation of UI without the need to reload the page. |
⚠️ MUST LEARN - Web Socket: Real-time communication, communication is bi-directional.
-
Use Pagination: limit the number of items returned in a single response.
-
asynchroneous logging: log the request and response in a separate thread.
-
Caching: cache the response of the API. use redis or memcached.
-
Payload Compression: compress the response payload.
-
Request Memoization (Pre-Request Cache) 💾 • Prevents duplicate API calls in the same render cycle. • If two components fetch the same data, the second one uses the cached result. • Only works with GET requests in Server Components.
An API Gateway is a server that acts as an entry point for multiple backend services. It manages API requests, handles authentication, monitors traffic, and ensures security. It simplifies client interactions by providing a single access point to multiple services. eg & tools. chatGPT | AWS API Gateway, NGINX Key Features of an API Gateway
- Request Routing: Directs client requests to appropriate backend services.
- Rate Limiting: Controls API request limits to prevent overload. Max requests per second (e.g., 100 req/sec) from a IP.
- Authentication & Authorization: Manages security protocols (OAuth, JWT, API keys).
- Load Balancing: Distributes traffic among multiple servers.
- Caching: Improves response time by storing frequently accessed data.
- Monitoring & Logging: Tracks API performance and detects errors.
API Methods:
- GET: Retrieve data from the server.
- POST: Send data to the server to create a new resource. (not idempotent)
- PUT: Update an existing resource or create it if it doesn't exist.
- PATCH: Partially update an existing resource.
- DELETE: Remove a resource from the server.
- HEAD: Retrieve metadata about a resource without the body.
- OPTIONS: Describe the communication options for the target resource.
- TRACE: Echo the received request back to the client.
- CONNECT: Establish a tunnel to the server.
- LINK: Create a link between two resources.
Service-Oriented Architecture (SOA): YT short
Monolithic vs Microservice Architecture: YT
Monolithic Architecture: Single-tiered software application in which different components are combined into a single program from a singleplatform. [payment, order, product, Email] all services at one place. Microservice Architecture: A software development technique that structures an application as a collection of small, loosely coupled services. Each service is self-contained and can be deployed independently. [payment, order, product, Email] all services at different places. can use gRPC for faster communication between services. TODO: pending...