date: 2025-07-23
topics: [nodejs, performance, clustering, event-loop, backend]
day: 7
π Day 7 β Node.js Cluster & Performance Benchmarking
β
What I Worked On
- Built a benchmarking setup using Node.js + Cluster + Worker Threads
- Benchmarked sync vs async route performance using
autocannon
- Integrated BullMQ for offloading heavy tasks to background workers
- Used
os.cpus()
to scale server instances across CPU cores
π What I Learned
Scenario | Requests/sec | Timeouts | Avg Latency |
---|
Async Route | β
~6.2k | β
~80 | β
~125ms |
Sync Route (Blocking) | β ~240 | β 3k+ | β up to 10s |
- π§ Async I/O in Node is highly performant when you avoid blocking operations
- β οΈ Sync code kills event loop β adds timeouts and high latency
- β
Use cluster module for horizontal CPU scaling
- β
Offload CPU-intensive work using:
worker_threads
- Background queues (BullMQ, Redis)
β Blockers
- Initial trouble spawning isolated workers without shared memory leak
- Needed to fine-tune autocannon settings for accurate measurements
π§ Reflection
This wasn't just coding β this was understanding Node.js under the hood. Benchmarking clarified why certain design patterns (queues, cluster, async APIs) aren't just optional β theyβre critical for real-world backend performance.
π References / Code