Redis Locking Module Provider

The Redis Locking Module Provider uses Redis to manage locks across multiple instances of Medusa. Redis ensures that locks are globally available, which is ideal for distributed environments.

This provider is recommended for production environments where Medusa is running in a multi-instance setup.


Register the Redis Locking Module Provider#

To register the Redis Locking Module Provider, add it to the list of providers of the Locking Module in medusa-config.ts:

medusa-config.ts
1module.exports = defineConfig({2  // ...3  modules: [4    {5      resolve: "@medusajs/medusa/locking",6      options: {7        providers: [8          {9            resolve: "@medusajs/medusa/locking-redis",10            id: "redis-lock",11            // set this if you want this provider to be used by default12            // and you have other Locking Module Providers registered.13            is_default: true,14            options: {15              redisUrl: process.env.LOCKING_REDIS_URL,16            }17          },18        ]19      }20    }21  ]22})

Environment Variables#

Make sure to add the following environment variable:

Terminal
LOCKING_REDIS_URL=<YOUR_LOCKING_REDIS_URL>

Where <YOUR_LOCKING_REDIS_URL> is the URL of your Redis server, either locally or in the deployed environment.

TipThe default Redis URL in a local environment is redis://localhost:6379.

Redis Locking Module Provider Options#

OptionDescriptionRequiredDefault

redisUrl

A string indicating the Redis connection URL.

Yes

-

redisOptions

An object of Redis options. Refer to the Redis API Reference for details on accepted properties.

No

-

namespace

A string used to prefix all locked keys with {namespace}.

No

medusa_lock:. So, all locked keys are prefixed with medusa_lock:.

waitLockingTimeout

A number indicating the default timeout (in seconds) to wait while acquiring a lock. This timeout is used when no timeout is specified when executing an asynchronous job or acquiring a lock.

No

5

defaultRetryInterval

A number indicating the time (in milliseconds) to wait before retrying to acquire a lock.

No

5

maximumRetryInterval

A number indicating the maximum time (in milliseconds) to wait before retrying to acquire a lock.

No

200


Test out the Module#

To test out the Redis Locking Module Provider, start the Medusa application:

You'll see the following message logged in the terminal:

Terminal
info:    Connection to Redis in "locking-redis" provider established

This message indicates that the Redis Locking Module Provider has successfully connected to the Redis server.

If you set the is_default flag to true in the provider options or you only registered the Redis Locking Module Provider, the Locking Module will use it by default for all locking operations.


Use Provider with Locking Module#

The Redis Locking Module Provider will be the default provider if you don't register any other providers, or if you set the is_default flag to true:

medusa-config.ts
1module.exports = defineConfig({2  // ...3  modules: [4    {5      resolve: "@medusajs/medusa/locking",6      options: {7        providers: [8          {9            resolve: "@medusajs/medusa/locking-redis",10            id: "redis-lock",11            is_default: true,12            options: {13              // ...14            }15          },16        ]17      }18    }19  ]20})

If you use the Locking Module in your customizations, the Redis Locking Module Provider will be used by default in this case. You can also explicitly use this provider by passing its identifier lp_locking-redis to the Locking Module's service methods.

For example, when using the acquire method in a workflow step:

Code
1import { Modules } from "@medusajs/framework/utils"2import { createStep } from "@medusajs/framework/workflows-sdk"3
4const step1 = createStep(5  "step-1",6  async ({}, { container }) => {7    const lockingModuleService = container.resolve(8      Modules.LOCKING9    )10
11    await lockingModuleService.acquire("prod_123", {12      provider: "lp_locking-redis",13    })14  } 15)
Was this page helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break