Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Event Sourcing,CQRS For PHP Application

Event Sourcing,CQRS For PHP Application

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

yuuki takezawa

June 12, 2018
Tweet

More Decks by yuuki takezawa

Other Decks in Technology

Transcript

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

    • Event Sourcing / Message Broker • ࣮ྫ
  2. Event / PHP • Symfony Event Dispatcher • Illuminate Events

    (Laravel) • Zend Event Manager • RxPHP • ReactPHP
  3. 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'); }
  4. Event Sourcing / Command Event Application Command RDBMS ΞϓϦέʔγϣϯ͔Β ॻ͖ࠐΈࢦࣔ(class)

    σʔλϕʔεʹinsert ਖ਼ৗʹॻ͖ࠐ·Εͨ ࣄ࣮Λฦ٫ ࣄ࣮ʹج͍ͮͨ
 ΠϕϯτΛൃߦ
  5. Event Sourcing / Query Event / Listener Application NoSQL Query

    EventͷൃߦΛݕ஌͠ NoSQLʹσʔλΛૠೖ
  6. Event Sourcing / Queryྫ Event / Listener Application NoSQL Query

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

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

    EventͷൃߦΛݕ஌͠ NoSQLʹσʔλΛૠೖ Application͔ΒQuery
 நग़໰͍߹Θͤ NoSQLʹ໰͍߹Θͤ υϝΠϯʹ࠷దԽ͞Εͨ΋ͷΛظ଴ ݁ՌΛฦ͢ͷΈ
  9. ΞϓϦέʔγϣϯ෼ׂྫ PHP WebΞϓϦέʔγϣϯ webΞϓϦ։ൃνʔϜ ूܭॲཧ push etc CLI։ൃνʔϜ / DWH

    etc ޿ࠂ഑৴ܥAPI ޿ࠂ഑৴։ൃνʔϜ ϢʔβʔͷӾཡ৘ใʹج͍ͮͨ ৘ใ͕ཉ͍͠Ͱ͢ ϦΞϧλΠϜͰ
  10. Event Sourcing / Redis • PubSubͰشൃੑͷͳ͍΋ͷΛબ୒͢Δ
 PUBLISH SUBSCRIBEͳͲ͕͋ΔͷͰ࣮ݱ͸Ͱ͖Δ͕ɺ
 شൃੑʹ͍ͭͯ͸ઃఆͰؤுΔ
 •

    ϝοηʔδอ؅͕ՄೳͰɺ
 ࠶ݱ͢Δ͜ͱ͕Մೳͳ΋ͷΛ࠾༻͢Δͷ͕๬·͍͠
 ͋·ΓಘҙͰ͸ͳ͍(τϥϯβΫγϣϯͳ͠) • εέʔϥϒϧͰ͋Δ͜ͱ
 ༰қʹͰ͖Δ
  11. Event Sourcing • ΠϕϯτΛΠϕϯτετΞʹอଘ
 Kafkaࣗମʹอଘ͢Δ͜ͱ͕Ͱ͖ɺ
 σʔλϕʔεʹసૹՄೳ • ه࿥͞ΕͨΠϕϯτ͔ΒΦϒδΣΫτΛ෮ݩͰ͖Δ
 ॲཧ͞ΕͨΠϕϯτ͸࠶ൃߦՄೳ •

    ΠϕϯτʴϩάʴΦϒδΣΫτɺΠϕϯτ͸ෆม
 ൃߦޙͷΠϕϯτΛॻ͖׵͑Δ͜ͱ͸Ͱ͖ͳ͍ • ൃੜͨ͠ࣄ࣮Λ఻͑Δ
 τϥϯβΫγϣϯ͋Γ
  12. ϝοηʔδ఻ୡͷอূ • 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/ ࢀর
  13. Performance • confluent: 250000 records, 34.84177593s • sarama-cluster: 250000 records,

    2m20.614963038s
 • confluent was almost 5x faster on the read.
  14. Event Sourcing+ CQRS / Kafka • Command QueryΛ୲౰͢ΔWebΞϓϦέʔγϣϯΛ
 γϯϓϧʹ •

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