Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
What is Piping in Node.js ?
Next article icon

What is Reactor Pattern in Node.js ?

Last Updated : 22 Jun, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

Reactor Pattern is used to avoid the blocking of the Input/Output operations. It provides us with a handler that is associated with I/O operations. When the I/O requests are to be generated, they get submitted to a demultiplexer, which handles concurrency in avoiding the blocking of the I/O mode and collects the requests in form of an event and queues those events.

There are two ways in which I/O operations are performed:

  1. Blocking I/O: Application will make a function call and pause its execution at a point until the data is received. It is called as ‘Synchronous’.
  2. Non-Blocking I/O: Application will make a function call, and, without waiting for the results it continues its execution. It is called as ‘Asynchronous’.

Note: Node.js is Asynchronous in nature.

Reactor Pattern comprises of:

  1. Resources: They are shared by multiple applications for I/O operations, generally slower in executions.
  2. Synchronous Event De-multiplexer/Event Notifier: This uses Event Loop for blocking on all resources. When a set of I/O operations completes, the Event De-multiplexer pushes the new events into the Event Queue.
  3. Event Loop and Event Queue: Event Queue queues up the new events that occurred along with its event-handler, pair.
  4. Request Handler/Application: This is, generally, the application that provides the handler to be executed for registered events on resources.

How Reactor Pattern works?

  1. Everything starts with the application. It makes a request and the event demultiplexer gathers those requests and then it forms queues know as Event Queues.
  2. Event demultiplexer is run by libuv which is a library that allows JavaScript code (via V8) to perform I/O, in-network, file, etc. It is an asynchronous IO library that allows Node.js to perform I/O.
  3. In the diagram above, there is only one event queue and there are 7 basics queues. Those queues have ascending priorities, the queue that has the highest priority is checked first by the event loop.
  4. The Timers queue has the highest priority. setTimeout and setInterval functions are queued here. Once the events are done in this queue, or time is up, the event loop passes those functions to call stack, named as executing handler.
  5. When one of the event queues is complete, instead of jumping to the next queue, the event loop firstly will check the other two queues which queue other micro tasks and process called nextTick functions. Then it will jump to the upcoming queue.

Callback queue is an event queue and call stack is execute handler.



Next Article
What is Piping in Node.js ?

S

sharmaanushka
Improve
Article Tags :
  • Node.js
  • Web Technologies
  • Node.js-Misc

Similar Reads

  • What is spawn in Node JS ?
    Node JS is a cross-platform, open-source back-end JavaScript runtime environment that uses the V8 engine to execute JavaScript code outside of an internet browser. In this article, we will learn about the Spawn in NodeJs. PrerequisitesNodeJS fundamentalsAsynchronous ProgrammingChild ProcessesSpawn i
    3 min read
  • What is Piping in Node.js ?
    Piping in NodeJS is the process by which byte data from one stream is sent to another stream. It is a powerful feature in Node.js that allows data to be transferred from one stream to another seamlessly. Streams are an integral part of Node.js, providing a mechanism for handling data that is too lar
    6 min read
  • What is Web Scraping in Node.js ?
    Web scraping is the automated process of extracting data from websites. It involves using a script or a program to collect information from web pages, which can then be stored or used for various purposes such as data analysis, research, or application development. In Node.js, web scraping is common
    3 min read
  • What is a stub in Node.js ?
    A small program routine that substitutes for a longer program which is possible to be loaded later or that is remotely located. Features of stub: Stubs can be either anonymous. Stubs can be wrapped into existing functions. When we wrap a stub into the existing function the original function is not c
    3 min read
  • Top 8 Node.js Design Patterns in 2025
    Node.js, the popular JavaScript runtime, helps developers build complex backend systems. With so many capabilities, it can get quite challenging to work with, and hence, design patterns are used.Design patterns help developers write high-quality, testable, and maintainable code. Some of the design p
    10 min read
  • What is Poll Phase in Node.js Event Loop ?
    The Node.js event loop is the heart of Node.js, responsible for executing non-blocking I/O operations and enabling the asynchronous behavior that defines the platform. Among the various phases of the event loop, the poll phase plays a crucial role in handling I/O events. In this article, we'll delve
    6 min read
  • What is REST API in NodeJS?
    NodeJS is an ideal choice for developers who aim to build fast and efficient web applications with RESTful APIs. It is widely adopted in web development due to its non-blocking, event-driven architecture, making it suitable for handling numerous simultaneous requests efficiently. But what makes Node
    7 min read
  • What is Chunk in Node.js ?
    In Node.js, the term "chunk" refers to a small, manageable piece of data that is part of a larger dataset. Node.js processes data in chunks, especially when dealing with streams, which makes handling large files or data sources more efficient and memory-friendly. This article explores what chunks ar
    4 min read
  • What is the purpose of process object in Node.js ?
    A process object is a global object available in the Node.js environment. It is globally available. We do not have to use the require() to import the module of the process object. The "process" object use to get current Node.js process details & also give control over that process. Properties of
    2 min read
  • What is Chaining in Node.js ?
    Chaining in Node.js can be achieved using the async npm module. In order to install the async module, we need to run the following script in our directory: npm init npm i async There are two most commonly used methods for chaining functions provided by the async module: parallel(tasks, callback): Th
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences