ããã¯ããã€ã¯ããµãŒãã¹ã¢ãŒããã¯ãã£ã䜿çšãããã©ã³ã¶ã¯ã·ã§ã³ã¢ããªã±ãŒã·ã§ã³ã®éçºã«é¢ãã翻蚳èšäºã®å®äºã§ãã ãéå§ããŸãã
èšäºã®æåã®éšåã§ããã€ã¯ããµãŒãã¹ã¢ãŒããã¯ãã£ã䜿çšããéã®äž»ãªé害ã¯ããã¡ã€ã³ã¢ãã«ããã©ã³ã¶ã¯ã·ã§ã³ãããã³ã¯ãšãªãæ©èœåé¢ã«å¯ŸããŠé©ãã»ã©èæ§ããããšããããšã§ãã 解決çã¯ãåãµãŒãã¹ã®ããžãã¹ããžãã¯ãDDDéåã®åœ¢åŒã§å®è£ ããããšã§ããããšã瀺ãããŸããã åãã©ã³ã¶ã¯ã·ã§ã³ã¯ãåäžã®éçŽãæŽæ°ãŸãã¯äœæããŸãã ã€ãã³ãã¯ãéåäœïŒããã³ãµãŒãã¹ïŒéã®ããŒã¿ã®æŽåæ§ãç¶æããããã«äœ¿çšãããŸãã
èšäºã®ç¬¬2éšã§ã¯ãã€ãã³ãã䜿çšããéã®éèŠãªã¿ã¹ã¯ããéçŽã®ç¶æ ã®ã¢ãããã¯ãªå€æŽãšã€ãã³ãã®åæçºè¡ã§ããããšã確èªããŸãã ã€ãã³ããœãŒã·ã³ã°ã§ãã®åé¡ã解決ããæ¹æ³ãèŠãŠã¿ãŸããããããžãã¹ããžãã¯ãšç¶æ ä¿åã·ã¹ãã ã®èšèšã«ã€ãã³ãæåã®ã¢ãããŒãã䜿çšããŸãã ãã®åŸããã€ã¯ããµãŒãã¹ã¢ãŒããã¯ãã£ãããŒã¿ããŒã¹ã¯ãšãªã®å®è£ ãå°é£ã«ããã³ãã³ãã¯ãšãªã®è²¬ä»»åé¢ïŒCQRSïŒãšåŒã°ããã¢ãããŒããã¹ã±ãŒã©ãã«ã§å¹ççãªã¯ãšãªã®å®è£ ã«ã©ã®ããã«åœ¹ç«ã€ãã説æããŸãã
ããŒãã€ã³ãïŒ
- ã€ãã³ããœãŒã·ã³ã°ã¯ãç¶æ ã確å®ã«å€æŽããã€ãã³ããçºè¡ããããã®ã¡ã«ããºã ã§ãããä»ã®ãœãªã¥ãŒã·ã§ã³ã®å¶éãå æã§ããŸãã
- ã€ãã³ããœãŒã·ã³ã°ã䜿çšããã€ãã³ãæåã®ã¢ãããŒãã¯ããã€ã¯ããµãŒãã¹ã¢ãŒããã¯ãã£ãšããäžèŽããŠããŸãã
- ã¹ãããã·ã§ããã¯ãç¹å®ã®æç¹ããåã«çºçãããã¹ãŠã®ã€ãã³ããçµã¿åãããããšã«ãããéèšç¶æ ã¯ãšãªã®ããã©ãŒãã³ã¹ãåäžãããããšãã§ããŸãã
- ã€ãã³ããœãŒã·ã³ã°ã¯ã¯ãšãªã«åé¡ãåŒãèµ·ããå¯èœæ§ããããŸãããCQRSãšãããªã¢ã©ã€ãºããã¥ãŒã«ãã£ãŠå æãããŸãã
- ã€ãã³ããœãŒã·ã³ã°ãšCQRSã¯ç¹å¥ãªããŒã«ããœãããŠã§ã¢ãå¿ èŠãšããŸãããå€ãã®æ¢åã®ãã¬ãŒã ã¯ãŒã¯ã¯ãå¿ èŠãªäœã¬ãã«æ©èœã®äžéšãåŒãåããããšãã§ããŸãã
ä¿¡é Œæ§ã®é«ãã¹ããŒã¿ã¹æŽæ°ãšã€ãã³ãçºè¡
äžèŠãããšãã€ãã³ããä»ããŠéèšéã®äžè²«æ§ã確ä¿ããããšã¯ãéåžžã«ç°¡åãªã¿ã¹ã¯ã®ããã§ãã ããŒã¿ããŒã¹ã§éèšãäœæãŸãã¯æŽæ°ããããšã«ããããµãŒãã¹ã¯åã«ã€ãã³ããå ¬éããŸãã ãã ããåé¡ããããŸããããŒã¿ããŒã¹ã®æŽæ°ãšã€ãã³ãã®å ¬éã¯ã¢ãããã¯ã«å®è¡ããå¿ èŠããããŸãã ããšãã°ãããŒã¿ããŒã¹ã®æŽæ°åŸãã€ãã³ãã®çºè¡åã«äœããå£ããå Žåãã·ã¹ãã ã¯äžå®å®ãªç¶æ ã«ãªããŸãã ãã®å Žåã®åŸæ¥ã®ãœãªã¥ãŒã·ã§ã³ã¯ãããŒã¿ããŒã¹ãšã¡ãã»ãŒãžãããŒã«ãŒãå«ãåæ£ãã©ã³ã¶ã¯ã·ã§ã³ã§ãã ããããèšäºã®ååã§èª¬æããçç±ã«ãããåæ£ãã©ã³ã¶ã¯ã·ã§ã³ã¯å®è¡å¯èœãªãªãã·ã§ã³ã§ã¯ãããŸããã
åæ£ãã©ã³ã¶ã¯ã·ã§ã³ã䜿çšããã«ãã®åé¡ã解決ããã«ã¯ãããã€ãã®æ¹æ³ããããŸãã ããšãã°ãã¡ãã»ãŒãžãããŒã«ãŒïŒ Apache Kafkaãªã© ïŒã䜿çšã§ããŸãã
åå¥ã«éžæãããã¡ãã»ãŒãžåä¿¡è ããããŒã«ãŒã¡ãã»ãŒãžããµãã¹ã¯ã©ã€ãããããããåä¿¡ãããšãããŒã¿ããŒã¹ãæŽæ°ããŸãã ãã®ã¢ãããŒãã«ãããããŒã¿ããŒã¹ãæŽæ°ãããã€ãã³ããçºè¡ãããŸãã ãã®çæã¯ãã¢ããªã±ãŒã·ã§ã³ãããŒã¿ããŒã¹ãžã®æžã蟌ã¿ã®ããã«éä¿¡ãããã®ãããã«èªã¿åããªããã¯ããã«è€éãªäžè²«æ§ã¢ãã«ã§ããããšã§ãã
å¥ã®è§£æ±ºçã¯ã ãã©ã³ã¶ã¯ã·ã§ã³ãã°ã®æ«å°Ÿãã¿ãŒã³ã§ãããã©ã³ã¶ã¯ã·ã§ã³ãã°ããã¬ã³ãŒããååŸãããã€ãã³ãã«å€æãããã¡ãã»ãŒãžãããŒã«ãŒã«éä¿¡ãããŸãã ãã®ã¢ãããŒãã®éèŠãªå©ç¹ã¯ãã¢ããªã±ãŒã·ã§ã³ãå€æŽããå¿ èŠããªãããšã§ãã ãã ããæ¬ ç¹ã¯ãäœã¬ãã«ã®å€æŽããããŒãã«å ã®è¡ãžã®é«ã¬ãã«ã®ããžãã¹ã€ãã³ãïŒããŒã¿ããŒã¹ãæŽæ°ããçç±ïŒããªããŒã¹ãšã³ãžãã¢ãªã³ã°ããããšãå°é£ã«ãªãå¯èœæ§ãããããšã§ãã
3çªç®ã®è§£æ±ºçïŒããŒã¿ããŒã¹ããŒãã«ãäžæçãªã¡ãã»ãŒãžãã¥ãŒãšããŠäœ¿çšããŸãã ãµãŒãã¹ãéèšãæŽæ°ãããšãããŒã«ã«ãã©ã³ã¶ã¯ã·ã§ã³å ã®EVENTSããŒã¿ããŒã¹ã®ç¹å¥ãªããŒãã«ã«ã€ãã³ããè¿œå ãããŸãã å¥ã®ããã»ã¹ãEVENTSããŒãã«ãå®æçã«ã¹ãã£ã³ããŠã€ãã³ããçºè¡ããã¡ãã»ãŒãžãããŒã«ãŒã«éä¿¡ããŸãã
ãã®ãœãªã¥ãŒã·ã§ã³ã®åªããæ©èœã¯ããµãŒãã¹ãé«ã¬ãã«ã®ããžãã¹ã€ãã³ããçºè¡ã§ããããšã§ãã æ¬ ç¹ïŒãã®æ¹æ³ã¯ãã€ãã³ãçºè¡ã³ãŒããããžãã¹ããžãã¯ãšåæããå¿ èŠãããããããšã©ãŒãçºçããå¯èœæ§ããããŸãã
3ã€ã®ãªãã·ã§ã³ã«ã¯ãã¹ãŠé倧ãªæ¬ ç¹ããããŸãã 延æãããããŒã¿ããŒã¹æŽæ°ã䜿çšããŠã¡ãã»ãŒãžãããŒã«ãŒãä»ããŠã€ãã³ããçºè¡ããŠããRead-your-writesæŽåæ§ã¢ãã«ã®æ¡ä»¶ã¯æäŸãããŸããã ãã©ã³ã¶ã¯ã·ã§ã³ãã°ã«åºã¥ããŠã€ãã³ããå ¬éãããšãããŒã¿èªã¿åãã®äžè²«æ§ãä¿èšŒãããŸãããé«ã¬ãã«ã®ããžãã¹ã€ãã³ããåžžã«å ¬éã§ãããšã¯éããŸããã ããŒã¿ããŒã¹ããŒãã«ãã¡ãã»ãŒãžãã¥ãŒãšããŠäœ¿çšãããšãé«ã¬ãã«ã®ããžãã¹ã€ãã³ãã®äžè²«ããèªã¿åããšçºè¡ãä¿èšŒãããŸãããéçºè ã¯ç¶æ ãå€åãããšãã«ã€ãã³ããçºè¡ããããšãå¿ããªãã§ãã ããã
幞ããªããšã«ãå¥ã®è§£æ±ºçããããŸãã ããã¯ãã€ãã³ããœãŒã·ã³ã°ãšåŒã°ãããç¶æ ä¿åãšããžãã¹ããžãã¯ã«å¯Ÿããã€ãã³ãé§ååã®ã¢ãããŒãã§ãã
ã€ãã³ããœãŒã·ã³ã°ã«ãããã€ã¯ããµãŒãã¹éçº
ã€ãã³ããœãŒã·ã³ã°ã¯ãã€ãã³ãé§ååã®ã¹ããŒããã«ã¢ãããŒãã§ãã ããã¯æ°ããã¢ã€ãã¢ã§ã¯ãããŸããã 5幎以äžåã«æåã«ã€ãã³ããœãŒã·ã³ã°ã«ã€ããŠåŠã³ãŸãããããã€ã¯ããµãŒãã¹ã®éçºãéå§ãããŸã§ã¯å¥œå¥å¿ãæ®ããŸããã ã€ãã³ããœãŒã·ã³ã°ã¯ãã€ãã³ãé§ååã®ãã€ã¯ããµãŒãã¹ã¢ãŒããã¯ãã£ãå®è£ ããåªããæ¹æ³ã§ããããšãå®èšŒãããŠããŸãã
ã€ãã³ããœãŒã·ã³ã°ã䜿çšãããµãŒãã¹ã¯ãéçŽã®ç¶æ ãäžé£ã®ã€ãã³ããšããŠä¿åããŸãã éèšãäœæãŸãã¯æŽæ°ããããšããµãŒãã¹ã¯ããŒã¿ããŒã¹å ã®ç¹å¥ãªã€ãã³ãã¹ãã¢ã«1ã€ä»¥äžã®ã€ãã³ããä¿åããŸãã
ãŠãããã®çŸåšã®ç¶æ ãååŸããããã«ãã€ãã³ããããŠã³ããŒããããŠåçãããŸãã é¢æ°åããã°ã©ãã³ã°ã®èŠ³ç¹ããããµãŒãã¹ã¯ã€ãã³ãã«å¯ŸããŠfold / reduceæ©èœãå®è¡ããããšã«ããããŠãããã®çŸåšã®ç¶æ ãåæ§ç¯ããŸãã ã€ãã³ãã¯çŸåšç¶æ ã§ãããããç¶æ ã®æŽæ°ãšã€ãã³ãã®å ¬éã®ååæ§ã«é¢ããåé¡ã¯ãããããŸããã
ããšãã°ã泚æãµãŒãã¹ãèããŸãã å泚æãORDERSããŒãã«ã®è¡ãšããŠä¿åãã代ããã«ã泚æã®åéèšããCreate Order ã Order Approved ã Order Sentãªã©ã®äžé£ã®ã€ãã³ããšããŠä¿åããŸãã SQLããŒã¿ããŒã¹ã䜿çšããŠãªã³ã©ã€ã³ã¹ãã¢ã«ä¿åããæ¹æ³ã次ã«ç€ºããŸãã
Entity_typeããã³entity_idåã¯éçŽèå¥åã§ãã
event_id-ã€ãã³ãèå¥åã
event_type-ã€ãã³ãã®ã¿ã€ãã
event_data-JSON圢åŒã®ã·ãªã¢ã«åãããã€ãã³ãå±æ§ã
äžéšã®ã€ãã³ãã«ã¯å€ãã®ããŒã¿ãå«ãŸããŠããŸãã ããšãã°ã 泚æäœæã€ãã³ãã«ã¯ã泚æã®æ§æãè«æ±æ å ±ãé éæ å ±ã«é¢ããæ å ±ãå«ãŸããŸãã Order Sentã€ãã³ãã«ã¯æå°éã®æ å ±ãå«ãŸããŠãããåã«ç¶æ éã®é·ç§»ã§ãã
ã€ãã³ãã®èª¿éãšã€ãã³ãã®å ¬é
å³å¯ã«èšãã°ãã€ãã³ããœãŒã·ã³ã°ã¯åçŽã«éçŽã®ç¶æ ãã€ãã³ããšããŠä¿åããŸãã ã€ãã³ããçºè¡ããããã®ä¿¡é Œã§ããã¡ã«ããºã ãšããŠäœ¿çšããã®ã¯éåžžã«ç°¡åã§ãã ã€ãã³ãã®ä¿åã¯æ¬è³ªçã«ã¢ãããã¯ãªæäœã§ãããã€ãã³ããªããžããªããã¹ãŠã®é¢å¿ã®ãããµãŒãã¹ãžã®ã€ãã³ããžã®ã¢ã¯ã»ã¹ãæäŸããããšãä¿èšŒããŸãã ããšãã°ãã€ãã³ããäžèšã®EVENTSããŒãã«ã«æ ŒçŽãããŠããå Žåããµãã¹ã¯ã©ã€ãã¯åã«å®æçã«ããŒãã«ãããŒãªã³ã°ããŠæ°ããã€ãã³ããåä¿¡ã§ããŸãã ããæŽç·Žãããã€ãã³ãã¹ãã¢ã¯ãåæ§ã®ä¿èšŒãæäŸããå¥ã®ã¢ãããŒãã䜿çšããŸãããããçç£çã§ã¹ã±ãŒã©ãã«ã§ãã ããšãã°ã Eventuate Localã¯ãã©ã³ã¶ã¯ã·ã§ã³ãã°ããŒãªã³ã°ãã¿ãŒã³ã䜿çšããŸãã MySQLã¬ããªã±ãŒã·ã§ã³ã¹ã¬ããããEVENTSããŒãã«ã«æ¿å ¥ãããã€ãã³ããèªã¿åããApache Kafkaã䜿çšããŠçºè¡ããŸãã
ç¶æ ã¹ãããã·ã§ããã䜿çšããŠããã©ãŒãã³ã¹ãæ¹åãã
ãªãŒããŒãŠãããã¯ãç¶æ é·ç§»ã®æ°ãæ¯èŒçå°ãªãããšãç¹åŸŽãšããŠãããããã€ãã³ãã®æ°ã¯ãããã§ãã ãã®å Žåãã€ãã³ãã¹ãã¢ããã®èŠæ±ãšOrderéèšã®çŸåšã®ç¶æ ã®åæ§ç¯ãæå¹ã«ãªããŸãã ãã ããäžéšã®ãŠãããã«ã¯å€æ°ã®ã€ãã³ãããããŸãã ããšãã°ã 顧客éèšã«ã¯ãå€ãã®ã¯ã¬ãžããäºçŽã€ãã³ããå«ãŸããå¯èœæ§ããããŸãã æéãçµã€ã«ã€ããŠããããã®ããŒããšåŠçã¯éå¹ççã«ãªããŸãã
åé¡ã®äžè¬çãªè§£æ±ºçã¯ããŠãããã®ç¶æ ã®ã¹ãããã·ã§ãããå®æçã«ä¿åããããšã§ãã ã¢ããªã±ãŒã·ã§ã³ã¯ãæåŸã®ã¹ãããã·ã§ãããšãã®äœæ以éã«çºçããã€ãã³ãã®ã¿ãããŒãããããšã«ããããŠãããã®ç¶æ ã埩å ããŸãã é¢æ°åããã°ã©ãã³ã°ã§ã¯ãã¹ãããã·ã§ããã¯fold / reduceã®åæå€ãè¡šããŸãã ãŠãããã®æ§é ãåçŽã§ç°¡åã«ã·ãªã¢ã«åã§ããå Žåãã¹ãããã·ã§ããã¯ãããšãã°JSON圢åŒã«ããããšãã§ããŸãã Mementoãã¿ãŒã³ã䜿çšããŠãããè€éãªéåäœã®åçãæ®åœ±ã§ããŸãã
ããšãã°ããªã³ã©ã€ã³ã¹ãã¢ã®é¡§å®¢ãŠãããã®æ§é ã¯éåžžã«åçŽã§ãã顧客ã«é¢ããæ å ±ãä¿¡çšé床ãããã³ä¿¡çšè³éã®äºçŽã«é¢ããããŒã¿ã§ãã ã¯ã©ã€ã¢ã³ãã®ã¹ãããã·ã§ããã¯ãJSON圢åŒã®ã¹ããŒã¿ã¹ã«é¢ããäžé£ã®ããŒã¿ã§ãã ãã®å³ã¯ãã€ãã³ã103ã®æç¹ã®ã¯ã©ã€ã¢ã³ãã®ç¶æ ã«å¯Ÿå¿ããã¹ãããã·ã§ããããã¯ã©ã€ã¢ã³ãã®ç¶æ ãåäœæããæ¹æ³ã瀺ããŠããŸããã«ã¹ã¿ããŒãµãŒãã¹ã¯ãã¹ãããã·ã§ãããããŠã³ããŒããã103çªç®ä»¥éã«çºçããã€ãã³ããåŠçããã ãã§ãã
ã¯ã©ã€ã¢ã³ããµãŒãã¹ã¯ãã¹ãããã·ã§ããã®JSONãéã·ãªã¢ã«åããã€ãã³ã104ã106ãèªã¿èŸŒãã§åŠçããããšã«ãããã¯ã©ã€ã¢ã³ãã®ç¶æ ãåäœæããŸãã
ã€ãã³ããœãŒã·ã³ã°ã®å®è£
ã€ãã³ããªããžããªã¯ãããŒã¿ããŒã¹ãšã¡ãã»ãŒãžãããŒã«ãŒã®ãã€ããªããã§ãã äž»ããŒã䜿çšããŠéçŽã€ãã³ããæ¿å ¥ããã³ååŸããããã®APIãåããŠãããããããŒã¿ããŒã¹ã§ãããã€ãã³ãããµãã¹ã¯ã©ã€ãããããã®APIãåããŠãããããã¡ãã»ãŒãžãããŒã«ãŒã§ããããŸãã
ã€ãã³ããªããžããªãå®è£ ããã«ã¯ãããã€ãã®ç°ãªãæ¹æ³ããããŸãã ãããã®1ã€ã¯ãç¬èªã®ã€ãã³ããœãŒã·ã³ã°ãã¬ãŒã ã¯ãŒã¯ãäœæããããšã§ãã ããšãã°ãã€ãã³ããRDBMSã«ä¿åã§ããŸãã ããã¯ãã€ãã³ããæçš¿ããããã®ãããã©ãŒãã³ã¹ã¯äœããŠãç°¡åãªæ¹æ³ã§ãã ãµãã¹ã¯ã©ã€ãã¯ãåã«å®æçã«EVENTSããŒãã«ãããŒãªã³ã°ããŠæ°ããã€ãã³ããæ¢ããŸãã
å¥ã®ãªãã·ã§ã³ïŒç¹å¥ãªã€ãã³ããªããžããªã䜿çšããŸããããã¯ãååãšããŠãè±å¯ãªæ©èœã»ãããããé«ãããã©ãŒãã³ã¹ãšã¹ã±ãŒã©ããªãã£ãæäŸããŸãã ã€ãã³ããœãŒã·ã³ã°ã®å é§è ã§ããGreg Youngã¯ãEvent StoreïŒhttps://geteventstore.comïŒãšåŒã°ãã.NETããŒã¹ã®ãªãŒãã³ãœãŒã¹ã€ãã³ããªããžããªãäœæããŸããã 以åTypesafeãšããŠç¥ãããŠããLightbend㯠ãã€ãã³ããœãŒã·ã³ã°ã«åºã¥ããŠLagomãã€ã¯ããµãŒãã¹ãã¬ãŒã ã¯ãŒã¯ãéçºããŸããã ã¯ã©ãŠããµãŒãã¹ãšããŠå©çšå¯èœãªã€ãã³ããœãŒã·ã³ã°ãã¬ãŒã ã¯ãŒã¯ãåããEventuateã¹ã¿ãŒãã¢ããã¯ãKafkaãšRDBMSã䜿çšãããªãŒãã³ãœãŒã¹ãããžã§ã¯ãã§ãã
ã€ãã³ããœãŒã·ã³ã°ã®é·æãšçæ
ã€ãã³ããœãŒã·ã³ã°ã«ã¯é·æãšçæããããŸãã ãã®ã¢ãããŒãã®äž»ãªå©ç¹ã¯ãéçŽã®ç¶æ ãå€åãããã³ã«ã€ãã³ããçºè¡ãããããšãä¿èšŒãããããšã§ãã ããã¯ãã€ãã³ãé§ååã®ãã€ã¯ããµãŒãã¹ã¢ãŒããã¯ãã£ã®åªããåºç€ã§ãã ããã«ãåã€ãã³ãã¯å€æŽãè¡ã£ããŠãŒã¶ãŒã®èå¥åãèšé²ã§ãããããã€ãã³ããœãŒã·ã³ã°ã¯æ£ç¢ºæ§ãä¿èšŒãããç£æ»èšŒè·¡ãæäŸããŸãã ã€ãã³ãã¹ããªãŒã ã¯ããŠãŒã¶ãŒãžã®éç¥ã®éä¿¡ãªã©ãä»ã®ç®çã«äœ¿çšã§ããŸãã
ã€ãã³ããœãŒã·ã³ã°ã®ãã1ã€ã®å©ç¹ã¯ãåãŠãããã®å±¥æŽå šäœãä¿åã§ããããšã§ãã éèšã®ç¶æ ãéå»ã«æ»ãäžæã¯ãšãªãç°¡åã«å®è£ ã§ããŸãã ç¹å®ã®æç¹ã§ã®ãŠãããã®ç¶æ ãå€æããã«ã¯ããã®ç¬éã®åã«çºçããã€ãã³ããåŠçããã ãã§ãã ããšãã°ãéå»ã®ããæç¹ã§å©çšå¯èœãªé¡§å®¢ããŒã³ãç°¡åã«èšç®ã§ããŸãã
éçŽèªäœã§ã¯ãªããã€ãã³ããä¿åããããšã«ãããã€ãã³ããœãŒã·ã³ã°ã¯éåžžããã€ã³ããŒãã³ã¹ã®äžäžèŽãã®åé¡ãåé¿ããŸãã ååãšããŠãã€ãã³ãã¯ã·ã³ãã«ã§ç°¡åã«ã·ãªã¢ã«åå¯èœãªæ§é ãæã£ãŠããŸãã ã·ãªã¢ã«åã«ããããµãŒãã¹ã¯è€éãªãŠãããã®ã¹ããŒã¿ã¹ã®ã¹ãããã·ã§ãããååŸã§ããŸãã Mementoãã¿ãŒã³ã¯ãéåäœãšãã®ã·ãªã¢ã«åãããè¡šçŸã®éã«éæ¥æ§ã®ã¬ãã«ãè¿œå ããŸãã
ãã ããã€ãã³ããœãŒã·ã³ã°ãã¯ãããžã¯ããã»ã©ã¹ã ãŒãºã§ã¯ãªããããã€ãã®æ¬ ç¹ããããŸãã ããã¯ã調æ»ããå¿ èŠããããã1ã€ã®çããããã°ã©ãã³ã°ã¢ãã«ã§ãã æ¢åã®ã¢ããªã±ãŒã·ã§ã³ã§ã€ãã³ããœãŒã·ã³ã°ã®äœ¿çšãéå§ããã«ã¯ãããžãã¹ããžãã¯ãæžãçŽãå¿ èŠããããŸãã 幞ããªããšã«ãããã¯ããªãæ©æ¢°çãªå€æã§ãããã¢ããªã±ãŒã·ã§ã³ããã€ã¯ããµãŒãã¹æ§é ã«è»¢éããããšã§å®è¡ã§ããŸãã
ã€ãã³ããœãŒã·ã³ã°ã®ãã1ã€ã®æ¬ ç¹ã¯ãã¡ãã»ãŒãžãããŒã«ãŒãéåžžå°ãªããšã1ã€ã®é ä¿¡ãä¿èšŒããããšã§ãã ã¹ãçã§ãªãã€ãã³ããã³ãã©ã¯ãç¹°ãè¿ãã€ãã³ããåå¥ã«æ€åºããã³æåŠããå¿ èŠããããŸãã ãã®å Žåãã€ãã³ããœãŒã·ã³ã°ãã¬ãŒã ã¯ãŒã¯ã¯ãåã€ãã³ãã«èªåã€ã³ã¯ãªã¡ã³ãèå¥åãå²ãåœãŠãããšã§åœ¹ç«ã¡ãŸãã ãã®åŸãã€ãã³ããã³ãã©ãŒã¯ãæ¢ã«åŠçããã€ãã³ãã®æ倧èå¥åã远跡ããããšã«ãããéè€ãæ€åºã§ããŸãã
ã€ãã³ããœãŒã·ã³ã°ã®ãã1ã€ã®åé¡ã¯ãã€ãã³ãïŒããã³ã¹ãããã·ã§ããïŒïŒã®ãã¿ãŒã³ãæéãšãšãã«å€åããããšã§ãã ã€ãã³ãã¯æ°žä¹ ã«ä¿åãããããããŠãããã®ç¶æ ãåæ§ç¯ããããã«ããµãŒãã¹ã¯ããã€ãã®ç°ãªãããŒãžã§ã³ã®ã¹ããŒã ã«å¯Ÿå¿ããã€ãã³ããåŠçããå¿ èŠããããŸãã ãµãŒãã¹ãç°¡çŽ åãã1ã€ã®æ¹æ³ã¯ãã€ãã³ããªããžããªããã€ãã³ããããŒããããšãã«ãã€ãã³ããœãŒã¹ãã¬ãŒã ã¯ãŒã¯ã«ãã¹ãŠã®ã€ãã³ããææ°ããŒãžã§ã³ã®åè·¯ã«ãã£ã¹ããããããšã§ãã ãã®çµæããµãŒãã¹ã¯ã€ãã³ãã®ææ°ããŒãžã§ã³ã®ã¿ãåŠçããå¿ èŠããããŸãã
ã€ãã³ããœãŒã·ã³ã°ã®ãã1ã€ã®æ¬ ç¹ã¯ãã€ãã³ããªããžããªãžã®ã¯ãšãªèªäœãå°é£ãªã¿ã¹ã¯ã«ãªãå¯èœæ§ãããããšã§ãã äœãä¿¡çšé床ã§ããŒã³ãçºè¡ãã䟡å€ã®ãã顧客ãèŠã€ããå¿ èŠããããšæ³åããŠãã ããã SELECT * FROM CUSTOMER WHERE CREDIT_LIMIT <ïŒãšæžãããšã¯ã§ããŸããã AND CREATION_DATE>ïŒ..äžä¿¡é床ãå«ãåã¯ååšããŸããã 代ããã«ããã¹ããããSELECTãå«ãããè€éã§æœåšçã«éå¹ççãªã¯ãšãªã䜿çšããŠãåæã¯ã¬ãžããå¶éãèšå®ããŠããå€æŽããã€ãã³ããåŠçããããšã«ããã¯ã¬ãžããå¶éãèšç®ããå¿ èŠããããŸãã ããã«æªãããšã«ãNoSQLã€ãã³ãã¹ãã¢ã¯éåžžãäž»ããŒã®ã¿ã§æ€çŽ¢ããŸãã ãããã£ãŠãã³ãã³ãã¯ãšãªã®è²¬ä»»åé¢ïŒCQRSïŒã¢ãããŒãã䜿çšããŠã¯ãšãªãå®è£ ããå¿ èŠããããŸãã
CQRSã䜿çšããã¯ãšãªã®å®è£
ã€ãã³ããœãŒã¹ã¯ããã€ã¯ããµãŒãã¹ã¢ãŒããã¯ãã£ã§ã®å¹ççãªã¯ãšãªã®å®è£ ã«å¯Ÿããäž»èŠãªé害ã®1ã€ã§ãã ãã ãããããå¯äžã®åé¡ã§ã¯ãããŸããã ããšãã°ãé«äŸ¡ãªæ³šæãåºããæ°èŠé¡§å®¢ãèŠã€ããSQLã¯ãšãªãèããŠã¿ãŸãããã
SELECT * FROM CUSTOMER c, ORDER o WHERE c.id = o.ID AND o.ORDER_TOTAL > 100000 AND o.STATE = 'SHIPPED' AND c.CREATION_DATE > ?
ãã€ã¯ããµãŒãã¹ã¢ãŒããã¯ãã£ã§ã¯ãåäžã®ã¯ãšãªã§CUSTOMERããŒãã«ãšORDERããŒãã«ãçµåããããšã¯ã§ããŸããã åããŒãã«ã¯ç¬èªã®ãµãŒãã¹ã«å±ãããã®ãµãŒãã¹ã®APIãä»ããŠã®ã¿ã¢ã¯ã»ã¹ã§ããŸãã ç°ãªããµãŒãã¹ã«å±ããããŒãã«ãçµåããåŸæ¥ã®ã¯ãšãªãäœæããããšã¯ã§ããŸããã ã€ãã³ããœãŒã·ã³ã°ã¯ç¶æ³ãæªåãããåçŽãªçŽæ¥èŠæ±ãæžãããšãå°é£ã«ããŸãã ãã€ã¯ããµãŒãã¹ã¢ãŒããã¯ãã£ã§ã¯ãšãªãå®è£ ããæ¹æ³ãèŠãŠã¿ãŸãããã
CQRSã䜿çšãã
ã¯ãšãªãå®è£ ããè¯ãæ¹æ³ã¯ãCommand Query Responsibility SegregationïŒ CQRS ïŒãšåŒã°ããã¢ãŒããã¯ãã£ãã¿ãŒã³ã䜿çšããããšã§ãã ã¢ããªã±ãŒã·ã§ã³ã¯2ã€ã®éšåã«åãããŠããŸãã
- ã³ãã³ãéšåã¯ãã³ãã³ãïŒHTTP POSTãPUTãDELETEãªã©ïŒãåŠçããŠãéçŽãäœæãæŽæ°ãããã³åé€ããŸãã ãã¡ããããããã®éçŽã¯ãã€ãã³ããœãŒã¹ã䜿çšããŠå®è£ ãããŸãã
- ã¢ããªã±ãŒã·ã§ã³ã®ãªã¯ãšã¹ãéšåã¯ãªã¯ãšã¹ãïŒHTTP GETãªã©ïŒãåŠçããéèšã®1ã€ä»¥äžã®ãããªã¢ã©ã€ãºãã»ãã¥ãŒããªã¯ãšã¹ãããŸãã ã¯ãšãªããŒãã¯ãã³ãã³ãããŒãã«ãã£ãŠçºè¡ãããã€ãã³ãããµãã¹ã¯ã©ã€ãããããšã«ããããã¥ãŒãéèšãšåæãããŸãã
èŠä»¶ã«å¿ããŠãã¢ããªã±ãŒã·ã§ã³ã®ã¯ãšãªéšåã¯ã次ã®ããŒã¿ããŒã¹ã®1ã€ä»¥äžã䜿çšã§ããŸãã
å¿ èŠãªã | 次ã«äœ¿çšãã | äŸãã° |
---|---|---|
äž»ããŒã«ããJSONãªããžã§ã¯ãã®æ€çŽ¢ | MongoGBãªã©ã®ããã¥ã¡ã³ãæåããŒã¿ããŒã¹ããŸãã¯Redisãªã©ã®ããŒããªã¥ãŒããŒã¿ãŠã§ã¢ããŠã¹ã | MongoDBã䜿çšããŠããã¹ãŠã®æ³šæãå«ã顧客ããã¥ã¡ã³ãã䜿çšããŠæ³šæå±¥æŽãå®è£ ããŸãã |
åºæ¬çãªJSONæ€çŽ¢ | MongoGBãªã©ã®ããã¥ã¡ã³ãæåããŒã¿ããŒã¹ã | MongoDBã䜿çšããŠã¯ã©ã€ã¢ã³ãåãã«ãã¬ãŒã³ããŒã·ã§ã³ãå®è£ ããŸãã |
å šææ€çŽ¢ | å šææ€çŽ¢ãšã³ãžã³ïŒ Elasticsearchãªã©ïŒ ã | 泚æããšã«Elasticsearchããã¥ã¡ã³ãã䜿çšããŠã泚æã§å šææ€çŽ¢ãå®è£ ããŸãã |
ã°ã©ããªã¯ãšã¹ã | Neo4jãªã©ã®ã°ã©ãããŒã¿ããŒã¹ç®¡çã·ã¹ãã ã | 顧客ã泚æããã®ä»ã®ããŒã¿ã®ã°ã©ãã䜿çšããäžæ£æ€åºã·ã¹ãã ã®å®è£ ã |
åŸæ¥ã®SQLã¯ãšãª | RDBMS | æšæºçãªããžãã¹ã¬ããŒããšåæã |
å€ãã®ç¹ã§ãCQRSã¯ãRDBMSãããŒã¿ãŠã§ã¢ããŠã¹ããã³å šææ€çŽ¢ïŒElasticsearchãªã©ïŒã®æ€çŽ¢ãšã³ãžã³ãšããŠäœ¿çšãããåºã䜿çšãããŠããã¢ãããŒãã®ããäžè¬çãªã€ãã³ãæåããŒãžã§ã³ã§ãã CQRSã¯ãå šææ€çŽ¢ãšã³ãžã³ã§ã¯ãªããããåºç¯ãªããŒã¿ããŒã¹ã¿ã€ãã䜿çšããŸãã ããã«ãã€ãã³ãã«ãµãã¹ã¯ã©ã€ãããããšã«ãããã¢ããªã±ãŒã·ã§ã³ã®èŠæ±éšåã®ãã¬ãŒã³ããŒã·ã§ã³ãã»ãŒãªã¢ã«ã¿ã€ã ã§æŽæ°ããŸãã
次ã®å³ã¯ããªã³ã©ã€ã³ã¹ãã¢ã®CQRSå³ã瀺ããŠããŸãã
ã«ã¹ã¿ããŒãµãŒãã¹ãšæ³šæãµãŒãã¹ã¯ãã¢ããªã±ãŒã·ã§ã³ã®ã³ãã³ãéšåã«å«ãŸããŠããŸãã ãããã¯ã顧客ãšæ³šæãäœæããã³æŽæ°ããããã®APIãæäŸããŸãã Customer View Serviceã¯ã¯ãšãªããŒãã«å«ãŸããŠããŸãã 顧客ããŒã¿ãç §äŒããããã®APIãæäŸããŸãã
Customer View Serviceã¯ãã¢ããªã±ãŒã·ã§ã³ã®ã³ãã³ãéšåã«ãã£ãŠçºè¡ãããã€ãã³ãããµãã¹ã¯ã©ã€ãããMongoDBã«å®è£ ãããŠãããã¥ãŒãªããžããªãæŽæ°ããŸãã MongoDBã³ã¬ã¯ã·ã§ã³ã«ã¯ãã¯ã©ã€ã¢ã³ãããšã«1ã€ã®ããã¥ã¡ã³ããå«ãŸããŠããŸãã åããã¥ã¡ã³ãã«ã¯ãç¹å®ã®é¡§å®¢ã説æããå±æ§ãšãææ°ã®é¡§å®¢æ³šæã®å±æ§ããããŸãã ãã®ã³ã¬ã¯ã·ã§ã³ã¯ãäžèšã®ã¯ãšãªãå«ãããŸããŸãªã¯ãšãªããµããŒãããŠããŸãã
CQRSã®é·æãšçæ
CQRSã®äž»ãªå©ç¹ã¯ãCQRSã®ãããã§ãç¹ã«ã€ãã³ããœãŒã¹ã䜿çšãããã€ã¯ããµãŒãã¹ã¢ãŒããã¯ãã£ã§ã¯ãšãªãå®è£ ã§ããããã«ãªãããšã§ãã ããã«ãããã¢ããªã±ãŒã·ã§ã³ã¯ããŸããŸãªã¯ãšãªã»ãããå¹æçã«ãµããŒãã§ããŸãã å¥ã®å©ç¹ã¯ã責任ã®åé¢ã«ãããã¢ããªã±ãŒã·ã§ã³ã®ã³ãã³ããšã¯ãšãªã®éšåãåçŽåãããããšãå€ãããšã§ãã
æ¬ ç¹ã®1ã€ã¯ãCQRSãã·ã¹ãã ã®éçºãšéçšã«è¿œå ã®åŽåãå¿ èŠãšããããšã§ãã ãã¥ãŒãæŽæ°ãããã¥ãŒããªã¯ãšã¹ãã§ãããªã¯ãšã¹ãåŽã®ãµãŒãã¹ãäœæããŠãããã€ããå¿ èŠããããŸãã ããã«ããã¥ãŒã®ãªããžããªãå±éããå¿ èŠããããŸãã
CQRSã®ãã1ã€ã®æ¬ ç¹ã¯ãã³ãã³ããšã¯ãšãªéã®ã¿ã€ã ã©ã°ã«é¢é£ããŠããŸãã äºæ³ãããããã«ãã³ãã³ãããŒããéçŽãæŽæ°ããç¬éãšããªã¯ãšã¹ãããŒãã®è¡šçŸããããã®å€æŽãåæ ããæºåãã§ããæã®éã«é 延ããããŸãã éçŽãæŽæ°ãããã¥ãŒã䜿çšããŠããã«èŠæ±ãè¡ãã¯ã©ã€ã¢ã³ãã¢ããªã±ãŒã·ã§ã³ã¯ãéçŽã®ä»¥åã®ããŒãžã§ã³ãèŠãããšãã§ããŸãã ãããã£ãŠãã¢ããªã±ãŒã·ã§ã³ã¯ããŠãŒã¶ãŒããã®æœåšçãªäžäžèŽãåãåããªãããã«äœæããå¿ èŠããããŸãã
ãŸãšã
ã€ãã³ãã䜿çšããŠãµãŒãã¹éã®ããŒã¿ã®æŽåæ§ãç¶æããéã®äž»ãªåé¡ã®1ã€ã¯ãã¢ãããã¯ããŒã¿ããŒã¹ã®æŽæ°ãšã€ãã³ãã®åæçºè¡ã§ãã åŸæ¥ã®ãœãªã¥ãŒã·ã§ã³ã¯ãããŒã¿ããŒã¹ãšã¡ãã»ãŒãžãããŒã«ãŒã䜿çšããŠãåæ£ãã©ã³ã¶ã¯ã·ã§ã³ã䜿çšããããšã§ãã ãã ãããã®ã¢ãããŒãã¯ææ°ã®ã¢ããªã±ãŒã·ã§ã³ã§ã¯å®è¡ã§ããŸããã æé©ãªãœãªã¥ãŒã·ã§ã³ã¯ãã€ãã³ããœãŒã·ã³ã°ã䜿çšããããšã§ããã€ãã³ããœãŒã·ã³ã°ã¯ãããžãã¹ããžãã¯ããã³ç¶æ ä¿åã·ã¹ãã ã®èšèšã«å¯Ÿããã€ãã³ãé§ååã®ã¢ãããŒãã§ãã
ãã€ã¯ããµãŒãã¹ã¢ãŒããã¯ãã£ã®å¥ã®åé¡ã¯ã¯ãšãªã§ãã å€ãã®å Žåãè€æ°ã®ãµãŒãã¹ã«å±ããããŒã¿ãçµåããå¿ èŠããããŸãã ãã ããããŒã¿ã¯ãµãŒãã¹ããšã«ãã©ã€ããŒãã§ãããããçµåã ãã䜿çšããããšã¯ã§ããªããªããŸããã çŸåšã®ç¶æ ã¯ããèªäœã§ã¯ä¿åãããªããããã€ãã³ããœãŒã¹ã䜿çšãããšãã¯ãšãªãå¹ççã«å®è£ ããããšãããã«é£ãããªããŸãã 解決çã¯ãCQRSã䜿çšããŠãç°¡åã«ã¯ãšãªã§ãã1ã€ä»¥äžã®ãããªã¢ã©ã€ãºããã¥ãŒãææ°ã®ç¶æ ã«ä¿ã€ããšã§ãã