---
title: "Comment by melonape on \"System Design Drill Day 8: Distributed Locks with Redis and Fencing Tokens\" | Hashnode"
description: "For the last-unit case, the conditional update does the actual protecting: UPDATE ... SET qty = qty - 1 WHERE sku = ? AND qty &gt;= 1, then check affected rows."
url: "https://hashnode.com/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens/6adec4d52d0de51d89be8ab0/comment/6ae087f4a03d5cfc8f99a8ae"
---

[Discussion](https://hashnode.com/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens/6adec4d52d0de51d89be8ab0)

Comment [System Design Drill Day 8: Distributed Locks with Redis and Fencing Tokens](https://hashnode.com/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens/6adec4d52d0de51d89be8ab0)

[melonape](https://hashnode.com/@melonape)

[Oct 27](https://hashnode.com/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens/6adec4d52d0de51d89be8ab0/comment/6ae087f4a03d5cfc8f99a8ae)

For the last-unit case, the conditional update does the actual protecting: UPDATE ... SET qty = qty - 1 WHERE sku = ? AND qty >= 1, then check affected rows. Two servers can race all day and only one gets a row back.

The lock earns its keep one step earlier, so you don't charge the card and only then find out the stock is gone.

[Anish Ansari](https://hashnode.com/@anish78)

[Oct 27](https://hashnode.com/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens/6adec4d52d0de51d89be8ab0/comment/6ae087f4a03d5cfc8f99a8ae#reply-6ae0a7f24be03282da42af65)

Spot on, the conditional UPDATE is what actually stops the oversell. The lock just saves you from charging a card for stock that's already gone.
