ãã«ãŠã§ã€ãã®åŠçãããŒã¯å°æ°ã§äžå®ã§ããããïŒéåžžã¯ã³ã¢ããšã«1ã€ïŒãã¡ã¢ãªãšããã»ããµãªãœãŒã¹ã¯åãæ¿ãã³ã³ããã¹ãã§ç¯çŽãããŸãã NGINXã®äŸã§ãã®ã¢ãããŒãã®ãã¹ãŠã®å©ç¹ãååã«èŠ³å¯ã§ããŸããNGINXèªäœã¯ãæ°çŸäžã®ãªã¯ãšã¹ããåæã«åŠçã§ããæ¡åŒµæ§ã«åªããŠããŸãã
åããã»ã¹ã¯ã¡ã¢ãªãæ¶è²»ããããã»ã¹ãåãæ¿ãããã³ã«è¿œå ã®ããã»ããµãµã€ã¯ã«ãå¿ èŠã«ãªããLãã£ãã·ã¥ã®ãªãŒãã³ã°ã«ãã€ãªãããŸãã
ã³ã€ã³ã«ã¯æ¬ ç¹ããããŸãã éåæã¢ãããŒãã®äž»ãªåé¡ããŸãã¯ãæµããšèšã£ãæ¹ãããã®ã¯ãæäœããããã¯ããããšã§ãã ãããŠãæ®å¿µãªãããNGINXã®æ©èœã®åçãç解ããŠããªããµãŒãããŒãã£ã¢ãžã¥ãŒã«ã®å€ãã®äœæè ã¯ãã¢ãžã¥ãŒã«ã§ããããã³ã°æäœãå®è¡ããããšããŸãã ãã®ãããªæäœã¯ãNGINXã®ããã©ãŒãã³ã¹ãå®å šã«æãªãå¯èœæ§ãããããããããªãå Žåã§ãåé¿ããå¿ èŠããããŸãã
ããããçŸåšã®NGINXå®è£ ã§ããããã¯ãåé¿ããããšãåžžã«å¯èœãšã¯éããŸããã ãããŠããã®åé¡ã解決ããããã«ãNGINXããŒãžã§ã³1.7.11ã¯ãã¹ã¬ããããŒã«ãã®æ°ããã¡ã«ããºã ãå°å ¥ããŸããã ãããäœã§ãããããããŠãããããã«é©çšããæ¹æ³ãåæããæåã«æµãçŽæ¥ç¥ãããšã«ãªããŸãã
åé¡
åé¡ãããããç解ããããã«ãNGINXãã©ã®ããã«æ©èœãããã«é¢ããäž»èŠãªãã€ã³ããæåã«è©³çŽ°ã«èª¿ã¹ãŸãã
åäœåçã«ããã°ãNGINXã¯ãã®ãããªã€ãã³ããã³ãã©ãŒã§ãããæ¥ç¶ã§çºçãããã¹ãŠã®ã€ãã³ãã«é¢ããæ å ±ãã«ãŒãã«ããåãåãããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã«äœãããããæ瀺ããã³ã³ãããŒã©ãŒã§ãã å®éãNGINXã¯ã·ã¹ãã ãªãœãŒã¹ãæäœããæãå°é£ãªã¿ã¹ã¯ã解決ãããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã¯ãã¹ãŠã®ã«ãŒãã³ãå®è¡ããæ å ±ã®ãã€ããèªã¿åããéä¿¡ããŸãã ãããã£ãŠãNGINXã¯ãŒã¯ãããŒãã€ãã³ãã«ã©ãã ãè¿ éãã€ã¿ã€ã ãªãŒã«å¿çããããéèŠã§ãã
ã¯ãŒã¯ãããŒã¯ãã«ãŒãã«ããã€ãã³ããåä¿¡ããŠââåŠçããŸãã
ãã®ãããªã€ãã³ãã«ã¯ãã¿ã€ããŒã€ãã³ããæ°ããããŒã¿ã®å°çãå¿çã®éä¿¡ããããã¡å ã®ã¹ããŒã¹ã®è§£æŸãæ¥ç¶ãšã©ãŒãŸãã¯ãã®ã¯ããŒãºã®éç¥ããããŸãã NGINXã¯ãã®ãããªã€ãã³ãã®ãã±ãããåä¿¡ããé çªã«åŠçãéå§ããå¿ èŠãªã¢ã¯ã·ã§ã³ãå®è¡ããŸãã ãããã£ãŠãã€ãã³ããã¥ãŒã®ãã¹ãŠã®åŠçã¯ã1ã€ã®ã¹ã¬ããã§åçŽãªãµã€ã¯ã«ã§è¡ãããŸãã NGINXã¯ãã¥ãŒããã€ãã³ãã次ã ãšæœåºãããœã±ãããžã®ããŒã¿ã®æžã蟌ã¿ãèªã¿åããªã©ã®ããã€ãã®ã¢ã¯ã·ã§ã³ãå®è¡ããŸãã ã»ãšãã©ã®å Žåãããã¯éåžžã«è¿ éã«è¡ãããããïŒã»ãšãã©ã®å Žåãã¡ã¢ãªå ã®å°éã®ããŒã¿ãã³ããŒããã ãã§ãïŒããã¹ãŠã®ã€ãã³ããç¬æã«åŠçããããšãæ€èšã§ããŸãã
ãã¹ãŠã®åŠçã¯ã1ã€ã®ã¹ã¬ããã§åçŽãªãµã€ã¯ã«ã§è¡ãããŸãã
ããããããçš®ã®é·ããŠé£ããæäœãå®è¡ããããšãããšã©ããªããŸããïŒ ã€ãã³ãã«ãŒãå šäœãããã®æäœã®å®äºãåŸ æ©ããªããªããŸãã
ãããã£ãŠãããããã³ã°æäœãšã¯ãã€ãã³ãåŠçã®ãµã€ã¯ã«ãå€§å¹ ã«é ãããæäœãæå³ããŸãã æäœã¯ãããŸããŸãªçç±ã§ããããã³ã°ãšåŒã°ããããšããããŸãã ããšãã°ãNGINXã¯é·æéã®èšç®éçŽçãªæäœã§ããžãŒã§ããããäœããã®ãªãœãŒã¹ïŒããŒããã£ã¹ã¯ããã¥ãŒããã¯ã¹ãã©ã€ãã©ãªåŒã³åºããåæã¢ãŒãã§ã®ããŒã¿ããŒã¹ããã®å¿çãåŸ æ©ãããªã©ïŒãžã®ã¢ã¯ã»ã¹ãæåŸ ããå ŽåããããŸãã ããã§éèŠãªã®ã¯ããããã®æäœäžãã¯ãŒã¯ãããŒã¯ä»ã«åœ¹ç«ã€ããšã¯äœãã§ãããä»ã®ã€ãã³ããåŠçã§ããªãããšã§ãããã ããå€ãã®å ŽåããŸã 空ããªãœãŒã¹ãããããã¥ãŒã§ããã«åŸ æ©ããŠããã€ãã³ãã¯ãããã䜿çšã§ããŸãã
巚倧ãªè²·ãæã䞊ãã§ããåºã®å£²ãæãæ³åããŠãã ããã ãããŠä»ãåã®æåã®äººããã§ãã¯ã¢ãŠãã«ãŠã³ã¿ãŒã«è¿ã¥ããçªã«ã¯ãªããé ãã®å庫ã«ããååãè³Œå ¥ããããšèããŠããŸãã 売ãæã¯æ°æéåŸ ã€ããã«é Œã¿ãååã®å庫ã«åããŠåºçºããŸãã 䞊ãã§ããä»ã®é¡§å®¢ã®åå¿ãæ³åã§ããŸããïŒ åŸ æ©æéã¯ããã2æéå¢å ããŸããããå€ãã®å Žåãå¿ èŠãªãã®ã¯ã«ãŠã³ã¿ãŒã®äžã«æ°ã¡ãŒãã«ãããŸãã
ã©ã€ã³å šäœã¯ãæåã®ãã€ã€ãŒã®æ³šæã®å®è¡ãåŸ æ©ããããåŸãŸããã
NGINXã§ãåæ§ã®ç¶æ³ãçºçããŸããéä¿¡ããããã¡ã€ã«ãã¡ã¢ãªå ã§ã¯ãªããããŒããã©ã€ãäžã«ããå Žåã§ãã ãã£ã¹ã¯ã¯äœéã§ããïŒç¹ã«å転ãããã£ã¹ã¯ïŒããã¥ãŒã§åŠçãããã®ãåŸ æ©ããŠããä»ã®èŠæ±ã¯ããŒããã©ã€ããžã®ã¢ã¯ã»ã¹ãå¿ èŠãšããªãå ŽåããããŸãããããã§ãåŸ æ©ããå¿ èŠããããŸãã ãã®çµæãé 延ãå¢å€§ããã·ã¹ãã ãªãœãŒã¹ãå®å šã«å©çšãããªãå ŽåããããŸãã
1åã®ããããã³ã°æäœã§ãåŸç¶ã®ãã¹ãŠã®åŠçãå€§å¹ ã«é 延ããå¯èœæ§ããããŸãã
äžéšã®ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã¯ããã¡ã€ã«ãéåæã§èªã¿åãããã®ã€ã³ã¿ãŒãã§ã€ã¹ãæäŸããNGINXã¯ããããå¹ççã«äœ¿çšã§ããŸãïŒ aioãã£ã¬ã¯ãã£ãã®èª¬æãåç §ïŒã ãã®ãããªã·ã¹ãã ã®è¯ãäŸã¯ãFreeBSDã§ãã æ®å¿µãªãããLinuxã§ãåãããšã¯èšããŸããã Linuxã«ã¯ãã¡ã€ã«ãèªã¿åãããã®ç¹å®ã®éåæã€ã³ã¿ãŒãã§ã€ã¹ããããŸãããå€ãã®é倧ãªæ¬ ç¹ããããŸãã ãã®ãããªèŠä»¶ã®1ã€ã¯ãèªã¿åããšãããã¡ãŒã®ã¢ã©ã€ã¡ã³ãã§ãã NGINXã¯ããã«ããŸã察åŠã§ããŸããã2çªç®ã®åé¡ã¯ããã«æ·±å»ã§ãã éåæèªã¿åãã§ã¯ããã¡ã€ã«èšè¿°åã«
O_DIRECT
ãã©ã°ãèšå®ããå¿ èŠããããŸãã ããã¯ããã¹ãŠã®ããŒã¿ããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã®ããŒãžãã£ãã·ã¥ïŒããããããŒãžãã£ãã·ã¥ ïŒããã€ãã¹ããŠãã£ã¹ã¯ããèªã¿åãããããšãæå³ããŸããå€ãã®å Žåãããã¯æé©ã§ã¯ãªãããã£ã¹ã¯ãµãã·ã¹ãã ã®è² è·ãå€§å¹ ã«å¢å ããŸãã
ç¹ã«ããã®åé¡ã解決ããããã«ãNGINX 1.7.11ã¯ã¹ã¬ããããŒã«ã®æ°ããã¡ã«ããºã ãå°å ¥ããŸããã ãããã¯ãŸã NGINX Plusã«å«ãŸããŠããŸããããã¹ã¬ããããŒã«ã䜿çšããŠNGINX Plus R6ã¢ã»ã³ããªãè©ŠããŠã¿ããå Žåã¯ãå¶æ¥éšéã«ãåãåãããã ããã
ãããŠãããããäœã§ãããã©ã®ããã«æ©èœãããããã詳现ã«åæããŸãã
ã¹ã¬ããããŒã«
äžéãªå£²ãæã«æ»ããŸãããã ããããä»åã¯åœŒã¯ããæ©ç¥ã«å¯ãã§ããããšãããããŸããïŒãŸãã¯ãæã£ã顧客ã«beatãããåŸã§ããïŒïŒãããŠãå® é 䟿ãçµç¹ããŸããã çŸåšããã€ã€ãŒãã«ãŠã³ã¿ãŒã«ãªãååãèŠæ±ãããšãã«ãŠã³ã¿ãŒãé¢ããã®ã§ã¯ãªããèªåã§ååãæ¢ããŠä»ã®å šå¡ã«åŸ ãããååã®é éèŠæ±ãå® é 䟿ã«éä¿¡ããŠã顧客ã®åã«ãµãŒãã¹ãæäŸãç¶ããŸãã ãããã£ãŠãã¹ãã¢ã«æ³šæããªãã£ãè²·ãæã®ã¿ãé éãåŸ ã£ãŠããããã®éã売ãæã¯åé¡ãªãæ®ãã«ãµãŒãã¹ãæäŸã§ããŸãã
ã¯ãŒãªãšã«ãã泚æåŠçã¯ããã¥ãŒããããã¯ããŸããã
NGINXã®å Žåãã¹ã¬ããããŒã«ã¯ã¯ãŒãªãšãšããŠæ©èœããŸãã ãžã§ããã¥ãŒãšããã®ãã¥ãŒãåŠçããåã ã®è»œéã¹ã¬ããã®ã»ããã§æ§æãããŸãã ã¯ãŒã¯ãããŒã¯ãããèªäœã§è¡ãã®ã§ã¯ãªããæœåšçã«é·ãæäœãå¿ èŠãšããå ŽåãåŠçã¿ã¹ã¯ãããŒã«ãã¥ãŒã«å ¥ããããããä»»æã®ããªãŒã¹ããªãŒã ãããã«åŠçãéå§ã§ããŸãã
ã¯ãŒã¯ãããŒã¯ãããããã³ã°åŠçãã¹ã¬ããããŒã«ã«éä¿¡ããŸãã
ããã§å¥ã®ã¿ãŒã³ã圢æãããããã§ãã ããã§ãã ãã ãããã®å Žåããã®ãã¥ãŒã¯ç¹å®ã®ãªãœãŒã¹ã«å¶éãããŸãã 圌ãã§ãããããéããã£ã¹ã¯ããèªã¿åãããšã¯ã§ããŸããããå°ãªããšãèªã¿åããåŸ ã€ããšã§ãä»ã®ã€ãã³ãã®åŠçãé ããããšã¯ãããŸããã
ãã£ã¹ã¯ããã®èªã¿åãã¯ãããããã³ã°æäœã®æãäžè¬çãªäŸãšããŠåãäžããããŠããŸãããå®éã«ã¯ãNGINXã®ã¹ã¬ããããŒã«ã¯ãã¡ã€ã³ã¯ãŒã¯ãµã€ã¯ã«å ã§å®è¡ããã®ãéåççãªä»ã®ã¿ã¹ã¯ã«äœ¿çšã§ããŸãã
çŸåšãã¹ã¬ããããŒã«ãžã®ã¢ããããŒãæäœã¯ãã»ãšãã©ã®ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã®readïŒïŒã·ã¹ãã ã³ãŒã«ãããã³Linuxã®sendfileïŒïŒã«å¯ŸããŠã®ã¿å®è£ ãããŠããŸãã ãã®åé¡ã®èª¿æ»ãç¶ç¶ããããããããã©ãŒãã³ã¹ãåäžããå Žåã¯ãä»åŸãã¹ã¬ããããŒã«ã«ããä»ã®æäœã®å®è¡ãå®è£ ããå¯èœæ§ããããŸãã
ããã©ãŒãã³ã¹ã®ãã¹ã
çè«ããå®è·µãžãšç§»è¡ããæãæ¥ãŸããã ã¹ã¬ããããŒã«ã䜿çšããå¹æã瀺ãããã«ãç°¡åãªå®éšãè¡ããŸãã ã€ãŸãããã£ã¹ã¯ã¢ã¯ã»ã¹ã§ã®ããããã³ã°ã®åé¡ãå®å šã«æããã«ãªã£ããšãã«ãNGINXã«ããããã³ã°ãšãã³ããããã³ã°ã®èªã¿åãã®æ··åãå®è¡ããããæãé£ããæ¡ä»¶ãåçŸããŸãã
ããã«ã¯ããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã®ãã£ãã·ã¥ã«åãŸããªãããšãä¿èšŒãããŠããããŒã¿ã»ãããå¿ èŠã§ãã RAM容éã48 GBã®ãã·ã³ã§ã¯ãã©ã³ãã ããŒã¿ãå«ãå4 MBã®256 GBã®ãã¡ã€ã«ãçæãããNGINXããŒãžã§ã³1.9.0ãèµ·åããŠããããé åžããŸããã
æ§æã¯éåžžã«ç°¡åã§ãã
worker_processes 16; events { accept_mutex off; } http { include mime.types; default_type application/octet-stream; access_log off; sendfile on; sendfile_max_chunk 512k; server { listen 8000; location / { root /storage; } } }
ã芧ã®ãšãããæé«ã®ããã©ãŒãã³ã¹ãåŸãããã«å°ã調æŽãè¡ãããŸããããã®ã³ã°ãç¡å¹ã«ãªãã accept_mutexãç¡å¹ã«ãªãã sendfileããªã³ã«ãªãã sendfile_max_chunkãèšå®ãããŸãã åŸè ã䜿çšãããšã
sendfile()
åŒã³åºãã®ããã¯æéãççž®ã§ããŸãããã®å ŽåãNGINXã¯äžåºŠã«ãã¡ã€ã«å šäœã®èªã¿åããšéä¿¡ãè©Šè¡ããã512ãããã€ãã®éšåã§å®è¡ããããã§ãã
ãã®ãã·ã³ã«ã¯ã2ã€ã®Intel Xeon E5645ããã»ããµïŒåèš12ã³ã¢ã24ã®HyperThreadingã¹ã¬ããïŒãš10 GBãããã¯ãŒã¯ã€ã³ã¿ãŒãã§ã€ã¹ãè£ åãããŠããŸãã ãã£ã¹ã¯ãµãã·ã¹ãã ã¯ãRAID10ã¢ã¬ã€ã«çµåããã4å°ã®Western Digital WD1003FBYXããŒããã©ã€ãã§æ§æãããŠããŸãã ãããã¯ãã¹ãŠãUbuntu Server 14.04.1 LTSãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã«ãã£ãŠå¶åŸ¡ãããŸãã
ãã¹ããããæ§æã
顧客ã¯ç¹æ§ã䌌ãŠãã2å°ã®ãã·ã³ã§ãã ãããã®1ã€ã¯wrkãå®è¡ããLuaã¹ã¯ãªããã§äžå®ã®è² è·ãäœæããŸãã ã¹ã¯ãªããã¯ã200ã®äžŠåæ¥ç¶ã䜿çšããŠãã¹ãã¬ãŒãžãããã¡ã€ã«ãã©ã³ãã ãªé åºã§èŠæ±ããŸãã ãã®è² è·ãå¯çãšåŒã³ãŸãã
å¥ã®ã¯ã©ã€ã¢ã³ããã·ã³ãã
wrk
ãå®è¡ãã50åã®ã¹ã¬ããã§åããã¡ã€ã«ãèŠæ±ããŸãã ãã®ãã¡ã€ã«ã¯åžžã«ã¢ã¯ã»ã¹ãããŠãããããã©ã³ãã ãªé åºã§èŠæ±ããããã¡ã€ã«ãšã¯ç°ãªãããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã®ãã£ãã·ã¥ããæŽãèœãšãããæéããªãããã®èªã¿åãã¯åžžã«ã¡ã¢ãªããè¡ãããŸãã ãããè² è·ãã¹ããšåŒã³ãŸãã
ãµãŒããŒäžã®
ifstat
ã€ã³ãžã±ãŒã¿ãš2çªç®ã®ã¯ã©ã€ã¢ã³ããã·ã³ããã®
wrk
çµ±èšã«ãã£ãŠããã©ãŒãã³ã¹ã枬å®ããŸãã
ãã®ãããã¹ã¬ããããŒã«ã䜿çšããªãæåã®å®è¡ã§ã¯ãéåžžã«æ§ãããªçµæã瀺ãããŸãã
% ifstat -bi eth2 eth2 Kbps in Kbps out 5531.24 1.03e+06 4855.23 812922.7 5994.66 1.07e+06 5476.27 981529.3 6353.62 1.12e+06 5166.17 892770.3 5522.81 978540.8 6208.10 985466.7 6370.79 1.12e+06 6123.33 1.07e+06
ãã®æ§æãèŠããšãããããã«ããã®ãããªè² è·ã®äžã§ããµãŒããŒã¯æ¯ç§çŽ1ã®ã¬ããããçºè¡ã§ããŸãã åæã«ãäžéšã§ã¯ãã»ãšãã©ã®å Žåããã¹ãŠã®NGINXã¯ãŒã¯ãããŒãI / Oã§ãããã¯ãããç¶æ ã«ããããšãããããŸãïŒæå
D
ããŒã¯ãããŠããŸãïŒã
top - 10:40:47 up 11 days, 1:32, 1 user, load average: 49.61, 45.77 62.89 Tasks: 375 total, 2 running, 373 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.0 us, 0.3 sy, 0.0 ni, 67.7 id, 31.9 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem: 49453440 total, 49149308 used, 304132 free, 98780 buffers KiB Swap: 10474236 total, 20124 used, 10454112 free, 46903412 cached Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 4639 vbart 20 0 47180 28152 496 D 0.7 0.1 0:00.17 nginx 4632 vbart 20 0 47180 28196 536 D 0.3 0.1 0:00.11 nginx 4633 vbart 20 0 47180 28324 540 D 0.3 0.1 0:00.11 nginx 4635 vbart 20 0 47180 28136 480 D 0.3 0.1 0:00.12 nginx 4636 vbart 20 0 47180 28208 536 D 0.3 0.1 0:00.14 nginx 4637 vbart 20 0 47180 28208 536 D 0.3 0.1 0:00.10 nginx 4638 vbart 20 0 47180 28204 536 D 0.3 0.1 0:00.12 nginx 4640 vbart 20 0 47180 28324 540 D 0.3 0.1 0:00.13 nginx 4641 vbart 20 0 47180 28324 540 D 0.3 0.1 0:00.13 nginx 4642 vbart 20 0 47180 28208 536 D 0.3 0.1 0:00.11 nginx 4643 vbart 20 0 47180 28276 536 D 0.3 0.1 0:00.29 nginx 4644 vbart 20 0 47180 28204 536 D 0.3 0.1 0:00.11 nginx 4645 vbart 20 0 47180 28204 536 D 0.3 0.1 0:00.17 nginx 4646 vbart 20 0 47180 28204 536 D 0.3 0.1 0:00.12 nginx 4647 vbart 20 0 47180 28208 532 D 0.3 0.1 0:00.17 nginx 4631 vbart 20 0 47180 756 252 S 0.0 0.1 0:00.00 nginx 4634 vbart 20 0 47180 28208 536 D 0.0 0.1 0:00.11 nginx 4648 vbart 20 0 25232 1956 1160 R 0.0 0.0 0:00.08 top 25921 vbart 20 0 121956 2232 1056 S 0.0 0.0 0:01.97 sshd 25923 vbart 20 0 40304 4160 2208 S 0.0 0.0 0:00.53 zsh
ãã®å Žåããã¹ãŠããã£ã¹ã¯ãµãã·ã¹ãã ã®ããã©ãŒãã³ã¹ã«äŸåããŸãããããã»ããµã¯ã»ãšãã©ã®æéã¢ã€ãã«ç¶æ ã§ãã
wrk
çµæãæ®å¿µã§ãã
Running 1m test @ http://192.0.2.1:8000/1/1/1 12 threads and 50 connections Thread Stats Avg Stdev Max +/- Stdev Latency 7.42s 5.31s 24.41s 74.73% Req/Sec 0.15 0.36 1.00 84.62% 488 requests in 1.01m, 2.01GB read Requests/sec: 8.08 Transfer/sec: 34.07MB
ã¡ã¢ãªãããã¡ã€ã«ã1ã€ã ãé åžããå Žåã§ããå€§å¹ ãªé 延ãçºçããŸãã ãã¹ãŠã®ã¯ãŒã¯ãããŒã¯ãã£ã¹ã¯ããã®èªã¿åãã§ããžãŒã§ãããã¹ããªã¢ã¹ããŒããäœæããæåã®ãã·ã³ãã200ã®æ¥ç¶ãæäŸãããããã®ãã¹ãèŠæ±ãã¿ã€ã ãªãŒã«åŠçã§ããŸããã
次ã«ãã¹ã¬ããããŒã«ãæ¥ç¶ããŸãããã®ããã«ãã¹ãã¬ãŒãžã䜿çšããŠ
location
ãããã¯ã«
aio threads
ãã£ã¬ã¯ãã£ããè¿œå ããŸãã
location / { root /storage; aio threads; }
ãããŠãNGINXã«èšå®ãåèªã¿èŸŒã¿ããããäŸé ŒããŸãã
ãã¹ããç¹°ãè¿ããŸãã
% ifstat -bi eth2 eth2 Kbps in Kbps out 60915.19 9.51e+06 59978.89 9.51e+06 60122.38 9.51e+06 61179.06 9.51e+06 61798.40 9.51e+06 57072.97 9.50e+06 56072.61 9.51e+06 61279.63 9.51e+06 61243.54 9.51e+06 59632.50 9.50e+06
ããã§ããµãŒããŒã¯9.5ã®ã¬ããã/ç§ïŒã¹ã¬ããããŒã«ãªãã§ã1ã®ã¬ããã/ç§ïŒãçºè¡ããŸãïŒ
ããã¯ããããããå€ããäžããå¯èœæ§ããããŸãããããã¯ç¹å®ã®ãããã¯ãŒã¯ã€ã³ã¿ãŒãã§ã€ã¹ã®å®éçãªå¶éã§ãããNGINXã¯ãããã¯ãŒã¯åž¯åå¹ ã«äŸåããŸãã ã¯ãŒã¯ãããŒã¯ãã»ãšãã©ã®æéãã€ãã³ããåŸ æ©ããŠã¹ãªãŒãããŸãïŒç¶æ
S
ïŒã
top - 10:43:17 up 11 days, 1:35, 1 user, load average: 172.71, 93.84, 77.90 Tasks: 376 total, 1 running, 375 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.2 us, 1.2 sy, 0.0 ni, 34.8 id, 61.5 wa, 0.0 hi, 2.3 si, 0.0 st KiB Mem: 49453440 total, 49096836 used, 356604 free, 97236 buffers KiB Swap: 10474236 total, 22860 used, 10451376 free, 46836580 cached Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 4654 vbart 20 0 309708 28844 596 S 9.0 0.1 0:08.65 nginx 4660 vbart 20 0 309748 28920 596 S 6.6 0.1 0:14.82 nginx 4658 vbart 20 0 309452 28424 520 S 4.3 0.1 0:01.40 nginx 4663 vbart 20 0 309452 28476 572 S 4.3 0.1 0:01.32 nginx 4667 vbart 20 0 309584 28712 588 S 3.7 0.1 0:05.19 nginx 4656 vbart 20 0 309452 28476 572 S 3.3 0.1 0:01.84 nginx 4664 vbart 20 0 309452 28428 524 S 3.3 0.1 0:01.29 nginx 4652 vbart 20 0 309452 28476 572 S 3.0 0.1 0:01.46 nginx 4662 vbart 20 0 309552 28700 596 S 2.7 0.1 0:05.92 nginx 4661 vbart 20 0 309464 28636 596 S 2.3 0.1 0:01.59 nginx 4653 vbart 20 0 309452 28476 572 S 1.7 0.1 0:01.70 nginx 4666 vbart 20 0 309452 28428 524 S 1.3 0.1 0:01.63 nginx 4657 vbart 20 0 309584 28696 592 S 1.0 0.1 0:00.64 nginx 4655 vbart 20 0 30958 28476 572 S 0.7 0.1 0:02.81 nginx 4659 vbart 20 0 309452 28468 564 S 0.3 0.1 0:01.20 nginx 4665 vbart 20 0 309452 28476 572 S 0.3 0.1 0:00.71 nginx 5180 vbart 20 0 25232 1952 1156 R 0.0 0.0 0:00.45 top 4651 vbart 20 0 20032 752 252 S 0.0 0.0 0:00.00 nginx 25921 vbart 20 0 121956 2176 1000 S 0.0 0.0 0:01.98 sshd 25923 vbart 20 0 40304 3840 2208 S 0.0 0.0 0:00.54 zsh
ãŸããããã»ããµãªãœãŒã¹ã¯ãŸã ååã«äŸçµŠãããŠããŸãã
2çªç®ã®ãã·ã³ã®
wrk
çµæïŒ
Running 1m test @ http://192.0.2.1:8000/1/1/1 12 threads and 50 connections Thread Stats Avg Stdev Max +/- Stdev Latency 226.32ms 392.76ms 1.72s 93.48% Req/Sec 20.02 10.84 59.00 65.91% 15045 requests in 1.00m, 58.86GB read Requests/sec: 250.57 Transfer/sec: 0.98GB
4 MBãã¡ã€ã«ã®å¹³åã¢ããããŒãæéã¯ã7.42ç§ãã226.32ããªç§ã«ççž®ãããŸããã ã33åã1ç§éã«åŠçããããªã¯ãšã¹ãã®æ°ã¯31åïŒ8ã«å¯ŸããŠ250ïŒå¢å ããŸããïŒ
ããã¯ãã¯ãŒã¯ãããŒããã£ã¹ã¯ããã®èªã¿åãããããã¯ãããŠããéããªã¯ãšã¹ããåŠçã®ããã«ãã¥ãŒã§åŸ æ©ããããšã¯ãªããªãã空ãã¹ã¬ããã«ãã£ãŠåŠçãããããã«ãªã£ããšããäºå®ã«ãã£ãŠèª¬æãããŸãã ãããŠããã£ã¹ã¯ãµãã·ã¹ãã ãã§ããéããžã§ããå®è¡ããæåã®ãã·ã³ãããåœã®ããã©ãã£ãã¯ãåŠçããŸãããNGINXã¯æ®ãã®ããã»ããµãªãœãŒã¹ãšãããã¯ãŒã¯åž¯åå¹ ã䜿çšããŠãã¡ã¢ãªãã2çªç®ã®ã¯ã©ã€ã¢ã³ããåŠçããŸãã
ç¹å¹è¬ã¯ååšããŸãã
ãããã¯æäœãšãã®ãããªé©ãã¹ãçµæã«é¢ãããã¹ãŠã®æããã話ã®åŸãããªãã®å€ãã¯ãããã圌ãã®ãµãŒããŒã§ã¹ã¬ããããŒã«ããªã³ã«ããããšæãã§ãããã æ¥ããªãã§ãã ããã
幞ããªããšã«ãã»ãšãã©ã®å Žåããã¡ã€ã«æäœã¯äœéã®ããŒããã©ã€ãããã®èªã¿åãã«ã¯ã€ãªãããŸããã ååãªRAMãããå Žåãææ°ã®ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã¯ãããããããŒãžãã£ãã·ã¥ã§é »ç¹ã«ã¢ã¯ã»ã¹ããããã¡ã€ã«ããã£ãã·ã¥ããã®ã«ååã¹ããŒãã§ãã
ããŒãžãã£ãã·ã¥ã¯éåžžã«ããåŠçãããããã«ãããNGINXã¯åžžã«æãäžè¬çãªç¶æ³ã§é«ãããã©ãŒãã³ã¹ãçºæ®ããããšãã§ããŸããã ããŒãžãã£ãã·ã¥ããã®èªã¿åãã¯éåžžã«è¿ éã«è¡ããããã®æäœãããããã³ã°ãšåŒã¶ããšã¯ã§ããŸããã åæã«ãã¹ã¬ããããŒã«ãšã®å¯Ÿè©±ã«ã¯ãè¿œå ã®åæã³ã¹ããããããŸãã
ãããã£ãŠãååãªRAMãšå°éã®ãããããŒã¿ãããã°ããã§ã«åé¡ã¯ãªããNGINXã¯ã¹ã¬ããããŒã«ã䜿çšããã«æé©ãªæ¹æ³ã§åäœããŸãã
å®éãèªã¿åãæäœãå¥ã®ã¹ã¬ããããŒã«ã«ã¢ã³ããŒããããšãããªãçãç¯å²ã®ã¿ã¹ã¯ã解決ãããŸãã å®æçã«èŠæ±ãããããŒã¿ã®éãRAMã«åãŸããªãç¶æ³ã«éå®ãããããããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã®ããŒãžãã£ãã·ã¥ãéå¹ççã«ãªããŸãã ãã®ãããªäŸã¯ãè² è·ã®é«ãã¡ãã£ã¢é ä¿¡ãµãŒãã¹ã§ãã ãã®ç¶æ³ããã¹ãã§ã·ãã¥ã¬ãŒãããŸããã
èªã¿åãæäœã®ã¹ã¬ããããŒã«ãžã®ã¢ã³ããŒãã¯ãå¿ èŠãªããŒã¿ãã¡ã¢ãªå ã«ãããã©ãããäºåã«ç¢ºèªããå¹æçãªæ¹æ³ãããã°ãèªã¿åãæäœã«å¯ŸããŠããæ±çšçã«ãªããé 延ãæžãããŸããåŸè ã®å Žåã®ã¿ãæäœãå¥ã®ã¹ããªãŒã ã«ã¢ã³ããŒãããŸãã
åºèãšé ãé¢ããå庫ã®äŸãã«æ»ããšã売ãæã¯ååãçªã«ããããšãç¥ãæ©äŒããªããåžžã«å® é 䟿ãå©çšããããåŸãŸããã
å®éã«ã¯ããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã®ã«ãŒãã«ããã®å¯Ÿå¿ãããµããŒãã¯ãããŸããã æåã«ããã®æ©èœãfincoreïŒïŒã·ã¹ãã ã³ãŒã«ã®åœ¢åŒã§Linuxã«è¿œå ããããšè©Šã¿ãŸãããã2010幎ã«é¡ããŸãããããŸã ãããŸããã åŸã«
preadv2()
ã·ã¹ãã ã³ãŒã«ããã³
RWF_NONBLOCK
ãã©ã°ã®åœ¢åŒã§è©Šè¡ãè¡ãããŸããïŒè©³çŽ°ã¯ãLWN.netã®ãã³ããããã³ã°ãããã¡ãŒãã¡ã€ã«èªã¿åãæäœããã³éåæãããã¡ãŒèªã¿åãæäœã®èšäºã«èšèŒãããŠããŸã ïŒ-ãããããããã®ãããã®éåœã¯äŸç¶ãšããŠçåã§ãã ãã®ãã¹ãŠã®ãããæªåé«ãèªè»¢è»é転 ïŒãã§ã«ããã³ã®åãã®è²ã«ã€ããŠã®è°è«ïŒã§ããããã«æãããã®ã¯æ²ããããšã§ãã
FreeBSDãŠãŒã¶ãŒã¯å¿é ããå¿ èŠã¯ãããŸããããã«ãŒãã«ã«å®è£ ãããæ£åžžã«æ©èœããéåæèªã¿åãã¡ã«ããºã ãæã£ãŠããŸãã ã¹ã¬ããããŒã«ã®ä»£ããã«äœ¿çšããããšããå§ãããŸãã
æ§æ
ãã®ãããã¿ã¹ã¯ã®ã¹ã¬ããããŒã«ããå©çãåŸãããšãã§ãããšç¢ºä¿¡ããŠããå Žåã¯ãã¹ã¬ããããŒã«ãæå¹ã«ããŠæ§æããæ¹æ³ã«é¢ããçåãå¿ ãçããŸãã
æ§æã¯éåžžã«ã·ã³ãã«ã§ãããšåæã«éåžžã«æè»ã§ãã ãŸãã-with
--with-threads
ãã©ã°ã§ã³ã³ãã€ã«ãããNGINXããŒãžã§ã³1.7.11以éãå¿ èŠã§ãã æãåçŽãªã±ãŒã¹ã§ã¯ãã»ããã¢ããã¯åºæ¬çã«èŠããŸãã èªã¿åãããã³ãã¡ã€ã«ã®ã¹ã¬ããããŒã«ãžã®ããŠã³ããŒããå¯èœã«ããããã«å¿ èŠãªã®ã¯ãã¹ã¬ããã«èšå®ããã
http
ã
server
ãŸãã¯
location
ã¬ãã«ã®
aio
ãã£ã¬ã¯ãã£ãã ãã§ãã
aio threads;
ããã¯ãã¹ã¬ããããŒã«ãæ§æããããã®æå°ã®ãªãã·ã§ã³ã§ãã å®éãããã¯ãã®æ§æã®ççž®ããŒãžã§ã³ã§ãã
thread_pool default threads=32 max_queue=65536; aio threads=default;
default
ã¹ã¬ããããŒã«ãèšå®ã
default
ã32åã®ã¹ã¬ãããæ©èœãããžã§ããã¥ãŒã®æ倧蚱容ãµã€ãºã¯65536ã§ãããžã§ããã¥ãŒããã£ã±ãã®å ŽåãNGINXã¯ãªã¯ãšã¹ããæåŠãããšã©ãŒãèšé²ããŸãã
thread pool "NAME" queue overflow: N tasks waiting
ããã¯ãã¹ã¬ãããäœæ¥éã«å¯Ÿå¿ããããã¥ãŒãåŠçããããããæ©ãæºããããå Žåã«å¯èœã§ãã ãã®å Žåãæ倧ãã¥ãŒãµã€ãºãå¢ããããšãè©Šã¿ãããšãã§ããŸããããã圹ã«ç«ããªãå Žåãã·ã¹ãã ã¯åçŽã«ãã®ãããªå€æ°ã®ãªã¯ãšã¹ããåŠçã§ããŸããã
ã芧ã®ãšããã thread_poolãã£ã¬ã¯ãã£ãã䜿çšããŠãã¹ã¬ããæ°ãæ倧ãã¥ãŒãµã€ãºãããã³ãã®ã¹ã¬ããããŒã«ã®ååãèšå®ã§ããŸãã åŸè ã«ã¯ãè€æ°ã®ç¬ç«ããããŒã«ãæ§æããããããç°ãªãã¿ã¹ã¯ã®æ§æã®ç°ãªãéšåã§äœ¿çšããæ©èœãå«ãŸããŸãã
thread_pool one threads=128 max_queue=0; thread_pool two threads=32; http { server { location /one { aio threads=one; } location /two { aio threads=two; } } ⊠}
ããŒã«2ã®ããã«
max_queue
ãã©ã¡ãŒã¿ãŒ
max_queue
æ瀺çã«æå®ãããŠããªãå Žåãããã©ã«ãå€ã®65536ã䜿çšãããŸãäŸãããããããã«ããã¥ãŒãµã€ãºããŒãã«èšå®ã§ããŸãã ãã®åŸãããŒã«ã¯ã空ãã¹ã¬ãããšåæ°ã®ãžã§ãã®ã¿ãåæã«åãå ¥ããããšãã§ãããã¥ãŒã§åŸ æ©ããŠãããžã§ãã¯ãããŸããã
ããã§ãããã¯ãšã³ãã®ãã£ãã·ã¥ãããã·ãšããŠæ©èœãã3å°ã®ããŒããã©ã€ããåãããµãŒããŒããããšããŸãã åæã«ãæšå®ãã£ãã·ã¥ãµã€ãºã¯ã䜿çšå¯èœãªRAMã®éã®äœåã«ããªããŸãã æ¬è³ªçã«ãããã¯å人ã®ã³ã³ãã³ãé ä¿¡ãããã¯ãŒã¯ïŒCDNïŒäžã®ãã£ãã·ã¥ããŒãã®ãããªãã®ã§ãã ãã®å Žåããã£ãã·ã¥ãããããŒã¿ãè¿ãéã®äž»ãªè² æ ã¯ãã£ã¹ã¯ãµãã·ã¹ãã ã«ãããŸãã ãã¡ãããå©çšå¯èœãª3ã€ã®ãã©ã€ãããæ倧ã®ããã©ãŒãã³ã¹ãåŒãåºãå¿ èŠããããŸãã
ããã§ã®è§£æ±ºçã®1ã€ã¯ãRAIDã¢ã¬ã€ã®ç·šæã§ãã ãã¡ããããã®ã¢ãããŒãã«ã¯é·æãšçæããããŸãã ããããä»æ¥NGINXã¯ããªãã«ç°ãªãã¢ãããŒããæäŸããæºåãã§ããŠããŸãïŒ
# : # /mnt/disk1, /mnt/disk2 /mnt/disk3 thread_pool pool_1 threads=16; thread_pool pool_2 threads=16; thread_pool pool_3 threads=16; http { proxy_cache_path /mnt/disk1 levels=1:2 keys_zone=cache_1:256m max_size=1024G use_temp_path=off; proxy_cache_path /mnt/disk2 levels=1:2 keys_zone=cache_2:256m max_size=1024G use_temp_path=off; proxy_cache_path /mnt/disk3 levels=1:2 keys_zone=cache_3:256m max_size=1024G use_temp_path=off; split_clients $request_uri $disk { 33.3% 1; 33.3% 2; * 3; } server { ⊠location / { proxy_pass http://backend; proxy_cache_key $request_uri; proxy_cache cache_$disk; aio threads=pool_$disk; sendfile on; } } }
ãã®æ§æã§ã¯ã3ã€ã®ç¬ç«ãããã£ãã·ã¥ã䜿çšããŸããåããŒããã©ã€ãã«1ã€ãšããã£ã¹ã¯ã«ã3ã€ã®ç¬ç«ããã¹ã¬ããããŒã«ã§ãã
ãã£ãã·ã¥ïŒããã³ãããã«å¿ããŠããŒããã©ã€ãïŒéã§è² è·ãåçã«åæ£ããã«ã¯ã split_clientsã¢ãžã¥ãŒã«ã䜿çšããŸããããã¯ããã«æé©ã§ãã
use_temp_path=off
ãã£ã¬ã¯ãã£ãã®
use_temp_path=off
ãã©ã¡ãŒã¿ãŒã¯ã
use_temp_path=off
ã«ããã£ãã·ã¥ããŒã¿ã眮ãããŠããã®ãšåããã£ã¬ã¯ããªã«äžæãã¡ã€ã«ãä¿åããããã«æ瀺ããŸãã ããã¯ãå¿çããã£ãã·ã¥ã«ä¿åãããšãã«ããããã©ã€ãããå¥ã®ãã©ã€ãã«ããŒã¿ãã³ããŒããããšãé¿ããããã§ãã
ããããã¹ãŠãçµã¿åãããããšã§ãNGINXã¯åå¥ã®ã¹ã¬ããããŒã«ãä»ããŠåãã£ã¹ã¯ãšäžŠè¡ããŠç¬ç«ããŠå¯Ÿè©±ããããããã®ãã£ã¹ã¯ãµãã·ã¹ãã ããæ倧ã®ããã©ãŒãã³ã¹ãåŒãåºãããšãã§ããŸãã åãã£ã¹ã¯ã¯16ã®ç¬ç«ããã¹ããªãŒã ã«ãã£ãŠåŠçããããã¡ã€ã«ã®èªã¿åããšéä¿¡ã®ããã®ã¿ã¹ã¯ã®åå¥ã®ãã¥ãŒã圢æãããŸãã
çµå±ã®ãšãããããªãã®é¡§å®¢ã¯åã ã®ã¢ãããŒãã奜ãã§ããïŒ ããŒããã©ã€ãã確èªããŠãã ããã ;ïŒ
ãã®äŸã¯ãããŒããŠã§ã¢ãçŽæ¥æ§æããéã®NGINXã®éåžžã«é«ãæè»æ§ã®ãã¢ã§ãã NGINXã«ããã®ãµãŒããŒãšããŒã¿ã®ãã£ã¹ã¯ãµãã·ã¹ãã ãšæé©ã«å¯Ÿè©±ããæ¹æ³ãæ瀺ããŠããŸãã ãããŠããã®ãããªåŸ®èª¿æŽã¯ããŠãŒã¶ãŒã¬ãã«ãŸã§ã®ãœãããŠã§ã¢ãæãæé©ãªæ¹æ³ã§æ©åšãšé£æºãããšãã«ãç¹å®ã®ã·ã¹ãã ã®ãã¹ãŠã®ãªãœãŒã¹ã®æãå¹ççãªäœ¿çšãä¿èšŒããŸãã
çµè«
ã¹ã¬ããããŒã«ã¯ãéåæã¢ãããŒãã®äž»ãªæåãªæµã§ããããããã³ã°æäœãšæŠãçŽ æŽãããã¡ã«ããºã ã§ããããã«ãããNGINXã¯ãç¹ã«éåžžã«å€§éã®ããŒã¿ã«ã€ããŠè©±ããŠããå Žåã«ãæ°ããã¬ãã«ã®ããã©ãŒãã³ã¹ãåãããšãã§ããŸãã
åè¿°ã®ããã«ãã¹ã¬ããããŒã«ã¯ä»ã®æäœã«äœ¿çšã§ããéåæã€ã³ã¿ãŒãã§ã€ã¹ãæããªãã©ã€ãã©ãªãæäœã§ããŸããæœåšçã«ãããã«ãããã¢ãžã¥ãŒã«ãšæ©èœã®å®è£ ã®æ°ããå¯èœæ§ãéãããŸãããããã©ãŒãã³ã¹ãæãªãããšãªãå®è£ ããããšã¯ã以åã¯åŠ¥åœãªæéã§ã¯å®çŸã§ããŸããã§ãããæ¢åã®ã©ã€ãã©ãªã®éåæããŒãžã§ã³ãäœæãããããã®ãããªã€ã³ã¿ãŒãã§ã€ã¹ãè¿œå ããããšãããšãå€ãã®åŽåãšæéãè²»ããããšãã§ããŸããããã²ãŒã ã«ã¯äŸ¡å€ããããŸããïŒããšããçåãçããŸãããã¹ã¬ããããŒã«ã䜿çšãããšããããã¯ã³ãŒã«ã§åäœããã¢ãžã¥ãŒã«ãäœæããNGINXãã¡ã€ã³ã¿ã¹ã¯ãå®è¡ããŠæ®ãã®ãªã¯ãšã¹ããåŠçããããšãªãããã®ã¿ã¹ã¯ãã¯ããã«ç°¡åã«è§£æ±ºã§ããŸãã
ãã®ãããNGINXã«ã¯å°æ¥ãå€ãã®æ°ããèå³æ·±ããã®ãåŸ ã£ãŠããŸããç§ãã¡ãšäžç·ã«ããŠãã ããïŒ