قالب وردپرس درنا توس
Home / Tips and Tricks / What are Unix sockets and how do they work? – CloudSavvy IT

What are Unix sockets and how do they work? – CloudSavvy IT



Fiber optic cable connections.
Shutterstock / asharkyu

Unix socket is a form of communication between two processes that is displayed as a file on disk. This file can be used by other applications to create very fast connections between two or more processes at no network cost.

What are withdrawals?

Withdrawal is a direct connection between two processes. Imagine if you wanted to call your friend on the road; you can make a call so that it is routed through your telephone company and back to their house, or you can run a wire directly to their house and cut out intermediaries. The latter is obviously impractical in real life, but in the Unix world it is very common to establish these direct links between programs.

The correct name for unix sockets is Unix domain socket, because they are all in one computer. In a way, the sockets is a network that is completely at the core; rather than using network interfaces to send data, the same data can be sent directly between applications.

Although files are created on the hard disk, Unix sockets do not actually write the data they send to the disk, as it would be far too slow. Instead, all data is stored in the core memory. the only point in the socket file is to maintain a reference to the socket and to give it file system permissions to control access. For example, the MySQL socket is usually at:

/var/lib/mysql/mysql.sock

This file contains nothing, and you should not modify it immediately, except for the permissions where applicable. It̵

7;s just a name.

How do outlets work?

Outlets simply provide the actual hardware to move data around. TCP-based sockets are called power sockets, where all data comes in order. UDP-based sockets are datagram sockets where order (or even delivery) is not guaranteed. There are also raw sockets, which have no restrictions, and which are used to implement various protocols and tools that need to inspect low-level network traffic, such as Wireshark.

Sockets usually still use TCP or UDP, as they are nothing special other than a neat tube in the core. TCP and UDP are transport protocols that define how data comes from place to place but do not really care what data is. TCP and UDP provide the platform for most other protocols such as FTP, SMTP and RDP, which work at higher levels.

It is possible for an application to use a slightly different implementation of TCP; power outlet uses SOCK_STREAM protocol, which is what TCP also uses for transport almost all the time, and while they are basically interchangeable, they are technically a little different. Even if these are low-level things and not really something you need to worry about, just know it most Traffic sent through unix domain sockets is TCP or UDP based, or at least quite similar to it, and TCP sent over unix domain sockets is faster than TCP over network interfaces such as ports.

Withdrawal in practice

Unix sockets are commonly used as an alternative to network-based TCP connections when running processes on the same machine. Data is usually still sent over the same protocol; it just stays within the same machine and knows it is running in the same domain (hence the name unix domain socket), so it never has to worry about a loopback network interface to connect to itself.

The biggest example of this is Redis, an extremely fast key value store that works completely in memory. Redis is often used on the same server as access to it, so you can usually use outlets. At such low levels and with how fast Redis is, the sockets provide a performance boost of 25% in certain synthetic benchmarks.

If you connect to a MySQL database, you can also use an outlet. Usually you would connect to host:port from a remote system, but if you connect to a database on the same server (for example, a REST API that accesses a database), you can use sockets for faster startup. This will not affect normal use, but it is very noticeable when under load, over 20% on an advanced 24-core with 128 concurrent users and one million queries per second. Whether or not you see an advantage with withdrawals is a different story, but at that point, you probably want to investigate replication and load balancing anyway.

If you want to work with outlets manually, you can use socat tools to expose them over network ports:

socat TCP-LISTEN:12345 UNIX-CONNECT:/var/lib/socket.sock

This technically defeats the purpose of unix domain extraction but can be used for troubleshooting the transport layer.


Source link