Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Awesome Architectures in Magento 2.3 - MM19PL

304 views

Published on

Awesome Architectures in Magento 2.3
Rookie track edition for MeetMagento Poland 2019

Published in: Technology
  • Be the first to comment

Awesome Architectures in Magento 2.3 - MM19PL

  1. 1. AWESOME ARCHITECTURES IN MAGENTO 2.3 Riccardo Tempesta CTO MAGESPECIALIST MAGENTO MASTER 2019 MAGENTO COMMUNITY MAINTAINER TOP 5 MAGENTO CONTRIBUTOR 2018 #MM19PL - @RicTempesta
  2. 2. AWESOME ARCHITECTURES IN MAGENTO 2.3 Riccardo Tempesta CTO MAGESPECIALIST MAGENTO MASTER 2019 MAGENTO COMMUNITY MAINTAINER TOP 5 MAGENTO CONTRIBUTOR 2018 #MM19PL - @RicTempesta
  3. 3. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta DISCLAIMER 1: The next slides contain a personal interpretation that may differ from the official Magento architects position.
  4. 4. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta DISCLAIMER 2: My english is bad, but you don’t want to try my polish!
  5. 5. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Why this speech?
  6. 6. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Becoming a Magento2 developer is not stressful at all -- Mark, 23 years old
  7. 7. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta «confusion» comes from latin: «cum + fusus» It means: «Bonded with» or «Fused with»
  8. 8. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta «confusion» comes from latin: «cum + fusus» It means: «Bonded with» or «Fused with»
  9. 9. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Magento 1 Another framework Do not try to approach Magento2 as you were approaching Magento1 or any other «classical» framework
  10. 10. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Magento2 is a very complex software with a very large codebase
  11. 11. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Developing process Developer Codebase Instructions CPU
  12. 12. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Developing process Developer Codebase Instructions CPU Machines do not have any problem interpreting and running more code
  13. 13. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Developing process Developer Codebase Instructions CPU Machines do not have any problem interpreting and running more codeWe start having problems understanding and maintaining a considerable amount of code
  14. 14. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Increasing the code size: 95% of time: reading to understand 5% of time: write new code We are the bottleneck
  15. 15. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta We are the bottleneck We need to optimize the 95% of time spent in reading and understaning our code
  16. 16. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta We are the bottleneck How can we avoid it? •Add structures •Raise abstractions •Reduce cognitive load •…
  17. 17. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta We are the bottleneck What are the tools? •Methods •Classes •Modules •Applications •…
  18. 18. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta We are the bottleneck What are the tools? •Methods •Classes •Modules •Applications •… Interactions and relations between these is what we call: Software Architecture
  19. 19. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Evolution of architectures from 1990’s to 2010’s
  20. 20. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta 1990’s Spaghetti coding (aka Copy & Paste)
  21. 21. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta 1990’s Spaghetti coding (aka Copy & Paste) 2000’s Lasagna coding (aka Layered Monolith)
  22. 22. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta 1990’s Spaghetti coding (aka Copy & Paste) 2000’s Lasagna coding (aka Layered Monolith) 2010’s Ravioli coding (aka Microservices)
  23. 23. the «Spaghetti coding era» #MM19PL - @RicTempesta
  24. 24. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta User Interface A blackbox working somehow
  25. 25. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta … inside the box: Code production over code maintainability … everything coupled with everything
  26. 26. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta No architecture at all • Hard to maintain • High cognitive load • Bad scalabilitycost timeEvolution limit
  27. 27. the «Lasagna coding era» #MM19PL - @RicTempesta
  28. 28. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Every upper layer depends on the underneath layers Software is built in layers The layered monolith
  29. 29. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta HTML PHP DATABASE The layered monolith
  30. 30. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Controller Model ResourceModel The layered monolith Like most of the existing frameworks, Magento1 is here.
  31. 31. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta The layered approach Customer Catalog Quote Controller Model Resource model
  32. 32. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta The layered approach Customer Catalog Quote Controller Model Resource model Every functional area is developed following a «horizontal» model
  33. 33. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta The layered approach Customer Catalog Quote Controller Model Resource model But we need interactions across different areas
  34. 34. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta The drawback The application becomes a N-layered monolyth requiring every single layer to be working. Only bing releases because we were not able to to deploy each layer separately. Unable to scale layers separately.
  35. 35. the «Ravioli coding era» #MM19PL - @RicTempesta
  36. 36. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Microservices The final application is a composition of multiple «small» features.
  37. 37. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Microservices Each component can be deployed separately. Each component can be scaled independently from the whole application.
  38. 38. Where is Magento2 now? #MM19PL - @RicTempesta
  39. 39. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Spaghetti Lasagna Ravioli Where is Magento2 now?
  40. 40. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Spaghetti Lasagna Ravioli Where is Magento2 now? Magento 1 Magento 2.0 Magento 2.1 Magento 2.2
  41. 41. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Whaaaaaat?!?!
  42. 42. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Something changed in Magento 2.3 after one of the most advanced community projects: MSI
  43. 43. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Igor Miniailo Magento 2 Architect, Magento / Adobe @iminyaylo Eugene Shakhsuvarov Software Development Engineer, Magento / Adobe @ishakhsuvarov https://github.com/magento/inventory The Magento Inventory Project (aka MSI)
  44. 44. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta
  45. 45. The service isolation #MM19PL - @RicTempesta
  46. 46. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Customer Catalog Quote Controller Model Resource model The Layered monolith approach: Software is developed «horizontally»
  47. 47. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Customer Catalog Quote Controller Model Resource model In the service isolation model we consider each functional area as an independent module Software is developed «vertically»
  48. 48. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Customer Catalog Quote Controller Model Resource model But we still have a problem with modules requiring acces to other modules information
  49. 49. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Customer Catalog Quote Controller Model Resource model Customer API Catalog API Quote API Customer Catalog Quote We should split each module in: • API module • Implementation module Note: This is an example, Magento2 modules implemenetation may differ
  50. 50. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Modules interact with other modules via API only Customer API Customer Catalog API Catalog Quote API Quote
  51. 51. «API first» approach #MM19PL - @RicTempesta
  52. 52. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta API / Interface API first
  53. 53. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta API / Interface API first Concrete module
  54. 54. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta •Single responsibility •Open for extensions, Closed for modification •Liskov substitution •Interface segregation •Dependency inversion A functional area should depend on the abstraction of another functional area and not on concrete its implementation. The «D» of SOLID brought at functional area level
  55. 55. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Functional area dependency inversion MyModule AnotherModuleAPI AnotherModule Depends on Dependson
  56. 56. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Functional area dependency inversion MyModule AnotherModuleAPI AnotherModule Depends on Dependson Another advantage: Can be replaced with a 3rd party implementation if needed
  57. 57. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Functional area dependency inversion
  58. 58. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Functional area dependency inversion
  59. 59. Cross contexts interactions #MM19PL - @RicTempesta
  60. 60. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Functional area dependency inversion Inventory In the old way: They would have been depending on each other We would have to modify «Sales» and «Inventory» accordingly their needs Mutual dependency Sales
  61. 61. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Functional area dependency inversion Inventory In the old way: They would have been depending on each other We would have to modify «Sales» and «Inventory» accordingly their needs Mutual dependency Sales
  62. 62. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Decoupled dependency Inventory SalesInventorySales There is no more direct dependency between «Sales» and «Inventory»
  63. 63. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Decoupled dependency Inventory SalesInventorySales There is no more direct dependency between «Sales» and «Inventory» Plugin to «Sales» to modifiy/add what we need
  64. 64. We need «smaller» classes #MM19PL - @RicTempesta
  65. 65. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta •Single responsibility •Open for extensions, Closed for modification •Liskov substitution •Interface segregation •Dependency inversion A class has a single «atomic» responsibility and must be easy to replace. This is why old Magento1 «Helpers» should not be used anymore. The «S» of SOLID
  66. 66. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Divide et impera! The rule: You should be able to understand the specific responsibility and behavior of a class by simply reading its name. «If you have problems naming your class, then you probably need a refactor!» -- The Rick
  67. 67. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta When the responsibility is an action <?php … class GetProductIdsBySkus implements GetProductIdsBySkusInterface { … public function execute(array $skus): array { $idsBySkus = $this->productResource->getProductsIdsBySkus($skus); $notFoundedSkus = array_diff($skus, array_keys($idsBySkus)); … return $idsBySkus; } … } You have a single public method class and its name should contain a verb.
  68. 68. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta The single responsibility corollary Class A extends B Class B extends C Class B Class C DI Class A Composition over extension
  69. 69. The future… #MM19PL - @RicTempesta
  70. 70. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Anton Kril’s architecture vision of Magento2 Director of architecture at Magento and Master of the Universe
  71. 71. The Modulegeddon! #MM19PL - @RicTempesta
  72. 72. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta OMG! So many modules… More than 40 for Inventory only
  73. 73. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta OMG! So many modules… First reaction…
  74. 74. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta OMG! So many modules… Second reaction… (or near death experience)
  75. 75. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta OMG! So many modules… Then you see the light!
  76. 76. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta You see all the advantages: •Fully modular system •Full service isolation •Clear code writing •Classes and responsibilities are easy to find •Each module or class is easy to replace •Easy to handle in a structured teamwork •No more «god modules» made of hundreds of classes
  77. 77. Oh wait! We forgot something! #MM19PL - @RicTempesta
  78. 78. Database! #MM19PL - @RicTempesta
  79. 79. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Customer Catalog Quote Controller Model Resource model Customer API Catalog API Quote API Customer Catalog Quote Database Isolation In the «vertical» approach, tables could be on different databases!
  80. 80. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta The foreign key problem Inventory area Catalog area Source item Product IDProduct ID Foreign key Product entity
  81. 81. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta The foreign key problem Inventory area Catalog area Source item Product IDProduct ID Foreign key Product entity
  82. 82. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta The foreign key problem Inventory area Catalog area Product SKUProduct SKU No DB dependency And use of business keys Source item Product entity
  83. 83. Mind the CAP theorem #MM19PL - @RicTempesta
  84. 84. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta The CAP theorem •Consistency •Availability •Partitioning
  85. 85. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta The CAP theorem •Consistency •Availability •Partitioning You can have only 2
  86. 86. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta The CAP theorem We need partitioning because our functional areas are vertically splitted. We need availability because we are building an e-commerce solution. So we cannot be immediately consistent.
  87. 87. The eventual consistency #MM19PL - @RicTempesta
  88. 88. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta The CRUD approach (if we had consistency) Write data Read same data We have: immediate consistency
  89. 89. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta In a partitioned (or distributed) system Write data We have: eventual consistency Read potentially oudated data
  90. 90. CQS: Command-Query Separation #MM19PL - @RicTempesta
  91. 91. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta CQS: Command Example: A hypothetical «save» (or «create») method, should not return the newly created object ID. Use of business keys is recommended in this case Modifies the DB (or application state) and does not return anything
  92. 92. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta CQS: Query Does not modify the database (or application state)
  93. 93. CQRS: Command-Query Responsibility Segregation #MM19PL - @RicTempesta
  94. 94. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Different read/write logic Write data (with a representation) Read data (with another representation)
  95. 95. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta The inventory example Write quantity Read salable quantityWrite reservations
  96. 96. Good examples #MM19PL - @RicTempesta
  97. 97. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Where to find good examples • https://github.com/magento/inventory • https://github.com/magento/security-package/pull/2 Inventory project (aka MSI) Notifier project
  98. 98. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Where to find good examples • https://github.com/magento/inventory • https://github.com/magento/security-package/pull/2 Inventory project (aka MSI) Notifier project
  99. 99. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Where to find good examples • https://github.com/magento/inventory • https://github.com/magento/security-package/pull/2 Inventory project (aka MSI) Notifier project
  100. 100. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta Magento is an open source Community To be involved in the Magento evolution: Join the Magento EngCom Slack Channel magentocommeng.slack.com #appdesign magentocommeng.slack.com #msi
  101. 101. Thank you! Riccardo Tempesta (aka The Rick) MageSpecialist @RicTempesta riccardo.tempesta@magespecialist.it #MM19PL

×