For context, I am building an application with a server that is written in python, hosted on AWS, and uses DynamoDB for a database.
My question pertains to the following operation: When a user purchases an item, I want to update the count of soldItems in my database. To do this, I essentially do
updateItemsSold() {
soldItems = db.get(soldItems) + 1
db.update(soldItems)
}
My question concerns the situation where two different customers buy an item at the same time. Is it possible that both db.get calls happen at the same time, leading to an error where the soldItems total only increases by one? Since I'm not using threads in my server I assume that this shouldn't be a problem, as one call of updateItemsSold() will need to finish before the next operation starts. However, I also know that in production multiple servers may be spun up to manage load. In that case, it seems like this could actually be an issue. Am I correct in thinking that? If so, how should I mitigate this? Are there some kind of locks I need to use?
ItemsSold
, which is just a list. When you need to "increment" the counter, insert a row intoItemsSold
. Then when you need a count, just useSELECT COUNT(*) FROM ItemsSold
. This design would be immune to the sort of concurrency problem you are coping with here.SET field = field + 1
.