Idempotency
What Is Idempotency?
An operation is idempotent if making the same request multiple times produces the same result as making it once.
GET /products/123 → Idempotent (read only)
PUT /products/123 → Idempotent (replace with same data)
DELETE /products/123 → Idempotent (delete is final)
POST /products → NOT Idempotent (creates new resource each time)
Why Idempotency Matters
Network Issue Scenario:
Client --POST /orders--> Server (saves order)
Client <--timeout (no response)
Client --POST /orders--> Server (creates DUPLICATE order!)
Idempotency Keys
POST /orders
Idempotency-Key: abc-123-def
Content-Type: application/json
{
"items": [...],
"total": 99.99
}
# Server checks if key exists:
# If yes → return cached response
# If no → process and store response with key
Implementation
@PostMapping("/orders")
public ResponseEntity<Order> createOrder(
@RequestHeader("Idempotency-Key") String idempotencyKey,
@RequestBody CreateOrderRequest request) {
// Check if already processed
Optional<Order> existing = orderService
.findByIdempotencyKey(idempotencyKey);
if (existing.isPresent()) {
return ResponseEntity.ok(existing.get());
}
// Process new order
Order order = orderService.create(request, idempotencyKey);
return ResponseEntity.status(201).body(order);
}
REST Best Practices
Principles
- Stateless communication
- Client-server separation
- Cacheable responses
- Uniform interface
HTTP Methods
- GET: Read (idempotent, safe)
- POST: Create
- PUT: Full update (idempotent)
- PATCH: Partial update
- DELETE: Remove (idempotent)
Status Codes
- 2xx: Success
- 3xx: Redirection
- 4xx: Client error
- 5xx: Server error
Key Points
- Understanding Idempotency in REST 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 Idempotency in REST in a backend system. Consider scalability, error handling, and production readiness.
Solution
// Idempotency in REST implementation
// Key aspects: validation, error handling, logging, testing
public class IdempotencyinREST {
// Production-ready implementation
}Identify and handle edge cases for Idempotency in REST. 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, idempotencyWrite a testing strategy for Idempotency in REST. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injectionQuiz
1. Which HTTP method is naturally idempotent?
2. What is an idempotency key?
3. What is the primary purpose of Idempotency in REST?
4. What is a common mistake when implementing Idempotency in REST?
Flashcards
Question
What makes an operation idempotent?
Click to reveal answer
Answer
Same request produces same result regardless of how many times called
Question
How to prevent duplicate POST requests?
Click to reveal answer
Answer
Use idempotency keys — server caches response by key
Question
What is Idempotency in REST?
Click to reveal answer
Answer
Idempotency in REST is a key concept in backend development.
Question
When to use Idempotency in REST?
Click to reveal answer
Answer
Use Idempotency in REST when building production systems that require reliability, scalability, and maintainability.
Question
Idempotency in REST 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.Idempotent = same result on repeated requests
- 2.GET, PUT, DELETE are naturally idempotent
- 3.POST needs idempotency keys to prevent duplicates
- 4.Server must check for existing key before processing
Interview Tips
- •Explain idempotency and why it matters for reliability
- •Know how to implement idempotency keys
Cheat Sheet
Idempotency
- Idempotent: Same result on repeated calls
- Naturally idempotent: GET, PUT, DELETE
- Not idempotent: POST (creates duplicates)
- Solution: Idempotency keys — cache response by key
- Implementation: Check key existence before processing