Bug: Idempotency Key Not Checked Before Processing in order_service#11
Open
kushpatel2601 wants to merge 3 commits intokeploy:mainfrom
Open
Bug: Idempotency Key Not Checked Before Processing in order_service#11kushpatel2601 wants to merge 3 commits intokeploy:mainfrom
kushpatel2601 wants to merge 3 commits intokeploy:mainfrom
Conversation
Author
|
Can you please review this PR and approved. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: Check Idempotency Key Before Processing in
order_serviceTeam 072
Problem
The
POST /api/v1/ordersendpoint supported anIdempotency-Keyheader to prevent duplicate orders, but the key was only enforced at the databaseINSERTlevel via aUNIQUE KEYconstraint.This meant that on every duplicate/retry request, the full order pipeline still executed:
INSERTfailed with a unique key violationIn a production environment with retrying clients or flaky networks, this could cause inventory to be incorrectly decremented on every duplicate request before being rolled back.
Solution
Added an early database lookup for the
Idempotency-Keyat the very start ofcreate_order(), before any external service calls are made.If the key already exists in the database, the endpoint now immediately returns the original order's response — a true no-op with zero side effects.
Changes
Testing
To manually verify the fix:
Start all services:
Login to get a token:
Create an order with an idempotency key:
Send the exact same request again with the same
Idempotency-Key.✅ Expected: Second request returns the same order id and status instantly — stock is not reserved again.
✅ Verify stock count has not changed after the duplicate request.
Checklist
order_service/app.pyIdempotency-Keyare unaffected200status (idempotent success, not a new creation201)Closes #10 ;