aws-samples / aws-serverless-shopping-cart

Serverless Shopping Cart is a sample implementation of a serverless shopping cart for an e-commerce website.
MIT No Attribution
326 stars 417 forks source link

Idempotency Concern in CartDBStreamHandler Function #67

Open NullPointer4096 opened 1 year ago

NullPointer4096 commented 1 year ago

Description: I would like to kindly bring attention to a potential issue with the CartDBStreamHandler function, which is used to change the quantity of items in the shopping cart stored as records in DynamoDB. The function operates by iterating through each pair of (item, quantity delta) in quantity_change_counter and using DynamoDB’s UpdateExpression : "ADD … to change the absolute quantity stored in the table. However, this method is not idempotent. Suppose when the function is invoked for the first time, x is added to the absolute quantity of itemA. If the function crashes and retries, x more items will be added to the record. This is undesirable, as it duplicates the user's action of adding items to the cart if untimely retries ever happen.

Suggested Fix: To address this issue, please consider adding a LastRequestId field to the record that stores the most recent lambda's request id, which is constant across retries. Then, use a condition expression along with the update expression; specifically, apply the quantity change and update the LastRequestId field only if context.aws_request_id != item.LastRequestId. This approach should help ensure idempotency and improve the reliability of the at-least-once executed CartDBStreamHandler function.

Closing Remarks: I appreciate your attention to this matter and hope that my suggestion proves helpful in enhancing the reliability of the CartDBStreamHandler function. Thank you for considering this feedback, and please don't hesitate to reach out if you have any questions or concerns.