Successfully reported this slideshow.
Your SlideShare is downloading. ×

Hiding The Lead: Coupling, cohesion and microservices

Ad

HIDING THE LEAD
COUPLING, COHESION AND MICROSERVICES
Sam Newman
https://www.flickr.com/photos/spongebabyalwaysfull/87930589...

Ad

@samnewman
Sam Newman
Building
Microservices
DESIGNING FINE-GRAINED SYSTEMS

Ad

@samnewman
NEW BOOK!
https://samnewman.io/books/monolith-to-microservices/

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Loading in …3
×

Check these out next

1 of 114 Ad
1 of 114 Ad

Hiding The Lead: Coupling, cohesion and microservices

Download to read offline

The terms coupling and cohesion come from the world of structured programming, but they are also thrown about in the context of microservices. In this session, I look at the applicability of these terms to microservice architecture, and also do a deep dive into the different types of coupling to explore how ideas from the 1970s still have a lot of relevance to the types of systems we build today.

The terms coupling and cohesion come from the world of structured programming, but they are also thrown about in the context of microservices. In this session, I look at the applicability of these terms to microservice architecture, and also do a deep dive into the different types of coupling to explore how ideas from the 1970s still have a lot of relevance to the types of systems we build today.

More Related Content

Similar to Hiding The Lead: Coupling, cohesion and microservices (20)

