Communication
REST
REST — resources, verbs, statelessness, HATEOAS, versioning, idempotency. The most common API style and its tradeoffs.
1 min readConcepts · System Design
What is REST?
REST (Representational State Transfer) is an architectural style for designing networked applications. RESTful APIs use HTTP methods to perform CRUD operations on resources.
REST Principles
| Principle | Description |
|---|---|
| Stateless | Each request contains all info needed |
| Client-Server | Separation of concerns |
| Cacheable | Responses can be cached |
| Uniform Interface | Consistent resource-based URLs |
| Layered System | Client doesn't know if talking to server or intermediary |
Resource-Based URLs
Good (resource-based):
GET /users # List users
GET /users/123 # Get user 123
POST /users # Create user
PUT /users/123 # Update user 123
DELETE /users/123 # Delete user 123
Bad (action-based):
GET /getUser?id=123
POST /createUser
POST /deleteUserHTTP Methods in REST
| Operation | Method | Example |
|---|---|---|
| Create | POST | POST /users |
| Read | GET | GET /users/123 |
| Update (full) | PUT | PUT /users/123 |
| Update (partial) | PATCH | PATCH /users/123 |
| Delete | DELETE | DELETE /users/123 |
Request/Response Example
Request
POST /api/users HTTP/1.1
Host: api.example.com
Content-Type: application/json
{
"name": "John",
"email": "john@example.com"
}Response
HTTP/1.1 201 Created
Content-Type: application/json
Location: /api/users/123
{
"id": 123,
"name": "John",
"email": "john@example.com"
}Best Practices
| Practice | Example |
|---|---|
| Use nouns, not verbs | /users not /getUsers |
| Use plural nouns | /users not /user |
| Nest for relationships | /users/123/orders |
| Use query params for filtering | /users?role=admin |
| Version your API | /v1/users or header |
Pagination
GET /users?page=2&limit=20
Response:
{
"data": [...],
"pagination": {
"page": 2,
"limit": 20,
"total": 100,
"next": "/users?page=3&limit=20"
}
}Interview Tips
- Explain REST principles (stateless, uniform interface)
- Design resource-based URLs properly
- Know when to use each HTTP method
- Discuss pagination, filtering, versioning
- Compare with GraphQL and gRPC
Finished reading?
Mark it done to track your progress in Concepts.