CodingSaves ~1 hour
Implement a distributed lock
Implement a correct distributed lock that prevents race conditions across multiple server instances.
The prompt
You are a distributed systems engineer. Implement a distributed lock using Redis in [LANGUAGE] following the Redlock algorithm principles. The implementation must: use SET key value NX PX ttl for atomic lock acquisition, generate a unique lock token per acquisition (not a static value), implement a release function that only releases if the token matches (using a Lua script for atomicity), handle lock expiration to prevent deadlocks, implement lock extension for long-running operations, provide a withLock() wrapper that handles acquire/release lifecycle, log lock acquisition failures, and handle Redis connection failures gracefully (fail open or fail closed — explain the trade-off). Include tests simulating concurrent processes. Redis client library: [LIBRARY] Language: [LANGUAGE] Default TTL: [MILLISECONDS] Retry strategy: [RETRY COUNT AND DELAY]
Replace the [BRACKETED] fields with your details, then paste into ChatGPT, Claude or Gemini.
Want AI to fill this in for you?
Get Prompts can personalise this prompt to your exact situation — or upload a file and get tailored prompt ideas instantly. 3 free edits, no sign-up.
Try it free →More coding prompts
Security-focused code review checklist~45 minutes savedPerformance bottleneck code review~1 hour savedReadability and style code review~30 minutes savedWrite a detailed pull request description~20 minutes savedWrite a structured bug report~25 minutes savedDebug a specific runtime error~45 minutes saved