Unix Domain Sockets vs Network Sockets
In the world of Linux and Unix-like systems, processes need ways to talk to each other. This is called IPC (Inter-Process Communication). The two most common ways to do this are via Unix Domain Sockets (UDS) and Network Sockets.
While they use the same standard API (the Berkeley Sockets API—socket(), bind(), listen(), accept()), they work very differently under the hood.
Unix Domain Sockets (UDS)
UDS is used for communication between processes on the same host machine.
- Addressing: It uses a filesystem path as its address (e.g.,
/var/run/docker.sockor/tmp/mysql.sock). - Performance: Extremely fast. Because the kernel knows both processes are on the same machine, it bypasses the entire network stack. There is no routing, no header encapsulation, no checksums, and no sequence numbers.
- Mechanism: It simply copies data from one process's memory buffer to another within the kernel.
Network Sockets (TCP/IP)
Network sockets are designed for communication over a network between different machines, though they can also be used locally via the loopback interface (127.0.0.1).
- Addressing: It uses an IP address and a Port number (e.g.,
127.0.0.1:8080). - Performance: Slower than UDS. Even when talking to a process on the same machine (localhost), the data must go through the TCP/IP stack overhead (loopback).
- Mechanism: It uses full network protocols (TCP, UDP, IP) which involve adding headers, calculating checksums, and handling acknowledgments (ACKs).
Key Comparison
| Feature | Unix Domain Sockets (UDS) | Network Sockets (TCP/IP) |
|---|---|---|
| Scope | Same machine only. | Same machine OR across a network. |
| Address | File path (e.g., /tmp/app.sock) |
IP & Port (e.g., 127.0.0.1:9000) |
| Speed | Very High (direct memory copy). | Lower (network stack overhead). |
| Security | File permissions (Owner/Group). | Firewall rules (iptables/nftables). |
| Reliability | Guaranteed (it’s a memory move). | Depends on protocol (TCP is reliable, UDP is not). |
Why use one over the other?
Use Unix Domain Sockets when:
- Performance is critical: If you have a web server (like Nginx) talking to an application server (like Python Gunicorn or PHP-FPM) on the same box, UDS is roughly 30% to 50% faster than using
127.0.0.1. - Security is a priority: You can use standard Linux file permissions (
chmod,chown) to control who can connect to the socket. Network sockets are "visible" to anyone who can reach the port unless you configure a firewall. - No Port Conflicts: You don't have to worry about "Port 8080 is already in use." You just pick a unique filename.
Use Network Sockets when:
- Scalability: If you might need to move your database or worker to a separate server in the future, network sockets allow this without changing your code architecture.
- Remote Access: Obviously, if the processes are on different physical or virtual machines, you must use network sockets.
- Ease of Testing: It is often easier to debug network sockets using tools like
telnet,curl, orncfrom other machines.
Common Real-World Examples
- Docker: When you run
docker ps, the Docker CLI talks to the Docker Daemon on your local machine via a UDS (/var/run/docker.sock). - Database Connections: When a WordPress site connects to MySQL on the same server, it usually uses a UDS (
/var/lib/mysql/mysql.sock). If the database were on a different server, it would use TCP/IP on port 3306. - Redis: Redis is often used for caching. If Redis is on the same machine as the app, using UDS provides a significant latency reduction.
Summary
If your processes are on the same machine, use Unix Domain Sockets for speed and security. If they are on different machines (or might be in the future), use Network Sockets.