diff --git a/packages/worker-relay/src/worker.ts b/packages/worker-relay/src/worker.ts index 4dfafb8f..ad2e0a33 100644 --- a/packages/worker-relay/src/worker.ts +++ b/packages/worker-relay/src/worker.ts @@ -31,9 +31,14 @@ let eventWriteQueue: Array = []; async function insertBatch() { // Only insert event batches when the command queue is empty // This is to make req's execute first and not block them - if (relay && eventWriteQueue.length > 0 && cmdQueue.length === 0) { - relay.eventBatch(eventWriteQueue); - eventWriteQueue = []; + if (eventWriteQueue.length > 0 && cmdQueue.length === 0) { + await barrierQueue(cmdQueue, async () => { + if (relay) { + const toWrite = [...eventWriteQueue]; + eventWriteQueue = []; + relay.eventBatch(toWrite); + } + }); } setTimeout(() => insertBatch(), 100); }