Asyncio TCP vs Unix Sockets

March 28, 2024 Python Asyncio

You can create TCP and Unix socket servers in asyncio programs.

TCP sockets are suited for network applications, allowing connections from both local and remote clients. Unix sockets can be faster than TCP sockets, although are file-based and typically limited to client connections on one machine.

In this tutorial, you will discover the difference between Asyncio TCP and Unix socket servers in Python.

Let's get started.

How to Create an Asyncio Server

Asyncio supports the development of socket servers that allow non-blocking reading and writing with connected clients.

There are two ways to create an asyncio server, they are:

  1. Create a TCP Socket server.
  2. Create a Unix Socket server.

Let's take a closer look at how to create each in turn.

How to Create a TCP Socket Server

We can create an asyncio TCP socket server using the asyncio.start_server() function.

This function takes many arguments that configure the server, although critically these include the client callback function, the host, and the port number.

Create a TCP server (socket type SOCK_STREAM) listening on port of the host address. Returns a Server object.

-- Event Loop, Asyncio API Documentation.

For example:

...
# create a tcp socket server
server = await asyncio.start_server(handler, '127.0.0.1', 888)

The server will accept stream or TCP socket connections, e.g. socket.SOCK_STREAM as opposed to datagram or UDP connections. By default, the family will be automatically chosen as either socket.AF_INET or socket.AF_INET6 (e.g. IPv4 or IPv6).

You can learn more about how to create an asyncio TCP socket server in the tutorial:

How to Create a Unix Socket Server

We can create an asyncio Unix socket server using the asyncio.start_unix_server() function.

This function takes many arguments, although critically these include a path to the file that represents the socket.

path is the name of a Unix domain socket, and is required, unless a sock argument is provided.

-- Event Loop, Asyncio API Documentation.

For example:

...
# create a unix socket server
server = await asyncio.start_unix_server('./unix.socket')

The server will accept socket.AF_UNIX family connections that are the stream type, e.g. socket.SOCK_STREAM.

This server can only be created on Unix and Unix-like platforms (e.g. Linux and macOS, not Windows).

Now that we know how to create TCP and Unix socket servers, let's learn more about them.

What Are Sockets

We now know that we can start two main types of socket servers in asyncio.

But what are they? Let's take a moment to understand the two main types of socket servers.

They are:

  1. TCP Socket Servers
  2. Unix Socket Servers

Let's review each in turn.

What Are TCP Sockets

TCP (Transmission Control Protocol) sockets are a fundamental communication mechanism in computer networks.

They provide a reliable, connection-oriented channel for data exchange between applications.

A network socket is a software structure within a network node of a computer network that serves as an endpoint for sending and receiving data across the network.

-- Network socket, Wikipedia.

In a TCP socket communication, two entities, often referred to as the client and the server, establish a connection before exchanging data. The connection is established through a process known as the three-way handshake, which involves the exchange of control messages to synchronize and establish a reliable connection. Once the connection is established, data can be sent bidirectionally.

TCP sockets are extensively used in client-server architectures, where a server listens for incoming connections from clients. Web servers and browsers utilize TCP for delivering web content, ensuring that data such as HTML pages, images, and other resources are reliably transmitted. Additionally, protocols like FTP (File Transfer Protocol), SMTP (Simple Mail Transfer Protocol), and SSH (Secure Shell) rely on TCP for secure and dependable communication.

Because of the standardization of the TCP/IP protocols in the development of the Internet, the term network socket is most commonly used in the context of the Internet protocol suite, and is therefore often also referred to as Internet socket. In this context, a socket is externally identified to other hosts by its socket address, which is the triad of transport protocol, IP address, and port number.

-- Network socket, Wikipedia.

One of the key features of TCP is its reliability. It guarantees the delivery of data without loss or corruption, and it ensures that data is delivered in the same order in which it was sent. This makes TCP suitable for applications where the integrity and order of data are critical, such as file transfers, remote login sessions, and database transactions.

While TCP provides a robust and dependable communication channel, it introduces some overhead due to its connection-oriented nature and additional control messages. This can result in slightly higher latency compared to connectionless protocols like UDP (User Datagram Protocol). However, the trade-off is the assurance of reliable data delivery.

You can learn more about TCP sockets in the article:

What Are Unix Sockets

UNIX sockets, also known as IPC (Inter-Process Communication) sockets or Unix Domain sockets, provide a communication mechanism between processes on the same Unix-like operating system.

A Unix domain socket aka UDS or IPC socket (inter-process communication socket) is a data communications endpoint for exchanging data between processes executing on the same host operating system. It is also referred to by its address family AF_UNIX.

-- Unix domain socket, Wikipedia.

Unlike TCP sockets, UNIX sockets operate locally within a single machine, offering a fast and efficient way for processes to exchange data without the overhead associated with network communication.

UNIX sockets leverage the file system to establish communication channels between processes. They are represented as special files in the file system hierarchy, residing in the file namespace. Processes can communicate through these sockets using standard file I/O operations, such as reading and writing.

One notable advantage of UNIX sockets is their low overhead and high speed, as they operate entirely within the kernel and avoid the complexities of network protocols. This makes them ideal for communication between processes on the same machine, providing a lightweight and efficient alternative to network-based communication.

UNIX sockets are commonly used for communication between different components of a single application or between separate applications running on the same host. They are well-suited for scenarios where low latency and high throughput are crucial, such as communication between a web server and a database server on the same machine.

Security is another notable aspect of UNIX sockets. Since they operate locally, there is no exposure to network-based attacks. Access to UNIX sockets is governed by file permissions, providing a level of control over which processes can communicate through a particular socket.

You can learn more about Unix sockets in the article:

Compare Asyncio TCP vs Unix Socket Server

TCP sockets and UNIX sockets serve as fundamental communication mechanisms, each with its own set of characteristics and use cases.

Let's compare each in turn.

TCP Sockets

TCP sockets are widely used for network communication in a distributed environment.

They provide reliable, connection-oriented communication over a network. Key features include:

UNIX Sockets

UNIX sockets provide communication channels within the same machine, typically between processes running on a Unix-like operating system. Key features include:

Comparison

Key Considerations Between TCP and Unix Sockets

There are additional critical considerations when choosing between TCP and UNIX sockets, depending on the specific requirements and characteristics of the application:

1. Network Boundaries:

2. Overhead and Efficiency:

3. Latency:

4. Firewall Considerations:

5. Platform Dependency:

6. Scaling and Load Balancing:

7. Complexity and Development Overhead:

Choosing

The choice between TCP and UNIX sockets depends on the specific requirements of the communication scenario.

It may involve trade-offs based on factors such as network boundaries, efficiency, latency, firewall considerations, platform dependency, scalability, and development complexity.

  1. TCP sockets are well-suited for networked environments where portability and reliability are crucial.
  2. UNIX sockets shine in scenarios where local communication speed and security within a single machine are the primary concerns.

Takeaways

You now know the difference between Asyncio TCP and Unix socket servers.