A pattern language for microservices (#gluecon #gluecon2016)

6,722 views

Published on

This is the version of the microservices pattern languages talk that I gave at Gluecon 2016

Published in: Software

A pattern language for microservices (#gluecon #gluecon2016)

  1. 1. @crichardson A pattern language for microservices Chris Richardson Founder of Eventuate.io Founder of the original CloudFoundry.com Author of POJOs in Action @crichardson chris@chrisrichardson.net http://eventuate.io
  2. 2. @crichardson Presentation goal Why patterns and pattern languages? A pattern language for microservices
  3. 3. @crichardson About Chris
  4. 4. @crichardson Agenda Why a pattern language for microservices? Core patterns: monolith vs. microservices Distributed data management patterns
  5. 5. @crichardson Let’s imagine you are building a large, complex application, e.g. an online store
  6. 6. @crichardson Successful software development Architecture Process Organization Agile Continuous delivery … Small, autonomous, teams
  7. 7. @crichardson ?Architecture
  8. 8. @crichardson No silver bullets http://en.wikipedia.org/wiki/Fred_Brooks
  9. 9. @crichardson We need architecture and design patterns Reusable solution to a problem occurring in a particular context
  10. 10. @crichardson Patterns force you to consider tradeoffs Resulting context Benefits Drawbacks Issues to resolve
  11. 11. @crichardson Patterns force you to consider other patterns Related patterns Alternative solutions Solutions to problems introduced by this pattern
  12. 12. @crichardson Microservices pattern language http://microservices.io/
  13. 13. @crichardson Agenda Why a pattern language for microservices? Core patterns: monolith vs. microservices Distributed data management patterns
  14. 14. @crichardson
  15. 15. @crichardson The monolithic architecture Tomcat Browser WAR SQL database HTML REST/JSON Client App Simple to …. Develop Test Deploy Scale Catalog Module Reviews Module Orders Module StoreFront UI Module
  16. 16. @crichardson But successful applications keep growing ….
  17. 17. @crichardson Monolithic architecture Process Organization Agile Continuous delivery … Small, autonomous, teams
  18. 18. @crichardson Microservice architecture = functional decomposition Brows er Mobile Device Store Front UI API Gateway Catalog Service Review Service Order Service … Service Catalog Database Review Database Order Database … Database HTML REST REST
  19. 19. @crichardson Microservice architecture Process Organization Agile Continuous delivery … Small, autonomous, teams✔ ✔
  20. 20. @crichardson Drawbacks Complexity
  21. 21. @crichardson Drawbacks Complexity of developing a distributed system Implementing inter-process communication Handling partial failures Complexity of implementing business transactions that span multiple databases (without 2PC) Complexity of testing a distributed system Complexity of deploying and operating a distributed system Managing the development and deployment of features that span multiple services Fortunately solutions exists
  22. 22. @crichardson The benefits typically outweigh the drawbacks for large, complex applications
  23. 23. @crichardson Issues to address How to deploy the services? How do the services communicate? How do clients of the application communicate with the services? How to partition the system into services? How to deal with distributed data management problems? ….
  24. 24. @crichardson Agenda Why a pattern language for microservices? Core patterns: monolith vs. microservices Distributed data management patterns
  25. 25. Data management patterns
  26. 26. @crichardson The Database Shared database Order Service Customer Service … Service Order table Customer table … orderTotal creditLimit Tight coupling Simple and ACID
  27. 27. @crichardson Database per service Order Service Customer Service Order Database Customer Database Order table Customer table orderTotal creditLimit Loose coupling 😀 but more complex 😓 and….
  28. 28. @crichardson 2PC (aka. distributed transactions) are not viable choice for most modern applications
  29. 29. @crichardson Customer management How to maintain data consistency without 2PC? Order management Order Service placeOrder() Customer Service updateCreditLimit() Customer creditLimit ... has ordersbelongs toOrder total Invariant: sum(open order.total) <= customer.creditLimit ?
  30. 30. @crichardson Event-driven architecture
  31. 31. @crichardson Use event-driven, eventually consistent order processing Order Service Customer Service Order created Credit Reserved Credit Check Failed Place Order OR
  32. 32. @crichardson How atomically update database and publish an event Order Service Order Database Message Broker insert Order publish OrderCreatedEvent dual write problem ?
  33. 33. @crichardson Reliably publish events when state changes
  34. 34. @crichardson Use event-sourcing Event table Aggregate type Event id Aggregate id Event data Order 902101 …OrderApproved Order 903101 …OrderShipped Event type Order 901101 …OrderCreated
  35. 35. @crichardson Replay events to recreate state Order state OrderCreated(…) OrderAccepted(…) OrderShipped(…) Events Periodically snapshot to avoid loading all events
  36. 36. But what about queries?
  37. 37. @crichardson Find recent, valuable customers 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 Service Order Service What if event sourcing is used?…. is no longer easy
  38. 38. @crichardson Command Query Responsibility Segregation (CQRS) Command side Commands Aggregate Event Store Events Query side Queries Materialized View Events POST PUT DELETE GET MongoDB Redis Neo4j SQL ElasticSearch More complex 😓 but high performance, scalable views 😀
  39. 39. @crichardson Many more patterns…
  40. 40. @crichardson Summary: Patterns and pattern languages are a great way to … Think about technology Discuss technology Apply technology
  41. 41. @crichardson Summary: The Microservices pattern language is a great way to … Think about microservices Discuss microservices Apply microservices (or not)
  42. 42. @crichardson @crichardson chris@chrisrichardson.net http://eventuate.io http://plainoldobjects.com http://microservices.io Questions?

×