PostgreSQL UNIX domain sockets vs TCP sockets - performance

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?

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.

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.

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.

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

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.

Related

Socket of which type for data sharing between Kernel and userspace

Im new to socket programing . I wanted to send few data from Kernel to userspace. I wanted know socket of which family and protocol is suitable to create?
UDP,
TCP,
RAW,
NETLINK,
It will be better if anyone explains usecase of socket types.
Thanks in advance
Check the following socket API
int socket(int domain, int type, int protocol);
1)Netlink sockets are used for communicating between Userspace and the kernel space. Check the following link for example.
2)TCP(of type SOCK_STREAM) and UDP(SOCK_DGRAM) are used mostly for communicating over network. These sockets are of the domain AF_INET. TCP is used for file downloading like application, where delivey and order is guaranteed. UDP is used in cases where latency is important, than delivery or retransmission. Like in the case of Live Video stream. Even a frame of video is skipped, it still should not go for retransmission and slow down the 'LIVE' effect.
3)Then there is usage of sockets for IPC(Inter process Communication). In that case the domain is AF_UNIX and the type used can be SOCK_SEQPACKET(similar to TCP)

How Windows routing works on application level?

Imagine I have Windows TCP socket. And the application connects this socket once on startup. And then sends/receives TCP traffic for long time.
Windows has ability to route IP traffic. Imagine you have multiple network adaptors and you have to set static routing for your application that the traffic goes to a particular NIC.
The question is - will Windows waste CPU cycles to route TCP socket connection only or will it route every IP packet?
I am counting microseconds and I need to know precisely - will be there CPU overhead on sending / receiving the traffic or connection only ?
I assume by "routing" you mean the process of looking at the local routing table to decide where an outgoing packet should be sent. This is decided first by which router to use, and second which interface to use to get to that router.
If you have established a static route, "routing" must still occur for the system to see that route. This consists of a table lookup which takes just a few dozen machine instructions. It is absolutely negligible compared to the cost of copying the packet around.
Keep in mind that binding a socket to a network interface is not the same as entering static rules into the routing table, and that a network interface is not the same as a Network Interface Controller (NIC). This is important when considering overhead, because the effect of binding or routing to a particular network interface, may be that the packet gets copied extra times which will create substantial overhead.
It is possible to contrive a scenario in which a packet is transmitted on the LAN, re-read by the same computer that transmitted it, then transmitted again through a different NIC to the correct router. Most often, the best performance will be had by binding to INADDR_ANY (address 0.0.0.0) and letting the routing tables handle the optimization for you.
Binding to a particular network interface should only be done if you need to ensure that a particular IP address is used for sending and receiving. Static routing to a particular NIC seems unlikely to produce useful results unless the local routing is already broken in some way. Otherwise, interfering with the normal routing process just risks adding to the overhead.

About comet Long TCP connection and perforance

I am new to comet,and have two questions:
I think comet will cause the TCP connection between client and server become long(than normal request/response),this will reduce server performance?(server has TCP connection size limit)
And sometimes the nature of the device or network can prevent an application from maintaining a long-lived TCP connection to a server.how comet aviod this issue?
On Linux (epoll) or BSD (kqueue), you can have hundreds of thousands of idle connections without a performance pennalty (except memory usage). The same is not true on other systems which hit the wall much earlier: because of the limited pool of Windows handles allocated for this purpose in the kernel, your applications will suffer (unless you invest in an 'unlimited' Windows Server license).
Proxy servers notably (low-end routers also), will cut idle connections after a short delay but the usual workaround is to use connection keep-alives.
Hope it helps.

How to access ethernet port using VC++? without using sockets

I'm a beginner to ethernet programming, I want to access the ethernet port and send/recieve packets to/from it. I thought of using the CIM_EthernetPort class, but don't know how to do it. please help. I have to write a program to send data to an embedded system.
I don't think you quite understand what sockets are.
What protocol does your embedded device use? If it's TCP or UDP (or even a raw link protocol) it's pretty much using a socket by definition.
The only reason you might need to talk to an ethernet adapter without using sockets is if you want to write something like a wire level monitor or packet sniffer.
ps -------------------------------------------------------------
Sockets are just an abstraction of a network connection, since a UDP connection has an endpoint and a port it's a bit philosophical wether you are using sockets if you talk directly to the network card hardware.
You don't specify a baud rate as such on ethernet, the card hw will negotiate a speed.
There is very little overhead in sending data by UDP using sockets and it's usually a very efficent way of receiving data. Do you have an RTOS on the embedded device or were you trying to talk to the network adaptor directly with some sort of polling?
pps ------------------------------------------------------------------
If it is at all possible to use UDP on the embedded device - DO SO.
If you start with, I just need to send a few numbers, you ultimately end up re-inventing and re-solving all the things UDP was invented to do.
By using standard protocols you also get tools to let you test each end of the connection (there are UDP equivalents of hyperterm for free).
Also there is no need for multiple devices now, but there may be in the future. I argued for using UDP on a previous product I worked on - where the designers were thinking of ethernet as just a faster serial link. Being able to network many units of this product together has created a huge new market.

AF_INET for IPC in windows

I wish to know that If protocol family AF_INET is used for local communication (IPC) in windows systems. (with loopback interface) will it be able to give performance same as AF_UNIX in unix. I tried for Named pipe in windows but it doesn't seems to me working for my case.
AF_INET when used it will go through the TCP/IP stack. which will be overhead in case of intranode communication.
Please let me know for any clarification.
The IPC using the loopback interface is quiet fast. I can't compare the performance with a AF_UNIX implementation. But it should give you a similar programming interface (sockets).
For high performance IPC you might consider using shared memory. But in that case you would miss the comfort of the streaming communication as you find with sockets.
You should also check, what requirement leads you to the performance question?

Resources