All Projects → joewalnes → Port Sharding

joewalnes / Port Sharding

Example of Linux SO_REUSEPORT

Programming Languages

python
139335 projects - #7 most used programming language

Linux port sharding demo

Demo showing how to use the SO_REUSEPORT linux socket option, allowing multiple processes to listen on the same TCP or UDP port.

Example available in Ruby and Python (both do the same thing):

What is this?

SO_REUSEPORT sharding

See my really quick Linux Port Sharding walkthrough first.

Setup

  • If you're on Linux with kernel 3.9 or later (check with uname -v), you can just run the Python or Ruby scripts directly.
  • If you're on Mac OSX, fret not, install Docker and run make, which will install a working Docker image and drop you into a ready to go tmux session, where you can call the scripts.

Usage

Run the server, it listens on port 1234. Connect to it with netcat or telnet to see it do something: nc localhost 1234.

Now run another server process, and a few more. You'll see the kernel allows these all to listen on the same port.

Connect to the port. You'll see the kernel picks one of the processes and allows it to handle it. Next time it may be another process.

You can add and remove processes. You can even run both the Python and Ruby processes at the same time and the kernel will share the workload. However, note that queued connections which have not been accepted will be lost when stopping a process.

More

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].