
Chess Architecture Design. The web socket connections are interesting to manage!
How many socket connections can one server handle?
10,000? 20,000?
The answer might surprise you.
1. Server Ports
Many engineers believe that each socket takes one port to listen to. If this were true, we would be limited to 65,535 sockets.
This isn't true.
A socket is a logical object that consists of three things:
- An IP Address
- A transport protocol
- A port number
That's why multiple clients can connect to the same 8080 port on most servers. The same port number can be used by multiple sockets!
So, there is no limit to the number of sockets in terms of ports.
2. Linux File Descriptors
Isn't a socket just a file? Is listening to a socket equivalent to reading a file?
Yes. Absolutely.
Aha, so the limit to the number of sockets we can open is the number of file descriptors allowed by the machine. In Linux, that's 590432.
Well, you could always go to the Linux Kernel and change the limit!
This is something we need to do as system engineers of Chat Applications, Live Streaming Apps, and Gaming Platforms to host millions of sockets on a single server.
As Pirates of the Caribbean tells us:
These aren't rules as much as guidelines.
So, engineers get to break them!
3. Theoretical Limit
As mentioned above, Sockets are defined by IP Address, Protocol, and Port.
If we specify the protocol, like TCP, we are left with Port and IP Address identifiers.
Isn't that a limit? 2^16 ports coupled with 2^32 IP addresses would be a limited (albeit massive) number of 2^48 sockets per machine.
Yes! That's the upper limit we have on sockets. Currently, a machine can host at most 281 trillion sockets.
But there is a catch. Connections and sockets are different concepts!
Every connection has two sockets. One on the client and one on the server.
One server socket can be used to maintain multiple connections. This means that there really is no limit to the number of users who can connect to your cloud server.
Theoretically.
WebSockets, Network Protocols, and Operating System limits are concepts that determine the feasibility of popular apps.
Knowing these concepts in depth as a software engineer adds value to your team. Our system design course at InterviewReady is filled with engineering knowledge nuggets.
Handling Connections at Scale: https://interviewready.io/learn/system-design-course/chess-design-building-a-highly-scalable-turn-based-gaming-website/handling_connections_at_scale
Connection-Related Thundering Herds: https://interviewready.io/learn/system-design-course/chess-design-building-a-highly-scalable-turn-based-gaming-website/connection_related_thundering_herds
Networks Deep Dive: https://interviewready.io/learn/system-design-course/networks-deep-dive/http_web_sockets_tcp_and_udp
You are worth the investment. Cheers!