<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens-8ssctjtpn" -->

---
title: System Design Drill Day 8: Distributed Locks with Redis...
description: Two customers can both buy the last unit of a hot SKU when checkout runs across multiple servers without coordination. This drill shows how to prevent...
canonical: https://daily.dev/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens-8ssctjtpn
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: System Design Drill Day 8: Distributed Locks with Redis and Fencing Tokens | daily.dev
og:description: Two customers can both buy the last unit of a hot SKU when checkout runs across multiple servers without coordination. This drill shows how to prevent...
og:url: https://daily.dev/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens-8ssctjtpn
og:image: https://api.daily.dev/og/posts/8sScTJTpn.png
og:image:alt: System Design Drill Day 8: Distributed Locks with Redis and Fencing Tokens
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# System Design Drill Day 8: Distributed Locks with Redis and Fencing Tokens

**[C\# Corner](https://daily.dev/sources/csharpcorner)** · 7 min read · 2 upvotes · 0 comments

## Summary

Two customers can both buy the last unit of a hot SKU when checkout runs across multiple servers without coordination. This drill shows how to prevent overselling using Redis distributed locks, leases and fencing tokens on a three-server Laravel and MySQL setup. A Redis lock (SET key value NX EX ttl, or Laravel's Cache::lock) reduces contention, but the real guarantee is a conditional database update like UPDATE ... SET stock = stock - N WHERE stock >= N. A fencing token, generated with INCR fence:sku:{id}, stops a late request whose lease expired from overwriting a newer owner's write. Payment and email are queued separately with an idempotency key so retries never double-charge, and Redis outages should fail closed with a 503 rather than skip protection.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.c-sharpcorner.com/article/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens>

## Questions this post answers

### How do fencing tokens prevent a stale Redis lock holder from corrupting data?

A fencing token is a monotonically increasing number issued on each successful lock acquire, typically via INCR fence:sku:{id}. The database stores the last accepted token and rejects any write carrying an older one, for example with a WHERE fence_token < :token clause. This stops a request whose lease already expired from overwriting a newer owner's work, even if it finishes its work late.

_daily.dev surfaces concurrency patterns like this for engineers hardening distributed checkout flows._

### Why is a Redis SET NX EX lock not enough to prevent overselling the last item in stock?

A Redis lock only reduces contention; it does not guarantee correctness on its own because its TTL can expire while a holder is still working, letting a second server acquire it and both commit. The actual guarantee comes from a conditional database update such as UPDATE ... SET stock = stock - N WHERE stock >= N, checked via affected-row count, which makes overselling impossible even if two requests slip past the lock.

_Developers designing safe checkout flows track patterns like this one on daily.dev._

### Why can't I just use DEL to release a Redis lock?

Using a plain DEL after a lock's TTL has expired can delete a different server's lock instead of your own, because another process may have already acquired it. The fix is to store a random token as the lock's value and only delete the key if it still holds that same token; Laravel's Cache::lock() already does this automatically with a random owner value checked on release.

_daily.dev helps backend engineers catch concurrency bugs like this before they ship._

## Similar posts on daily.dev

- [System Design Drill Day 3: Queues and Async Jobs for Checkout](https://daily.dev/posts/system-design-drill-day-3-queues-and-async-jobs-for-checkout-f7kvhxu3z) · C\# Corner · 0 upvotes · 0 comments
- [Why your distributed lock is probably broken](https://daily.dev/posts/why-your-distributed-lock-is-probably-broken-0b0zoqpp5) · Medium · 0 upvotes · 0 comments
- [System Design Drill Day 7: Rate Limiting APIs, Logins and OTPs with Redis](https://daily.dev/posts/system-design-drill-day-7-rate-limiting-apis-logins-and-otps-with-redis-vwvetnmnk) · C\# Corner · 3 upvotes · 1 comments
- [We replaced Redis with MySQL for inventory reservations—and it scaled \(2026\)](https://daily.dev/posts/we-replaced-redis-with-mysql-for-inventory-reservations-and-it-scaled-2026--7uecbcooa) · Hacker News · 1 upvotes · 0 comments
- [Fencing Tokens and Generation Clock in .NET: Stop Zombie Leaders From Writing - Chris Woody Woodruff](https://daily.dev/posts/fencing-tokens-and-generation-clock-in-net-stop-zombie-leaders-from-writing---chris-woody-woodruff-inrdihhhf) · We Are .NET · 0 upvotes · 0 comments

---

Tags: [#career](https://daily.dev/tags/career), [#laravel](https://daily.dev/tags/laravel), [#redis](https://daily.dev/tags/redis)

[View this post on daily.dev](https://daily.dev/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens-8ssctjtpn)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"System Design Drill Day 8: Distributed Locks with Redis and Fencing Tokens","url":"https://daily.dev/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens-8ssctjtpn","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens-8ssctjtpn"},"datePublished":"2026-10-26T05:48:59.787Z","dateModified":"2026-10-26T05:49:37.332Z","description":"Two customers can both buy the last unit of a hot SKU when checkout runs across multiple servers without coordination. This drill shows how to prevent...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/b6ea9ebfca1f89ca745f84ed19800ca7?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/b6ea9ebfca1f89ca745f84ed19800ca7?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"C# Corner","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"C# Corner","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/ada2d584df6241748fc4e71878dc70a3","url":"https://daily.dev/sources/csharpcorner"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens-8ssctjtpn","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"career,laravel,redis","timeRequired":"PT7M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"C# Corner","item":"https://daily.dev/sources/csharpcorner"},{"@type":"ListItem","position":3,"name":"System Design Drill Day 8: Distributed Locks with Redis and Fencing Tokens"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens-8ssctjtpn#faq","mainEntity":[{"@type":"Question","name":"How do fencing tokens prevent a stale Redis lock holder from corrupting data?","acceptedAnswer":{"@type":"Answer","text":"A fencing token is a monotonically increasing number issued on each successful lock acquire, typically via INCR fence:sku:{id}. The database stores the last accepted token and rejects any write carrying an older one, for example with a WHERE fence_token \u003c :token clause. This stops a request whose lease already expired from overwriting a newer owner's work, even if it finishes its work late. daily.dev surfaces concurrency patterns like this for engineers hardening distributed checkout flows."}},{"@type":"Question","name":"Why is a Redis SET NX EX lock not enough to prevent overselling the last item in stock?","acceptedAnswer":{"@type":"Answer","text":"A Redis lock only reduces contention; it does not guarantee correctness on its own because its TTL can expire while a holder is still working, letting a second server acquire it and both commit. The actual guarantee comes from a conditional database update such as UPDATE ... SET stock = stock - N WHERE stock \u003e= N, checked via affected-row count, which makes overselling impossible even if two requests slip past the lock. Developers designing safe checkout flows track patterns like this one on daily.dev."}},{"@type":"Question","name":"Why can't I just use DEL to release a Redis lock?","acceptedAnswer":{"@type":"Answer","text":"Using a plain DEL after a lock's TTL has expired can delete a different server's lock instead of your own, because another process may have already acquired it. The fix is to store a random token as the lock's value and only delete the key if it still holds that same token; Laravel's Cache::lock() already does this automatically with a random owner value checked on release. daily.dev helps backend engineers catch concurrency bugs like this before they ship."}}]}
```

