Tuning guide

Since 4.2 EMQX had been stress-tested with 1.3 million on an 8-core, 32G memory CentOS server.

This guide includes in general tuning suggestions for one EMQX broker to serve about 1 million clients.

Turn off swap

Linux swap partitions may cause nondeterministic memory latency to Erlang virtual machine, which in turn significantly affects the system stability. It is recommended to turn off swap permanently.

To turn off swap immediately, execute command sudo swapoff -a. To turn off swap permanently, comment out the swap line in /etc/fstab and reboot the host

Linux Kernel Tuning

The system-wide limit on max opened file handles:

  1. # 2 millions system-wide
  2. sysctl -w fs.file-max=2097152
  3. sysctl -w fs.nr_open=2097152
  4. echo 2097152 > /proc/sys/fs/nr_open

The limit on opened file handles for current session:

  1. ulimit -n 2097152

/etc/sysctl.conf

Persist ‘fs.file-max’ configuration to /etc/sysctl.conf:

  1. fs.file-max = 2097152

Set the maximum number of file handles for the service in /etc/systemd/system.conf:

  1. DefaultLimitNOFILE=2097152

emqx.service

Set the maximum number of file handles for emqx service in e.g. one of below paths depending on which linux distribution is in use.

  • /usr/lib/systemd/system/emqx.service
  • /lib/systemd/system/emqx.service
  1. LimitNOFILE=2097152

/etc/security/limits.conf

Persist the maximum number of opened file handles for users in /etc/security/limits.conf:

  1. * soft nofile 2097152
  2. * hard nofile 2097152

TCP Network Tuning

Increase number of incoming connections backlog:

  1. sysctl -w net.core.somaxconn=32768
  2. sysctl -w net.ipv4.tcp_max_syn_backlog=16384
  3. sysctl -w net.core.netdev_max_backlog=16384

Local port range

  1. sysctl -w net.ipv4.ip_local_port_range='1000 65535'

TCP Socket read/write buffer:

  1. sysctl -w net.core.rmem_default=262144
  2. sysctl -w net.core.wmem_default=262144
  3. sysctl -w net.core.rmem_max=16777216
  4. sysctl -w net.core.wmem_max=16777216
  5. sysctl -w net.core.optmem_max=16777216
  6. #sysctl -w net.ipv4.tcp_mem='16777216 16777216 16777216'
  7. sysctl -w net.ipv4.tcp_rmem='1024 4096 16777216'
  8. sysctl -w net.ipv4.tcp_wmem='1024 4096 16777216'

TCP connection tracking:

  1. sysctl -w net.netfilter.nf_conntrack_max=1000000
  2. sysctl -w net.netfilter.nf_conntrack_tcp_timeout_time_wait=30

TIME-WAIT Bucket Pool, Recycling and Reuse:

  1. sysctl -w net.ipv4.tcp_max_tw_buckets=1048576
  2. # Enabling following option is not recommended. It could cause connection reset under NAT
  3. # sysctl -w net.ipv4.tcp_tw_recycle=1
  4. # sysctl -w net.ipv4.tcp_tw_reuse=1

Timeout for FIN-WAIT-2 Sockets:

  1. sysctl -w net.ipv4.tcp_fin_timeout=15

Erlang VM Tuning

Tuning and optimize the Erlang VM in etc/emqx.conf file

  1. ## Erlang Process Limit
  2. node.process_limit = 2097152
  3. ## Sets the maximum number of simultaneously existing ports for this system
  4. node.max_ports = 2097152

When running in docker

Usually you should tune the linux docker host by following the above guide.

If you want to tune linux kernel by docker, you must ensure your docker is latest version (>=1.12).

Here is an example to show how it looks.

  1. docker run -d --name emqx -p 18083:18083 -p 1883:1883 \
  2. --sysctl fs.file-max=2097152 \
  3. --sysctl fs.nr_open=2097152 \
  4. --sysctl net.core.somaxconn=32768 \
  5. --sysctl net.ipv4.tcp_max_syn_backlog=16384 \
  6. --sysctl net.core.netdev_max_backlog=16384 \
  7. --sysctl net.ipv4.ip_local_port_range='1000 65535' \
  8. --sysctl net.core.rmem_default=262144 \
  9. --sysctl net.core.wmem_default=262144 \
  10. --sysctl net.core.rmem_max=16777216 \
  11. --sysctl net.core.wmem_max=16777216 \
  12. --sysctl net.core.optmem_max=16777216 \
  13. --sysctl net.ipv4.tcp_rmem='1024 4096 16777216' \
  14. --sysctl net.ipv4.tcp_wmem='1024 4096 16777216\ \
  15. --sysctl net.ipv4.tcp_max_tw_buckets=1048576 \
  16. --sysctl net.ipv4.tcp_fin_timeout=15 \
  17. emqx/emqx:latest

::: REMEMBER The best practice is NOT to run docker --privileged and NOT to mount system volumes to the container for kernel tuning. :::

EMQX Broker Tuning

tor pool, maxand socket options. config in etc/emqx.conf } config in etc/listeners.conf

  1. ## TCP Listener
  2. listener.tcp.external = 0.0.0.0:1883
  3. listener.tcp.external.acceptors = 64
  4. listener.tcp.external.max_connections = 1024000

Client Machine Tuning

Tune the client machine to benchmark emqttd broker:

  1. sysctl -w net.ipv4.ip_local_port_range="500 65535"
  2. echo 1000000 > /proc/sys/fs/nr_open
  3. ulimit -n 100000

emqtt_bench

Test tool for concurrent connections: http://github.com/emqx/emqtt_benchSystem tuning - 图1 (opens new window)