Skip to content

Instantly share code, notes, and snippets.

@joennlae
Forked from mustafaturan/network-tweak.md
Last active June 13, 2023 20:54
Show Gist options
  • Save joennlae/7c822f641d78117eedcae6a68c2c3964 to your computer and use it in GitHub Desktop.
Save joennlae/7c822f641d78117eedcae6a68c2c3964 to your computer and use it in GitHub Desktop.
Linux Network Tweak for 2 million web socket connections

Sample config for 2 million web socket connection

    sysctl -w fs.file-max=12000500
    sysctl -w fs.nr_open=20000500
    # Set the maximum number of open file descriptors
    ulimit -n 20000000

    # Set the memory size for TCP with minimum, default and maximum thresholds 
    sysctl -w net.ipv4.tcp_mem='10000000 10000000 10000000'

    # Set the receive buffer for each TCP connection with minumum, default and maximum thresholds
    sysctl -w net.ipv4.tcp_rmem='1024 4096 16384'

    # Set the TCP send buffer space with minumum, default and maximum thresholds 
    sysctl -w net.ipv4.tcp_wmem='1024 4096 16384'

    # The maximum socket receive buffer sizemem_max=16384
    sysctl -w net.core.rmem_max=16384
    
    # The maximum socket send buffer size
    sysctl -w net.core.wmem_max=16384

Changing values through the ulimit command only applies to the current shell session, So if you want to set a new limit permanently, edit the /etc/security/limits.conf file and set your hard and soft limits. Here's an example:

# <domain> <type> <item>  <value>
    *       soft  nofile  20000
    *       hard  nofile  20000

Save the file, log-out, log-in again and test the configuration through the ulimit -n command.

Keep the following in mind:

Soft limit: value that the kernel enforces for the corresponding resource. Hard limit: works as a ceiling for the soft limit. Additional files in /etc/security/limits.d/ might affect what is configured in limits.conf.

nginx config

    worker_processes  4;  # 2 * Number of CPUs

    events {
        worker_connections  19000;  # It's the key to high performance - have a lot of connections available
    }

    worker_rlimit_nofile    20000;  # Each connection needs a filehandle (or 2 if you are proxying)
    # Total amount of users you can serve = worker_processes * worker_connections

[1] - https://stackoverflow.com/questions/7325211/tuning-nginx-worker-process-to-obtain-100k-hits-per-min/8217856#8217856

References

[1] - https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Tuning_and_Optimizing_Red_Hat_Enterprise_Linux_for_Oracle_9i_and_10g_Databases/sect-Oracle_9i_and_10g_Tuning_Guide-Adjusting_Network_Settings-Changing_Network_Kernel_Settings.html

[2] - https://wwwx.cs.unc.edu/~sparkst/howto/network_tuning.php

[3] - http://stackoverflow.com/questions/11342167/how-to-increase-ulimit-on-amazon-ec2-instance

More Reading

Talks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment