PostgreSQL UNIX domain sockets vs TCP sockets
Asked Answered
D

5

59

I wonder if the UNIX domain socket connections with postgresql are faster then tcp connections from localhost in high concurrency rate and if it does, by how much?

Diet answered 2/11, 2008 at 21:50 Comment(1)
The context as written is 'postgresql': In that case, it's a very specific Q. about the two ways to connect to a local DB.Emilioemily
V
45

UNIX domain sockets should offer better performance than TCP sockets over loopback interface (less copying of data, fewer context switches), but I don't know whether the performance increase can be demonstrated with PostgreSQL.

I found a small comparison on the FreeBSD mailinglist: http://lists.freebsd.org/pipermail/freebsd-performance/2005-February/001143.html.

Vernavernacular answered 2/11, 2008 at 22:21 Comment(0)
M
65

Postgres core developer Bruce Momjian has blogged about this topic. Momjian states, "Unix-domain socket communication is measurably faster." He measured query network performance showing that the local domain socket was 33% faster than using the TCP/IP stack.

Mccord answered 24/8, 2012 at 0:1 Comment(3)
The overhead is +175% when SSL for TCP is enabled.Mecke
@GregDan perhaps you can point me to a source for this claim? tia!Pahl
@Pahl This from the linked article.Mecke
V
45

UNIX domain sockets should offer better performance than TCP sockets over loopback interface (less copying of data, fewer context switches), but I don't know whether the performance increase can be demonstrated with PostgreSQL.

I found a small comparison on the FreeBSD mailinglist: http://lists.freebsd.org/pipermail/freebsd-performance/2005-February/001143.html.

Vernavernacular answered 2/11, 2008 at 22:21 Comment(0)
C
7

afaik, unix domain socket (UDS) work like system pipes and it send ONLY data, not send checksum and other additional info, not use three-way handshake as TCP sockets...

ps: maybe UDS will be more faster

Cesaria answered 2/11, 2008 at 22:24 Comment(0)
P
7

I believe that UNIX domain sockets in theory give better throughput than TCP sockets on the loopback interface, but in practice the difference is probably negligible.

Data carried over UNIX domain sockets don't have to go up and down through the IP stack layers.

re: Alexander's answer. AFAIK you shouldn't get any more than one context switch or data copy in each direction (i.e. for each read() or write()), hence why I believe the difference will be negligble. The IP stack doesn't need to copy the packet as it moves between layers, but it does have to manipulate internal data structures to add and remove higher-layer packet headers.

Phore answered 2/11, 2008 at 22:26 Comment(0)
E
-5

TCP sockets on localhost are usually implemented using UNIX domain sockets, so the answer on most systems is neglijable to none. However, this is not standard in any way -- it is just how usually it is done, therefore you should not depend on this.

Exocrine answered 2/11, 2008 at 22:7 Comment(3)
Just out of curiosity, on which OSes is TCP loopback implemented using UNIX domain sockets?Vernavernacular
none AFAIK - but several databases automatically attempt to use the UNIX domain socket if you specify "localhost" for the DB connection string.Phore
PostgreSQL is one which doesn't - if you specify localhost, it will use TCP loopback, if you specify nothing, it will use the domain socket.Burletta

© 2022 - 2024 — McMap. All rights reserved.