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
:
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:
Where <YOUR_LOCKING_REDIS_URL>
is the URL of your Redis server, either locally or in the deployed environment.
redis://localhost:6379
.Redis Locking Module Provider Options#
Option | Description | Required | Default |
---|---|---|---|
| A string indicating the Redis connection URL. | Yes | - |
| An object of Redis options. Refer to the Redis API Reference for details on accepted properties. | No | - |
| A string used to prefix all locked keys with | No |
|
| 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 |
|
| A number indicating the time (in milliseconds) to wait before retrying to acquire a lock. | No |
|
| A number indicating the maximum time (in milliseconds) to wait before retrying to acquire a lock. | No |
|
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:
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
:
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:
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)