Scale out Medusa into multiple instances: Events are likely duplicated if we are unlucky with timing #5971
Replies: 2 comments 3 replies
-
Hey @khanh-to-niteco I added a You could always add that into this enqueuer function to ensure only a single instance is running this at a time.
|
Beta Was this translation helpful? Give feedback.
-
Hi @dwene , It seems that RedLock is not safe: https://martin.kleppmann.com/2016/02/08/how-to-do-distributed-locking.html, but in our case, if we need only efficiency instead of correctness, it may work if it can help to avoid running concurrent batch jobs. And ensuring only single instance running is not an optimal solution, would it be better if we can partition the events for the instances so they are less likely to step on each other toes and execute the events more quickly? |
Beta Was this translation helpful? Give feedback.
-
Hi Medusa team,
We are using Medusajs 1.17.4 and planning to scale out the app to support the increasing load of our system. But looking into the code of the
event-bus.ts
, it does not seem confident for us to scale outWe have added
await sleep(12000)
on line 208 to make it "unlucky" with the timing so we can reproduce the issue consistently.After adding that line of code, we observed that the same events were executed on multiple instances which could increase load unnecessarily on the instances and could increase the concurrency level on the system.
Even worse if the events trigger an import or export batch jobs, these are resource-intensive workflows in the application.
I know that we cannot achieve exactly once semantic 100% in distributed systems (or very difficult), so in the end, we have to rely on deduplication or idempotency to cover 0.001% of the edge cases. Idempotency also helps to solve the issue of events being dispatched out of order in this case.
But it would be great if the Medusa system has some synchronized control over this process or has any mechanism that this duplicated event executions are kept to the minimal.
Beta Was this translation helpful? Give feedback.
All reactions