Urgent.News

What's breaking now, across thousands of outlets.

Tech

Distributed Locks

One-liner: A distributed lock ensures that only one node in a cluster can perform a critical operation at a time — preventing race conditions across services. ❓ Why Do You Need Distributed Locks? In a single-server world, a mutex or semaphore handles concurrency. But in distributed systems: Multiple service replicas run simultaneously They all share the same database or resource Without…

Distributed locks are essential in multi-node systems to ensure only one node performs a critical operation at a time, preventing race conditions. In traditional single-server environments, mutexes or semaphores handle concurrency, but distributed systems pose unique challenges. Multiple service replicas run simultaneously, sharing the same database or resource. Without coordination, two instances might process the same job, double-charge a user, or corrupt shared state.

A classic example is a flash sale with 100 units in stock and 10,000 concurrent requests. Without a lock, overselling occurs. Redis Distributed Lock, or Redlock, is the most common approach, utilizing Redis SET NX EX commands. SET sets a lock key with a unique token and a 10-second expiry time (TTL), ensuring auto-expiry and preventing lock loss due to slow processes. If the lock is acquired, the service proceeds with work. If not, it retries or fails.

Redis's atomic nature and TTL prevent deadlocks from crashed processes. However, clock drift can still cause issues. A unique token is essential to avoid releasing a lock acquired by someone else after TTL expiry. The release lock step involves a Lua script that checks if the stored token matches the current token, deleting the key if they match and returning an error otherwise.

However, distributed locks come with trade-offs. Redis is a single point of failure, mitigated by using Redlock for high availability. Clock drift can cause TTL-based expiry issues, while Redlock remains controversial due to debates between Martin Kleppmann and Antirez. Distributed locks are not suitable for long-held locks; instead, consider using queues for idempotent operations, or implement strong consistency with ZooKeeper and fencing tokens in cases requiring it.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Second brain ที่ไม่ใช้ vector DB, บทเรียน 736 ดาวจาก PM Brain

Second brain ที่ไม่ใช้ vector DB, บทเรียน 736 ดาวจาก PM Brain โดย Nokka (นก-กา) | 12 กันยายน 2026 บทความนี้เขียนโดย AI (deepseek-v4.1-flash) ผ่าน Hermes Agent ตรวจสอบและเรียบเรียงโดย Nokka เมื่อวานนี้…

I tested 31 MCP servers for contract compliance. Only 3% passed.

MCP has outputSchema so agents can validate tool results. But does the schema actually reject a wrong answer? I built mcp-drill -- a fault-injection harness that speaks MCP -- and scanned 31 popular…

  • Only 3% of 31 MCP servers pass contract compliance test
  • 56% of servers lack any schema declaration
  • 42% of servers accept invalid results due to improper schema

More from Sunday 13 September →