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

Event-based Architecture and Implementation wit...

Eberhard Wolff
November 07, 2017

Event-based Architecture and Implementation with Atom and Kafka

Event-based Architecture is a great answer to typical challenge of microservices architectures like resilience. This presentation shows how asynchronous microservices can be implemented with Atom / REST and Kafka.

Eberhard Wolff

November 07, 2017
Tweet

More Decks by Eberhard Wolff

Other Decks in Technology

Transcript

  1. Modules Macro / Micro Architecture Independent Continuous Delivery Pipeline Resilience

    Integration & Communication Authentication & Metadata Standardized Operations Standards: Interface only Container Independent Systems Architecture
  2. Order Accepted Item prices Billing address Item sizes Item weights

    Delivery address ??? Invoice Delivery ??? • Lots of data • Which Bounded Context uses what? • Changes can impact all modules • DDD: Published Language • THE order model?
  3. Delivery Order Accepted Invoice Order Accepted Item prices Billing address

    Item sizes Item weights Delivery address • Specialized “events” • Independent evolution • Easy to figure out which Bounded Context uses what • Extensibility = new event • DDD: Customer / Supplier ??? Order Accepted ???
  4. Order Accepted Order ID • Just an ID • Provide

    additional specialized API for data for each consumer • Compromise between extensibility and specialized events
  5. Events: Conclusion • Events per se provide limited decoupling. •

    What data should be included? • Prefer specialized events and just IDs
  6. Events: Conclusion • Best option for communication between Bounded Contexts.

    • Great fit for a Ubiquitous Language. • But: Implicit replication
  7. What Kind of Inconsistencies? • One system for invoices •

    One for deliveries • One for orders • One source of truth for invoices/deliveries/orders. • No “invoice might be there or not”
  8. Inconsistencies • Either delivery without invoice • …or invoice without

    delivery. • Will eventually be solved. • Delays are not unheard of. • What is the alternative? • Centralized complex database?
  9. Inconsistencies • Consequences if data appears after 0.1s / 1s

    / 1min / 1h / 1d ? • “Invoice has to be there in <1min!!!!!”
  10. Event Sourcing • Store the events that lead to a

    specific state + state (optional) • System of record: Event or state • No clear definition https://martinfowler.com/ eaaDev/EventSourcing.html
  11. Microservice Interface Calls, messages, … Event Store Order Cancelled 42

    Order Accepted 23 Order Accepted 42 Order 42 Order 23 Order Delivered 23 Order 23 ✔ State
  12. Event Sourcing: Advantages • Might be a better model •

    History log • Audit log • Can rebuild state • Even after the domain model changed
  13. Event Sourcing: Advantages • Temporal queries easy (yesterday’s state) •

    Replay • Reversal • Scalability – might replicate state using events
  14. Event Sourcing: Challenges • External systems might have different data

    • …and behave differently during replay. • Code changes might change response to events • …even if it is just a bug fix.
  15. Persistence • Responsibility of the microservices • Might be changed

    • Might be different for each microservice • Event Sourcing should be a local decision • Event sourcing makes it easier to fix inconsistencies.
  16. Kafka Records • Key • Value • Timestamp • Headers

    • Stored forever (!) Record Key Value Timestamp
  17. Kafka Topics • Topics have a name • Topics have

    partitions • Order guaranteed per partition • Consumer commits offset per partition • Consumer group: One consumer per partition Topic Partition Record Record Record
  18. Topic Partition Record Record Record Consumer Offset Offset committed per

    consumer Offset Partition Record Record Record Partition Record Record Record Producer Consumer Consumer group
  19. Docker-Host Apache httpd Load Balancer Postgres Order Shipping Invoicing Kafka

    Zookeeper HTTP-Routing 8080 8080 80 8080 8080 9092
  20. Events • Events contain all information for invoice and delivery

    • Alternative: specific events • …more independence • Alternative: just ID in the event • …plus service to get detailed information
  21. Data • Data is copied into invoice / delivery database

    • …and denormalized • Each invoice and delivery contains customer and item data from when the order was created. • Bounded Context • No complex historization
  22. Kafka Conclusion • Provides access to old events • Guaranteed

    order per partition • Consumer groups send records to one consumer • Additional (complex) infrastructure
  23. Atom • Data format • …for feeds, blogs, podcasts etc

    • Access through HTTP • Idea: Provide a feeds of events / orders
  24. Atom Feed • Some unneeded information author, summary, title… •

    Mostly links to details • ...and timestamps • Content negotiation can provide different data formats or details
  25. Subscribing to Feed • Poll the feed (HTTP GET) •

    Client decides when to process new events • …but very inefficient • Lots of unchanged data
  26. HTTP Caching • Client GETs feed • Server send data

    + Last-Modified header • Client sends GET + If-Modified-Since header • Server: 304 (Not modified) …or new data
  27. Alternative • Roll your own format …that has no information

    only relevant for blogs …with links to details ...use content negotiation for details • Only issue: Atom reader cannot handle format
  28. Atom Conclusion • Can provides access to old events if

    they are stored anyway. • One consumer: idempotency • No additional infrastructure • But: Can’t send event to just one receiver
  29. Conclusion • Most important: Getting the split right! • Bounded

    Context • …communicate via business events • Inconsistencies can be dealt with • What is in the event? • Don’t share events for event sourcing! • Do not overuse event sourcing. • Kafka: Great solution for messaging • …but REST might be enough.
  30. Send email to [email protected] Slides + Microservices Primer DE /

    EN + Microservices Recipes DE / EN + Sample Microservices Book DE / EN + Sample Practical Microservices DE/EN + Sample of Continuous Delivery Book DE Powered by Amazon Lambda & Microservices