← All notes

Networking Field notes

Socket options with clear boundaries

Socket options are small controls with specific effects. Treat each option as a response to an observed requirement, and check its return value instead of assuming it took effect.

Know which layer you are changing

setsockopt() takes a socket descriptor, an option level, an option name, and a value. General socket settings use SOL_SOCKET; TCP-specific settings use IPPROTO_TCP. A failed call reports an error. Unsupported options and invalid values should be visible in the application's diagnostics, because silently continuing can hide a changed operating assumption.

Address reuse is not one feature

SO_REUSEADDR relaxes certain local address binding restrictions. A common use is restarting a TCP listener while old connections remain in TIME_WAIT. It does not grant unrestricted permission to bind an address already held by an active listener. Set it before bind(), and check the rules of the operating system you are targeting.

SO_REUSEPORT is a separate mechanism. On Linux, cooperating sockets can bind the same address and port when they all enable it before binding, subject to ownership and other checks. This can distribute incoming connections across listeners. It does not distribute the bytes of one established TCP stream among independent readers.

Liveness and latency answer different questions

SO_KEEPALIVE enables transport-level probes for an otherwise idle connection. The default wait may be much longer than an application's response deadline. TCP-specific settings can adjust probe timing on Linux, but a healthy TCP peer does not prove that its application is making progress. Use an application deadline or heartbeat when that distinction matters.

TCP_NODELAY disables Nagle's algorithm. It can help interactive workloads that send small messages and wait for responses, at the cost of potentially sending more small packets. It does not guarantee immediate delivery, eliminate congestion, or override every source of buffering. Grouping related writes in the application may be a better first step.

Keep a record of effective settings

For buffer options, use getsockopt() to read back the applied value. Linux's accounting can report twice the size requested through SO_SNDBUF or SO_RCVBUF. Configured limits may also constrain an ordinary request. Compare the returned value with the documented semantics, rather than treating a numerical mismatch as automatic failure.

Test options under the intended workload, including connection setup, idle periods, and shutdown. Record the kernel version and error path as well as the successful case. A short list of justified options is easier to maintain than a collection copied from unrelated services.