highload.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <h3>Optimizing 3proxy for high load</h3>
  2. <p>Precaution 1: 3proxy was not initially developed for high load and is positioned as a SOHO product, the main reason is "one connection - one thread" model 3proxy uses. 3proxy is known to work with above 200,000 connections under proper configuration, but use it in production environment under high loads at your own risk and do not expect too much.
  3. <p>Precaution 2: This documentation is incomplete and is not sufficient. High loads may require very specific system tuning including, but not limited to specific or cusomized kernels, builds, settings, sysctls, options, etc. All this is not covered by this documentation.
  4. <h4>Configuring 'maxconn'</h4>
  5. A number of simulatineous connections per service is limited by 'maxconn' option.
  6. Default maxconn value since 3proxy 0.8 is 500. You may want to set 'maxconn'
  7. to higher value. Under this configuration:
  8. <pre>
  9. maxconn 1000
  10. proxy -p3129
  11. proxy -p3128
  12. socks
  13. </pre>
  14. maxconn for every service is 1000, and there are 3 services running
  15. (2 proxy and 1 socks), so, for all services there can be up to 3000
  16. simulatineous connections to 3proxy.
  17. <p>Avoid setting 'maxconn' to arbitrary high value, it should be carefully
  18. choosen to protect system and proxy from resources exhaution. Setting maxconn
  19. above resources available can lead to denial of service conditions.
  20. <h4>Understanding resources requirements</h4>
  21. Each running service require:
  22. <ul>
  23. <li>1*thread (process)
  24. <li>1*socket (file descriptor)
  25. <li>1 stack memory segment + some heap memory, ~64K-128K depending on the system
  26. </ul>
  27. Each connected client require:
  28. <ul>
  29. <li>1*thread (process)
  30. <li>2*socket (file descriptor). For FTP 4 sockets are required.
  31. <br>Under linux since 0.9 splice() is used. It's much more effective, but requires
  32. <br>2*socket (file descriptor) + 2*pipe (file descriptors) = 4 file descriptors.
  33. <br>For FTP 4 sockets and 2 pipes are required with splice().
  34. <br>Up to 128K (up to 256K in the case of splice()) of kernel buffers memory. This is theoretical maximum, actual numbers depend on connection quality and traffic amount.
  35. <br>1 additional socket (file descriptor) during name resolution for non-cached names
  36. <br>1 additional socket during authentication or logging for RADIUS authentication or logging.
  37. <li>1*ephemeral port (3*ephemeral ports for FTP connection).
  38. <li>1 stack memory segment of ~32K-128K depending on the system + at least 16K and up to few MB (for 'proxy' and 'ftppr') of heap memory. If you are short of memory, prefer 'socks' to 'proxy' and 'ftppr'.
  39. <li>a lot of system buffers, specially in the case of slow network connections.
  40. </ul>
  41. Also, additional resources like system buffers are required for network activity.
  42. <h4>Setting ulimits</h4>
  43. Hard and soft ulimits must be set above calculated requirements. Validate
  44. ulimits match your expectation, especially if you run 3proxy under dedicated account
  45. by adding e.g.
  46. <pre>
  47. system "ulimit -Ha >>/tmp/3proxy.ulim.hard"
  48. system "ulimit -Sa >>/tmp/3proxy.ulim.soft"
  49. </pre>
  50. in the beginning (before first service started) and the end of config file.
  51. Make both hard restart (that is kill and start 3proxy process) and soft restart
  52. by sending SIGUSR1 to 3proxy process, check ulimits recorded to files match your
  53. expecation.
  54. <h4>Extending system limitation</h4>
  55. Check manuals / documentation for your system limitations. You may need to change
  56. sysctls or even rebuild the kernel from source.
  57. To help with system-dependant settings, since 0.9-devel 3proxy supports different
  58. socket options which can be set via -ol option for listening socket, -oc for proxy-to-client
  59. socket and -os for proxy-to-server socket. Example:
  60. <pre>
  61. proxy -olSO_REUSEADDR,SO_REUSEPORT -ocTCP_TIMESTAMPS,TCP_NODELAY -osTCP_NODELAY
  62. </pre>
  63. available options are system dependant.
  64. <h5>Using 3proxy in virtual environment</h5>
  65. If 3proxy is used in VPS environment, there can be additional limitations.
  66. For example, kernel resources / system CPU usage can be limited in a different way, and this can become a bottleneck.
  67. Since 0.9 devel, 3proxy uses splice() by default on Linux, splice() prevents network traffic from being copied from
  68. kernel space to 3proxy process and generally increases throughput, epecially in the case of high volume traffic. But
  69. since some work is moved to kernel, it requires up to 2 times more system resources in terms of CPU and memory.
  70. Use -s0 option to disable splice() usage for given service, if system resources are additionally limited, e.g.
  71. <pre>
  72. socks -s0
  73. </pre>
  74. <h4>Extending ephemeral port range</h4>
  75. Check ephemeral port range for your system and extend it to the number of the
  76. ports required.
  77. Ephimeral range is always limited to maximum number of ports (64K). To extend the
  78. number of outgoing connections above this limit, extending ephemeral port range
  79. is not enough, you need additional actions:
  80. <ol>
  81. <li> Configure multiple outgoing IPs
  82. <li> Make sure 3proxy is configured to use different outgoing IP by either setting
  83. external IP via RADIUS
  84. <pre>
  85. radius secret 1.2.3.4
  86. auth radius
  87. proxy
  88. </pre>
  89. or by using multiple services with different external
  90. interfaces, example:
  91. <pre>
  92. allow user1,user11,user111
  93. proxy -p1001 -e1.1.1.1
  94. flush
  95. allow user2,user22,user222
  96. proxy -p1001 -e1.1.1.2
  97. flush
  98. allow user3,user33,user333
  99. proxy -p1001 -e1.1.1.3
  100. flush
  101. allow user4,user44,user444
  102. proxy -p1001 -e1.1.1.4
  103. flush
  104. </pre>
  105. or via "parent extip" rotation,
  106. e.g.
  107. <pre>
  108. allow user1,user11,user111
  109. parent 1000 extip 1.1.1.1 0
  110. allow user2,user22,user222
  111. parent 1000 extip 1.1.1.2 0
  112. allow user3,user33,user333
  113. parent 1000 extip 1.1.1.3 0
  114. allow user4,user44,user444
  115. parent 1000 extip 1.1.1.4 0
  116. proxy
  117. </pre>
  118. or
  119. <pre>
  120. allow *
  121. parent 250 extip 1.1.1.1 0
  122. parent 250 extip 1.1.1.2 0
  123. parent 250 extip 1.1.1.3 0
  124. parent 250 extip 1.1.1.4 0
  125. socks
  126. </pre>
  127. <pre>
  128. </pre>
  129. Under latest Linux version you can also start multiple services with different
  130. external addresses on the single port with SO_REUSEPORT on listening socket to
  131. evenly distribute incoming connections between outgoing interfaces:
  132. <pre>
  133. socks -olSO_REUSEPORT -p3128 -e 1.1.1.1
  134. socks -olSO_REUSEPORT -p3128 -e 1.1.1.2
  135. socks -olSO_REUSEPORT -p3128 -e 1.1.1.3
  136. socks -olSO_REUSEPORT -p3128 -e 1.1.1.4
  137. </pre>
  138. for Web browsing last two examples are not recommended, because same client can get
  139. different external address for different requests, you should choose external
  140. interface with user-based rules instead.
  141. <li> You may need additional system dependant actions to use same port on different IPs,
  142. usually by adding SO_REUSEADDR (SO_PORT_SCALABILITY for Windows) socket option to
  143. external socket. This option can be set (since 0.9 devel) with -os option:
  144. <pre>
  145. proxy -p3128 -e1.2.3.4 -osSO_REUSEADDR
  146. </pre>
  147. Behavior for SO_REUSEADDR and SO_REUSEPOR is different between different system,
  148. even between different kernel versions and can lead to unexpected results.
  149. Specifics is described <a href="https://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t">here</a>.
  150. Use this options only if actually required and if you fully understand possible
  151. consiquences. E.g. SO_REUSEPORT can help to establish more connections than the
  152. number of the client port available, but it can also lead to situation connections
  153. are randomely fail due to ip+port pairs collision if remote or local system
  154. doesn't support this trick.
  155. </ol>
  156. <h4>Setting stacksize</h4>
  157. 'stacksize' is a size added to all stack allocations and can be both positive and
  158. negative. Stack is required in functions call. 3proxy itself doesn't require large
  159. stack, but it can be required if some
  160. purely-written libc, 3rd party libraries or system functions called. There is known\
  161. dirty code in Unix ODBC
  162. implementations, build-in DNS resolvers, especially in the case of IPv6 and large
  163. number of interfaces. Under most 64-bit system extending stacksize will lead
  164. to additional memory space usage, but do not require actual commited memory,
  165. so you can inrease stacksize to relatively large value (e.g. 1024000) without
  166. the need to add additional phisical memory,
  167. but it's system/libc dependant and requires additional testing under your
  168. installation. Don't forget about memory related ulimts.
  169. <p>For 32-bit systems address space can be a bottlneck you should consider. If
  170. you're short of address space you can try to use negative stack size.
  171. <h4>Known system issues</h4>
  172. There are known race condition issues in Linux / glibc resolver. The probability
  173. of race condition arises under configuration with IPv6, large number of interfaces
  174. or IP addresses or resolvers configured. In this case, install local recursor and
  175. use 3proxy built-in resolver (nserver / nscache / nscache6).
  176. <h4>Do not use public resolvers</h4>
  177. Public resolvers like ones from Google have ratelimits. For large number of
  178. requests install local caching recursor (ISC bind named, PowerDNS recursor, etc).
  179. <h4>Avoid large lists</h4>
  180. Currently, 3proxy is not optimized to use large ACLs, user lists, etc. All lists
  181. are processed lineary. In devel version you can use RADIUS authentication to avoid
  182. user lists and ACLs in 3proxy itself. Also, RADIUS allows to easily set outgoing IP
  183. on per-user basis or more sophisicated logics.
  184. RADIUS is a new beta feature, test it before using in production.
  185. <h4>Avoid changing configuration too often</h4>
  186. Every configuration reload requires additional resources. Do not do frequent
  187. changes, like users addition/deletaion via connfiguration, use alternative
  188. authentication methods instead, like RADIUS.
  189. <h4>Consider using 'noforce'</h4>
  190. 'force' behaviour (default) re-authenticates all connections after
  191. configuration reload, it may be resource consuming on large number of
  192. connections. Consider adding 'noforce' command before services started
  193. to prevent connections reauthentication.
  194. <h4>Do not monitor configuration files directly</h4>
  195. Using configuration file directly in 'monitor' can lead to race condition where
  196. configuration is reloaded while file is being written.
  197. To avoid race conditions:
  198. <ol>
  199. <li> Update config files only if there is no lock file
  200. <li> Create lock file then 3proxy configuration is updated, e.g. with
  201. "touch /some/path/3proxy/3proxy.lck". If you generate config files
  202. asynchronously, e.g. by user's request via web, you should consider
  203. implementing existance checking and file creation as atomic operation.
  204. <li>add
  205. <pre>
  206. system "rm /some/path/3proxy/3proxy.lck"
  207. </pre>
  208. at the end of config file to remove it after configuration is successfully loaded
  209. <li> Use a dedicated version file to monitor, e.g.
  210. <pre>
  211. monitor "/some/path/3proxy/3proxy.ver"
  212. </pre>
  213. <li> After config is updated, change version file for 3proxy to reload configuration,
  214. e.g. with "touch /some/path/3proxy/3proxy.ver".
  215. </ol>
  216. <h4>Use TCP_NODELAY to speed-up connections with small amount of data</h4>
  217. If most requests require exchange with a small amount of data in a both ways
  218. without the need for bandwidth, e.g. messengers or small web request,
  219. you can eliminate Nagle's algorithm delay with TCP_NODELAY flag. Usage example:
  220. <pre>
  221. proxy -osTCP_NODELAY -ocTCP_NODELAY
  222. </pre>
  223. sets TCP_NODELAY for client (oc) and server (os) connections.
  224. <p>Do not use TCP_NODELAY on slow connections with high delays and then
  225. connection bandwidth is a bottleneck.
  226. <h4>Use slice to speedup large data amount transfers</h4>
  227. slice() allows to copy data between connections without copying to process
  228. addres space. It can speedup proxy on high bandwidth connections, if most
  229. connections require large data transfers. "-s" allows slice usage. Example:
  230. <pre>
  231. proxy -s
  232. </pre>
  233. Slice is only available in Linux and is currently beta option available in
  234. devel version. Do not use it in production without testing. Slice requires
  235. more system buffers, but reduces process memory usage.
  236. Do not use slice if there is a lot of short-living connections with no bandwidth
  237. requirements.
  238. <p>Use slice only on high-speed connections (e.g. 10GBE), if processor or
  239. bus are bottlenecks.
  240. <p>TCP_NODELAY and slice are not contrary to each over and can be combined on
  241. high-speed connections.