Sketech #9 Idempotency, Circuit Breakers and REST APIs to Prevent Duplicate Requests, Avoid System Failures and Enhance API Performance
3 Must-Read Posts You Loved Last Month
Hi there, and welcome to this week’s Sketech, crafted to sharpen your visual thinking skills as a software developer.
Here’s what’s in store:
Idempotency to Prevent Duplicate Requests — A fresh redesign to make it even better🔥
Circuit Breaker in Action — Quick recap for you
REST API Hacks for Quick Wins — Revisiting some favorites
Idempotency to Prevent Duplicate Requests
1/ What is Idempotency?
Idempotency is a fundamental concept in API design. It ensures that multiple identical requests produce the same outcome as a single request, preventing unintended side effects.
For instance, sending a "create order" request several times will result in only one order being created.
2/ Idempotency in HTTP Methods
In REST APIs, some HTTP methods are idempotent by design:
GET
,PUT
, andDELETE
are naturally idempotent.POST
requests, however, are not inherently idempotent. Without proper handling, sending duplicatePOST
requests can create multiple records.
3/ How to Implement Idempotency: The Idempotency Key
To achieve idempotency, use an idempotency key:
It’s a unique identifier generated by the client for each request.
The key is usually sent as a header in the request.
When the server receives the request, it uses this key to track and register it.
4/ Handling Duplicate Requests
If the server receives a duplicate request with the same idempotency key, it recognizes it as a retry:
The server can return the same response as the initial request.
Alternatively, it can ignore the duplicate to avoid creating multiple resources.
5/ Why Idempotency Matters
Idempotency ensures data consistency and prevents duplication in critical operations like payments, orders or resource creation. By incorporating idempotency into API design, developers enhance the reliability and robustness of their APIs.
Circuit Breaker in Action
3 Circuit Breaker Insights Every Dev Needs to Know
1/
Closed State Isn't Just "Business as Usual" ⭢ I used to think this meant everything was fine, but really, it's about actively monitoring. Don’t overlook those "small" failures—they’re the early signs of bigger issues.
2/
Open State Is More Than Just Blocking Requests ⭢ Early on, I thought blocking was enough to protect a system. Now I know it’s about managing resources intelligently. By stopping repeated failures, we’re giving the system room to recover.
3/
Half-Open State Isn't a Full Reset ⭢ It’s tempting to treat this state as a green light, but it's a test phase. Allowing a few requests checks if the service is stable without risking a full load.
My advice: Don’t assume resilience is automatic. The Circuit Breaker Pattern is a proactive measure, not a “set it and forget it” solution.
Building resilient systems requires attention to these details—find what works best in your setup.
Take a look at the LinkedIn post and discover the gold hidden in the comments.
REST API Hacks for Quick Wins
1/ Versioning
Define it early.
Path (/v1/users) is simple; headers (Accept: v1) or query parameters (/users?version=1) add flexibility but complexity.
2/ Idempotency
Idempotent Methods:
GET: Retrieves data without changes, same result on repeat calls.
PUT: Updates or creates a resource, result remains consistent.
DELETE: Removes a resource, no change after first call.
Non-Idempotent Methods:
POST: Creates a new resource, generates different outcomes.
PATCH: Partially updates a resource, result depends on state.
For a detailed explanation, check out previous post: Idempotency to Prevent Duplicate Requests.
3/ HTTP Status Codes
Correct usage is key:
200 (OK), 201 (Created).
400 (Bad Request), 401 (Unauthorized), 404 (Not Found).
500 (Server Error), 503 (Service Unavailable).
4/ Domain Model Driven
Align endpoints with business logic, e.g., /orders for an order system.
5/ Authentication
Use OAuth2 or JWT.
Always transmit over HTTPS.
6/ HTTP Methods
Standard use:
GET: Fetch data.
POST: Create entries.
PUT: Full updates.
DELETE: Remove resources.
PATCH: Partial updates
7/ Semantic Paths
Describe the action, e.g., /users/{id}/orders.
8/ Query Languages
Pagination: Use offset-limit or page-size.
Filtering: Example: /products?category=books.
Sorting: Example: /users?sort=name.
These tips cover some of the key aspects of REST APIs, but there’s always room for more. What other tips would you include?
That’s all for today! Hope you found something inspiring to take into your week. Until next time, take care and keep creating!
Nina
Sketech Newsletter Crafted to make concepts unforgettable 🩵