REST_Interview_Problems
REST_Interview_Problems
What are the key principles of REST and how do they influence API design?
REST (Representational State Transfer) is based on architectural principles for building
scalable web services.
Key principles:
Stateless — each request contains all context needed for processing.
Client-Server — separation of concerns improves scalability and flexibility.
Uniform Interface — consistent use of HTTP methods (GET, POST, PUT, DELETE, etc.).
Resource-Based — everything is treated as a resource identified by a URI.
Cacheable — responses should indicate cacheability to improve performance.
Layered System — allows intermediaries (proxies, load balancers).
Influence on design:
Clear, consistent resource URIs (e.g., /users/123).
Use of standard HTTP methods and status codes.
How would you design a versioning strategy for a public REST API?
Versioning ensures backward compatibility as APIs evolve.
Common strategies:
URI versioning (e.g., /v1/users).
Header versioning (e.g., Accept: application/vnd.api.v2+json).
Query param versioning (e.g., ?version=2).
Best practices:
Avoid breaking existing clients.
Document deprecated versions and support timelines.
Use semantic versioning if maintaining multiple major versions.
What are idempotent and safe HTTP methods and why do they matter?
Safe methods:
Do not modify server state (e.g., GET, HEAD).
Idempotent methods:
Multiple identical requests produce the same result (e.g., PUT, DELETE).
Importance:
Helps clients (e.g., retries) and intermediaries (e.g., caches, proxies) behave predictably.
Improves fault tolerance and system resilience.
POST is neither safe nor idempotent — used for creating new resources or complex
operations.
How do you ensure REST API security for public and internal use cases?
Security measures include:
Authentication — OAuth2, JWT, API Keys.
Authorization — role-based access control (RBAC), scopes.
Rate limiting — protect from abuse (e.g., using API Gateway or filters).
Input validation — prevent injection and malformed data.
HTTPS — always enforce TLS for confidentiality and integrity.
Additional practices:
CORS configuration for cross-domain access.
HATEOAS or OpenAPI for discoverability and self-descriptive APIs.