Questions: Sockets and Network Inter-Process Communication
5 questions to test your understanding
Score: 0 / 5
Question 1 Multiple Choice
A DNS server needs to respond to thousands of short queries per second from many clients. Occasional packet loss is acceptable because clients retry automatically. Which socket type is most appropriate?
ATCP (SOCK_STREAM), because reliability is important for DNS correctness
BUDP (SOCK_DGRAM), because low latency and connectionless operation suit short, retry-tolerant queries
CUnix domain sockets, because DNS servers and clients run on the same machine
DTCP with SO_LINGER set to zero, to avoid the three-way handshake overhead
DNS is the canonical real-world example of UDP's strengths. Each query is a single small datagram with a self-contained response — no state needs to persist between exchanges. UDP eliminates TCP's three-way handshake, which would double the round-trip time for a protocol where sub-millisecond response is normal. Clients already implement their own retry logic, so OS-level reliability is unnecessary overhead. TCP is appropriate when data ordering, reliability, and multi-message sessions matter (HTTP, SSH, databases). Unix domain sockets are for local IPC only.
Question 2 Multiple Choice
A web application server and its database run on the same physical machine. The developer is choosing between TCP loopback (127.0.0.1) and a Unix domain socket for their connection. What is the primary advantage of the Unix domain socket?
AUnix domain sockets support both TCP and UDP semantics simultaneously
BUnix domain sockets bypass the network stack entirely, reducing overhead and latency
CUnix domain sockets are more secure because they cannot be accessed over a network
DUnix domain sockets allow multiple processes to share a single file descriptor
The decisive advantage is performance. TCP loopback, despite being local, still processes the full network stack — IP header construction, checksum calculation, routing decisions, and TCP segment handling — even though the data never leaves the machine. Unix domain sockets use a filesystem path as their address and transfer data entirely within kernel memory, bypassing all of that. The result is measurably lower latency and higher throughput. While the security benefit (option C) is real, it is a secondary consideration; the primary motivation in production systems like Nginx-to-PostgreSQL connections is reduced overhead.
Question 3 True / False
Unix domain sockets use IP addresses and port numbers to identify communication endpoints.
TTrue
FFalse
Answer: False
Unix domain sockets use filesystem paths as their addresses (e.g., /var/run/postgres/.s.PGSQL.5432 or /tmp/app.sock). Instead of binding to an IP address and port, the server calls bind() with a filesystem path, and the client connects to that path. This is why Unix domain sockets are purely local — they have no concept of a network address. The tradeoff is that both processes must be on the same machine and have access to the same filesystem. This path-based addressing is also why Unix domain sockets leave a socket file on the filesystem that must be cleaned up when the server exits.
Question 4 True / False
A TCP server must call listen() and accept() before a client can connect, establishing a connection through a three-way handshake.
TTrue
FFalse
Answer: True
This is the TCP connection lifecycle. listen() marks the socket as passive (willing to accept incoming connections) and creates a backlog queue for pending connection requests. accept() blocks until a client connects, then returns a new socket dedicated to that client session. The three-way handshake (SYN → SYN-ACK → ACK) happens automatically between client's connect() and server's accept() returning. This setup cost is the price of TCP's reliability guarantees. UDP, by contrast, has no connection phase — the server simply calls recvfrom() and messages arrive without prior setup.
Question 5 Short Answer
Why might a production system prefer Unix domain sockets over TCP loopback for communication between two processes running on the same machine?
Think about your answer, then reveal below.
Model answer: Unix domain sockets bypass the network stack entirely. TCP loopback still processes IP and TCP headers, performs checksums, and goes through the full kernel networking subsystem even though no data ever crosses a network interface. Unix domain sockets transfer data directly between kernel buffers using a filesystem path as the address, eliminating that overhead. The result is lower latency and higher throughput for local IPC — which matters at scale when a web server makes hundreds of database calls per request.
The network stack isn't free even on loopback. Profiling studies consistently show that Unix domain socket connections have 30-50% lower latency than TCP loopback for typical database query workloads. Production systems like PostgreSQL, Redis, and Nginx all support Unix domain sockets specifically for co-located deployments. The tradeoff is that you lose the ability to move either process to a separate host without a configuration change — but that's usually acceptable when performance is the priority.