Node Js Interview
Node Js Interview
pa
ep
de
an
/s
/in
om
.c
in
k ed
lin
w.
w
//w
s:
tp
Product Companies
Follow for daily System Design, .NET, Node & Full Stack interview mastery
🔹 Q1. What is [Link] and why is it used in backend
systems?
Answer
l/
pa
[Link] is a JavaScript runtime built on Chrome’s V8 engine that allows executing JS on the
server side.
ep
Why product companies use [Link]:
de
● Non-blocking I/O model → handles high concurrency
an
● Event-driven architecture
/s
● Lightweight & fast startup
/in
om
Real-world example
ed
Netflix uses [Link] for edge services to handle millions of requests with minimal latency.
k
Answer
//w
[Link] uses a single-threaded event loop with a callback queue to handle async operations.
s:
3. Idle/prepare
Key insight:
● Heavy computation blocks the loop → bad for scalability
l/
Production Example
pa
If your API processes image compression synchronously → all requests get blocked.
ep
Solution: move to worker threads or queue (BullMQ).
de
🔹 Q3. Difference between [Link](),
an
setImmediate(), and setTimeout()
/s
/in
Method Execution Time
om
Real-world usage:
lin
[Link] executes I/O operations asynchronously without blocking the main thread.
tp
Example
ht
● Writable
● Duplex
l/
● Transform
pa
ep
Production Example
de
Uploading a 1GB file using stream instead of loading in memory → avoids memory crash.
an
🔹 Q6. What is Buffer in [Link]?
/s
/in
Buffer is used to handle binary data directly in memory.
om
Used in:
● File handling
.c
● TCP streams
in
ed
● Image processing
k
lin
fork?
//w
Example
Use fork for parallel background job processing
🔹 Q8. What is clustering in [Link]?
Clustering allows multiple CPU cores to run [Link] instances.
const cluster = require('cluster')
Benefit:
Improves horizontal scaling on single machine
l/
pa
🔹 Q9. What are Worker Threads?
ep
de
Worker threads run CPU-heavy tasks in separate threads.
an
Example:
/s
● Image resizing
/in
● PDF generation
om
● ML processing
.c
in
Express Example
w
[Link]("Logging")
next()
})
s:
tp
Types:
1. Sync errors → try/catch
Best Practice
Use centralized error handler middleware
l/
pa
ep
REPL = Read Eval Print Loop
Used for interactive debugging
de
🔹 Q13. What is [Link]?
an
/s
It manages: /in
● dependencies
om
● scripts
.c
● metadata
in
ed
Example
k
"scripts": {
lin
}
w
devDependencies
tp
Type Purpose
ht
npx executes
packages
l/
pa
🔹 Q16. What is the difference between CommonJS and ES
ep
de
Modules?
an
Feature CommonJS ES Modules
/s
Syntax require import
/in
Loading synchronou asynchronou
s s
om
Solution
lin
Use:
w.
● Promises
w
● Async/await
//w
s:
● Resolved
● Rejected
🔹 Q19. What is async/await?
Syntactic sugar over promises for cleaner async code.
l/
Stored in .env
pa
PORT=3000
DB_URL=...
ep
Used to store secure configuration
de
🔹 Q21. What is [Link] and why is it widely used?
an
/s
/in
Answer
om
[Link] is a minimal, unopinionated web framework for [Link] used to build REST APIs
and web servers.
.c
● Middleware-based architecture
lin
● Huge ecosystem
w.
w
● Flexible routing
//w
s:
Real-world example
tp
Building microservices APIs for an e-commerce platform (orders, users, payments) using
Express.
ht
l/
pa
Routing maps HTTP request → handler function.
ep
[Link]('/users/:id', getUser)
[Link]('/orders', createOrder)
de
RESTful best practice:
an
● GET → read
/s
● POST → create
/in
om
● PUT/PATCH → update
● DELETE → remove
.c
in
ed
Type Example
w.
Query /users?page=
param 2
s:
l/
message: [Link]
pa
})
})
ep
🔹 Q27. How do you structure a production-level Express
de
an
project?
/s
Recommended structure
/in
src/
om
├── controllers/
├── services/
.c
├── repositories/
in
├── middlewares/
├── routes/
ed
├── utils/
k
└── config/
lin
Why important?
w.
● Separation of concerns
w
//w
● Clean architecture
s:
● Testability
tp
ht
● Yup
● Zod
● express-validator
Example (Joi)
const schema = [Link]({
l/
email: [Link]().email().required()
pa
})
ep
🔹 Q29. What is request sanitization?
de
an
Sanitization removes malicious input.
/s
Example threats: /in
● XSS
om
● SQL Injection
.c
in
Libraries:
ed
● [Link]
k
lin
● DOMPurify
w.
Example
ht
Real-world example
Login API limited to 5 attempts per minute
🔹 Q31. What is CORS and how do you configure it?
CORS allows cross-origin requests.
[Link](cors({
origin: '[Link]
}))
l/
pa
ep
Helmet secures Express apps by setting HTTP headers.
[Link](helmet())
de
Prevents:
● XSS
an
● Clickjacking
/s
/in
● MIME sniffing
om
Best practices:
k
● HTTPS
lin
w.
● JWT authentication
w
● Input validation
//w
● Rate limiting
s:
● Helmet
tp
ht
l/
pa
ep
Access token expires quickly
Refresh token used to generate new access token
de
Benefit
an
Improves security + UX
/s
/in
🔹 Q36. What is OAuth in [Link]?
om
● Google
in
● Facebook
k ed
● GitHub
lin
Methods:
ht
● Header versioning
● Query versioning
🔹 Q38. What is pagination in APIs?
Used to fetch large datasets in chunks.
Example
GET /products?page=1&limit=20
l/
🔹 Q39. What is API caching?
pa
ep
Caching improves performance.
de
Types:
an
● In-memory (Node cache)
/s
● Redis
/in
om
● CDN caching
.c
Example
in
Code Meaning
w
//w
200 OK
201 Created
s:
tp
401 Unauthorized
403 Forbidden
Common stacks:
l/
● SQL → PostgreSQL, MySQL (pg, mysql2, Sequelize, TypeORM)
pa
ep
● NoSQL → MongoDB (Mongoose)
de
Production Example
an
Order service using PostgreSQL with TypeORM and connection pooling.
important?
.c
Benefits:
● Reduces connection overhead
k
lin
● Improves performance
w.
● Prevents DB overload
w
//w
Real-world Example
s:
l/
Indexes improve query performance by reducing full table scans.
pa
ep
Example
CREATE INDEX idx_user_email ON users(email);
de
🔹 Q45. What are ACID properties?
an
/s
● Atomicity
/in
om
● Consistency
.c
● Isolation
in
● Durability
ed
Example
tp
l/
pa
Example
ep
Users split by region:
● India shard
de
● US shard
an
🔹 Q49. What is replication? /s
/in
om
Types:
in
ed
● Master-Slave
k
● Multi-master
lin
w.
Benefit:
w
● High availability
//w
● Fault tolerance
s:
tp
Used for:
● Caching
● Session store
● Pub/Sub messaging
● Rate limiting
l/
pa
ep
Types:
de
1. Cache-aside
an
2. Write-through
3. Write-back
/s
/in
om
Example
.c
Pattern:
//w
● Check cache
s:
Strategies:
● TTL expiration
● Manual invalidation
● Event-driven invalidation
l/
pa
Message queue enables async communication between services
ep
Tools:
de
● RabbitMQ
an
● Kafka
/s
● BullMQ (Redis-based) /in
om
Use when:
● Background jobs
k ed
● Email sending
lin
● Payment processing
w.
w
● Order fulfillment
//w
Example
Order placed → notify:
● Inventory service
● Notification service
🔹 Q57. What is Kafka used for?
Kafka is used for:
● Event streaming
● Log aggregation
● Real-time analytics
l/
pa
ep
Example
de
Tracking user activity events
Use cases:
● Email jobs
.c
in
● Image processing
ed
● Report generation
k
lin
Example
tp
Tools:
● Redis locks
● Zookeeper
l/
pa
Vertical Increase CPU/RAM
ep
Horizontal Add more servers
Node apps usually scale horizontally
de
🔹 Q62. What is load balancing?
an
/s
Distributes traffic across multiple servers.
/in
om
Tools:
● Nginx
.c
in
● AWS ELB
ed
Example
If payment service down → stop retrying → fallback response
s:
tp
Responsibilities:
● Authentication
● Routing
● Rate limiting
● Logging
l/
pa
CQRS = Command Query Responsibility Segregation
Separate:
ep
● Read operations
de
● Write operations
an
/s
Benefit: /in
Better scalability for large systems
om
architecture?
k ed
Answer
lin
Production Architecture
w
//w
● Stateless services
ht
● Horizontal scaling
Example services:
● Auth Service
l/
● Order Service
pa
● Payment Service
ep
de
● Notification Service
an
Benefits:
/s
● Independent deployment
/in
om
● Team ownership
Monolith Microservices
w.
codebase
//w
Responsibilities:
● Auth verification
● Rate limiting
● Routing
● Response aggregation
l/
Tools:
pa
● Kong
ep
● NGINX
de
● AWS API Gateway
an
🔹 Q70. What is service discovery? /s
/in
om
Tools:
in
● Consul
ed
● Eureka
k
lin
● Kubernetes DNS
w.
w
Benefits:
ht
● Easy deployment
● Isolation
🔹 Q72. Example Dockerfile for [Link]
FROM node:20
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["node", "[Link]"]
l/
pa
🔹 Q73. What is Kubernetes (K8s)?
ep
de
Kubernetes orchestrates containers.
an
Features:
/s
● Auto scaling /in
● Self healing
om
● Rolling deployment
.c
in
Automates:
● Build
w.
● Test
w
//w
● Deploy
s:
tp
Tools:
ht
● GitHub Actions
● Jenkins
● Azure DevOps
🔹 Q75. How do you manage environment configurations?
Use:
● .env files
l/
🔹 Q76. What is logging in [Link] production apps?
pa
ep
Logging tracks system activity.
de
Tools:
an
● Winston
/s
● Pino
/in
om
Best practice:
.c
Log:
in
● request id
ed
● user id
k
lin
● error stack
w.
w
Tools:
ht
● Prometheus
● Grafana
● Datadog
🔹 Q78. What is distributed tracing?
Tracks request across microservices.
Tools:
● Jaeger
l/
● Zipkin
pa
ep
🔹 Q79. What is health check endpoint?
de
an
Used by load balancers to verify service health.
[Link]('/health', (req, res) => [Link]("OK"))
Two environments:
.c
● Blue (current)
in
● Green (new)
ed
Example
s:
tp
● vault services
l/
pa
Implemented at:
ep
● API gateway
de
● NGINX
an
● Cloudflare
/s
/in
🔹 Q85. What is zero downtime deployment?
om
Techniques:
ed
● Rolling updates
k
lin
● Blue-green
w.
● Canary
w
//w
Example
Cloudflare for:
● images
● js files
🔹 Q87. What is SSR vs CSR in Node ecosystem?
SSR CSR
l/
pa
🔹 Q88. What is BFF (Backend for Frontend)?
ep
de
Separate backend tailored for each frontend.
an
Example
/s
● Mobile BFF /in
● Web BFF
om
Tools:
w.
● LaunchDarkly
w
Ensures:
● No data loss
● Close DB connections