QA
Senior LevelNode JS

How do UNIX Domain Sockets achieve 40% lower latency than TCP loopback for IPC communication?

❓ Interview Prompt: Explain UNIX Domain Sockets (`/tmp/app.sock`), IPC communication, bypassing TCP/IP stack overhead, and NGINX proxy integration.

When routing traffic between an NGINX reverse proxy and a local Node.js app, connecting over TCP ('localhost:3000') incurs TCP 3-way handshake and checksum overhead.

UNIX Domain Sockets ('server.listen('/tmp/app.sock')'):

Bypasses the entire TCP/IP network layer and routing tables in the Linux kernel, transferring data directly through kernel memory buffers for 40% lower latency!

Code Implementation Example

javascript
import http from 'node:http';
import fs from 'node:fs';

const socketPath = '/tmp/node_app.sock';

// Unlink stale socket file if it exists
if (fs.existsSync(socketPath)) fs.unlinkSync(socketPath);

const server = http.createServer((req, res) => {
  res.end('Response over UNIX Domain Socket!');
});

server.listen(socketPath, () => {
  console.log('Server listening on UNIX Domain Socket: ${socketPath}');
});
💡Binding a Node.js HTTP server to a UNIX Domain Socket file path.
🎯 Interview Key Takeaway

Use UNIX Domain Sockets for local IPC and NGINX reverse proxies to bypass TCP network stack overhead.

#UNIX Sockets#IPC#Performance#Networking#Senior
Back to all Node JS Questions→

More Node JS Interview Questions

Beginner LevelNode JS

How does the Node.js Event Loop work? Explain the 6 execution phases (Timers, Pending, Poll, Check, Close).

Beginner LevelNode JS

What does 'Single-Threaded, Event-Driven, Non-Blocking I/O' mean in Node.js?

Beginner LevelNode JS

What is the difference between process.nextTick() and setImmediate()?