HTTP, Networking & Real-Time Communication
Node.js HTTP internals, DNS, connection reuse, HTTP/2, WebSockets, and SSE.
Q27: How do you create a basic HTTP server using core Node.js modules?
Use http.createServer((req, res) => {}) to handle incoming HTTP connections, set response headers with res.setHeader(), and end the response using res.end().
- Core
httpmodule creates web servers without external frameworks. - Request handlers accept
IncomingMessage(req) andServerResponse(res).
Which method sends the final response body and signals request completion?
Q28: What is HTTP Keep-Alive, and how do you configure connection pooling in Node.js?
HTTP Keep-Alive reuses established TCP sockets for multiple HTTP request/response cycles, eliminating repeated TCP three-way handshake and TLS negotiation latencies. In Node.js, enable socket pooling by passing keepAlive: true and configuring maxSockets and maxFreeSockets on http.Agent or https.Agent instances.
- Keep-Alive avoids establishing new TCP sockets on every outgoing HTTP request.
- Configured via
http.Agent({ keepAlive: true, maxSockets: 100 }).
Which parameter on http.Agent limits total parallel open connections permitted per origin host?
Q29: How does HTTP/2 multiplexing differ from HTTP/1.1 keep-alive in Node.js?
In HTTP/1.1 with persistent connections (keep-alive), requests over a single TCP connection must be served sequentially (head-of-line blocking). HTTP/2 uses binary framing to split messages into independent frames transmitted over a single TCP socket concurrently (multiplexing). The Node.js http2 core module supports stream prioritization, server push, and header compression (HPACK), allowing hundreds of concurrent requests without opening multiple parallel TCP connections.
- HTTP/1.1 causes head-of-line blocking by queueing requests sequentially on a connection.
- HTTP/2 multiplexing sends interleaved binary frames over a single TCP connection simultaneously.
Which HTTP/1.1 issue is directly eliminated by HTTP/2 multiplexing?
Q30: Why can dns.lookup be blocking in Node.js?
dns.lookup uses the underlying operating system getaddrinfo(3) function synchronously on Libuv thread pool workers, whereas dns.resolve makes network calls asynchronously without thread pool workers.
dns.lookupuses system resolver calls on Libuv threads.dns.resolvebypasses system files (/etc/hosts) making network queries directly.
Which DNS module function directly performs DNS queries over the network without Libuv thread pool workers?
Q31: Why does Node.js core HTTP client lack built-in DNS caching and how do you resolve it?
Node.js core http.request() resolves DNS hostnames on every single request using dns.lookup(), which executes system calls on the Libuv thread pool without caching results. Under heavy HTTP traffic, this can stall worker threads and cause socket bottlenecks. Developers mitigate this by using custom HTTPS Agents with lookup caching packages (e.g., dnscache) or keep-alive sockets.
- Core HTTP clients perform fresh DNS lookups per request without caching.
- Absence of DNS caching causes thread pool saturation; fix using persistent agents or lookup caching modules.
Which core subsystem handles dns.lookup() system resolution calls in Node.js?
Q32: What are WebSockets and when should they be used over HTTP polling?
WebSockets provide persistent, full-duplex TCP connections for real-time bidirectional communication. Use them for real-time features like chat applications or live data feeds.
- Full-duplex real-time communication over single TCP connections.
- Eliminates overhead associated with HTTP header polling requests.
Which protocol upgrade header converts an HTTP connection into a WebSocket?
Q33: How do Server-Sent Events (SSE) work in Node.js and how do they differ from WebSockets?
Server-Sent Events (SSE) provide unidirectional real-time text streaming from server to client over standard HTTP using the text/event-stream content type. Unlike WebSockets, which are full-duplex TCP connections, SSE runs over standard HTTP, includes native automatic client reconnection, and works seamlessly with standard HTTP proxies and load balancers.
- SSE enables single-direction text streaming using the standard
text/event-streamMIME type. - WebSockets provide bidirectional TCP messaging, whereas SSE simplifies server-to-client updates over HTTP.
Which HTTP Content-Type header must a Node.js response set to stream Server-Sent Events?
Q34: How do Sticky Sessions work when load balancing WebSocket connections across Node.js cluster workers?
WebSockets require initial HTTP upgrades that transition into persistent TCP sockets. In a clustered Node.js environment, if handshake requests land on worker A but subsequent frames land on worker B, connection state breaks. Sticky sessions ensure that all requests from a single client IP or session cookie are routed consistently to the exact same worker process.
- Stateful protocols like WebSockets require persistent routing to the same worker instance.
- Sticky sessions route requests consistently using IP hashing or session cookies.
Why are sticky sessions required when load balancing WebSocket handshakes across multiple cluster workers?