Hiding The Lead: Coupling, cohesion and microservices

  1. 1. HIDING THE LEAD COUPLING, COHESION AND MICROSERVICES Sam Newman https://www.flickr.com/photos/spongebabyalwaysfull/8793058944/
  2. 2. @samnewman Sam Newman Building Microservices DESIGNING FINE-GRAINED SYSTEMS
  3. 3. @samnewman NEW BOOK! https://samnewman.io/books/monolith-to-microservices/
  4. 4. @samnewman INDEPENDENT DEPLOYABILITY Inventory Returns Invoicing Accounts v1 Customer Service Shipping
  5. 5. @samnewman INDEPENDENT DEPLOYABILITY Inventory Returns Invoicing Customer Service Shipping Accounts v2
  6. 6. @samnewman INDEPENDENT DEPLOYABILITY Inventory Returns Invoicing Customer Service Shipping Accounts v2
  7. 7. @samnewman Customer Service Accounts v1 Independent deployability requires interface stability Maintaining backwards compatibility is key
  8. 8. @samnewman Customer Service Accounts v2 Independent deployability requires interface stability Maintaining backwards compatibility is key
  9. 9. @samnewman Customer Service Accounts v2 Independent deployability requires interface stability Maintaining backwards compatibility is key
  10. 10. @samnewman https://www.flickr.com/photos/photographingtravis/16939917735/
  11. 11. @samnewman MODULES
  12. 12. @samnewman MODULES Module A Module D Module B Module C Module E
  13. 13. @samnewman MODULES Module A Module D Module B Module C Module E Allow for independent working
  14. 14. @samnewman MODULES Module A Module D Module B Module C Module E Allow for independent working And reuse
  15. 15. @samnewman MODULES Module A Module D Module B Module C Module E Allow for independent working And reuse Namespaces, packages
  16. 16. @samnewman MODULES Module A Module D Module B Module C Module E Allow for independent working And reuse Namespaces, packages NPMs, Gems, JARs etc
  17. 17. @samnewman AND MICROSERVICES? Module A Module DModule B Module C Module E
  18. 18. @samnewman AND MICROSERVICES? Module A Module DModule B Module C Module E “Modules” run on different computers
  19. 19. @samnewman AND MICROSERVICES? Module A Module DModule B Module C Module E Communication between modules is done via network calls “Modules” run on different computers
  20. 20. @samnewman AND MICROSERVICES? Module A Module DModule B Module C Module E Communication between modules is done via network calls “Modules” run on different computers Allows for easier independent deployability
  21. 21. @samnewman Microservices, when done right, are a form of modular architecture
  22. 22. @samnewmanhttps://www.flickr.com/photos/ibm_media/33838065805/
  23. 23. @samnewman INFORMATION HIDING http://repository.cmu.edu/cgi/viewcontent.cgi?article=2979&context=compsci
  24. 24. @samnewman INFORMATION HIDING http://repository.cmu.edu/cgi/viewcontent.cgi?article=2979&context=compsci Published in 1971
  25. 25. @samnewman INFORMATION HIDING http://repository.cmu.edu/cgi/viewcontent.cgi?article=2979&context=compsci Published in 1971 Looked at how best to define module boundaries
  26. 26. @samnewman INFORMATION HIDING http://repository.cmu.edu/cgi/viewcontent.cgi?article=2979&context=compsci Published in 1971 Looked at how best to define module boundaries Found that “information hiding” worked best
  27. 27. @samnewman INFORMATION HIDING AND MICROSERVICES https://blog.acolyer.org/2016/09/05/on-the-criteria-to-be-used-in-decomposing-systems-into-modules/
  28. 28. @samnewman Accounts NOT HIDING?
  29. 29. @samnewman Accounts NOT HIDING? Shipping
  30. 30. @samnewman Accounts NOT HIDING? Shipping If an upstream consumer can reach into your internal implementation..
  31. 31. @samnewman Accounts NOT HIDING? Shipping If an upstream consumer can reach into your internal implementation.. …then you can’t change this implementation without breaking the consumer
  32. 32. @samnewman Accounts NOT HIDING? Shipping If an upstream consumer can reach into your internal implementation.. …then you can’t change this implementation without breaking the consumer
  33. 33. @samnewman Accounts NOT HIDING? Shipping If an upstream consumer can reach into your internal implementation.. …then you can’t change this implementation without breaking the consumer
  34. 34. @samnewman HIDING! Accounts Shipping
  35. 35. @samnewman HIDING! Accounts Shipping Hide your secrets! Hidden
  36. 36. @samnewman HIDING! Accounts Shipping Hide your secrets! Be explicit about what is shared, and what is hidden Hidden
  37. 37. @samnewman HIDING! Accounts Shipping Hide your secrets! Be explicit about what is shared, and what is hidden Shared Hidden
  38. 38. @samnewman HIDING! Accounts Shipping Hide your secrets! Be explicit about what is shared, and what is hidden Shared Hidden
  39. 39. @samnewman HIDING! Accounts Shipping Hide your secrets! Be explicit about what is shared, and what is hidden Shared Hidden Hidden things can change, shared things can’t
  40. 40. @samnewman ACCIDENTAL BREAKAGE public class Customer { private int id; private String name; private int age; … } Code
  41. 41. @samnewman ACCIDENTAL BREAKAGE public class Customer { private int id; private String name; private int age; … } Code
  42. 42. @samnewman ACCIDENTAL BREAKAGE public class Customer { private int id; private String name; private int age; … } Code { “id” : 123, “name” : “sam”, “age” : 15 } JSON
  43. 43. @samnewman ACCIDENTAL BREAKAGE public class Customer { private int id; private String name; private int age; … } Code { “id” : 123, “name” : “sam”, “age” : 15 } JSON
  44. 44. @samnewman ACCIDENTAL BREAKAGE public class Customer { private int id; private String name; private int age; … } Code { “id” : 123, “name” : “sam”, “age” : 15 } JSON public class Customer { private int id; private String name; private LocalDate dob; … } Code
  45. 45. @samnewman ACCIDENTAL BREAKAGE public class Customer { private int id; private String name; private int age; … } Code { “id” : 123, “name” : “sam”, “age” : 15 } JSON public class Customer { private int id; private String name; private LocalDate dob; … } Code
  46. 46. @samnewman ACCIDENTAL BREAKAGE public class Customer { private int id; private String name; private int age; … } Code { “id” : 123, “name” : “sam”, “age” : 15 } JSON public class Customer { private int id; private String name; private LocalDate dob; … } Code { “id” : 123, “name” : “sam”, “dob” : 15/02/1974 } JSON
  47. 47. @samnewman DATA TRANSFER OBJECT https://martinfowler.com/eaaCatalog/dataTransferObject.html Customer
  48. 48. @samnewman DATA TRANSFER OBJECT https://martinfowler.com/eaaCatalog/dataTransferObject.html Customer public class Customer { private int id; private String name; private LocalDate dob; … }
  49. 49. @samnewman DATA TRANSFER OBJECT https://martinfowler.com/eaaCatalog/dataTransferObject.html Customer public class Customer { private int id; private String name; private LocalDate dob; … } public class CustomerDTO { private int id; private String name; private int age; … }
  50. 50. @samnewman DATA TRANSFER OBJECT https://martinfowler.com/eaaCatalog/dataTransferObject.html Customer public class Customer { private int id; private String name; private LocalDate dob; … } public class CustomerDTO { private int id; private String name; private int age; … } { “id” : 123, “name” : “sam”, “age” : 15 } JSON
  51. 51. @samnewman AND CONNECTIONS BETWEEN THEM?
  52. 52. @samnewman AND CONNECTIONS BETWEEN THEM? “The connections between modules are the assumptions which the modules make about each other.” - David Parnas
  53. 53. @samnewman AND CONNECTIONS BETWEEN THEM? “The connections between modules are the assumptions which the modules make about each other.” - David Parnas Information hiding is about reducing the assumptions one module has for another
  54. 54. @samnewman EXPLICIT CONTRACTS? Module DModule B
  55. 55. @samnewman EXPLICIT CONTRACTS? Module DModule B
  56. 56. @samnewman EXPLICIT CONTRACTS? Module DModule B Need an explicit understanding of the assumptions of consumers
  57. 57. @samnewman EXPLICIT CONTRACTS? Module DModule B Need an explicit understanding of the assumptions of consumers To help us understand what can change easily, and what cannot
  58. 58. @samnewman EXPLICIT CONTRACTS? Module DModule B Need an explicit understanding of the assumptions of consumers To help us understand what can change easily, and what cannot Schemas can help!
  59. 59. @samnewman Schemas FTW!
  60. 60. @samnewman SCHEMAS https://json-schema.org/ https://developers.google.com/protocol-buffers/
  61. 61. @samnewman CHECKING SCHEMA COMPATIBILITY https://protolock.dev/
  62. 62. @samnewman And of course testing…
  63. 63. @samnewman STRUCTURED PROGRAMMING
  64. 64. @samnewman COUPLING AND COHESION
  65. 65. @samnewman COUPLING AND COHESION Coupling
  66. 66. @samnewman COUPLING AND COHESION Coupling Cohesion
  67. 67. @samnewman COUPLING AND COHESION Coupling Cohesion Low
  68. 68. @samnewman COUPLING AND COHESION Coupling Cohesion Low Strong
  69. 69. @samnewman COHESION The code that changes together, stays together
  70. 70. @samnewman WEAK COHESION
  71. 71. @samnewman WEAK COHESION
  72. 72. @samnewman COUPLING The degree to which changing one module requires a change in another
  73. 73. @samnewman CONSTANTINE’S LAW “A structure is stable if cohesion is strong and coupling is low.” - Albert Endres and Dieter Rombach
  74. 74. @samnewman TYPES OF COUPLING
  75. 75. @samnewman TYPES OF COUPLING Domain
  76. 76. @samnewman TYPES OF COUPLING Domain Tramp
  77. 77. @samnewman TYPES OF COUPLING Domain CommonTramp
  78. 78. @samnewman TYPES OF COUPLING Domain Common ContentTramp
  79. 79. @samnewman TYPES OF COUPLING Domain Common ContentTramp
  80. 80. @samnewman TYPES OF COUPLING Domain Common Content Increasing coupling Tramp
  81. 81. @samnewman DOMAIN COUPLING Order Processor Warehouse Reserve Stock Payment Take Payment
  82. 82. @samnewman DOMAIN COUPLING Order Processor Warehouse Reserve Stock Payment Take Payment Domain Coupling
  83. 83. @samnewman TRAMP COUPLING
  84. 84. @samnewman Order Processor TRAMP COUPLING
  85. 85. @samnewman Order Processor Warehouse TRAMP COUPLING
  86. 86. @samnewman Order Processor Warehouse Shipping Manifest Send Order Request # Item ID 3 2231 2 134 TRAMP COUPLING
  87. 87. @samnewman Order Processor Shipping Warehouse Shipping Manifest Send Order Request # Item ID 3 2231 2 134 TRAMP COUPLING
  88. 88. @samnewman Order Processor Shipping Warehouse Shipping Manifest Send Order Request # Item ID 3 2231 2 134 TRAMP COUPLING Shipping Manifest Dispatch Package Request
  89. 89. @samnewman AVOIDING TRAMP COUPLING
  90. 90. @samnewman AVOIDING TRAMP COUPLING Order Processor
  91. 91. @samnewman AVOIDING TRAMP COUPLING Order Processor Warehouse
  92. 92. @samnewman AVOIDING TRAMP COUPLING Order Processor Warehouse Send Order Request # Item ID 3 2231 2 134
  93. 93. @samnewman AVOIDING TRAMP COUPLING Order Processor Shipping Warehouse Send Order Request # Item ID 3 2231 2 134
  94. 94. @samnewman AVOIDING TRAMP COUPLING Order Processor Shipping Warehouse Shipping Manifest Send Order Request # Item ID 3 2231 2 134
  95. 95. @samnewman AVOIDING TRAMP COUPLING (CONT) Order Processor Warehouse Shipping 1342 22313 ItemID# Send Order Request Shipping Manifest Dispatch Package Request 123 The Place, UK Express Shipping
  96. 96. @samnewman COMMON COUPLING Warehouse
  97. 97. @samnewman COMMON COUPLING Warehouse Stock Levels
  98. 98. @samnewman COMMON COUPLING Warehouse Stock Levels
  99. 99. @samnewman COMMON COUPLING Warehouse Stock Levels Order Processor
  100. 100. @samnewman COMMON COUPLING Warehouse Stock Levels Order ProcessorForecasting
  101. 101. @samnewman COMMON COUPLING Warehouse Stock Levels Order ProcessorForecasting Change to the common data impacts multiple microservices
  102. 102. @samnewman CONTENT COUPLING Order ID Status 123 PLACED 456 SHIPPED
  103. 103. @samnewman CONTENT COUPLING Order ID Status 123 PLACED 456 SHIPPED Order Processor
  104. 104. @samnewman CONTENT COUPLING Order ID Status 123 PLACED 456 SHIPPED WarehouseOrder Processor
  105. 105. @samnewman CONTENT COUPLING Order ID Status 123 PLACED 456 SHIPPED WarehouseOrder Processor Information hiding bypassed
  106. 106. @samnewman CONTENT COUPLING Order ID Status 123 PLACED 456 SHIPPED WarehouseOrder Processor Information hiding bypassed Unclear what can safely be changed in the Order service
  107. 107. @samnewman CONTENT COUPLING Order ID Status 123 PLACED 456 SHIPPED WarehouseOrder Processor Information hiding bypassed Unclear what can safely be changed in the Order service Aka Pathological Coupling
  108. 108. @samnewman TYPES OF COUPLING Domain Common ContentTramp
  109. 109. @samnewman TYPES OF COUPLING Domain Common Content Increasing coupling Tramp
  110. 110. @samnewman IN SUMMARY
  111. 111. @samnewman IN SUMMARY Information hiding is super important
  112. 112. @samnewman IN SUMMARY Information hiding is super important Some coupling is worse than others
  113. 113. @samnewman IN SUMMARY Information hiding is super important Some coupling is worse than others Information hiding, low coupling, strong cohesion = Independent Deployability
  114. 114. @samnewman THANKS! https://samnewman.io/

×