Node.js Architecture (Event Loop, Event Queue & Thread Pool) Node.js is single-threaded, yet it handles thousands of concurrent requests efficiently. How? With Event Loop, Event Queue, and Thread Pool. 1. Event Loop & Event Queue -Every incoming request enters the Event Queue. -The Event Loop continuously checks the queue and executes tasks. -This is how Node.js manages multiple requests without creating new threads for each one. 2. Blocking (Synchronous) Tasks -Executed directly on the main thread. -Node.js cannot process other tasks until it finishes. Example: heavy computations, synchronous file read. 3. Non-Blocking (Asynchronous) Tasks -Offloaded to Thread Pool (for file I/O, crypto) or handled via OS-level async APIs (network requests, timers). -Node.js continues processing other tasks while the async task runs. -Callback or promise executes when the task completes, sending the final response. Example: fs.readFile, HTTP requests, setTimeout. 4.Thread Pool -Handles tasks that could block the main thread. -Default size: 4 threads. -Not all async tasks use the Thread Pool — network and timers usually don’t. Key Insight: Async does not mean immediate response. Node.js keeps the main thread free and executes callbacks when tasks finish — all thanks to the Event Loop and Event Queue. #NodeJS #JavaScript #WebDevelopment #BackendDevelopment #EventLoop #AsyncProgramming Image Credits:https://lnkd.in/dnCXrptn
Node.js Architecture: Event Loop, Queue & Thread Pool
More Relevant Posts
-
⚖️ Day 13: The Great Angular Debate—Signals vs. NgRx. Which side are you on? We are right in the middle of Architecture Week, and today we are tackling the single most debated topic in the Angular ecosystem: State Management. It’s the classic battle of simplicity vs. structure. For years, the question was "Service-with-a-BehaviorSubject" vs. NgRx. In 2025, with Angular Signals, the landscape has shifted dramatically. Look at the spectrum in the visual below: 🔵 The Lightweight Approach (Left Side): Service + Signals This is the modern default for most applications. You create a service, hold state in a signal(), and components read or update it directly. > Pros: Almost zero boilerplate. Easy to understand. Natively reactive. > Best for: Sharing data between sibling components, UI state (isSidebarOpen), or smaller applications. 🟣 The Enterprise Approach (Right Side): NgRx / Flux Pattern This is the heavy lifter implementing the Redux pattern (Unidirectional Data Flow). Components never mutate state; they dispatch Actions. Reducers process actions to create new immutable state. Selectors give that state back to components. > Pros: Incredible traceability (using Redux DevTools), predictable state transitions, easier testing of complex business logic. > Best for: massive applications with hundreds of components, complex data dependencies, or teams that need strict guardrails. The verdict? Signals have killed the need for NgRx in many small-to-medium scenarios. But when complexity explodes, the structured indirection of the Flux pattern is still a lifesaver. Where does your current project fall on this spectrum? Drop a comment below! 👇 🔥 Architecture Week rolls on... Tomorrow (Day 14), we are looking at scaling your codebase physically with Nx Monorepos and Module Federation. 👉 Hit "Follow" + 🔔 on my profile so you don't miss the rest of Decem-Angular! #Angular #StateManagement #NgRx #Signals #RxJS #WebArchitecture #Frontend #JavaScript #TypeScript #SoftwareEngineering #DecemAngular #Day13
To view or add a comment, sign in
-
-
𝗥𝗲𝗮𝗰𝘁 𝟭𝟵. 𝗣𝗿𝗼 𝗽𝗲𝗿𝗳𝗼𝗿𝗺𝗮𝗻𝗰𝗲. 𝗕𝘂𝗶𝗹𝘁 𝗶𝗻. 40% of your 𝗥𝗲𝗮𝗰𝘁 𝟭𝟴 skills just 𝗲𝘅𝗽𝗶𝗿𝗲𝗱. Stop wrapping every React component in useCallback just to be safe. In React 19, it’s officially legacy thinking. The new React Compiler handles memoization at the Abstract Syntax Tree level. It doesn't just 𝗺𝗲𝗺𝗼𝗶𝘇𝗲. It solves the 𝗗𝗲𝗽𝗲𝗻𝗱𝗲𝗻𝗰𝘆 𝗧𝗿𝗮𝗽 before your 𝗰𝗼𝗱𝗲 even reaches the 𝗯𝗿𝗼𝘄𝘀𝗲𝗿. I recently lead a high-traffic dashboard refactor. We switched to React 19 and deleted 40% of our manual optimization boilerplate. Here is the 𝗛𝗶𝗱𝗱𝗲𝗻 𝗹𝗲𝗮𝗱 𝗱𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿 𝗽𝗹𝗮𝘆𝗯𝗼𝗼𝗸 for blazing fast hooks: 𝟭. 𝗙𝗶𝗯𝗲𝗿 𝗖𝗵𝗮𝗶𝗻 over Render Tree React Fiber uses a 𝗹𝗶𝗻𝗸𝗲𝗱-𝗹𝗶𝘀𝘁 (O(1) traversal) instead of a 𝗿𝗲𝗰𝘂𝗿𝘀𝗶𝘃𝗲 𝘀𝘁𝗮𝗰𝗸. Impact: We stopped recursion stack overflows on massive data trees. 𝟮. 𝗠𝗲𝗺𝗼𝗿𝘆 𝗦𝗰𝗮𝘃𝗲𝗻𝗴𝗶𝗻𝗴 Every useMemo keeps a closure alive in memory. By moving to React 19, we cleared the heap pressure by letting the Compiler handle garbage collection hints. Result: 0 Memory Leaks. 𝟯. 𝗟𝗮𝗻𝗲-𝗕𝗮𝘀𝗲𝗱 𝗣𝗿𝗶𝗼𝗿𝗶𝘁𝗶𝘇𝗮𝘁𝗶𝗼𝗻 React 19 uses "Lanes" to group updates. High-priority clicks bypass heavy background renders instantly. INP Impact: 92% improvement. 𝟰. 𝗦𝘂𝗿𝗴𝗶𝗰𝗮𝗹 𝗟𝗼𝗴𝗶𝗰 (The useActionState) Stop creating 𝗜𝘀𝗟𝗼𝗮𝗱𝗶𝗻𝗴 state waterfalls. React 19 Actions handle transitions at the reconciler level. No more manual re-render loops. 𝟱. 𝗥𝗲𝘀𝗼𝘂𝗿𝗰𝗲 𝗣𝗿𝗲-𝘄𝗮𝗿𝗺𝗶𝗻𝗴 We implemented preinit for 𝗰𝗿𝗶𝘁𝗶𝗰𝗮𝗹 𝗝𝗦 𝗰𝗵𝘂𝗻𝗸𝘀. Perceived Load: Instant. The Results: Total Blocking Time: 280ms ➔ 8ms (Main thread unblocked). Heap Size: Reduced by 30%. Interaction to Next Paint (INP): < 40ms. (Perfect score). #React19 #ReactJS #WebPerformance #FrontendArchitecture #SoftwareEngineering
To view or add a comment, sign in
-
-
Ever removed a console.log and the feature immediately broke? (o´・_・)っ We label these Heisenbugs in engineering. You observe it, it works. You look away, it fails. This is not magic. Most of the time, it is synchronous I/O blocking. In environments like Node.js, writing to stdout (the terminal) can block the execution thread. That innocent log introduces a small CPU pause, often 5 to 10 ms. If your logic has a race condition, that tiny pause accidentally becomes a synchronization point. It gives an async operation just enough time to finish before the next line runs. --- The anti-pattern (the race condition): // ❌ The Heisenbug let cache = null; // Async operation fires but is not awaited fetchData().then(data => { cache = data; }); // This log "fixes" the bug by slowing execution // console.log("Waiting..."); // Without the log, this runs too early if (cache) { process(cache); } The code depends on timing, not correctness. --- The fix (explicit synchronization): // ✅ The fix const data = await fetchData(); process(data); No guessing. No luck. Just enforced order. --- How to avoid this architectural trap: • Lint for floating promises Flag any Promise that is neither awaited nor returned. • Immutable state Avoid mutating outer variables inside async closures. • Strict typing TypeScript makes it very hard to treat Promise<T> like T. --- The next time a console.log saves your logic, remember: It is not a solution. It is a symptom of an unmanaged race condition. Always enjoyable diving into these engineering nuances at Zignuts Technolab. ⊂((・▽・))⊃ #SoftwareEngineering #JavaScript #React #NodeJS #Architecture #Debugging
To view or add a comment, sign in
-
-
Mastering Worker Threads in Node.js - 05 | Worker Threads Aren’t Always the Answer (Here’s When to Pause) Worker Threads can be a powerful tool in Node.js, but they are not a universal solution. In Part 5 of this series, I step back from implementation details and focus on decision-making — when Worker Threads genuinely help and when they introduce unnecessary complexity. This article breaks down the types of workloads that benefit from Worker Threads, such as CPU-bound and blocking computations, and clearly explains scenarios where they should be avoided, including I/O-heavy tasks, trivial operations, or problems better solved through scaling strategies. If you’re evaluating performance issues in a Node.js application and want to choose the right tool instead of the most complex one, this post will help you make that call with confidence. Read the full article here: https://lnkd.in/gXtxfdYT #NodeJS #JavaScript #WorkerThreads #BackendDevelopment #Performance #Architecture
To view or add a comment, sign in
-
-
🚀 Boost Your Node.js Performance with Worker Threads Node.js is powerful — but when it comes to CPU-heavy tasks, the event loop can quickly become a bottleneck. That’s where Worker Threads step in. 🔥 What are Worker Threads? They allow you to run JavaScript in parallel on multiple threads, offloading heavy computations without blocking the main event loop. 🧩 Why you should care: • ⚡ Improved performance for CPU-intensive tasks • 🚀 Non-blocking architecture stays smooth • 🧵 Perfect for tasks like hashing, image processing, data transformations, ML ops • 🔧 Designed for real-world scalability Instead of slowing down your entire app, you offload work to a separate thread — a clean and efficient approach. 💡 If you’re still handling heavy tasks in the main event loop, now is the time to rethink your architecture. Worker Threads aren’t just a feature — they’re a performance upgrade. #NodeJS #JavaScript #BackendDevelopment #WorkerThreads #WebPerformance #Programming #SoftwareEngineering
To view or add a comment, sign in
-
-
𝗧𝗼𝗽 𝟮𝟬 𝗡𝘂𝗴𝗲𝘁 𝗣𝗮𝗰𝗸𝗮𝗴𝗲𝘀 𝗳𝗼𝗿 .𝗡𝗘𝗧 𝗱𝗲𝘃𝘀 𝗶𝗻 𝟮𝟬𝟮𝟲 You don't need to reinvent the wheel. 👇 Here is a list of fantastic libraries I have personally used, and I recommend everyone try them out for efficient backend development in .NET: 1. Entity Framework Core 2. Dapper 3. Serilog 4. FluentValidation 5. Polly 6. Refit 7. Wolverine 8. Dapr 9. Quartz .NET 10. Swagger (Swashbuckle) 11. xUnit 12. Shouldly 13. TestContainers 14. Bogus 15. Moq 16. FluentEmail 17. FastEndpoints 18. HotChocolate GraphQL 19. SignalR 20. UnitsNet What else should be on this list? —— 📌 Save this post for future reference! ♻️ Repost to help other .NET developers discover these libraries ➕ Follow me ( Anton Martyniuk ) to improve your .NET and Architecture Skills
To view or add a comment, sign in
-
Stop creating "Base Components" to share logic. 🛑 We have all been there. You have a component that needs a Tooltip. Later, requirements change, and it also needs to be Draggable. Your instinct is to create a class hierarchy: class SmartButton extends BaseButton { ... } But what happens when you need a component that is Draggable but doesn't need a Tooltip? Or what if you want to combine Draggable + Resizable + Loggable? In TypeScript (and most OOP languages), you cannot extend multiple classes. This is known as the "Diamond Problem", a logical ambiguity where the compiler wouldn't know which parent method to run if two parents shared the same method name. So, we end up creating massive "God Classes" that contain every possible feature, just to be safe. This leads to the classic Gorilla/Banana problem: "You wanted a banana, but what you got was a gorilla holding the banana and the entire jungle." The Solution? Composition. 🧩 Angular v15+ introduced hostDirectives, which effectively solves this. It allows you to "mix in" capabilities by attaching standalone directives directly to a component’s metadata without inheritance. Why hostDirectives is the future: Bypasses the Diamond Problem: You can attach as many directives as you want. Clean API: You can alias inputs/outputs (e.g., exposing tooltipInput as just tip). Decoupling: Your components remain lightweight and only "lease" the logic they actually need. Swipe to see the full architecture breakdown and code implementation! 👉 #Angular #SoftwareArchitecture #CleanCode #TypeScript #WebDevelopment #Frontend #CodingTips
To view or add a comment, sign in
-
Day 15 of #180daysofcode Part 4 Express js interview questions ✅ 25. What is MVC architecture? A structure with: Model → data View → UI Controller → logic Express supports clean folder separation. ✅ 26. What is a template engine? Used to generate dynamic HTML. Examples: EJS, Pug, Handlebars. ✅ 27. How does JWT Authentication work? User logs in → server creates token Client stores token Each request sends token Server verifies token ✅ 28. What is session handling? Server stores user session data using a session ID. ✅ 29. Difference: Session vs Token? Session Token Stored server-side Stored client-side Needs memory Stateless Less scalable Highly scalable ✅ 30. What is Helmet? Security middleware that protects from common web attacks. ✅ 31. How to prevent XSS in Express? Use Helmet Escape user input Validate data ✅ 32. What is rate limiting? Limits number of requests from same IP to protect API. Example: const rateLimit = require('express-rate-limit'); app.use(rateLimit({ windowMs: 60000, max: 10 })); ✅ 33. Difference: Express.js vs Nest.js? Express → lightweight, flexible Nest.js → opinionated, enterprise structure, TypeScript-based #webdev #expressjs #backenddevelopment #reactjs #javascript
To view or add a comment, sign in
-
𝐃𝐨𝐞𝐬 𝐍𝐨𝐝𝐞.𝐣𝐬 𝐫𝐞𝐚𝐥𝐥𝐲 𝐫𝐮𝐧 𝐨𝐧 𝐨𝐧𝐥𝐲 𝐨𝐧𝐞 𝐭𝐡𝐫𝐞𝐚𝐝? This is one of the most misunderstood topics in Node.js interviews and backend system design.Understanding the “Node has 4 threads” concept: 1. 𝐌𝐚𝐢𝐧 𝐓𝐡𝐫𝐞𝐚𝐝 (𝐄𝐯𝐞𝐧𝐭 𝐋𝐨𝐨𝐩 𝐓𝐡𝐫𝐞𝐚𝐝) Node.js executes all JavaScript code on a single main thread. This thread is responsible for: *)Running JavaScript code *)Executing Express routes and controllers *)Handling promises and async/await *)Managing the event loop 2. 𝐋𝐢𝐛𝐮𝐯 𝐓𝐡𝐫𝐞𝐚𝐝 𝐏𝐨𝐨𝐥 (4 𝐭𝐡𝐫𝐞𝐚𝐝𝐬 𝐛𝐲 𝐝𝐞𝐟𝐚𝐮𝐥𝐭) Node.js uses a C library called libuv to handle certain blocking operations. libuv creates a thread pool with 4 worker threads by default. These threads are used for: *)File system operations (fs) *)Crypto operations (hashing, encryption) *)Compression (zlib) *)DNS lookup operations 3. 𝐎𝐒 𝐊𝐞𝐫𝐧𝐞𝐥 𝐓𝐡𝐫𝐞𝐚𝐝𝐬 (𝐍𝐨𝐧-𝐛𝐥𝐨𝐜𝐤𝐢𝐧𝐠 𝐈/𝐎) Network-related operations do not use the libuv thread pool. These include: *)HTTP requests *)WebSocket connections *)Database socket communication *)Timers These operations are handled asynchronously by the operating system kernel.This is the main reason Node.js can handle thousands of concurrent connections without creating thousands of threads. 4. 𝐖𝐨𝐫𝐤𝐞𝐫 𝐓𝐡𝐫𝐞𝐚𝐝𝐬 (𝐄𝐱𝐩𝐥𝐢𝐜𝐢𝐭 𝐌𝐮𝐥𝐭𝐢𝐭𝐡𝐫𝐞𝐚𝐝𝐢𝐧𝐠) Worker Threads allow true parallel execution of JavaScript. Each worker thread: *)Has its own event loop *)Has its own memory *)Runs JavaScript independently Worker threads are useful for: *)CPU-intensive calculations *)Image or video processing *)Data analysis tasks
To view or add a comment, sign in
-
-
𝗜𝘀 𝘆𝗼𝘂𝗿 𝗻𝗼𝗱𝗲_𝗺𝗼𝗱𝘂𝗹𝗲𝘀 𝗳𝗼𝗹𝗱𝗲𝗿 𝗲𝗮𝘁𝗶𝗻𝗴 𝘆𝗼𝘂𝗿 𝗵𝗮𝗿𝗱 𝗱𝗿𝗶𝘃𝗲 𝗮𝗹𝗶𝘃𝗲? 📂💀 Working on backend architecture across multiple projects, I’ve constantly looked for ways to optimize my workflow. I started with 𝗡𝗣𝗠, migrated to 𝗬𝗮𝗿𝗻 for speed, but recently, I’ve realized that 𝗣𝗡𝗣𝗠 is the actual game-changer. Here is why my workflow is evolving (and why yours probably should too): 1️⃣ 𝗧𝗵𝗲 𝗕𝗮𝘀𝗲𝗹𝗶𝗻𝗲: 𝗡𝗣𝗠 It works, but it can be slow. It usually installs packages one by one (serially). 2️⃣ 𝗧𝗵𝗲 𝗦𝗽𝗲𝗲𝗱 𝗨𝗽𝗴𝗿𝗮𝗱𝗲: 𝗬𝗮𝗿𝗻 Most of us switched to Yarn because it felt faster. • Parallel Installation: It downloads multiple packages at once (utilizing your CPU cores). • Caching: It remembers what you downloaded so it doesn't fetch it twice. • Resilience: Better handling of network drops. 3️⃣ 𝗧𝗵𝗲 𝗔𝗿𝗰𝗵𝗶𝘁𝗲𝗰𝘁𝘂𝗿𝗲 𝗨𝗽𝗴𝗿𝗮𝗱𝗲: 𝗣𝗡𝗣𝗠 🚀 This is where it gets interesting. PNPM isn't just "faster" due to downloading; it’s smarter about storage. • 𝗧𝗵𝗲 𝗙𝗹𝗮𝘁 𝗠𝗼𝗱𝗲𝗹: Unlike NPM/Yarn which copy dependency files into every single project's folder, PNPM uses a 𝗴𝗹𝗼𝗯𝗮𝗹 𝘀𝘁𝗼𝗿𝗲. • 𝗦𝗺𝗮𝗿𝘁 𝗟𝗶𝗻𝗸𝗶𝗻𝗴: It saves one copy of a library on your disk and hard links it to your projects. • 𝗥𝗲𝘀𝘂𝗹𝘁? Lightning-fast installs and massive disk space savings. No more duplicate files cluttering your system! 💡 𝗠𝘆 𝗩𝗲𝗿𝗱𝗶𝗰𝘁: I still use Yarn on legacy projects, but for anything new? It's 𝗣𝗡𝗣𝗠 all the way. The speed difference on a cold install is undeniable. 𝗪𝗵𝗶𝗰𝗵 𝗽𝗮𝗰𝗸𝗮𝗴𝗲 𝗺𝗮𝗻𝗮𝗴𝗲𝗿 𝗮𝗿𝗲 𝘆𝗼𝘂 𝗰𝘂𝗿𝗿𝗲𝗻𝘁𝗹𝘆 𝘂𝘀𝗶𝗻𝗴 𝗶𝗻 𝗽𝗿𝗼𝗱𝘂𝗰𝘁𝗶𝗼𝗻? 👇 #nodejs #javascript #webdevelopment #softwareengineering #productivity #pnpm #yarn #npm #DeveloperExperience
To view or add a comment, sign in
-
More from this author
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development