Back to results for “Rate Limiting with Redis”
Redis › Distributed Locks with Redis

What is a distributed lock, and why would an application need one when using Redis?

Beginner

Answer

A distributed lock ensures that only one process or server, out of potentially many running the same application, can perform a specific action at a time, which is important when multiple application instances share access to a common resource and you need to prevent them from all trying to modify it simultaneously.
SET lock:inventory:product123 'owner1' NX EX 10
Real-world example An order processing system running on several servers uses a distributed lock to ensure only one server at a time updates a specific product's inventory count, preventing two servers from accidentally overselling the same limited stock.

Common follow-ups: What happens if the process holding the lock crashes before releasing it?;How is a distributed lock different from a lock used within a single application process?