1 import { Table, Prerequisites } from "docs-ui"
2
3 export const metadata = {
4 title: `Redis Locking Module Provider`,
5 }
6
7 # {metadata.title}
8
9 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.
10
11 This provider is recommended for production environments where Medusa is running in a multi-instance setup.
12
13 <Note title="Using Cloud?">
14
15 Our Cloud offering automatically provisions a Redis instance and configures the Redis Locking Module Provider for you. Learn more in the [Redis](!cloud!/redis) Cloud documentation.
16
17 </Note>
18
19 ---
20
21 ## Register the Redis Locking Module Provider
22
23 <Prerequisites
24 items={[
25 {
26 text: "A redis server set up locally or a database in your deployed application.",
27 link: "https://redis.io/download",
28 }
29 ]}
30 />
31
32 To register the Redis Locking Module Provider, add it to the list of providers of the Locking Module in `medusa-config.ts`:
33
34 ```ts title="medusa-config.ts"
35 module.exports = defineConfig({
36 // ...
37 modules: [
38 {
39 resolve: "@medusajs/medusa/locking",
40 options: {
41 providers: [
42 {
43 resolve: "@medusajs/medusa/locking-redis",
44 id: "locking-redis",
45 // set this if you want this provider to be used by default
46 // and you have other Locking Module Providers registered.
47 is_default: true,
48 options: {
49 redisUrl: process.env.LOCKING_REDIS_URL,
50 },
51 },
52 ],
53 },
54 },
55 ],
56 })
57 ```
58
59 ### Environment Variables
60
61 Make sure to add the following environment variable:
62
63 ```bash
64 LOCKING_REDIS_URL=<YOUR_LOCKING_REDIS_URL>
65 ```
66
67 Where `<YOUR_LOCKING_REDIS_URL>` is the URL of your Redis server, either locally or in the deployed environment.
68
69 <Note title="Tip">
70
71 The default Redis URL in a local environment is `redis://localhost:6379`.
72
73 </Note>
74
75 ### Redis Locking Module Provider Options
76
77 <Table>
78 <Table.Header>
79 <Table.Row>
80 <Table.HeaderCell>
81 Option
82 </Table.HeaderCell>
83 <Table.HeaderCell>
84 Description
85 </Table.HeaderCell>
86 <Table.HeaderCell>
87 Required
88 </Table.HeaderCell>
89 <Table.HeaderCell>
90 Default
91 </Table.HeaderCell>
92 </Table.Row>
93 </Table.Header>
94 <Table.Body>
95 <Table.Row>
96 <Table.Cell>
97
98 `redisUrl`
99
100 </Table.Cell>
101 <Table.Cell>
102
103 A string indicating the Redis connection URL.
104
105 </Table.Cell>
106 <Table.Cell>
107
108 Yes
109
110 </Table.Cell>
111 <Table.Cell>
112
113 \-
114
115 </Table.Cell>
116 </Table.Row>
117 <Table.Row>
118 <Table.Cell>
119
120 `redisOptions`
121
122 </Table.Cell>
123 <Table.Cell>
124
125 An object of Redis options. Refer to the [Redis API Reference](https://redis.github.io/ioredis/index.html#RedisOptions) for details on accepted properties.
126
127 </Table.Cell>
128 <Table.Cell>
129
130 No
131
132 </Table.Cell>
133 <Table.Cell>
134
135 \-
136
137 </Table.Cell>
138 </Table.Row>
139 <Table.Row>
140 <Table.Cell>
141
142 `namespace`
143
144 </Table.Cell>
145 <Table.Cell>
146
147 A string used to prefix all locked keys with `{namespace}`.
148
149 </Table.Cell>
150 <Table.Cell>
151
152 No
153
154 </Table.Cell>
155 <Table.Cell>
156
157 `medusa_lock:`. So, all locked keys are prefixed with `medusa_lock:`.
158
159 </Table.Cell>
160 </Table.Row>
161 <Table.Row>
162 <Table.Cell>
163
164 `waitLockingTimeout`
165
166 </Table.Cell>
167 <Table.Cell>
168
169 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.
170
171 </Table.Cell>
172 <Table.Cell>
173
174 No
175
176 </Table.Cell>
177 <Table.Cell>
178
179 `5`
180
181 </Table.Cell>
182 </Table.Row>
183 <Table.Row>
184 <Table.Cell>
185
186 `defaultRetryInterval`
187
188 </Table.Cell>
189 <Table.Cell>
190
191 A number indicating the time (in milliseconds) to wait before retrying to acquire a lock.
192
193 </Table.Cell>
194 <Table.Cell>
195
196 No
197
198 </Table.Cell>
199 <Table.Cell>
200
201 `20` (changed from `5` in v2.13.6+)
202
203 </Table.Cell>
204 </Table.Row>
205 <Table.Row>
206 <Table.Cell>
207
208 `maximumRetryInterval`
209
210 </Table.Cell>
211 <Table.Cell>
212
213 A number indicating the maximum time (in milliseconds) to wait before retrying to acquire a lock.
214
215 </Table.Cell>
216 <Table.Cell>
217
218 No
219
220 </Table.Cell>
221 <Table.Cell>
222
223 `1000` (changed from `200` in v2.13.6+)
224
225 </Table.Cell>
226 </Table.Row>
227 <Table.Row>
228 <Table.Cell>
229
230 `backoffFactor` (v2.13.6+)
231
232 </Table.Cell>
233 <Table.Cell>
234
235 A number indicating the multiplier used for exponential backoff when retrying to acquire a lock. Each retry will wait longer than the previous one by this factor.
236
237 </Table.Cell>
238 <Table.Cell>
239
240 No
241
242 </Table.Cell>
243 <Table.Cell>
244
245 `2`
246
247 </Table.Cell>
248 </Table.Row>
249 </Table.Body>
250 </Table>
251
252 ---
253
254 ## Test out the Module
255
256 To test out the Redis Locking Module Provider, start the Medusa application:
257
258 ```bash npm2yarn
259 npm run dev
260 ```
261
262 You'll see the following message logged in the terminal:
263
264 ```bash
265 info: Connection to Redis in "locking-redis" provider established
266 ```
267
268 This message indicates that the Redis Locking Module Provider has successfully connected to the Redis server.
269
270 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.
271
272 ---
273
274 ## Use Provider with Locking Module
275
276 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`:
277
278 export const defaultHighlights = [
279 ["11", "is_default"]
280 ]
281
282 ```ts title="medusa-config.ts" highlights={defaultHighlights}
283 module.exports = defineConfig({
284 // ...
285 modules: [
286 {
287 resolve: "@medusajs/medusa/locking",
288 options: {
289 providers: [
290 {
291 resolve: "@medusajs/medusa/locking-redis",
292 id: "locking-redis",
293 is_default: true,
294 options: {
295 // ...
296 },
297 },
298 ],
299 },
300 },
301 ],
302 })
303 ```
304
305 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.
306
307 For example, when using the `acquire` method in a [workflow step](!docs!/learn/fundamentals/workflows):
308
309 ```ts
310 import { Modules } from "@medusajs/framework/utils"
311 import { createStep } from "@medusajs/framework/workflows-sdk"
312
313 const step1 = createStep(
314 "step-1",
315 async ({}, { container }) => {
316 const lockingModuleService = container.resolve(
317 Modules.LOCKING
318 )
319
320 await lockingModuleService.acquire("prod_123", {
321 provider: "lp_locking-redis",
322 })
323 }
324 )
325 ```