Quick revision
The short answer
tryLock(0, 30, SECONDS) gives the lock a fixed 30-second lease, which turns off Redisson's watchdog. When the carrier API makes a job run 45 seconds, the lock expires at second 30, another instance takes it and ships the same order again, and the unlock error is the first job trying to release a lock it no longer owns. The quick fix is to drop the explicit lease and add timeouts to the carrier call; the real fix is to make shipping idempotent so a second run cannot create a second shipment.
What to remember
In this answer, we saw that an explicit 30-second lease disables Redisson's watchdog, so a job slowed to 45 seconds by the carrier loses its lock at second 30 and a second instance ships the order again. The unlock error is the first job trying to release a lock now owned by someone else. We fixed it in two layers: drop the explicit lease and bound the carrier call with timeouts, and make shipping idempotent with a unique constraint and a carrier idempotency key. Metrics on lock hold time and lost locks make the next near miss visible.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
tryLock(0, 30, SECONDS) gives the lock a fixed 30-second lease, which turns off Redisson's watchdog. When the carrier API makes a job run 45 seconds, the lock expires at second 30, another instance takes it and ships the same order again, and the unlock error is the first job trying to release a lock it no longer owns. The quick fix is to drop the explicit lease and add timeouts to the carrier call; the real fix is to make shipping idempotent so a second run cannot create a second shipment.
Why this question is asked
Double shipments are expensive and embarrassing, and the code looks correct at a glance: there is a lock, it is released in finally, and it works in testing because tests are fast. Interviewers use this scenario to see whether we can connect a strange log line to a timing bug and whether we know the limits of a lease-based lock. In this answer, we will recall what the lease does, reconstruct the timeline from the log, apply the short-term fix, then make the operation safe even when locking fails, and add the metrics that would have warned us.
1A Quick Recap of Leases in Redisson
A Redisson lock is a Redis key with a TTL. The TTL is the lease: the time after which Redis deletes the key even if nobody unlocks it. Leases exist so that a crashed instance cannot hold a lock forever.
How long the lease is depends on how we call it. tryLock(waitTime, unit) without a lease uses the watchdog, which sets a 30-second lease and renews it every 10 seconds while the holder is alive. tryLock(waitTime, leaseTime, unit) with an explicit lease, which is what this service calls, sets the TTL once and never renews it. The arguments here are a wait of 0 (fail immediately if busy) and a lease of 30 seconds.
2Reconstructing the Timeline
Usually the carrier API answers in a few seconds and everything is fine. Occasionally it is slow, and a job takes 45 seconds. Here is what happens to order 1234 in that case.
7 messages in order. Play them, or step through them one at a time.
Two shipments, and the refused unlock is the only trace in the logs.
The unlock error is the evidence. When A finally calls unlock(), the key exists again but belongs to B, whose client and thread id are stored in it. Redisson compares the owner and refuses, which is correct behaviour: it will not release a lock someone else holds. Every occurrence of that log line marks a job that ran past its lease, and therefore a window in which two instances were working on the same order. Counting those lines over the last month tells us roughly how many orders were at risk.
3The Short-Term Fix: Let the Watchdog Renew, and Bound the Work
The quickest improvement is to remove the explicit lease, calling tryLock(0, TimeUnit.SECONDS), so the watchdog renews the lease every 10 seconds for as long as the job runs. A 45-second job then keeps its lock to the end.
But the watchdog alone would let a stuck job hold the lock forever. So we also put timeouts on the carrier call, for example a 5-second connect timeout and a 20-second read timeout in the HTTP client, and an overall deadline for the job. Now a job cannot run indefinitely, and the lock is held for as long as the job is genuinely running.
The isHeldByCurrentThread() check keeps the error out of the logs if the lock was lost anyway, but we should still count those cases as a metric rather than hide them.
4The Real Fix: Make Shipping Idempotent
Even with the watchdog, the lock is a lease. A long garbage-collection pause, a network partition between our instance and Redis, or a Redis failover that loses the key can all let a second instance in while the first still thinks it holds the lock. If a duplicate run costs a shipment, the protection must live in the data, not in the lock.
Idempotent means a second run has no additional effect. In the database, we add a unique constraint on the order's shipment, for example a shipments table with UNIQUE (order_id). The job first inserts a row with status PENDING; a second instance's insert fails on the constraint, and it stops. Then we call the carrier with an idempotency key, such as the order id, if the carrier API supports one, so even a retried call after a timeout returns the existing shipment instead of creating a new one.
With that in place, the lock becomes an efficiency measure, saving duplicate work, rather than the only thing standing between us and a double shipment.
5Metrics That Would Have Warned Us
We record how long each job holds the lock and alert when the 99th percentile approaches the lease (or, with the watchdog, approaches the job deadline). We also count lost locks, the unlock failures or isHeldByCurrentThread() returning false at the end, because each one is a near miss. And the carrier call's latency, broken down by carrier, would have shown the slow tail that triggered all of this.
6Follow-up questions
Is the watchdog safe if the JVM pauses for a minute in GC?
No. The watchdog is a timer inside the same JVM, so a stop-the-world pause stops it too. If the pause lasts longer than the 30-second lease, the key expires, another instance acquires the lock, and when our thread resumes it carries on believing it still holds the lock. No lease-based lock can prevent this, which is why correctness must come from idempotency or fencing tokens checked by the resource.
Why not just set a much longer lease, like 10 minutes?
It reduces the chance of expiry mid-job but makes crashes worse: a killed instance would block that order for 10 minutes. It also does not fix pauses or failovers. The watchdog with a short lease plus bounded work gives fast recovery after a crash, and idempotency covers the rest.
Should the second instance have retried at all?
Retries are fine if the operation is idempotent. The bug was not that a second instance ran, but that running twice had an effect. Once a duplicate is harmless, retries become a safety feature rather than a risk.
7Conclusion
In this answer, we saw that an explicit 30-second lease disables Redisson's watchdog, so a job slowed to 45 seconds by the carrier loses its lock at second 30 and a second instance ships the order again. The unlock error is the first job trying to release a lock now owned by someone else. We fixed it in two layers: drop the explicit lease and bound the carrier call with timeouts, and make shipping idempotent with a unique constraint and a carrier idempotency key. Metrics on lock hold time and lost locks make the next near miss visible.
Open with
The explicit 30-second lease in tryLock(0, 30, SECONDS) is the cause. With a lease time Redisson doesn't start its watchdog, so the key simply expires after 30 seconds.
Then, if they want more
When the carrier API is slow and a job takes 45 seconds, the lock disappears at second 30, another instance acquires it and ships the same order. The log line proves it: when the first job finishes and calls unlock, the lock now belongs to the second instance, so Redisson refuses with 'not locked by current thread'.
Each of those lines marks a window where two instances worked on the same order. Short term, I'd remove the lease so the watchdog renews every 10 seconds while the job runs, and put connect and read timeouts plus a job deadline on the carrier call, so a job can't hold the lock forever.
But the real fix is idempotency, because any lease-based lock can be lost to a GC pause, a partition or a failover. I'd add a unique constraint on the shipment per order, insert that row before calling the carrier, and send the order id as an idempotency key to the carrier, so a second run can't create a second shipment.
Then I'd add metrics on lock hold time and lost locks and alert when hold times approach the lease.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Redis documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain what is happening and how you would fix it properly.": https://scrivio-interview-library.netlify.app/databases/redis-patterns/c5ed5d0a5c/