How it works...
Overselling is often a headache for seckilling application design. The reason why the overselling happens is that there was a race condition at the time you retrieved the counter; the number might be valid for the business scenario, but before you decreased the counter, the counter may have been modified by other requests. In our example, we use a Redis transaction to avoid such a situation.
First, we call a WATCH command on the key to set a flag indicating if the key has been changed before the EXEC command; if so, we just discard the whole transaction. Then, we get the value of the counter.
Second, we start a transaction by calling the MULTI command, and if the counter is an invalid value, the transaction is stopped directly ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access