r/programming • u/ThanksMorningCoffee • Apr 14 '22
1,000,000 Concurrent Connections
https://josephmate.github.io/2022-04-14-max-connections/7
u/Girlydian Apr 15 '22
Really cool write-up. One question though, why are you adding a bunch of 10.0.0.0/8 addresses to your loopback if you already have the whole 127.0.0.0/8 pointing that way?
You could just connect to 127.0.0.1, 127.0.0.2, 127.0.0.3 etc. without doing anything. This works at the very least on Linux, and I'd expect it to work on macOS as well to be honest.
2
u/ThanksMorningCoffee Apr 15 '22
Wow thanks. That would have been much easier. I didn't know that. I thought you just got 127.0.0.1.
I tried it on my Mac and it didn't work :( :
ping 127.0.0.5 PING 127.0.0.5 (127.0.0.5): 56 data bytes Request timeout for icmp_seq 0 Request timeout for icmp_seq 1 Request timeout for icmp_seq 2 Request timeout for icmp_seq 3 ^C --- 127.0.0.5 ping statistics --- 5 packets transmitted, 0 packets received, 100.0% packet lossI'll try again when I'm on my Linux desktop.
2
u/Girlydian Apr 15 '22
I didn't try pinging it tbho, I just tried to connect to the SSH server on that address. But yeah, this should work
5
u/ThanksMorningCoffee Apr 15 '22
It works in Linux! Thanks again!
ping 127.0.0.5 PING 127.0.0.5 (127.0.0.5) 56(84) bytes of data. 64 bytes from 127.0.0.5: icmp_seq=1 ttl=64 time=0.048 ms 64 bytes from 127.0.0.5: icmp_seq=2 ttl=64 time=0.041 ms 64 bytes from 127.0.0.5: icmp_seq=3 ttl=64 time=0.040 ms ^C --- 127.0.0.5 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2043ms rtt min/avg/max/mdev = 0.040/0.043/0.048/0.003 ms1
4
u/ioneska Apr 15 '22
Besides the default socket buffer, you can freely change it per-socket: https://stackoverflow.com/questions/10614962/tweaking-performance-of-javas-sockets
1
u/ThanksMorningCoffee Apr 15 '22
Very nice
Sets a default proposed value for the SO_RCVBUF option for sockets accepted from this ServerSocket. The value actually set in the accepted socket must be determined by calling Socket.getReceiveBufferSize() after the socket is returned by accept().
The value of SO_RCVBUF is used both to set the size of the internal socket receive buffer, and to set the size of the TCP receive window that is advertized to the remote peer.
It is possible to change the value subsequently, by calling Socket.setReceiveBufferSize(int). However, if the application wishes to allow a receive window larger than 64K bytes, as defined by RFC1323 then the proposed value must be set in the ServerSocket before it is bound to a local address. This implies, that the ServerSocket must be created with the no-argument constructor, then setReceiveBufferSize() must be called and lastly the ServerSocket is bound to an address by calling bind().
Failure to do this will not cause an error, and the buffer size may be set to the requested value but the TCP receive window in sockets accepted from this ServerSocket will be no larger than 64K bytes.
ServerSocket.html#setReceiveBufferSize(int)
Sets the SO_RCVBUF option to the specified value for this Socket. The SO_RCVBUF option is used by the platform's networking code as a hint for the size to set the underlying network I/O buffers.
Increasing the receive buffer size can increase the performance of network I/O for high-volume connection, while decreasing it can help reduce the backlog of incoming data.
Because SO_RCVBUF is a hint, applications that want to verify what size the buffers were set to should call getReceiveBufferSize().
The value of SO_RCVBUF is also used to set the TCP receive window that is advertized to the remote peer. Generally, the window size can be modified at any time when a socket is connected. However, if a receive window larger than 64K is required then this must be requested before the socket is connected to the remote peer. There are two cases to be aware of:
For sockets accepted from a ServerSocket, this must be done by calling ServerSocket.setReceiveBufferSize(int) before the ServerSocket is bound to a local address.For client sockets, setReceiveBufferSize() must be called before connecting the socket to its remote peer.
Socket.html#setReceiveBufferSize(int)
Sets the SO_SNDBUF option to the specified value for this Socket. The SO_SNDBUF option is used by the platform's networking code as a hint for the size to set the underlying network I/O buffers.
Because SO_SNDBUF is a hint, applications that want to verify what size the buffers were set to should call getSendBufferSize().
36
u/Grouchy_Client1335 Apr 14 '22
Pretty much. A connection is identified by a tuple - source ip, source port, destination ip and destination port.
4
Apr 15 '22
Phoenix and WhatsApp are two different things are they not?
I assume WhatsApp has a robust system of many servers, load balancing, and segmentation for regions and a ton of other things I probably don't understand.
Phoenix is a Framework that is being tested to handle specifically the connections in ideal conditions.
I appreciate the experiment, but not everything needs to be compared especially when so many details are unknown.
4
u/Aistina Apr 15 '22
They are two different things, but there are similarities too. Phoenix is written in Elixir, which runs on the Erlang VM. WhatsApp is written in Erlang*, so runs on the same VM.
*it may use less Erlang than it used to before being bought by Facebook, there is conflicting information about this if you Google for it.
3
u/ThanksMorningCoffee Apr 15 '22
Yeah they are different, but they both demonstrate 2,000,000 connections on a single server port. I was using their results to help argue that severs don't consume a port for each connection and are not limited to 65k ports.
37
u/nada_b Apr 14 '22
Nice article, Thank you!. I may try the same test steps but with C# and WCF. I hope I can reach realistic figures.
5
3
-6
u/Brilliant-Sky2969 Apr 14 '22 edited Apr 14 '22
The code example is not very good and practical, it's blocking everywhere. https://github.com/josephmate/java-by-experiments/blob/main/max_connections/src/Main.java#L80
As of 2022, it's pretty easy to make 1M connection at decent speed on most modern languages because we have libraries that can handle that.
I'm not sure why I'm being downvoted actually, I stand my point, node , go rust, c#, java etc ... can handle a lot of connections with way less memory than this article, I mean 8 GB for 80k connections is insane.
Simple Go example with 1M connections using 600MB of memory: https://speakerdeck.com/eranyanay/going-infinite-handling-1m-websockets-connections-in-go?slide=35
52
u/ThanksMorningCoffee Apr 14 '22 edited Apr 14 '22
My choice of the word 'practical' is causing confusion. I chose it to contrast against the theoretical analysis. It's not meant to reflect a real world workload. The purpose of the code was to demonstrate the max number of concurrent connections you can open simultaneously. By sending and receiving 4 bytes on each connection, it shows that they are still open have not been disconnected.
In this workload, I don't believe any language would have performed better because the workload is minimal. The main constraint is memory and the OS config, not the programming language.
-2
u/Brilliant-Sky2969 Apr 14 '22 edited Apr 14 '22
In this workload, I don't believe any language would have performed better because the workload is minimal. The main constraint is memory and the OS config, not the programming language.
Yes any modern library would have performed much much better, since you use Java try Netty for example, I bet you will reduce your usage by 10-100x.
30
u/dacian88 Apr 14 '22
kernel space memory usage != user space memory usage...the real bottleneck is in the kernel, if you have tons of open connections with large buffers in the kernel, you will hard OOM there much faster than in user space.
you're really missing the point of the article if you think the user space framework is what is being focused on here...if you download netty and try to connect 1 milion sockets to your server you'd hit the same issues.
-2
u/Brilliant-Sky2969 Apr 14 '22 edited Apr 14 '22
OP's server is wrong anyway, there is a single thread looping over every sockets, so in a real scenario with OP example you would need one thread per connection which would add 8KB*1M which would add 10GB or more of memory. ( don't remember exactly the stack size for a posix thread on linux )
The example implementation does not even work, like you're connection #123123 and you have to wait the loop to finish reading buffers ....
16
u/Kangalioo Apr 14 '22
You can run multiple connections on a single thread in parallel with non blocking IO
-4
u/Brilliant-Sky2969 Apr 15 '22
Well I don't think op knows about epoll or equivalent looking at the current implementation.
12
u/dacian88 Apr 14 '22
The article makes no commentary on how to manage your user space programs, you’re focusing on the implementation of a testing tool used to verify the opening and use of connections.
-2
u/Brilliant-Sky2969 Apr 15 '22
Why call it a client / server then, the server does not work at all. Then the post should be, this is how much memory the mac os / linux kernel needs to open 1m socket, which every pretty much every knows that 2x buffer x xx KB is in the GB range.
The very minimum of this should be a server that can reply at any time, not 3 sequential loops. At least a ping server or something, not a "script".
9
4
u/Hugehead123 Apr 14 '22
I'd be really interested to see the results of this. If these async frameworks really do manage concurrent connections that much more effectively I'd be seriously impressed! However I think OP is probably right that the limit is the OS config and resources, as Phoenix had to do the same steps of increasing file handles as well. They weren't using additional loopback addresses to get around the port number limitation (great trick!) and instead used discrete servers so they didn't need quite as many handles, but it looks like they still increased the number of file handles for each client server as well. I think that would end up being the big limit with the sort of demo from the OP regardless, that running 1 million client sockets simultaneously would be an unavoidable bottleneck of this type of benchmark.
8
u/Brilliant-Sky2969 Apr 14 '22
1m connections with 600MB usage: https://speakerdeck.com/eranyanay/going-infinite-handling-1m-websockets-connections-in-go?slide=35
I'm sure you can do even better in C++ or Rust.
The key is to re-use buffer, thread pool and proper syscalls.
-18
u/karl-marks Apr 14 '22
Where? Sounds like you don't have any actual experience with this and are just talking out of your ass and using vague language like "decent speed".
-1
74
u/[deleted] Apr 14 '22
Not that it matters much, but the "theoretical limit" is not 1 quadrillion, because such things as IPv6 exist. This bumps your calculation by a few (30) orders of magnitude:
IPv4: 232 × 216 = 281 × 1012 vs.
IPv6: 2128 × 216 = 22.3 × 1042