APPENDIX · P12 · REDIS · REVISIONS · FOR SLIDE 16
← Back to Slide 16
P12. Impotence and Blockage: SET NX
# повтор события и замок
DEL lesson03:match:777:applied lesson03:lb lesson03:lock:mm:1042
SET lesson03:match:777:applied 1 NX EX 86400
ZINCRBY lesson03:lb 25 player:1042
SET lesson03:match:777:applied 1 NX EX 86400
ZSCORE lesson03:lb player:1042
SET lesson03:lock:mm:1042 worker-1 NX PX 5000
SET lesson03:lock:mm:1042 worker-2 NX PX 5000
GET lesson03:lock:mm:1042
What the result should be
(integer) 0
OK // apply the 777 match
"25"
(nil) // repeat: do not apply
"25" // points were not doubled
OK // lock held by worker-1
(nil) // worker-2 is waiting
"worker-1"
Event tag
Not atomic
TTL Shutter
SET NX displays only the take without any glitches. The marker duration must cover the entire take; if a marker is lost, a retake will be triggered.
The +ZINCRBY tag here is not atomic: a failure will leave the tag without points. We need either a single Lua script or PG deduplication.
PX sets a time limit, but after it expires, a second worker can enter. The lock can only be removed atomically using a token.
This is a demonstration of a trap, not a permanent condition. When a tag expires or is lost, points can be credited again. For money, use durable deduplication in PostgreSQL.