Event Sourcing,CQRS For PHP Application

Event Sourcing,CQRS For PHP Application

PHPアプリケーションでEvent Sourcing、CQRSを実践する場合の
ミドルウェアとの組み合わせ、基本的な考え方
実例を交えた資料です

17d4ef53b432ebf7c566fd6a11345570?s=128

yuuki takezawa

June 12, 2018
Tweet

Transcript

  1. 2.

    ͸ͳ͢͜ͱ • Event • CQRS • Event Sourcing + CQRS

    • Event Sourcing / Message Broker • ࣮ྫ
  2. 6.
  3. 9.

    Event / PHP • Symfony Event Dispatcher • Illuminate Events

    (Laravel) • Zend Event Manager • RxPHP • ReactPHP
  4. 11.

    public function __invoke( FulltextRequest $request ): RedirectResponse { // ొ࿥ॲཧޙʹevent

    try { $entity = new Entity(1, ‘testing’); $this->dispatcher->dispatch( new SinkConnect($this->usecase->run($entity)) ); } catch(\RuntimeException $e) { // Կ͔errorॲཧ } return $this->redirector->route('fulltext.index'); }
  5. 26.
  6. 38.
  7. 39.
  8. 40.
  9. 41.
  10. 42.
  11. 43.
  12. 44.
  13. 52.

    Event Sourcing / Command Event Application Command RDBMS ΞϓϦέʔγϣϯ͔Β ॻ͖ࠐΈࢦࣔ(class)

    σʔλϕʔεʹinsert ਖ਼ৗʹॻ͖ࠐ·Εͨ ࣄ࣮Λฦ٫ ࣄ࣮ʹج͍ͮͨ
 ΠϕϯτΛൃߦ
  14. 55.

    Event Sourcing / Query Event / Listener Application NoSQL Query

    EventͷൃߦΛݕ஌͠ NoSQLʹσʔλΛૠೖ
  15. 56.

    Event Sourcing / Queryྫ Event / Listener Application NoSQL Query

    EventͷൃߦΛݕ஌͠ NoSQLʹσʔλΛૠೖ Application͔ΒQuery
 நग़໰͍߹Θͤ
  16. 57.

    Event Sourcing / Query Event / Listener Application NoSQL Query

    EventͷൃߦΛݕ஌͠ NoSQLʹσʔλΛૠೖ Application͔ΒQuery
 நग़໰͍߹Θͤ NoSQLʹ໰͍߹Θͤ υϝΠϯʹ࠷దԽ͞Εͨ΋ͷΛظ଴
  17. 58.

    Event Sourcing / Query Event / Listener Application NoSQL Query

    EventͷൃߦΛݕ஌͠ NoSQLʹσʔλΛૠೖ Application͔ΒQuery
 நग़໰͍߹Θͤ NoSQLʹ໰͍߹Θͤ υϝΠϯʹ࠷దԽ͞Εͨ΋ͷΛظ଴ ݁ՌΛฦ͢ͷΈ
  18. 70.

    ΞϓϦέʔγϣϯ෼ׂྫ PHP WebΞϓϦέʔγϣϯ webΞϓϦ։ൃνʔϜ ूܭॲཧ push etc CLI։ൃνʔϜ / DWH

    etc ޿ࠂ഑৴ܥAPI ޿ࠂ഑৴։ൃνʔϜ ϢʔβʔͷӾཡ৘ใʹج͍ͮͨ ৘ใ͕ཉ͍͠Ͱ͢ ϦΞϧλΠϜͰ
  19. 74.

    Event Sourcing / Redis • PubSubͰشൃੑͷͳ͍΋ͷΛબ୒͢Δ
 PUBLISH SUBSCRIBEͳͲ͕͋ΔͷͰ࣮ݱ͸Ͱ͖Δ͕ɺ
 شൃੑʹ͍ͭͯ͸ઃఆͰؤுΔ
 •

    ϝοηʔδอ؅͕ՄೳͰɺ
 ࠶ݱ͢Δ͜ͱ͕Մೳͳ΋ͷΛ࠾༻͢Δͷ͕๬·͍͠
 ͋·ΓಘҙͰ͸ͳ͍(τϥϯβΫγϣϯͳ͠) • εέʔϥϒϧͰ͋Δ͜ͱ
 ༰қʹͰ͖Δ
  20. 75.
  21. 77.
  22. 79.

    Event Sourcing • ΠϕϯτΛΠϕϯτετΞʹอଘ
 Kafkaࣗମʹอଘ͢Δ͜ͱ͕Ͱ͖ɺ
 σʔλϕʔεʹసૹՄೳ • ه࿥͞ΕͨΠϕϯτ͔ΒΦϒδΣΫτΛ෮ݩͰ͖Δ
 ॲཧ͞ΕͨΠϕϯτ͸࠶ൃߦՄೳ •

    ΠϕϯτʴϩάʴΦϒδΣΫτɺΠϕϯτ͸ෆม
 ൃߦޙͷΠϕϯτΛॻ͖׵͑Δ͜ͱ͸Ͱ͖ͳ͍ • ൃੜͨ͠ࣄ࣮Λ఻͑Δ
 τϥϯβΫγϣϯ͋Γ
  23. 81.

    ϝοηʔδ఻ୡͷอূ • At least once semantics
 ॏෳΛڐՄ • At most

    once semantics
 ܽଛΛڐՄ • Exactly once semantics
 ॏෳɺ͓ΑͼܽଛΛڐՄ͠ͳ͍
 
 "Exactly-once Semantics are Possible: Here’s How Kafka Does it". Confluent APACHE KAFKA. 
 https://www.confluent.io/blog/exactly-once-semantics-are-possible-heres-how-apache-kafka-does-it/ ࢀর
  24. 85.

    Performance • confluent: 250000 records, 34.84177593s • sarama-cluster: 250000 records,

    2m20.614963038s
 • confluent was almost 5x faster on the read.
  25. 86.
  26. 90.

    Event Sourcing+ CQRS / Kafka • Command QueryΛ୲౰͢ΔWebΞϓϦέʔγϣϯΛ
 γϯϓϧʹ •

    Event SourcingʹΑͬͯɺΞϓϦέʔγϣϯʹՁ஋Λ௥ Ճ͢Δ͜ͱ͕Ͱ͖Δ(෼ੳͳͲ) • Command QueryͷಉظΛαϙʔτ͠ɺ
 ΞϓϦέʔγϣϯʹ߹Θͤͯෛՙ෼ࢄ͕༰қ
  27. 92.

  28. 93.
  29. 100.

    Now