New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

distributed-locks-redis

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

distributed-locks-redis

The redis package for the storage layer for distributed locks

  • 1.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Redis Algorithm for lock guarantees

Acquiring Lock

At the start of the connection a lua script is loaded into the redis instance

local key = redis.call('GET', KEYS[1])
if (not key)
    then
        redis.call('SET',KEYS[1], ARGV[1], 'NX', 'EX', ARGV[2])
        return 'OK'
    else
        return nil
        end

it takes the key (represents the critical section) and two arguments the value (represents the lock that is trying to get hold of a critical section) and the expiration time in seconds (ttl)

Since redis is single threaded and lua scripts are atomic we don't expect race conditions in this script. Steps:

  • Get the current key value
    • If nil it means we can acquire the lock, set it's value to the current lock value and return (truthy value)
    • If not nil means someone else already has a lock on this critical section so return nil (falsy value)

Releasing Lock

Same as acquiring a lock a script is loaded for releasing a lock

local key = redis.call('GET', KEYS[1])
if (key == ARGV[1])
    then
        redis.call('DEL', KEYS[1])
    end
        return 'OK'

Again single threaded no race conditions and the script is atomic. Steps:

  • Retrieve the key from redis that represents the critical section we obtained a lock on
    • If the value of that key matches our lock (means we still hold a lock on that critical section) then we delete the key
    • else it's either null which means our ttl is over and it's too late to release the key as it was already released, or it might have another value as again our ttl is over and another lock was able to obtain it
  • In either case return truthy value

FAQs

Package last updated on 15 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc