You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
No async offloading — every operation blocks inside the request lifecycle until fully resolved.
// slow — blocks response until emails finishawaitsendEmail(employeePayload);awaitsendEmail(managerPayload);res.status(201).json({message: "Expense submitted", expense });// sequential DB readsconstemployee=awaitUser.findById(req.user.userId);constmanager=awaitUser.findOne({role: "manager"});// nothing escapes the request — user waits for all of it// slow — blocks response until emails finishconstexpense=awaitExpense.create(data);constemployee=awaitUser.findById(req.user.userId);constmanager=awaitUser.findOne({role: "manager"});awaitsendEmail(employeePayload);// still waiting...awaitsendEmail(managerPayload);// still waiting...res.status(201).json({ ... });// user finally gets a response
Active — fire-and-forget email + parallel DB reads
Response sent immediately after DB write; emails offloaded via setImmediate.
DB reads parallelised with Promise.all.
Result: latency drops sharply, 0% failures, all thresholds pass.
Emails wrapped in setImmediate with full async error handling — completely outside the request lifecycle.
// respond first, email in background// fire-and-forget with proper async/error handlingres.status(201).json({message: "Expense submitted", expense });setImmediate(()=>{sendEmail(employeePayload);sendEmail(managerPayload);});// parallel DB readsconst[employee,manager]=awaitPromise.all([User.findById(req.user.userId).select("name email"),User.findOne({role: "manager"}).select("name email"),]);
Server: createExpense lives in server/controllers/expense.js.
k6 run -e BASE_URL=http://localhost -e TEST_EMAIL=...-e TEST_PASSWORD=... load-tests/testscript-expense-create.js
(load-tests/testscript-expense-approve.js) — two scenarios
Commented — sequential DB reads + race condition
Two separate DB operations create a race window — another request can modify the expense between reads.
Sequential DB reads stall the request chain.
Blocking email I/O inside the request lifecycle adds tail latency.
Result: 0.33% failure rate, 10s max latency spike, approve success or race check fails.
// two round trips — race window between themconstexpense=awaitExpense.findById(id);if(expense.status!=="pending"){thrownewBadRequestError(...);}constupdatedExpense=awaitExpense.findByIdAndUpdate(id,{ status },{new: true});
Active — atomic DB operation, no race
Single atomic findOneAndUpdate with condition — no race window possible.
Eliminates the ~0.33% concurrent approval failures seen under load.
Removes long tail latency spikes; p(95) drops from 376ms to 169ms.
Result: 0.00% failures, 100% checks passed, all thresholds pass.
// atomic — condition + update in one DB round tripconstupdatedExpense=awaitExpense.findOneAndUpdate({_id: id,status: "pending"},// atomic condition{$set: { status }},{new: true},);if(!updatedExpense){thrownewBadRequestError("Expense already processed or not found");}
Server: approveExpense lives in server/controllers/expense.js.
k6 run -e BASE_URL=http://localhost -e TEST_EMAIL=...-e TEST_PASSWORD=... load-tests/testscript-expense-approve.js