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.
- 32-bit vs 64-bit
- AES vs RSA
- Agent vs Agentless
- Annotations vs Decorators
- Terraform vs Ansible
- BASH vs ZSH
- BigQuery vs Bigtable
- Block Storage vs File Storage vs Object Storage
- Canvas vs SVG
- cgroupfs vs systemd
- Constructor vs Init() vs Factory
- Container Image vs Helm Chart vs OCI Bundle
- Containers vs Virtual Machines (VMs)
- C vs C++
- Java vs C++
- Rust vs C++
- crictl vs ctr
- Dagger vs Guice
- DMA vs RDMA
- DOM vs Virtual DOM vs Shadow DOM
- DQL vs DDL vs DCL vs DML
- eBPF vs Sidecar
- Flux vs Redux
- Full Disk Encryption vs Partition Encryption
- Gatekeeper vs OPA
- Google vs Open Source
- Why MCP uses JSON-RPC 2.0 instead of grpc
- gRPC vs OpenAPI
- gtag.js vs Google Tag Manager (GTM)
- Versus
- Kotlin vs Java
- Jetty vs Netty
- .js vs .jsx vs .ts vs .tsx
- Kata Containers vs KubeVirt
- Linux Namespace vs Kubernetes Namespace
- Linux vs BSD
- LLVM vs JVM
- Data Mining vs Machine Learning vs Artificial Intelligence vs Data Science
- Managed Kubernetes vs Self-managed Kubernetes
- Management Plane vs Control Plane vs Data Plane
- Microcontroller vs Microprocessor vs Computer
- MMU vs IOMMU
- Single-tenant vs Multi-tenant vs Multi-single-tenant
- Multithreading vs Multiprocessing vs Distributed Systems
- Node.js vs Erlang
- OCI Image vs Docker Image
- OCI vs CRI
- OpenSSH vs OpenSSL
- Pass-by-value vs Pass-by-reference
- Podman vs nerdctl
- POSIX vs SUS vs LSB
- Private Key vs Public Key vs Certificate
- Proto Edition 2024 vs Proto Edition 2023
- Proto2 vs Proto3 vs Proto Editions
- Message Queue vs PubSub
- React vs Flutter vs Angular
- REST vs SOAP
- RSA vs ECDSA
- select vs. poll vs. epoll
- SLI vs SLO vs SLA
- Spanner vs Bigtable
- Static Site Generation vs Server-side Rendering vs Client-side Rendering
- Stack based VM vs Register based VM
- Stateless vs Stateful
- Strong Consistency vs Eventual Consistency
- stubs vs mocks vs fakes
- Subroutines vs Coroutines vs Generators
- Software Engineer vs Site Reliability Engineer
- Symlinks vs Hard Links
- JAX vs Tensorflow
- Tensorflow vs PyTorch
- Terminal vs Shell
- Throttling vs Load Shedding
- Unix Domain Sockets vs Network Sockets
- Unix Domain Sockets vs Shared Memory
- URI vs URN vs URL vs URC
- Valkey vs Redis vs Memcached
- Vi vs Vim vs gVim vs Neovim
- WAL vs rollback journal

