0% found this document useful (0 votes)
4 views3 pages

REST API All in One Interview Guide-2

This document serves as a comprehensive guide for REST API design, covering essential topics such as resource design, request and response structure, pagination methods, rate limiting, error handling, security, versioning, and performance optimization. It emphasizes best practices for creating efficient and secure APIs suitable for backend interviews. Key recommendations include using resource-oriented URLs, implementing cursor-based pagination, and ensuring proper error responses and security measures.

Uploaded by

raghu1234
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

REST API All in One Interview Guide-2

This document serves as a comprehensive guide for REST API design, covering essential topics such as resource design, request and response structure, pagination methods, rate limiting, error handling, security, versioning, and performance optimization. It emphasizes best practices for creating efficient and secure APIs suitable for backend interviews. Key recommendations include using resource-oriented URLs, implementing cursor-based pagination, and ensuring proper error responses and security measures.

Uploaded by

raghu1234
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

REST API – All■in■One Interview Guide (Backend /

Microservices)
This document is a concise but complete reference for REST API design, pagination, rate limiting, security,
error handling, and performance — with coding examples suitable for 8–10 years backend interviews.

1. Resource Design (Foundation)


REST APIs are resource■oriented. URLs represent nouns (resources) and HTTP methods represent actions.
GET /users/{id}
POST /users
PUT /users/{id}
DELETE /users/{id}

Best practices: use nouns, avoid verbs in URLs, use proper HTTP methods, and design hierarchical resources
only when there is a strong relationship.

2. Request & Response Design


This aspect controls how clients fetch data efficiently and consistently.
GET /orders?status=PAID&sort=createdAt,desc&limit=50

Include filtering, sorting, pagination, and field selection. Responses should have consistent structure and
optional metadata.
{
"data": [ { "orderId": "O123", "amount": 500 } ],
"meta": {
"limit": 50,
"nextCursor": "2025-01-01T10:30:00"
}
}

3. Pagination
Pagination prevents large responses and protects databases.
Offset■Limit Pagination
GET /orders?page=2&size=20

SELECT * FROM orders


ORDER BY created_at DESC
LIMIT 20 OFFSET 20;

Simple but inefficient for large datasets and unstable when data changes.
Cursor / Keyset Pagination (Preferred)
GET /orders?limit=20&after=2025-01-01T10:30:00

SELECT * FROM orders


WHERE created_at < :cursor
ORDER BY created_at DESC
LIMIT 20;

Fast, scalable, and stable. Best choice for high■volume systems.

4. Rate Limiting & Throttling


Rate limiting protects services from abuse and traffic spikes. It is usually implemented at the API Gateway.
Common algorithms:
• Token Bucket (most common)
• Fixed Window
• Sliding Window
• Leaky Bucket

HTTP/1.1 429 Too Many Requests


Retry-After: 60

Token Bucket is preferred because it allows short bursts while enforcing limits.

5. Error Handling & Status Codes


Use standard HTTP status codes and structured error responses.
200 OK
201 Created
400 Bad Request
401 Unauthorized
403 Forbidden
404 Not Found
409 Conflict
429 Too Many Requests
500 Internal Server Error

{
"errorCode": "ORDER_NOT_FOUND",
"message": "Order does not exist"
}

6. Security
Security ensures only authorized users can access APIs.
Authorization: Bearer <JWT_TOKEN>

Authentication identifies the user. Authorization controls access. Always use HTTPS and validate tokens at
gateway or service layer.

7. Versioning & Evolution


Versioning allows APIs to evolve without breaking clients.
/v1/orders
/v2/orders

8. Performance & Resilience


Performance is improved using caching, timeouts, retries, and idempotency.
Idempotency-Key: 123e4567

Idempotency is critical for payment and retry■based systems.


Interview 30■Second Summary
A well■designed REST API uses resource■oriented URLs, correct HTTP methods, consistent
request■response formats, cursor■based pagination for large data, rate limiting at the gateway, proper error
handling with status codes, secure authentication, and versioning for backward compatibility.

You might also like