Node.js: Code Outside the Browser / Node.js Internals and How it Works

Processes, Threads and Thread Pool

🎯 Goal of This Note

Understand what a thread is, why Node.js runs in a single thread, and how the thread pool helps handle heavier tasks without blocking that thread.


πŸ–₯️ What Is a Process?

  • When you run Node.js, your computer starts a process β€” just a fancy word for "a program that's currently running."

  • Since Node.js is really a C++ program under the hood, running it creates a real, actual process at the operating system level β€” same as opening Chrome or any other app.

  • Because of this, Node gives you access to a built-in process variable in your code β€” it exists precisely because Node itself is an OS process. You'll use this later in the course.


🧡 What Is a Thread?

  • A thread is simply a sequence of instructions being run.

  • Think of it as a "box" where your code actually executes on the computer's processor β€” that's all you need to know for now.


⭐ Node.js Runs on a Single Thread

  • 🧡 Node apps run inside just one thread β€” whether you have 10 users or 10 million, they all share it.

  • ⚠️ Since there's only one thread, it's easy to accidentally block the whole app if you're not careful. This is one of Node's most important characteristics.


πŸš€ What Happens When a Node App Starts

  1. ▢️ Top-level code runs first β€” anything not inside a callback (e.g. reading files at the top of index.js)

  2. πŸ“¦ Modules load β€” core, custom, and third-party

  3. πŸ“ Callbacks get registered β€” e.g. the function passed to http.createServer() is set up, but doesn't run yet

  4. πŸ”„ The event loop starts β€” this is where most of the actual work happens; the "heart" of Node


⚠️ The Problem: Some Tasks Are Too Heavy

  • Certain operations would block the single thread if run directly in the event loop β€” bad for a server handling many users.

βœ… The Solution: Thread Pool

  • 🧰 Provided by libuv, the thread pool gives Node 4 extra threads (configurable up to 128).

  • πŸ”„ The event loop automatically offloads heavy tasks to these threads, keeping the main thread free.

  • πŸ€– This happens automatically β€” developers don't choose what gets offloaded.

πŸ“‹ What Gets Offloaded?

πŸ“‚ Category

πŸ“ Examples

πŸ“ File system

Reading/writing files

πŸ” Cryptography

Password hashing, encryption

πŸ—œοΈ Compression

Zipping/unzipping

🌐 DNS lookups

Domain β†’ IP resolution

These are exactly the tasks most likely to block the main thread β€” so Node routes them to the thread pool instead.


πŸ”‘ Key Takeaways

πŸ“Œ Concept

βœ… Explanation

πŸ’» Process

A running instance of a program (Node itself, as a C++ program)

🧡 Thread

A sequence of instructions β€” "where code executes"

🧡 Single thread

True no matter how many users β€” makes blocking code dangerous

▢️ Top-level code

Runs first, immediately, not inside a callback

πŸ”„ Event loop

Starts after setup; handles ongoing work β€” Node's "heart"

🧰 Thread pool

4 (up to 128) extra threads from libuv, for heavy tasks

πŸ€– Auto-offloading

Node/libuv decide automatically β€” not developer-controlled

πŸ“‹ Offloaded tasks

File ops, crypto, compression, DNS lookups