英文:
Multiple users buys last product at same time
问题
我正在使用MERN堆栈创建一个电子商务网站,假设有一个产品X,只剩下1个数量。
两个用户同时购买相同的产品。但问题是产品的数量只有1个。
我有一个解决方案,可以锁定产品10分钟并减少数量。
但如果用户在10分钟内不购买该产品,那么产品将被锁定。
是否有其他替代解决方案?
此外,我提出的解决方案是否是最佳的?
英文:
I am creating an ecommerce website using MERN stack and suppose there is a Product X which has 1 quantity left.
Two users are buying the same product simultaneously. But the problem is that the quantity of the product is only 1.
I have a solution to lock the product for 10mins and reduce the quantity.
But if the user doesn't buy the product then for 10mins the product will be locked.
If there any other alternative solution?
Also if the solution discussed by me is optimal or not?
答案1
得分: 2
当用户将产品添加到购物车时,不要立即减少数量,可以将产品保留10分钟。
如果用户在保留期内未完成购买,则释放保留,并在10分钟后再次提供产品。
要实施此保留系统,您可以利用服务器端和客户端逻辑的组合。在服务器端,您可以跟踪产品的保留状态并处理保留的过期。在客户端,您可以根据保留状态处理用户界面更新和交互。
通过实施保留系统,您可以为用户提供公平的机会购买有限数量的产品,同时确保如果用户放弃购买过程,产品不会永久锁定。
英文:
When a user adds the product to their cart, instead of immediately reducing the quantity, you can reserve the product for 10 minutes.
If the user does not complete the purchase within the reservation period, release the reservation and make the product available again after 10 mins.
To implement this reservation system, you can utilize a combination of server-side and client-side logic. On the server side, you can track the reservation status of the product and handle the expiration of the reservation. On the client side, you can handle the user interface updates and interaction based on the reservation status.
By implementing a reservation system, you can provide a fair opportunity for users to purchase a limited quantity of products while ensuring that the product is not locked indefinitely if a user abandons the purchase process.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论