tune.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. .. _tune:
  2. ============
  3. Tuning Guide
  4. ============
  5. Tuning the Linux Kernel, Networking, Erlang VM and emqttd broker for one million concurrent MQTT connections.
  6. -------------------
  7. Linux Kernel Tuning
  8. -------------------
  9. The system-wide limit on max opened file handles::
  10. # 2 millions system-wide
  11. sysctl -w fs.file-max=2097152
  12. sysctl -w fs.nr_open=2097152
  13. echo 2097152 > /proc/sys/fs/nr_open
  14. The limit on opened file handles for current session::
  15. ulimit -n 1048576
  16. /etc/sysctl.conf
  17. ----------------
  18. Add the 'fs.file-max' to /etc/sysctl.conf, make the changes permanent::
  19. fs.file-max = 1048576
  20. /etc/security/limits.conf
  21. -------------------------
  22. Persist the limits on opened file handles for users in /etc/security/limits.conf::
  23. * soft nofile 1048576
  24. * hard nofile 1048576
  25. --------------
  26. Network Tuning
  27. --------------
  28. Increase number of incoming connections backlog::
  29. sysctl -w net.core.somaxconn=32768
  30. net.ipv4.tcp_max_syn_backlog=16384
  31. sysctl -w net.core.netdev_max_backlog=16384
  32. Local Port Range::
  33. sysctl -w net.ipv4.ip_local_port_range=1000 65535
  34. Read/Write Buffer for TCP connections::
  35. sysctl -w net.core.rmem_default=262144
  36. sysctl -w net.core.wmem_default=262144
  37. sysctl -w net.core.rmem_max=16777216
  38. sysctl -w net.core.wmem_max=16777216
  39. sysctl -w net.core.optmem_max=16777216
  40. #sysctl -w net.ipv4.tcp_mem='16777216 16777216 16777216'
  41. sysctl -w net.ipv4.tcp_rmem='1024 4096 16777216'
  42. sysctl -w net.ipv4.tcp_wmem='1024 4096 16777216'
  43. Connection Tracking::
  44. sysctl -w net.nf_conntrack_max=1000000
  45. sysctl -w net.netfilter.nf_conntrack_max=1000000
  46. sysctl -w net.netfilter.nf_conntrack_tcp_timeout_time_wait=30
  47. The TIME-WAIT Buckets Pool, Recycling and Reuse::
  48. net.ipv4.tcp_max_tw_buckets=1048576
  49. net.ipv4.tcp_tw_recycle = 1
  50. net.ipv4.tcp_tw_reuse = 1
  51. Timeout for FIN-WAIT-2 sockets::
  52. net.ipv4.tcp_fin_timeout = 15
  53. ---------
  54. Erlang VM
  55. ---------
  56. Tuning and optimize the Erlang VM in etc/vm.args file::
  57. ## max number of erlang processes
  58. +P 2097152
  59. ## Sets the maximum number of simultaneously existing ports for this system
  60. +Q 1048576
  61. ## Increase number of concurrent ports/sockets, deprecated in R17
  62. -env ERL_MAX_PORTS 1048576
  63. -env ERTS_MAX_PORTS 1048576
  64. ## Mnesia and SSL will create temporary ets tables.
  65. -env ERL_MAX_ETS_TABLES 1024
  66. ## Tweak GC to run more often
  67. -env ERL_FULLSWEEP_AFTER 1000
  68. -------------
  69. emqttd broker
  70. -------------
  71. Tune the acceptor pool, max_clients limit and sockopts for TCP listener in etc/emqttd.config::
  72. {mqtt, 1883, [
  73. %% Size of acceptor pool
  74. {acceptors, 64},
  75. %% Maximum number of concurrent clients
  76. {max_clients, 1000000},
  77. %% Socket Access Control
  78. {access, [{allow, all}]},
  79. %% Connection Options
  80. {connopts, [
  81. %% Rate Limit. Format is 'burst, rate', Unit is KB/Sec
  82. %% {rate_limit, "100,10"} %% 100K burst, 10K rate
  83. ]},
  84. ...
  85. --------------
  86. Client Machine
  87. --------------
  88. Tune the client machine to benchmark emqttd broker::
  89. sysctl -w net.ipv4.ip_local_port_range="500 65535"
  90. sysctl -w fs.file-max=1000000
  91. echo 1000000 > /proc/sys/fs/nr_open
  92. ulimit -n 100000
  93. ---------------
  94. emqtt_benchmark
  95. ---------------
  96. Test tool for concurrent connections: http://github.com/emqtt/emqtt_benchmark