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. 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. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta
Why this speech?
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. 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. 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. 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. 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. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta
Developing process
Developer Codebase Instructions CPU
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. 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. 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. 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. 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. 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. 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. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta
Evolution of architectures
from 1990’s to 2010’s
24. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta
User Interface A blackbox working
somehow
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. 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
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. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta
HTML
PHP
DATABASE
The layered monolith
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. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta
The layered approach
Customer Catalog Quote
Controller
Model
Resource model
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. 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. 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.
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. 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.
39. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta
Spaghetti Lasagna Ravioli
Where is Magento2 now?
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. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta
Whaaaaaat?!?!
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. 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. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta
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. 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. 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. 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. 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
52. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta
API / Interface
API first
53. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta
API / Interface
API first
Concrete module
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. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta
Functional area dependency inversion
MyModule
AnotherModuleAPI
AnotherModule
Depends on
Dependson
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. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta
Functional area dependency inversion
58. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta
Functional area dependency inversion
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. 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. 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. 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
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. 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. 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. 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
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
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. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta
OMG! So many modules…
First reaction…
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. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta
OMG! So many modules…
Then you see the light!
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. Oh wait! We forgot something!
#MM19PL - @RicTempesta
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. 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. 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. 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
84. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta
The CAP theorem
•Consistency
•Availability
•Partitioning
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. 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.
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. 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
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. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta
CQS: Query
Does not modify the database (or application state)
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. Awesome Architectures in Magento 2.3 – 16 September 2019 – Riccardo Tempesta #MM19PL - @RicTempesta
The inventory example
Write quantity Read salable quantityWrite reservations
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. 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. 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. 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