ãªãã§ãããã®ããæãã«ã¹ãã¼ãã¢ããã³ã³ãã¹ã ISUCON ã 2 ã«ãªã£ã¦å¸°ã£ã¦ããã®ã§ãåå ãã¦åªåãåã¡åã£ã¦ãã¾ããã
ã¾ã¨ãçãªãã®ã¯ãã¡ããã livedoor Techブログ : ISUCON
ãã¼ã ã¡ã³ãã¼ã®blogãä½µãã¦ã覧ãã ããã
ä»åã¯ååã® ISUCON åªåã¡ã³ãã¼ã®ã²ã¨ã @sugyan ã転è·ãã¦åºé¡å´ã«åã£ã¦ãã¾ã£ãã®ã§ã@typester ãæèãã¦ãã¼ã ç·¨æã@songmu ã¨å
±ã«3人ã§ãã¼ã ãfujiwaraçµãã¨ãã¦ååæ¦ã§ãã
以ä¸ãä½æ¥ç¨IRCã®ãã°ãããµããããã¾ãã¨â¦â¦
11:39:29 <typester> ã¨ããããrecent_soldã¯ãã£ãã·ã¥ã£ã¦ã®ã¯ã¾ããããã¨ããªã¼ã¨ 11:39:33 <typester> ãããã¾ãã 11:39:36 <typester> ãã¶ãæ¾æ¨ãããããããã®ã¯ããã¨ãããã®ã§ 11:39:57 <typester> åã¯Redisãã©ã³ãããã¡ããã¾ãã®ã§ããããããããããã 11:40:01 <typester> fujiwara: Songmu ^ 11:40:05 <Songmu> ã 11:40:22 <Songmu> äºè§£ã§ãã 11:40:27 <typester> ãã±ãã販売ã¨ããã ãã¶redisã¤ããã¨ãããã£ã¦ã®ã¨ 11:40:32 <typester> èªåããã®ãã³ãã«èå³ããã 11:40:43 <Songmu> ï½ï½ï½
éå§30å㧠redis ãå©ç¨ããã¢ããªã±ã¼ã·ã§ã³ãå®è£ ãããã¨ã typester ã宣è¨ãããã®å®è£ ãå¾ ã¤éã«ãããã©ã«ãã®ã¢ããªã±ã¼ã·ã§ã³ã®ããã«ããã¯ã調æ»ã
12:25:05 <fujiwara> éçãã¡ã¤ã«ã¯apacheã«å¤æ´ 12:36:33 <fujiwara> create index stock_order_id on stock(order_id); 12:36:35 <fujiwara> indexã¯ã£ã 12:36:43 <Songmu> ã¯ã
åæç¶æ ã§ã¯ MySQL ã®è¡èªã¿è¾¼ã¿ãé常ã«å¤ã㦠CPU ã使ã£ã¦ããã®ã§ãslow query ãçºãã¤ã¤ explain ã㦠index ã追å ãããã
GET ãæ¥ã URL ã¯å¤§ããåãã¦
- / (topãã¼ã¸)
- /artist/*
- /ticket/*
ã ã£ãã®ã§ãããããåå¥ã« http_load ã§ãã³ããæãã¦ã©ããéãã®ãè¨æ¸¬ã
ãã®éãrev, app, db ã®ãããã㧠dstat ãå®è¡ãã¤ã¤ãã©ãã«è² è·ãæããã®ããçºãã¦ãã¾ããã
12:54:52 <fujiwara> http://127.0.0.1/ => 492.856 fetches/sec 12:56:11 <fujiwara> http://127.0.0.1/artist/1 => 126.98 12:56:26 <fujiwara> artistã¯mysqlã®è² è·ãé«ã 12:58:10 <fujiwara> http://127.0.0.1/ticket/1 => 49.698 12:58:18 <fujiwara> mysqlã®è² è·ã¯artistã»ã©ã§ã¯ãªã
ããã« MySQL å´ãå°å³ã«æ¹åãã¤ã¤â¦
13:17:03 <fujiwara> create index variation_order on stock (variation_id, order_id); 13:17:05 <fujiwara> ããã 13:18:32 <fujiwara> artist 374.4 ã¾ã§ããã£ã 13:35:59 <typester> redisãã©ã³ãããã·ã¥ãã
ããã§è³¼å ¥å¦ç (éã) ã Redis ãå©ç¨ããã¢ããªã±ã¼ã·ã§ã³ã®å®è£ ãåæãã¼ã¸ã§ã³æå ¥ã
ãã®æç¹ã§ã¯ MySQL å©ç¨ãã¼ã¸ã§ã³ã§ ticket 1000 ãããã®ã¹ã³ã¢ã ã£ãã®ã§ãããä»åã®ã¢ããªã±ã¼ã·ã§ã³ã§ã¯é常ã«ä¸¦å度ãé«ã (åè¨200並å) ãããããç¨åº¦ Apache ã®åããã»ã¹æ°ãçµã (MaxClients 32ã¨ã) ã»ããæ§è½ãå®å®ãã1400ç¨åº¦ã«ãªãã¾ããã
MySQLã®å¦çã Redis ã®å¹ççãªãã¼ã¿åã§ã®å¦ç(setãlistãªã©) ãå©ç¨ãããã®ã«ç½®ãæãããããããDBå´ã®è² è·ã¯å ¨ãåé¡ãªããªãã¾ããã
ãããããã®å²ã«ã¯ã¹ã³ã¢ (ticket) ã¯ä¼¸ã³ãâ¦
æ¨å¹´åæ§ããã®ãããããIRCã§ã®ããã¨ããå°ãªããå£é ãã¼ã¹ã«ãªã£ã¦è¨é²ãé絶ããã¡ãªã®ã§ããã
- ãã®æç¹ã§ã¯ããã«ããã¯ã¯ app ã® CPU è² è·ã§ããã®ãã¡ã©ããéãã®ããåå¥ URL ã® http_load èªåãã³ã㧠/ticket ã ã¨ç¹å®
- Devel::KYTProf 㧠Text::Xslate->render ãè¨æ¸¬ããããã«è¨å®ã 参考ããã³ãã¬ã¼ãã®ã¬ã³ããªã³ã°ã§ 50ã100ms ç¨åº¦æãããã¨ãç¹å®
ã¬ã®ã¥ã¬ã¼ã·ã§ã³çã«ããã¼ã¿æ´æ°ãã1ç§ä»¥å ã«åæ ããã¦ããããã¨ãæ±ããããã®ã§ããããã¼ã¿ã®æ´æ°ãè¡ããã /buy ã«ã¯ç§éæ°åãªã¯ã¨ã¹ããæ¼ãå¯ãããããPOST /buy ã®ã¿ã¤ãã³ã°ã§ cache çæ + ç ´æ£ãç¹°ãè¿ãã¦ãã»ã¼ä½¿ãéãããªãã®ã§ãã
ã¨ãããã¨ã§ãéãã¬ã³ããªã³ã°ã worker ã«ç¬ç«ããã¦1ç§ä»¥å ã« cache çæãç¶ãã + Webã¢ããªã±ã¼ã·ã§ã³ã¯ãã® cache ãåç §ããã®ã¿ã«ãããã¨ããæ¹éã§å®è£ ãé²ãã¾ããã
worker ãä¸ããä½æããã®ã¯é¢åãªã®ã§ãWebã¢ããªã±ã¼ã·ã§ã³ã® /ticket/update_table_cache ã¨ãã URL ã« POST ãã¦ãã£ãã·ã¥çæãè¡ãããã«å®è£ ï¼ æ¯ç§ HTTP request ãéãç¶ããããã»ã¹ã supervisord ããèµ·åãã¨ããå®è£ ã«ãã¾ãã
ä»åã®ã¢ããªã±ã¼ã·ã§ã³ã§ã¯ããã®ãã£ãã·ã¥çæã¯1ç§ä»¥å ã«5ãã¼ã¸åè¡ãå¿ è¦ãããã®ã§ããããªã«ããã¬ã³ããªã³ã°ãéãã®ã§ã1ãªã¯ã¨ã¹ãã§5ãã¼ã¸åãä¸æ°ã«çæããã¨é«è² è·æã«2ç§ä»¥ä¸æãããã¨ãæã çºçããã®é 延ããã³ããã¼ã¯ãã¼ã«ã«æ¤åºããã¦ãã¾ã㨠fail ã§å¤±æ ¼ã§ãã
Devel::KYTProf ã§ãªã¯ã¨ã¹ããéãç¶ããããã»ã¹ããã¬ã¹ãã³ã¹ã¿ã¤ã ãç£è¦ããçµæã
- 1ããã»ã¹ã§ã¯ææ 1ç§ä»¥ä¸æããã®ãåé¿ã§ããªã
- è² è·ã¯ãã³ãã¬ã¼ãã¬ã³ããªã³ã°ã® CPU bound ãªã®ã§è¤æ°ããã»ã¹ã«åå²ãããã«ãã³ã¢ãæå¹ã«å©ç¨ããã°ãªãã¨ããªããã
ã¨ããçµè«ã«è³ã£ããããæçµçã«ã¯4ã³ã¢ãã DB ãµã¼ãã§ã2ããã»ã¹ã§ /ticket/update_table_cache/{1,2} ã¨ãããã£ãã·ã¥çæããã»ã¹ã«åå²ãããã®ã«ä¸¦åã«ãªã¯ã¨ã¹ããéãç¶ãããã¨ããç¶æ ã«æã£ã¦ããã¾ããã
ãã®æç¹ã§ãã競æçµäº20ååãå種åæèµ·åè¨å®ã確èªããå ¨å°ã®ãã¹ãã reboot ãã¦ãã³ããå®èµ°ãããã¨ã確èªãæå¾ã®5åã¯ä½ãããããã
ä¸éè¨æ¸¬ã§ã®ãã³ãã§ã¯ãã¼ã 山形çµã¨å å·®ã§ã®ããããã¼ããç¹°ãåºãã¦ãã¾ããããæ¬çªè¨æ¸¬ã«éã天ã«ä»»ãã¦çµäºã
çµæãã¹ã³ã¢çã«ã¯å å·®ã§ããåªåã§ãã¾ããã
è¬è©ã¿ã¤ã ã§ãããã話ãèãã¨ã
- å¤æ´ãããURLã¯å°ãªãã®ã§å ¨é¨çæã㦠front å´ã«é ç½®ããããã£ã¨æ§è½åºããããªããã¨ã
- kernel module ã§ç©¶æ¥µæ§è½ãæ±ãã人éã¯ã»ãã¨ãã¸ãã¡(ã»ãè¨è) ã¨ãã
- ä¸ããã¢ããªã±ã¼ã·ã§ã³ãåå®è£ ãã¦ãããã£ãã®ã¯å°æ¬ãããããªããã¨ã
ã¿ããªã¢ããã¼ããéã£ã¦ãããªãå俵ã§æ¦ãã¦é¢ç½ãã£ããªãã¨æãã¾ãã
ã§ãããmaster ãã¼ã¿ã¨ã5è¡ãããªãããDBãããªãã¦ã¢ããªã®ã³ã¼ãã«æ¸ãã¦ãã¾ã£ããããããããæ¹åã®ã¢ããã¼ããåããã«åå©ã§ããã®ã§
å®éç¨ã«çªã£è¾¼ãã§ãéç¨ãç ´ç¶»ããªãç¶æ (å²ã¨è²ããªã)ã§çµæåã¦ãã®ã§æºè¶³ã§ãããã¾ã #isucon2
éç¨ã大äºãªèªåã¨ãã¦ã¯æ¬æã§ãããã¾ãããã¨ãå®è£ ãå¾æ㪠@typester ã¨ãããã«ããã¯ç¹å®ãå¾æãªèªåã¨ãããããå·éã«è¦ã¦ããã @songmu ã¨ãå½¹å²åæ ããã¾ãã§ããã®ãåå ããªã¨ãæã£ã¦ãã¾ãã
次åã¯å¶ä½å§å¡ä¼æ¹å¼ã«ãªãã¨ãã話ãèãã¾ããã®ã§ãISUCON 1, 2 ã§æ¥½ãã¾ãã¦ããã ããã礼ã @tagomoris ããã @kazeburo ããã«ãè¿ãã§ããããªã¼ãªã©ã¨æã£ã¦ããã¾ãã
ISUCON ãéå¶ãã¦ããã ããçæ§ãåå è ã®çæ§ãæ¬å½ã«æ¥½ããã¤ãã³ãã§ããï¼ãããã¨ããããã¾ãï¼
[追è¨] ç¨æãã¦ããã ããååºããã©ã¹ããã¯è£½ã®ç«æ´¾ãªãã®ã§ããã