Redis Distributed Mutex Lock with Automatic Expiry & Lease Renewal
30 Production Challenge Scenarios • Real-Time Automated Quality Scoring

Interactive Prompt Engineering Lab

Test and score your prompts against real-world enterprise scenarios. Get immediate 4-axis quality telemetry, negative guardrail verification, and golden reference prompts.

Read Syllabus
1. Authoritative Persona
Expert role + zero fluff
25 pts
2. Deterministic Schema
XML tags + JSON / keys
25 pts
3. Negative Guardrails
NEVER / fallback rules
25 pts
4. Token Efficiency
150–1,800 chars concise
25 pts
Web & System ArchitectureStaff

Redis Distributed Mutex Lock with Automatic Expiry & Lease Renewal

Design a production Redis distributed lock algorithm (Redlock pattern) in TypeScript with unique token ownership, automatic lease renewal (watchdog timer), and safe atomic Lua script release.

Target Input Context / Untrusted Data:
Resource: inventory:sku_9842_lock
TTL: 5000ms
Concurrency: 50 concurrent worker nodes competing for lock
Failure Mode: Crash of holder node must not permanently deadlock the cluster.
Quality Requirements & Guardrails:
Define a Principal Distributed Systems Engineer persona.
Use SET resource_key random_token NX PX 5000 for atomic acquisition.
Release the lock ONLY via Lua script verifying that the token matches the owner to prevent deleting another node's lock.
Implement an active watchdog interval extending TTL for long-running batch jobs.
Your System Prompt Draft
94 chars~24 tokens
Golden Reference Architecture (100% Quality Benchmark)
<system_instructions>
You are a Principal Distributed Systems Architect.
Design a production-grade Distributed Mutex Lock in TypeScript using Redis (ioredis).

DISTRIBUTED LOCKING CONSTRAINTS:
1. ATOMIC ACQUISITION: Use `SET lock_key unique_owner_id NX PX 5000`. Never use separate EXISTS and SET commands.
2. ATOMIC RELEASE VIA LUA: Never issue a plain `DEL lock_key`. A delayed process could delete a lock acquired by a subsequent worker. Use an atomic Lua script that compares `redis.call('get', KEYS[1]) == ARGV[1]` before deleting.
3. WATCHDOG LEASE RENEWAL: Implement a background heartbeat timer that extends the lock TTL periodically while the critical section is still executing.
4. Output clean, documented TypeScript code inside <redis_lock> tags.
</system_instructions>