The PATCH Method
PATCH applies partial modifications to a resource. Unlike PUT, you only send the fields you want to change.
PATCH Characteristics
PATCH /api/users/123 HTTP/1.1
Host: api.example.com
Content-Type: application/json
{
"email": "alice.updated@example.com"
}
→ Not safe: Modifies server state
→ Not idempotent (in general)
→ Not cacheable by default
→ Has a request body (partial resource)
PATCH vs PUT
| Aspect | PATCH | PUT |
|---|---|---|
| Purpose | Partial update | Full replacement |
| Body | Only changed fields | Complete resource |
| Idempotent | Generally no | Yes |
| Network efficiency | More efficient | Less efficient |
PATCH Examples
# Update only email
PATCH /api/users/123
Content-Type: application/json
{"email": "new@example.com"}
# Update multiple fields
PATCH /api/orders/456
Content-Type: application/json
{"status": "SHIPPED", "trackingNumber": "1Z999AA10123456784"}
# JSON Patch (RFC 6902)
PATCH /api/users/123
Content-Type: application/json-patch+json
[
{"op": "replace", "path": "/email", "value": "new@example.com"},
{"op": "remove", "path": "/phone"}
]
Making PATCH Idempotent
While PATCH isn't inherently idempotent, you can make it idempotent by:
- Storing the full state and re-applying the same changes
- Using a unique request ID to deduplicate
// Idempotent PATCH (replaces with same result)
PATCH /api/users/123
{"email": "new@example.com"}
// Server stores: "email update from old to new"
// Same request again → no-op (already applied)
PATCH Design Best Practices
- Return the updated resource after PATCH
- Support JSON Merge Patch (RFC 7396) for simple updates
- Consider JSON Patch (RFC 6902) for complex operations
- Validate partial updates — Ensure required fields aren't removed
HTTP Best Practices
Methods
- GET: Read (safe, idempotent)
- POST: Create
- PUT: Replace (idempotent)
- PATCH: Partial update
- DELETE: Remove (idempotent)
Headers
- Content-Type: Body format
- Cache-Control: Caching rules
- Authorization: Authentication
- Accept: Desired response format
Status Codes
- 2xx: Success
- 3xx: Redirection
- 4xx: Client error
- 5xx: Server error
Key Points
- Understanding PATCH is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for PATCH in a backend system. Consider scalability, error handling, and production readiness.
Solution
// PATCH implementation
// Key aspects: validation, error handling, logging, testing
public class PATCH {
// Production-ready implementation
} Identify and handle edge cases for PATCH. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotency Write a testing strategy for PATCH. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injection Quiz
1. What is PATCH used for?
2. Is PATCH idempotent by default?
3. What is a common mistake when implementing PATCH?
Flashcards
Question
What does PATCH do?
Click to reveal answer
Answer
Partially updates a resource — sends only changed fields
Question
PATCH vs PUT?
Click to reveal answer
Answer
PATCH = partial update; PUT = full replacement
Question
PATCH best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1. PATCH applies partial updates to a resource
- 2. PATCH is not idempotent by default
- 3. Only send changed fields in the request body
- 4. JSON Patch (RFC 6902) and JSON Merge Patch (RFC 7396) are standards
Interview Tips
- • Know when to use PATCH vs PUT
- • Understand JSON Patch format
Cheat Sheet
PATCH
- Purpose: Partial update (only changed fields)
- Properties: Not safe, Not idempotent
- Standards: JSON Patch (RFC 6902), JSON Merge Patch (RFC 7396)
- vs PUT: PATCH=partial, PUT=full replacement