如何通过Redis CLI释放崩溃Java实例持有的Redlock锁?
Hey there,
Absolutely, you can manually release the Redlock held by a crashed instance using Redis CLI—but you need to do it carefully to avoid introducing new consistency issues. Let’s break this down step by step, plus cover critical caveats to watch for:
Step 1: Identify the Lock Details
Redlock works by acquiring locks across multiple Redis nodes, and each lock entry includes a unique random value (like a UUID) that identifies the instance holding the lock. First, you’ll need to:
- Locate the lock key for your shared database (e.g., let’s say it’s
shared_db_resource_lock). - On each Redis node in your Redlock cluster, run this command to get the lock’s value (the unique identifier of the crashed instance):
Save this value—you’ll need it to safely release the lock.GET shared_db_resource_lock
Step 2: Safely Release the Lock with an Atomic Lua Script
Never use a plain DEL command to remove the lock! If you do, you risk deleting a lock that was already acquired by another instance after the crashed one’s lock expired. Instead, use an atomic Lua script to verify the lock belongs to the crashed instance before deleting it.
Run this script on every Redis node in your Redlock cluster:
if redis.call("GET", KEYS[1]) == ARGV[1] then return redis.call("DEL", KEYS[1]) else return 0 end
Execute it via CLI with:
EVAL "if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('DEL', KEYS[1]) else return 0 end" 1 shared_db_resource_lock <unique_lock_value_from_step_1>
This ensures you only delete the lock if it still holds the crashed instance’s unique identifier.
Critical Caveats & Next Steps
- Why are other instances blocking? Your current setup’s stability issues (and blocking behavior) suggest a problem with your Redlock implementation. Normally, if an instance crashes, the lock should expire after the configured timeout, allowing other instances to acquire it. Check if:
- Your lock timeout is set too long (trapping instances in wait loops).
- Your retry logic for acquiring locks doesn’t have a maximum wait limit.
- Your app isn’t handling Redis node failures properly (Redlock requires quorum to acquire locks).
- Only release locks you’re certain belong to the crashed instance. If you can’t retrieve the unique lock value, don’t guess—you could break the shared database’s integrity by deleting a valid lock held by another instance.
- Automate lock cleanup long-term. Manual CLI fixes are a band-aid. Consider adding:
- Health checks for your app instances to detect crashes and trigger automated lock release.
- Proper lock expiration handling in your Redlock client code (never hold locks longer than necessary).
内容的提问来源于stack exchange,提问作者Gayan Viranka

