ã¬ãããããã§ã¤ã³ãã°ã¬ã¼ã·ã§ã³ã®ããã®ããã«ã¦ã§ã¢ã®ãã¯ãã«ã«ãµãã¼ããæ å½ãã¦ããå±±ä¸ã§ãã æè¿ã¯ãã¤ã¯ããµã¼ãã¹ã§ã·ã¹ãã ãéçºãã¦ããã¨ãã話ãããèãããã«ãªã£ã¦ãã¾ãããã§ã¯ããã§ã¡ãã»ã¼ã¸ã³ã°ãããã¦Kafkaã使ã£ã¦ã¾ãã§ããããï¼ãã¤ã¯ããµã¼ãã¹ã§ã¯ä½æ ãRESTã°ãããä¸ã®ä¸ã«æ³¨ç®ããã¦ãã¾ããã¨ãå¤ãããã«ãä»åã¯ã¡ãã»ã¼ã¸ã³ã°æ¨ãã®å 容ã«ãã¦ãã¾ãã
ãã¤ã¯ããµã¼ãã¹ã§ã¯ã¡ãã»ã¼ã¸ã³ã°ãç¨ããã³ãã³ããã¤ãã³ãããä¸å¿ã§ãã£ã¦ä¸å¯æ¬ ã§ãããã¤ã¯ããµã¼ãã¹ã®ä¸ã§ã¡ãã»ã¼ã¸ã³ã°ã¯ã©ã®ããã«å©ç¨ãããããã¦ãªãå¿ è¦ãªã®ã§ãããããä»åã¯ããã¤ã¯ããµã¼ãã¹ã¨ã¡ãã»ã¼ã¸ã³ã°ã®ãªã [æ¦è¦ç·¨]ãã¨é¡ãã¦ãããæ¦è¦³ãã¦ããã¾ãã
- Kafkaã®ç°¡åãªãããã
- ã©ãã§ã¡ãã»ã¼ã¸ã³ã°ã¯å©ç¨ãããã®ãï¼
- RESTã¯ãæ軽ãªè§£æ±ºçï¼
- ãªããã¤ã¯ããµã¼ãã¹ã«ã¡ãã»ã¼ã¸ã³ã°(Kafka)ãå¿ è¦ãªã®ãï¼
- å®å®ããã·ã¹ãã ãæ¯ããã¡ãã»ã¼ã¸ã³ã°
- ã¤ãã³ãã¯å±¥æ´ã«ãªãï¼ã¤ãã³ãã¯ç¶æ ã®å¤åã表ããã¤ãã³ãã®èç©ãç¶æ ãåçããï¼
- æ§ã ãªãµã¼ãã¹ãã·ã¹ãã ã¨é£æºãããæ å ±ãçµ±åããããã®ãã¼ã¿ãã
- ã¤ãã³ããå©ç¨ãããã¨ã§ãµã¼ãã¹éã®ç§©åºããé¢ä¿ãç¶æã§ãã
- ãã¤ã¯ããµã¼ãã¹ã§ã®è²¬åã®æ確åãä¿é²ããã
- ã³ãã³ãå®è¡é ã®ä¿è¨¼ã¨ä¸¦åå®è¡
- ã¾ã¨ã
- Special Thanks
- åèæç®
- [PR]
Kafkaã®ç°¡åãªãããã
ã¹ããªã¼ã å¦çã®ããã®ããã¡ã¯ããã¨ãããã¨ã¦ãåªç§ãªãããã¯ãã§ããã¯ã©ã¹ã¿ãªã³ã°åæã®è¨è¨ã§ãé«ã¹ã«ã¼ãããã§æ°´å¹³ã«ã¹ã±ã¼ã«ãã¾ããããããããã¼ã¿ã¯ã¬ããªã«ã«ã³ãã¼ããé«ãå¯ç¨æ§ã¨èé害æ§ãæã¡ã¾ããããã¦ããã¼ã¸ãã£ãã·ã¥ãSendfileã¨ãã£ãLinuxã®æ©è½ãå©ç¨ãã¦å¹çããå¦çãå®ç¾ãã¾ãã
ã©ãã§ã¡ãã»ã¼ã¸ã³ã°ã¯å©ç¨ãããã®ãï¼
ããã§ã¯ããããããã¤ã¯ããµã¼ãã¹ã«ããã·ã¹ãã ã®ã©ãã§ã¡ãã»ã¼ã¸ã³ã°ãå©ç¨ãããã®ã§ãããããã¤ã¯ããµã¼ãã¹ã§ã¯ãåãµã¼ãã¹ãç¬ç«ãã¦ç¨¼åãã¾ããããã¦ãã®ãµã¼ãã¹éã§ã¯ããã¼ã¿ã®åãåãããè¡ãã¯ã¨ãªãä½ããã®æ示ãè¡ãã³ãã³ããç¶æ å¤åãéç¥ããã¤ãã³ãã¨ããï¼ç¨®é¡ã®é£æºãè¡ããã¾ã(CQRS+ES)ã
ãããRESTãç¨ããã®ã¯ãã¯ã¨ãªã¨ä¸é¨ã®ã³ãã³ãã®ã¿ã§ããæ®ãå¤ãã®ã³ãã³ãã¨ã¤ãã³ãã¯ã¡ãã»ã¼ã¸ã³ã°ï¼ã¤ã¾ãã¯Kafkaã§ããï¼ãç¨ãã¦å®ç¾ãã¾ãã
次ã®ãã¤ã¯ããµã¼ãã¹ã®å ¨ä½å³ãè¦ã¦ã©ãã§ã¡ãã»ã¼ã¸ã³ã°ãå©ç¨ããã¦ãããã¿ã¦ã¿ã¾ãããããªã説æã®ããã«åç´åãããã¤ã¯ããµã¼ãã¹ã®ä¸ä¾ã«éããªããã¨ã«æ³¨æãã¦ãã ããã
ã¯ã©ã¤ã¢ã³ãã¸ã®è¡¨ç¤ºãæ å½ãããã¥ã¼ãµã¼ãã¹ï¼query-side service å¾è¿°ï¼ãããã¦ãã®èå¾ã«åãµããã¡ã¤ã³ãã¨ã®ãµã¼ãã¹ãåå¨ãã¾ããRESTã«ããéä¿¡ã¯ã主ã¨ãã¦ã·ã¹ãã ã®ã¨ãã¸ã§ããã¯ã©ã¤ã¢ã³ãããã®ã¯ã¨ãªã¨ã³ãã³ãã§å©ç¨ãã¾ããä¸æ¹ã§ãã®èå¾ã§è¡ããããµã¼ãã¹éã®éä¿¡ã¯ãKafkaãç¨ããã¡ãã»ã¼ã¸ã³ã°ã«ãããã³ãã³ããã¤ãã³ããå©ç¨ãã¾ãã
ãã¤ã¯ããµã¼ãã¹ã§å©ç¨ãããæãåºæ¬çãªKafkaã®ãããã¯ã¯ãåãµã¼ãã¹ãã¨ï¼æ£ç¢ºã«ã¯ãµã¼ãã¹å ã®éç´ãã¨ï¼ã«ä½æãããã³ãã³ãã®ããã®ãããã¯ã¨ãã¤ãã³ãã®ããã®ãããã¯ã§ãã
ã¡ã¢: å®éã«ã¯ãã以å¤ã«ãã·ã¹ãã ã®è¦ä»¶ã«å¿ãã¦æ§ã ãªãããã¯ãä½æããããã¨ãããã¾ããä¾ã¨ãã¦ãã·ã¹ãã ã®å¢çã«é ç½®ãã¦ã¤ãã³ãå½¢å¼ã®å·®ç°ãå¸åãããããã¯ããã³ãã³ãã®å®è¡çµæãå°ç¨ã«åãããããã¯ããè¤æ°ã®ãããã¯ãã¸ã§ã¤ã³ããããã®ãããã¯ããä¸æçãªãã¼ã¿å¤æã®ããã®ãããã¯ãªã©ã§ãããããã¾ãã¯ãã®ï¼ã¤ã®åºæ¬ã®ãããã¯ãæãããã¨ã大åã§ãã
ãµã¼ãã¹ã¯ã³ãã³ãã®ãããã¯â ãé 次å¦çãã¦â¡ããã¡ã¤ã³ãªãã¸ã§ã¯ãã®ç¶æ å¤åãèµ·ããâ¢ãããã¦ã¤ãã³ããçºè¡ãã¾ãâ£ãããã¦ããã®ã¤ãã³ããè³¼èªãã¦ããä»ã®ãµã¼ãã¹â¤ã¯ãã¤ãã³ãããåãåºããæ å ±ãèªèº«ã®ãµã¼ãã¹å ã«ãã£ãã·ã¥ãããããããã¯ãããããªã¬ã¨ãã¦ä½ããã®å¦çãè¡ããã¨ãç¹°ãè¿ããªãããå ¨ä½ã¨ãã¦ãã¸ãã¹ããã»ã¹ãå®ç¾ããã¾ãï¼ãã¸ãã¹ããã»ã¹ã«ã¤ãã¦ã¯å¥è¨äºã§èª¬æï¼ã
ãã®ããã«ãã¯ã©ã¤ã¢ã³ãããã®éä¿¡ã§ã¯RESTã使ãä¸æ¹ã§ããµã¼ãã¹éã®éä¿¡ã§ã¯ã¡ãã»ã¼ã¸ã³ã°ãå©ç¨ããåãµã¼ãã¹ã¯ã¡ãã»ã¼ã¸ã³ã°ãä»ãã¦å ¨ä½ã¨ãã¦ãã¸ãã¹ããã»ã¹ãå®ç¾ãã¾ãã
RESTã¯ãæ軽ãªè§£æ±ºçï¼
ããããã¡ãã»ã¼ã¸ã³ã°ãããRESTã«ããåæéä¿¡ã®ã»ãããæ軽ã¨æããããããã¾ããããã¡ããåãµã¼ãã¹éã§RESTãå©ç¨ããéä¿¡ãå©ç¨ã§ããªãããã§ã¯ãªããå®æ ã¨ãã¦å¤ç¨ããã¦ããç¾å®ãããã¾ãããããæ¬å½ã«ãæ軽ãªè§£æ±ºçã¨ãããã®ã§ããããï¼
RESTã§ã¯éä¿¡å ãµã¼ãã¹ãããã«ãã®å ãã¹ã¦ã®ãµã¼ãã¹ãæ£å¸¸ã«ç¨¼åãã¦ããªããã°å¦çã¯è¡ãã¾ãããããã¦ãã®ããã«ã©ããã§é害ãçºçããã¨é£éçã«æ©è½ä¸å ¨ã®ãµã¼ãã¹ãä¸æµã«åãã£ã¦çã¾ãã¦ã·ã¹ãã å ¨ä½ã«ã¾ã§åé¡ãæ³¢åãã¦ãã¾ããã¨ããããã¾ããããã«ã¯ãããã¦ã©ããã§åããã¼ããã¦ãã¾ã£ããªã¯ã¨ã¹ãã¯åã«å¤±ããã¦ãã¾ãã¾ãã
ã¾ãRESTãã¼ã¹ã®æ§æã§ã¯ãªã¯ã¨ã¹ãã¯ãã¤ã¬ã¯ãã«WEBãµã¼ãã¼ã¸ã®è² è·ã¨ãªãã¾ããé«è² è·ä¸ã§ã¯ã·ã¹ãã ãæ§æããæãå¼±ããµã¼ãã¼ãçãæã¡ã«ããã§ãããã
ãããããã¨ããä¿¡é ¼æ§ãé«ãããé«è² è·ä¸ã§é害ãåºããªãããã®ä»çµã¿ï¼ã¯ã©ã¹ã¿ãªã³ã°ããã¼ããã©ã³ãµããµã¼ãã¹ã¡ãã·ã¥ããµã¼ããããã¬ã¼ã«ã¼ãªã©ï¼ããã¹ãã¤ã¯ã¢ã¯ã»ã¹ãèæ ®ãããªã¯ã¨ã¹ãæ°ãããã¯ã¨ã³ãã®å¦çæéã®äºæ¸¬ããªã½ã¼ã¹ä½¿ç¨éã®åæãã¹ã¬ããæ°ãã¿ã¤ã ã¢ã¦ãæéã®èª¿æ´ããªã¼ãã¹ã±ã¼ã«ã®å ¥å¿µãªè¨è¨ãããã¦ãããã®ååãªè¨ç»ã¨ãã¹ããå¿ è¦ã¨ãªãã¾ãã
ãããã©ãã ãä¿¡é ¼æ§ãé«ããã¨ãã¦ããé害ãçºçããªããã¨ãåæã¨ããã·ã¹ãã è¨è¨ã許ãããããã§ã¯ããã¾ããããå®éã«é害ãã¨ã©ã¼ã¯å¿ ãçºçãã¾ãããã¼ã¿ã®ã¯ã¨ãªã§ããã°ãªãã©ã¤ã§æ¸ãããããã¾ããããå¦çéä¸ã§ã¨ã©ã¼ã¨ãªã£ãã³ãã³ãããªãã©ã¤ããã°éè¤ããå¦çããã¼ã¿ã®ä¸æ´åããããã¯ã¿ã¤ã ã¢ã¦ããªã©ã®ãããªãã¨ã©ã¼ãèµ·ããã¦ç¶æ³ã¯ããè¤éã«ãªãããã®è¨è¨ã¯å°é£ãã¤é«ä¾¡ã«ãªãã¾ãã
çµæã¨ãã¦RESTã§ã¯ã·ã¹ãã ãè¤éã«ãªãã°ãªãã»ã©ã巨大ãªãã¡ã¤ã³ã¢ãã«ã«ãããã¼ã«ã«ãã©ã³ã¶ã¯ã·ã§ã³ã¨ã¢ããªã·ãã¯ãªã·ã¹ãã æ§æã¨ããå§åãé«ã¾ãããã¤ã¯ããµã¼ãã¹ããé ããã£ã¦ãã¾ããã¨ãããã¾ãã
ãªããã¤ã¯ããµã¼ãã¹ã«ã¡ãã»ã¼ã¸ã³ã°(Kafka)ãå¿ è¦ãªã®ãï¼
ä¸æ¹ã§ã¡ãã»ã¼ã¸ã³ã°ã§ã¯ã©ããªã®ã§ãããï¼ã¡ãã»ã¼ã¸ã³ã°ã«ã¯ã©ã®ãããªã¡ãªãããããããªãå¿ è¦ã¨ãããã®ã§ããããï¼
å®å®ããã·ã¹ãã ãæ¯ããã¡ãã»ã¼ã¸ã³ã°
ãããã¥ã¼ãµå´(éä¿¡å´)ã§ã¯ãã¡ãã»ã¼ã¸ã®å®è¡çµæãå¾ ã¤ãã¨ã¯ãªããããå¿çæéãåä¸ããå°ãªããµã¼ãã§å¤§ããªã¹ã«ã¼ããããå®ç¾ã§ãã¾ããããã¦å¦çæéã®äºæ¸¬ãé¢é£ããã¿ã¤ã ã¢ã¦ã調æ´ãªã©ãæå°éã®èæ ®ã§æ¸ã¿ã¾ãã
ã¾ãã³ã³ã·ã¥ã¼ãå´(åä¿¡å´)ã¯ãã¡ãã»ã¼ã¸ã³ã°ã«ãããããã¡ã«ãããã¹ãã¤ã¯ã¢ã¯ã»ã¹ã«ãã£ã¦ãé«è² è·ç¶æ ã«é¥ããã¨ã¯ãªãã決ããããã¹ã¬ããæ°ã®ä¸ã§ã¡ãã»ã¼ã¸ãç¶ç¶çã«å¦çãç¶ãã¾ãã常ã«å®å®ããç¶æ³ä¸ã§ã¡ãã»ã¼ã¸ãå¦çãããã¨ãã§ãããªã½ã¼ã¹å©ç¨å¹çãé«ãããªã½ã¼ã¹å²å½ã®è¨è¨ã容æã§ããããã¦è² è·ãã¹ããããç¨åº¦ç°¡ç¥åãããã¨ãå¯è½ã§ããããã«ã¡ãã»ã¼ã¸ãéãããªãæé帯ã«ä¸æçã«ã³ã³ã·ã¥ã¼ãå´(åä¿¡å´)ããã¹ã¦åæ¢ããã¦ãã¾ããã¨ããæºã¾ã£ãã¡ãã»ã¼ã¸ã®ç¶æ³ã«åããã¦ãªã¼ãã¹ã±ã¼ã«ããããã¨ãæ¯è¼ç容æã§ãããã»ã©ã·ãã¢ãªå©ç¨äºæ¸¬ãè¨è¨ãæ±ãããããã¨ãããã¾ããã
ã¡ãã»ã¼ã¸ã³ã°ã¯ã¨ã©ã¼ãé害ã«ãå¼·ããã·ã¹ãã ã®å¯ç¨æ§ãä¿ã¤ã®ã«å½¹ç«ã¡ã¾ãããµã¼ãã¹ã®å調ã®ä¸ã§çºè¡ãããã¡ãã»ã¼ã¸ãKafkaã®ãããã¯ã«ä¿åããã¦ãããã¨ã§ãé害ãã¨ã©ã¼ã«ãã£ã¦ãå¦çã®ä¸éç¶æ ã¯å¸¸ã«æ確ã¨ãªããããã失ããããã¨ãããã¾ãããé害ã«é¥ã£ã¦ã復æ§å¾ã«ã¡ãã»ã¼ã¸ãå¦çããã¦ããã¼ã¿ã®ä¸æ´åãï¼éå¦çãªã©ãçºçããã(éè¤é ä¿¡ã®å¯¾å¦æ³ã¯çåç·¨ã«ã¦èª¬æ)ã«ãå¼ãç¶ããã¸ãã¹ããã»ã¹ãé²è¡ãã¾ãããã®ããå¦çéä¸ã§ãµã¼ãã¼ãè½ã¡ããã¨ã«ãå±éºã¯ãªããªããåå¥ãµã¼ãã¼ã«é度ãªä¿¡é ¼æ§ãããããå¿ è¦ãããã¾ããã
ã¾ãã¡ãã»ã¼ã¸ã³ã°ãéãã¦ãµã¼ãã¹éãæ¥ç¶ããããã¨ã§ããµã¼ãã¹ã®é害ãã·ã¹ãã å ¨ä½ã¸ã¨æ³¢åãããã¨ã¯é²ãããä»®ã«é害ä¸ã§ãã£ã¦ãéå®çãªãµã¼ãã¹ã®æä¾ãå¯è½ã§ãï¼ãã«ã¯ãããï¼ã
ãã®ããã«ãã¦ãã¡ãã»ã¼ã¸ã³ã°ã«ããã·ã¹ãã å ¨ä½ã®å®å®æ§ãåä¸ããã®ã§ãã
ã¤ãã³ãã¯å±¥æ´ã«ãªãï¼ã¤ãã³ãã¯ç¶æ ã®å¤åã表ããã¤ãã³ãã®èç©ãç¶æ ãåçããï¼
ã¨ã¯ãããä¸è¬çãªã¢ããªã±ã¼ã·ã§ã³ã§ã¯ãããããã¤ãã³ããªãã使ã£ã¦ãªããã¨ãå¤ãã§ãããããããã¤ãã³ããã·ã¹ãã ã«è¿½å ããã£ã¦ããã®ã¯è³æ¥µé¢åã«æãã¾ãã
ããããããããã¤ãã³ãã¯ä»ã®ãµã¼ãã¹ãã·ã¹ãã ã¸ã¨æ å ±ãéãããã ãã®ä»çµã¿ã§ã¯ããã¾ããããµã¼ãã¹ã®å é¨ã§ãã¤ãã³ãã¯éè¦ã§ãããããã¤ãã³ãããããã°æ¬æ¥ã¯ãªãã¸ã§ã¯ããDBã«ä¿åããå¿ è¦ããããã¾ããã
ä¸è¬çãªã¢ããªã±ã¼ã·ã§ã³ã§ã¯ããã¼ã¿ãã¼ã¹ä¸ã®ã¬ã³ã¼ãã«ææ°ã®ç¶æ ã®ã¿ãä¿åãã¾ããããã¦ãããä¸å¿ã«æããã¨ããªãã¸ã§ã¯ãç¶æ ã®å¤æ´ï¼ã¬ã³ã¼ãã®å¤æ´ï¼ã®çµæã¨ãã¦ãã¤ãã³ãã¯è¿½å ã§çºè¡ãããã®ã¨èããã¡ã§ãã
ããããããéã«èããã°ãçºè¡ãããã¤ãã³ãã®èç©ãããªãã¸ã§ã¯ãã®ç¶æ ã復å ã§ããã¨ãããã¨ã§ãããã¾ãï¼ã¤ãã³ãã½ã¼ã·ã³ã°ï¼ããã®èãæ¹ã®å ´åã«ã¯ããã¼ã¿ãã¼ã¹å ã«ã¤ãã³ãã®ã¿ãä¿å/èç©ããããªãã¸ã§ã¯ãããèªä½ã¯ä¿åããã¾ãããããããªãã¸ã§ã¯ãç¶æ ãä¿åããå ´åã«ã¯ç¹å®æç¹ã®ã¹ãããã·ã§ããã¨ãã¦æ±ãã¾ãã
ã³ãã³ãã®å®è¡æãªã©ããªãã¸ã§ã¯ããå¿ è¦ã¨ãªãå ´åã«ã¯ç´¯ç©ãããã¤ãã³ããé ã«åç(applyã¡ã½ãã)ããªãããªãã¸ã§ã¯ãã®ç¶æ ã復å ãã¾ãã ããã¦ãã³ãã³ãã®å®è¡ã®çµæã¨ãã¦ãï¼ãªãã¸ã§ã¯ãããèªä½ã®ç¶æ å¤æ´ã¯è¡ããã«ãï¼æ°ããã¤ãã³ãã追è¨ãã¾ãã ãµã¼ãã¹ã®å¦çã¯é常ãã³ãã³ããåãåã£ã¦ã¤ãã³ããè¿ãã¡ã½ãã(processã¡ã½ãã)ã¨ãã¦å®è£ ãããï¼ã¢ã¼ããã¯ãã£ã«ãããã¾ããï¼ãã¼ã¿ãæ¸ãæããå¦çã¯ããã¾ãããã¤ãã³ãã«ããäºå®ã追è¨ã«ãã£ã¦ç©ã¿éãããã¦ããã¾ãã
ã¤ãã³ãã¯ãã¸ãã¹ä¸ãéè¦ãªãã¼ã¿ã§ããã¤ãã³ãããããã®ä¸ã«ã¯ãªãã¸ã§ã¯ãå ã§ä½ãèµ·ãã£ãã®ããè¨é²ããã¦ãããããããã¹ã¦ãå±¥æ´ã¨ãã¦ä¿åããã¦ãããã¨ã§ãå種ã®åæãç£æ»ã§å©ç¨ããããä»»æã®æç¹ã®ãªãã¸ã§ã¯ãç¶æ ã復å ãããã¨ããå¯è½ã§ãã
ã¤ãã³ãã¯ä½ããã®ä»å±ç©ã§ã¯ãªããä»ã®ãµã¼ãã¹ã®ããã«çºè¡ãããã®ã§ãããã¾ããããããã¤ãã³ãããåå¨ãã¦ããã°å¾ã®ãã®ã¯ã©ãã¨ã§ãå°åºã§ãã¾ãï¼æ¥µè«ã§ããï¼ãã¤ãã³ãã¯ããåä½ã§åå¨ããã¹ããã®ã§ããä»ã®ã·ã¹ãã ã®ããã«è¿½å ã§è¨è¨ãå¿ è¦ãªé¢åãªãã®ã¨èããã®ã¯é©åã§ã¯ããã¾ããã
æ§ã ãªãµã¼ãã¹ãã·ã¹ãã ã¨é£æºãããæ å ±ãçµ±åããããã®ãã¼ã¿ãã
ã¤ãã³ãã¯åä½ã¨ãã¦ãåå¨ããã¹ããã®ã§ãããï¼é¨åçã«ã§ãï¼ãããä»ã®ãµã¼ãã¹ã«å ¬éããã°éç¥ããã¼ã¿è»¢éã®å½¹å²ãæ ãããã«ãªãã¾ãã
ã¡ãã»ã¼ã¸ã³ã°ã¯ãã¤ã¯ããµã¼ãã¹ã®ãããªå°ããç¬ç«ãããµã¼ãã¹ãã¤ãã³ããéãã¦é£æºãããããã«å¿ è¦ãªã®ã¯ãã¡ããããã¼ã¿ã¦ã§ã¢ãã¦ã¹ãåæã®ã·ã¹ãã ã¸ã®ãã¼ã¿è»¢éã®ããã«ãå¿ è¦ã§ãã
ãããããç¾å®çãªåé¡ã¨ãã¦ç»é¢è¡¨ç¤ºã®ãã¼ã¿ãæä¾ããã®ã«ç¹åãããã¥ã¼ãµã¼ãã¹ï¼query-side serviceï¼ãä½æãããã¨ãå¤ãããã¾ãããã¤ã¯ããµã¼ãã¹ã§ã¯åå¥ã®ãµã¼ãã¹ãåæãããä¸æ¹ã§ãã¯ã©ã¤ã¢ã³ãã«å¯¾ãã¦æ§ã ãªæ¤ç´¢æ¡ä»¶ããµã¼ãã¹ã横æãã¦çµã¿åãããããã¼ã¿ãä¸åº¦ã«æä¾ã§ããå¿ è¦ãããã¾ããã¤ã¾ããï¼ãµã¼ãã¹å ã®ãã¡ã¤ã³ã¢ãã«ã¨ã¯å¥ã«ãï¼è¡¨ç¤ºãæ¤ç´¢ã¨ãã£ãã¯ã¨ãªã®ã¿ã«ç¹åããåç §ç¨ã®ã¢ãã«ãå¿ è¦ã§ã(CQRS)ããã®ããã«ãå種ã®ãµã¼ãã¹ããçºè¡ãããã¤ãã³ããéãã¦ãçµ±åããã表示ç¨ã®ãã¼ã¿ãä½æããå°ç¨ã®ãã¥ã¼ãµã¼ãã¹ãå¿ è¦ã«ãªãã®ã§ãããã¥ã¼ãµã¼ãã¹ã¯ãã¦ã¼ã¶ãæ±ããæ§ã ãªè¦ä»¶ã«å¿ãã¦ãããã«é©ããè¤æ°ã®ãã¼ã¿ãã¼ã¹ã¨ãã¥ã¼ãçµã¿åããã¦å©ç¨ãã¾ãã
ããã¦ãããã®å®ç¾ãæ¯ããã®ãã¤ãã³ãã転éãããã¼ã¿ããã¨ãã¦ã®Kafkaã®å½¹å²ã§ããKafkaã¯åæ£ã·ã¹ãã ã§ã®ã³ããããã°ãããã¯ãã©ã³ã¶ã¯ã·ã§ã³ãã°ã¨è¡¨ç¾ãããå ´åãããã¾ãã
ã¤ãã³ããå©ç¨ãããã¨ã§ãµã¼ãã¹éã®ç§©åºããé¢ä¿ãç¶æã§ãã
ã¤ãã³ããå©ç¨ããªãã¨ããµã¼ãã¹éã®ç§©åºããé¢ä¿ãç¶æãããã¨ã¯ã§ãã¾ãããRESTã«ããåæéä¿¡ã¯åºæ¬çã«ãã«åï¼é æ¹åã®ä¾åï¼ãªã®ã§ããµã¼ãã¹ãRESTã®ã¿ãå©ç¨ããã¨éä¿¡ä¸ã®é¢ä¿ããã®ã¾ã¾ãµã¼ãã¹ã®ä¾åé¢ä¿ã¨ãªã£ã¦ç¬ç«æ§ãé»å®³ãã¾ãã
åãµã¼ãã¹ã¯ç¬ç«ãã¤ã¤ããä¸æµãä¸æµã¨ãã£ã秩åºããé¢ä¿æ§ãç¶æãã¦ãã¾ã(ã³ã³ããã¹ãããã)ããµã¼ãã¹ã«å¯¾ãã¦ã³ãã³ããéãã¨ãçµæã¨ãã¦ã¤ãã³ããåºåãã¾ãããµã¼ãã¹ã¯ã³ãã³ããåãåããã¨ã«ãã£ã¦ããã¤ãã³ããçºè¡ãããã¨ã«ãã£ã¦ãä»ãµã¼ãã¹ã«ä¾åãããã¨ã¯ããã¾ããã
ãããããã¨ã¯éã«ããµã¼ãã¹ãã³ãã³ããéãããããã¯ã¤ãã³ããåããããã«ã¯ãå½è©²ã®ãµã¼ãã¹ã¨ãã¡ã¤ã³ã®ç¥èã«å¯¾ãã¦ä¾åãããã¨ã«ãªãã¾ãããã®ããä¸æµãä¸æµã®é¢ä¿ã«ãã£ã¦ãé常ã¯åãµã¼ãã¹éã§ã¯ã¨ãªãã³ãã³ããéãå´ããããã¯éã«ã¤ãã³ããçºè¡ããå´ã¯æ±ºã¾ãã¾ãã
ä¾ãã°ãå注管çãµã¼ãã¹ããã®å¨åº«ç®¡çãµã¼ãã¹ã¸ã®ã³ãã³ãçºè¡ã¯è¨±ããã¦ãããã®éã®æ¹åããã®ã³ãã³ãã®çºè¡ã¾ã§è¨±ãã°ãç¸äºã®ä¾åé¢ä¿ã¨ãªãããã«ç¬ç«ãããµã¼ãã¹ã®é¢ä¿ãé»å®³ãã¦ãã¾ãã¾ããããããå¨åº«ç®¡çãµã¼ãã¹ãç¶æ å¤æ´ã«å¿ãã¦ã¤ãã³ããçºè¡ããå注管çãµã¼ãã¹å´ã§ã¤ãã³ããè³¼èªãããã¨ã§ãä¾åé¢ä¿ãç ´ç¶»ããããã¨ãªããµã¼ãã¹éã®å調ãå®ç¾ãããã¨ãã§ãã¾ãã
RESTã«ããåæéä¿¡ã¯åºæ¬çã«ã¯ãã«åï¼é æ¹åã®ä¾åï¼ãªã®ã§ãä¸æµãä¸æµã¨ãã£ã秩åºãããµã¼ãã¹ã®é¢ä¿æ§ãç¶æããããã«ããã¡ãã»ã¼ã¸ã³ã°ãå©ç¨ããã¤ãã³ãï¼éæ¹åã®ä¾åï¼ã¯å¿ è¦ã«ãªãã¾ãã
ã¨ããã§ããµã¼ãã¹éã§ã³ãã³ããå©ç¨ããã«ãã¤ãã³ãã®ã¿ã§ã¤ãªãã§å調ãããã¨ããè¨è¨ããç¡ç§©åºã«ãµã¼ãã¹ãé¢ä¿ã¥ãããã¨ã«ãªãã¾ãããããã¾ããµã¼ãã¹ã®ç¬ç«æ§ãé»å®³ããåæ£ã¢ããªã·ãã¯ãªã·ã¹ãã ã¨ãªãããã«ãå§ããããã¨ã¯ã§ãã¾ããã
å¾ç·¨ã§èª¬æããããã«ãã¹ãã¼ããã·ã³ãã¼ã¹ã§SAGAãè¡ãããã»ã¹ããã¼ã¸ã£ã¼ãå©ç¨ããã°ãããã§ä¾åé¢ä¿ã®èª¿åãè¡ããã¨ãã§ããé©åãªãµã¼ãã¹ã®é¢ä¿ãç¶æããã¾ã¾ã«å調ããã¦ãçµææ´åæ§ãä¿ã£ããã©ã³ã¶ã¯ã·ã§ã³ãå®ç¾ãããã¨ãå¯è½ã§ãã
ãã¤ã¯ããµã¼ãã¹ã§ã®è²¬åã®æ確åãä¿é²ããã
ä¸è¬çãªã¢ããªã±ã¼ã·ã§ã³ã§ã¯ãªãã¸ã§ã¯ããã¾ããã¨CRUDã«ãããã¼ã¿ãã¼ã¹ã«åºãå ¥ããããããªå®è£ (CQRSã§ã¯ãªãå®è£ )ã¨ãããã¨ãããã¾ããããããã¤ã¯ããµã¼ãã¹ã§ã¯ããã«ãªãã£ã¦ã³ãã³ãã«ã¾ããã¨ãªãã¸ã§ã¯ãã渡ãã¦ãµã¼ãã¹ã«æ´æ°ãä¾é ¼ãããããªæ±ç¨ã³ãã³ããè¨è¨ãããã¨ã¯ã§ãã¾ããã
ã¦ã¼ã¹ã±ã¼ã¹è¨è¿°ããµã¼ãã¹éã®ã¤ã³ã¿ã©ã¯ã·ã§ã³ããé©åã«ããããã®ã³ãã³ããè¨è¨ããå®è£ ã¸ã¨åæãããªãã¦ã¯ãªãã¾ãããé©åã«ã³ãã³ãã®å®ç¾©ããªãããªãå ´åã«ã¯ãããã«ãã£ã¦çºçããã¤ãã³ãã®ã¿ã¤ããéå®(ä¾ãã°å¸¸ã«UserUpdatedã¤ãã³ãã«ãªã)ããã¦ããã®å¾ã®é©åãªãã¸ãã¹ãã¸ãã¯ã®é§åãåæãå°é£ã«ãªãã¾ãã
ä¾ãã°ãä¸å£ã«ã¦ã¼ã¶ãªãã¸ã§ã¯ããæ´æ°ããã³ãã³ãã¨ãã£ã¦ããã¡ã¼ã«ã¢ãã¬ã¹ãæ´æ°ããå ´åãä½æãå¤æ´ããå ´åãåºåã¡ã¼ã«ãåæ¢ããå ´åãéä¼ãã¦ã¢ã«ã¦ã³ããåé¤ããå ´åãããããã§ç°ãªããã¸ãã¹ä¸ã®å¯¾å¿ãç°ãªãå®è¡æ¨©éãå¿ è¦ã«ãªãã¯ãã§ãããã®ããã«ããããããããã®ããã®ã³ãã³ãã¨ããã®å®è¡çµæã«å¯¾å¿ããé©åãªã¤ãã³ããå¿ è¦ã§ãã
ã³ãã³ãã¨ã¤ãã³ãã¯é©åãªè¨è¨ãè¡ã£ã¦å®è£ ã¾ã§æ£ç¢ºã«ãããã³ã°ãããã¹ããã®ã§ããã¡ãã»ã¼ã¸ã³ã°ãå©ç¨ãããã¨ã«ãããé©åã«ã³ãã³ãã¨ã¤ãã³ãã®è¨è¨ãè¡ããããã¤ã¯ããµã¼ãã¹ã§ã®è²¬åã®æ確åãä¿é²ããã¾ãã
ã³ãã³ãå®è¡é ã®ä¿è¨¼ã¨ä¸¦åå®è¡
Kafkaãå©ç¨ãã¦ã³ãã³ããé ä¿¡ãããã¨ã«ã¯æ´ãªãã¡ãªãããããã¾ãã並åå®è¡ã«ããã¹ã«ã¼ããããåä¸ããå¦çé ãä¿è¨¼ãããããã¦ãµã¼ãã¹å ã®æä»å¶å¾¡ãçç¥ãããã¨ãã§ãã¾ãã
ä¸è¬çãªã¡ãã»ã¼ã¸ã³ã°ã·ã¹ãã (Apache ActiveMQãªã©)ã§ã¯ã³ã³ã·ã¥ã¼ãï¼åä¿¡å´ï¼ãï¼ã¤ã«ããã°ï¼ã¤ãã¤é çªã«å¦çãããã¨ãå¯è½ã§ãããè¤æ°ã®ã³ã³ã·ã¥ã¼ããå©ç¨ããã¨ããããã¸ã®é ä¿¡é èªä½ã¯é çªã©ããã§ãã£ã¦ãã並åã«å¦çãããããã«å¦çé ã¯åå¾ããã®ãä¸è¬çï¼MQ次第ã§ããåé¿çãããã¾ãï¼ã§ãã
ä¸æ¹ã§ãKafkaã§ã¯ãããã¯å ã®ãã¼ãã£ã·ã§ã³ãã¨ã«ã³ã³ã·ã¥ã¼ãã¯åºå®ããã¾ãããã®ããããããã¯å ¨ä½ã¨ãã¦è¦ãå ´åã«ã¯ä¸¦åå¦çããã¦ã¹ã«ã¼ããããåä¸ãããä¸æ¹ã§ãåãã¼ãã£ã·ã§ã³ã®ã¡ãã»ã¼ã¸ã¯å²ãå½ã¦ãããã³ã³ã·ã¥ã¼ãã®ä¸ã§ã·ã³ã°ã«ã¹ã¬ããã§ï¼ã¤ãã¤é çªã«å¦çï¼Single Writerï¼ããããã¨ãã§ãã¾ãã
ãã®ããããªãã¸ã§ã¯ãã®IDï¼æ£ç¢ºã«ã¯éç´ã«ã¼ãã®IDï¼ããã¼ãã£ã·ã§ã³ã®ãã¼ã¨ãã¦å©ç¨ããã°ãåãªãã¸ã§ã¯ãã«å¯¾ãã¦ã³ãã³ãã¯ä¸åº¦ã«ï¼ã¤ãã¤ã®ã¿ãå¦çããã¾ããããã¦è¤æ°ã®ã¹ã¬ããã§åæã«å®è¡ãããã¨ãã¼ã¿ã®ä¸æ´åãçºçãããããªã®ç®æ(ã¯ãªãã£ã«ã«ã»ã¯ã·ã§ã³)ã§ã®ãæ²è¦³çããã¯ã楽観çããã¯ã¨ãã£ããããªæä»å¶å¾¡ãçç¥ãããã¨ãã§ãã¾ããããã¯ãµã¼ãã¹å®è£ ãã·ã³ãã«ã«ããã¹ã«ã¼ããããåä¸ãããå©ç¨å¯è½ãªãã¼ã¿ãã¼ã¹ã®é¸æè¢ï¼æä»å¶å¾¡ã®å¼±ãNoSQLãªã©ãå«ãï¼ãå¢ããã¾ããã¾ããªãã¸ã§ã¯ããã¨ã®ã¡ãã»ã¼ã¸å¦çé ï¼é ä¿¡é ã§ã¯ãªãï¼ãåæã¨ããã·ã¹ãã è¨è¨ãå¯è½ã¨ãªãã¾ãã
ã¾ã¨ã
ä»åã¯æ¦è¦ç·¨ã¨ãã¦ããã¤ã¯ããµã¼ãã¹ã§ãªãã¡ãã»ã¼ã¸ã³ã°ãå¿ è¦ãªã®ãã説æãã¦ãã¾ãããã¾ããã¤ã¯ããµã¼ãã¹ã«ããã·ã¹ãã ã«ããã¦ãã©ãã§ã¡ãã»ã¼ã¸ã³ã°ãå©ç¨ãããã®ãã説æãã¾ãããããã¦RESTãå©ç¨ããå ´åã«çºçãããå種ã®èª²é¡ã確èªããä¸æ¹ã§ãã¡ãã»ã¼ã¸ã³ã°ã«ãã£ã¦ã·ã¹ãã å ¨ä½ã¨ãã¦ã®å®å®æ§ãåä¸ãããã¨ã説æãã¾ããããã¡ãããã¤ã¯ããµã¼ãã¹ã§ãRESTã¯å©ç¨ããã®ã§ããããµã¼ãã¹éã§ã¯ã¡ãã»ã¼ã¸ã³ã°ãå©ç¨ãããã¨ãæ¤è¨ãã¦ãã ããã
ã¾ããã®å¾ãã¤ãã³ãã¯å±¥æ´ã¨ãªããã¾ããµã¼ãã¹éãé£æºãã秩åºããé¢ä¿ãç¶æããããã«ãéè¦ãªå½¹å²ãæ ã£ã¦ãããã¨ã確èªãã¾ãããããã¦æå¾ã«Kafkaã«ãã£ã¦ã³ãã³ãã®å®è¡é ãä¿è¨¼ãã並åå®è¡ã«ãå½¹ç«ã¤ãã¨ã説æãã¾ããããã¤ã¯ããµã¼ãã¹ã«ããã¦ãã¡ãã»ã¼ã¸ã³ã°ã«ããã³ãã³ããã¤ãã³ãã¯è¨è¨ä¸ãå®è£ ä¸ããªãã¦ã¯ãªããªããã®ã§ãã
ä»åã¯æ¦è¦ç·¨ã¨ãã¦ã¡ãã»ã¼ã¸ã³ã°ã®è¯ãé¢ã°ããã«ç¦ç¹ãå½ã¦ã¾ãããã次ã®çåç·¨ã§ã¯at least once(ä¸å以ä¸)ã®é ä¿¡ã«ããã¡ãã»ã¼ã¸ã®éè¤é ä¿¡ã«ã©ã対å¦ããã®ãã¨ãããã¼ã¿æ´æ°ã¨ã¡ãã»ã¼ã¸éä¿¡ãã©ã®ããã«ã¢ãããã¯ã«è¡ãã®ãã¨ãã£ããããªãã¡ãã»ã¼ã¸ã³ã°ã§ããçåã¨ããããã¨ã説æãããã¨æãã¾ãã
ããã¤ã¯ããµã¼ãã¹ã¨ã¡ãã»ã¼ã¸ã³ã°ã®ãªã [çåç·¨]ãã¯ãã¡ãã§ãã
Special Thanks
åå¼·ä¼ã§ã®è°è«ããã®è¨äºã®åºã«ãªã£ã¦ãã¾ããåå è ã®æ¹ã ãããã¦ã¬ãã¥ã¼ãã¦ãã ãã£ãæ¹ã ãã©ãããããã¨ããããã¾ããï¼
åèæç®
- Microservices Patterns
- Enterprise Integration Patterns
- Designing Event-Driven Systems
- å®è·µãã¡ã¤ã³é§åè¨è¨
- Exploring CQRS and Event Sourcing
- UMLã«ãããã¸ãã¹ã¢ããªã³ã°
[PR]
- ãªã¼ãã³ã½ã¼ã¹ã®Apache Kafkaããã¼ã¹ã¨ãã製åã¨ãã¦ãRed HatããAMQ Streamsãæä¾ããã¦ãã¾ããAMQ Streamsã«ã¯ããã«ãKubernetesãOpenShiftã®ä¸ã§Apache Kafkaã®éç¨ã®èªååãå®ç¾ããããªã¼ãã³ã½ã¼ã¹ã®Strimziããã¼ã¹ã¨ããæ©è½ãå«ã¾ãã¦ãã¾ããRed Hat製åã«èå³ãªãæ¹ãKubernetesãå©ç¨ãã¦ããæ¹ã¯Strimziããã²ä½¿ã£ã¦ã¿ã¦ãã ããã