[转帖]How fast are Unix domain sockets?
https://blog.myhro.info/2017/01/how-fast-are-unix-domain-sockets
Jan 3, 2017 • Tiago Ilieve
Warning: this is my first post written in English, after over five years writing only in Portuguese. After reading many technical articles written in English by non-native speakers, I’ve wondered: imagine how much information I would be missing if they wrote those posts in French or Russian. Following their examples, this blog can also reach a much wider audience as well.
It probably happened more than once, when you ask your team about how a reverse proxy should talk to the application backend server. “Unix sockets. They are faster.”, they’ll say. But how much faster this communication will be? And why a Unix domain socket is faster than an IP socket when multiple processes are talking to each other in the same machine? Before answering those questions, we should figure what Unix sockets really are.
Unix sockets are a form of inter-process communication (IPC) that allows data exchange between processes in the same machine. They are special files, in the sense that they exist in a file system like a regular file (hence, have an inode and metadata like ownership and permissions associated to it), but will be read and written using recv() and send() syscalls instead of read() and write(). When binding and connecting to a Unix socket, we’ll be using file paths instead of IP addresses and ports.
In order to determine how fast a Unix socket is compared to an IP socket, two proofs of concept (POCs) will be used. They were written in Python, due to being small and easy to understand. Their implementation details will be clarified when needed.
IP POC
ip_server.py
#!/usr/bin/env python
import socket
server_addr = '127.0.0.1'
server_port = 5000
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((server_addr, server_port))
sock.listen(0)
print 'Server ready.'
while True:
conn, _ = sock.accept()
conn.send('Hello there!')
conn.close()
ip_client.py
#!/usr/bin/env python
import socket
import time
server_addr = '127.0.0.1'
server_port = 5000
duration = 1
end = time.time() + duration
msgs = 0
print 'Receiving messages...'
while time.time() < end:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((server_addr, server_port))
data = sock.recv(32)
msgs += 1
sock.close()
print 'Received {} messages in {} second(s).'.format(msgs, duration)
Unix domain socket POC
uds_server.py
#!/usr/bin/env python
import os
import socket
server_addr = '/tmp/uds_server.sock'
if os.path.exists(server_addr):
os.unlink(server_addr)
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.bind(server_addr)
sock.listen(0)
print 'Server ready.'
while True:
conn, _ = sock.accept()
conn.send('Hello there!')
conn.close()
uds_client.py
#!/usr/bin/env python
import socket
import time
server_addr = '/tmp/uds_server.sock'
duration = 1
end = time.time() + duration
msgs = 0
print 'Receiving messages...'
while time.time() < end:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(server_addr)
data = sock.recv(32)
msgs += 1
sock.close()
print 'Received {} messages in {} second(s).'.format(msgs, duration)
As we can see by those code snippets, both implementations are close to each other as possible. The differences between them are:
- Their address family:
socket.AF_INET(IP) andsocket.AF_UNIX(Unix sockets). - To bind a process using
socket.AF_UNIX, the socket file should be removed and created again if it already exists. - When using
socket.AF_INET, thesocket.SO_REUSEADDRflag have to be set in order to avoidsocket.error: [Errno 98] Address already in useerrors that may occur even when the socket is properly closed. This option tells the kernel to reuse the same port if there are connections in theTIME_WAITstate.
Both POCs were executed on a Core i3 laptop running Ubuntu 16.04 (Xenial) with stock kernel. There is no output at every loop iteration to avoid the huge performance penalty of writing to a screen. Let’s take a look at their performances.
IP POC
First terminal:
$ python ip_server.py
Server ready.
Second terminal:
$ python ip_client.py
Receiving messages...
Received 10159 messages in 1 second(s).
Unix domain socket POC
First terminal:
$ python uds_server.py
Server ready.
Second terminal:
$ python uds_client.py
Receiving messages...
Received 22067 messages in 1 second(s).
The Unix socket implementation can send and receive more than twice the number of messages, over the course of a second, when compared to the IP one. During multiple runs, this proportion is consistent, varying around 10% for more or less on both of them. Now that we figured their performance differences, let’s find out why Unix sockets are so much faster.
It’s important to notice that both IP and Unix socket implementations are using TCP ( (see update 1). What happens is that when Unix sockets are used, the entire IP stack from the operating system will be bypassed. There will be no headers being added, socket.SOCK_STREAM), so the answer isn’t related to how TCP performs in comparison to another transport protocol like UDP, for instancechecksums being calculated (see update 2), encapsulation and decapsulation of packets being done nor routing being performed. Although those tasks are performed really fast by the OS, there is still a visible difference when doing benchmarks like this one.
There’s so much room for real-world comparisons besides this synthetic measurement demonstrated here. What will be the throughput differences when a reverse proxy like nginx is communicating to a Gunicorn backend server using IP or Unix sockets? Will it impact on latency as well? What about transfering big chunks of data, like huge binary files, instead of small messages? Can Unix sockets be used to avoid Docker network overhead when forwarding ports from the host to a container?
References:
- Beej’s Guide to Unix IPC, 11. Unix Sockets (Brian “Beej Jorgensen” Hall)
- Programmation Systèmes, Cours 9 - UNIX Domain Sockets (Stefano Zacchiroli)
- Unix Domain Sockets - Python Module of the Week (Doug Hellmann)
- unix domain sockets vs. internet sockets (Robert Watson)
- What exactly does SO_REUSEADDR do? (Hermelito Go)
Updates:
- John-Mark Gurney and Justin Cormack pointed out that
SOCK_STREAMdoesn’t mean TCP under Unix domain sockets. This makes sense, but I couldn’t find any reference affirming nor denying it. - Justin Cormack also mentioned that there’s no checksumming on local interfaces by default. Looking at the source code of the Linux loopback driver, this seems to be present in kernel since version
2.6.12-r2.
[转帖]How fast are Unix domain sockets?的更多相关文章
- PHP 调用 Go 服务的正确方式 - Unix Domain Sockets
* { color: #3e3e3e } body { font-family: "Helvetica Neue", Helvetica, "Hiragino Sans ...
- UNIX DOMAIN SOCKETS IN GO unix域套接字
Unix domain sockets in Go - Golang News https://golangnews.org/2019/02/unix-domain-sockets-in-go/ pa ...
- Unix domain sockets
#server: SERVER_PATH = "/tmp/python_unix_socket_server" def run_unix_domain_socket_server( ...
- php, hhvm与odp & Unix domain Socket方式
接上一篇,复习一下 启动php或hhvm: php/sbin/php-fpm start hhvm/bin/hhvm_control start 启动nginx或lighttpd: webserver ...
- 网络协议之:socket协议详解之Unix domain Socket
目录 简介 什么是Unix domain Socket 使用socat来创建Unix Domain Sockets 使用ss命令来查看Unix domain Socket 使用nc连接到Unix do ...
- 由一个简单需求到Linux环境下的syslog、unix domain socket
本文记录了因为一个简单的日志需求,继而对linux环境下syslog.rsyslog.unix domain socket的学习.本文关注使用层面,并不涉及rsyslog的实现原理,感兴趣的读者可以参 ...
- libpqxx接口的在linux下的使用,解决psql:connections on Unix domain socket "/tmp/.s.PGSQL.5432"错误
在项目中使用postgresql数据库时要求在windows和linux双平台兼容.于是在windows下使用的接口在linux下爆出异常: psql:connections on Unix doma ...
- Unix domain socket IPC
UNIX Domain socket 虽然网络socket也可用于同一台主机的进程间通讯(通过lo地址127.0.0.1),但是unix domain socket用于IPC更有效率:不需要经过网络协 ...
- 问题解决:psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
错误提示: psql: could not connect to server: No such file or directory Is the server running locally and ...
- UNIX域套接字(unix domain)
UNIX域套接字用于在同一台机器上运行的进程之间的通信. UNIX域套接字提供流和数据报两种接口. 说明:UNIX域套接字比因特网套接字效率更高.它仅赋值数据:不进行协议处理,如添加或删除网络报头.计 ...
随机推荐
- shutdown详解
linux下shutdown命令详解 shutdown命令安全地将系统关机. 有些用户会使用直接断掉电源的方式来关闭linux,这是十分危险的.因为linux与windows不同,其后台运行着许多进程 ...
- MySQL优化:12种提升SQL执行效率的有效方法
在数据库管理和优化的世界里,MySQL作为一个流行的关系型数据库管理系统,其性能优化是任何数据密集型应用成功的关键.优化MySQL数据库不仅可以显著提高SQL查询的效率,还能确保数据的稳定性和可靠性. ...
- JavaScript this 绑定详解
函数内 this 绑定 函数内this的绑定和函数定义的位置没有关系,和调用的方式和调用位置有关系,函数内的this是在被调用执行时被绑定的. this的具体绑定规则 this 绑定基本包含下面4种绑 ...
- Implicit隐式渲染入门 SDF SphereTracing
Implicit 何为隐式?隐式(Implicit)的是显式(explicit)的反义词. explicit可以简单理解为用网格等信息描述的几何形状,网格信息是离散的,信息量越大描述越精准.Impli ...
- C++篇:第十四章_编程_知识点大全
C++篇为本人学C++时所做笔记(特别是疑难杂点),全是硬货,虽然看着枯燥但会让你收益颇丰,可用作学习C++的一大利器 十四.编程 (一)概念 系统函数及其库是 C++语言所必须的,预处理命令不是 C ...
- 抖音上超好听的神曲音乐,Python教你一次性下载
不知道什么时候开始,中国出现了南抖音.北快手的互文格局(东市买骏马,西市买鞍鞯-).刚才提到了,之前比较喜欢刷抖音,对于我这种佛系程序猿,看网上这些整容妹子基本一个样.喜欢抖音主要是两个初衷,学做菜听 ...
- 六步带你完成博流wifi模组对接华为云流程
摘要:本文主要采用基于博流wifi模组以及我们的SDK移植实现华为云的对接,上报数据以及命令下发等,希望对您有所帮助. 1 简介 首先需要研究透彻博流项目的编译运行流程,首先看其根目录中包括compo ...
- NanoDet:这是个小于4M超轻量目标检测模型
摘要:NanoDet 是一个速度超快和轻量级的移动端 Anchor-free 目标检测模型. 前言 YOLO.SSD.Fast R-CNN等模型在目标检测方面速度较快和精度较高,但是这些模型比较大,不 ...
- Redis现网那些坑:用个缓存,还要为磁盘故障买单?
摘要:向业务查询超时say goodbye! 本文分享自华为云社区<Redis现网那些坑:用个缓存,还要为磁盘故障买单?>,作者: GaussDB 数据库 . 近日,网上一些电商用户出现了 ...
- 边缘AI研发落地生态挑战调研报告发布
摘要:随着边缘设备的广泛使用和性能提升,将机器学习相关的部分任务迁移到边缘,也即边缘AI技术,已成为必然趋势.但仅凭技术是不足够完成落地和产业转化的.近日我们启动了边缘AI研发落地生态挑战调研.我们发 ...