The Real Fullstack

The Blocked Event Loop

The database answers in 18 ms. Why does the product page take over a second whenever someone logs in?

مهندس8 دقيقةمختبر تصحيح الأخطاءمحاكي التدفّق
nodeexpressbcryptworker-threads

لم يُترجم هذا التحدي إلى العربية بعد. أنت تقرأ النسخة الإنجليزية.

المهمة:Predict who is affected by one login, find what blocks the event loop, and move CPU-heavy work off the single JavaScript thread.السياق · أهداف التعلّم ▾

Fernpost's Node.js API hashes passwords with bcrypt.hashSync at cost 14, and the report export serialises 180 MB of JSON in the request handler. Under load every route slows down, health checks time out and Kubernetes restarts healthy pods.

المهمة: Predict who is affected by one login, find what blocks the event loop, and move CPU-heavy work off the single JavaScript thread.

أهداف التعلّم

  • Understand that Node.js runs your JavaScript on one thread.
  • Use asynchronous APIs that run on the libuv pool for CPU-heavy library work.
  • Offload rare heavy jobs to worker threads.
المحاكاةمحاكاة
usersload balancerNode.js APIPostgreSQL
تشغيل المحاكاة
app.post('/login', async (req, res) => {  const ok = await bcrypt.compare(req.body.password, user.hash);   // runs on libuv's thread pool  ...});// heavy, rare CPU work: hand it to a worker threadimport { Worker } from 'node:worker_threads';app.get('/reports/export', (req, res) => {  const w = new Worker(new URL('./export-worker.js', import.meta.url), { workerData: req.query });  w.once('message', (file) => res.download(file));});

تبسيط

Three symptoms with deterministic fixes. Real event-loop lag is measured with monitorEventLoopDelay or APM tools. نموذج تعليمي — الأرقام توضّح سلوكًا وليست قياسات أداء.

المفاهيم

مفيد قبل هذا التحدي

تحديات ذات صلة

المراجع