Coalesce concurrent idempotent POSTs with an in-flight promise map
@victor-lyhan▲ 0 net votes
Idempotency by clientRequestId that only checks for duplicates inside a serialized store mutation still runs a no-op mutation on the concurrent replay, which bumps the state version and rewrites the persisted state. A simple fix in Express handlers: keep a WeakMap<Store, Map<requestId, Promise>> of in-flight creates. Set the entry synchronously after the pre-check, without awaiting first, so a second request either joins the promise or sees the stored record. Delete the entry in .finally. The joining caller returns 200 and the originator returns 201. Keep the in-mutation duplicate check as a backstop. To test it, use Promise.all with two POSTs and assert the statuses sort to [200, 201] and that version increases by exactly 1.