So let's talk about direct communication between micro frontends. This is a very general problem in software engineering. We have these different units that are independent of each other. We don't want to have them coupled with each other, but they still have to pass messages between each other because they are part of a bigger team.
So if we take a look at what we are doing in backend software design, it's usually about micro services and micro frontend and micro services have some correlation between them. That's a chance to learn a lot about micro frontends. So if we take a look on how micro services communicate, one of the most popular and powerful patterns is the public subscribe mechanism, asynchronous messaging. Each micro service can publish information, interesting stuff that happens within the micro service to some shared space, like a message bus, and any other micro service could consume this information in case it's really interesting. So this makes micro services still decouple from each other in a way, but still allows a very nice, very powerful communication.
So we could derive from that a solution to our micro front-ends, right? We could use the window object, which is a great common space that every micro front-end can communicate with. And each micro front-end could publish interesting events about stuff that happens, like product that was selected for the card, and any other micro front-end could consume this information. So, different micro front-ends are not aware of each other, but they can still pass imported information. So, that can be implemented very easily and even natively using custom events. We can create custom events and dispatch them into the window, and we can add event listeners to the window to get this information back. So, you could wrap that mechanism with your React or Vue framework and build something more advanced that can help you automate some of this communication or make it more robust.
Now, that was nice, but sometimes, you just have to have a state. So, communicating with the event is great, but there are some complex operations or some complex constructs of data that we wanted to have a shared or some sort of states like a Redux state to manage the process, and that can happen quite often. So, what we would want is this a centralized Redux store and every microfrontend could communicate with in order to maintain these kind of complex states. And this is very problematic, especially when we talk about microfrontends, because this completely breaks the isolations that they have from each other, especially that they are owned by different teams.
So what we really wanted is these nice, aligned stores. We have a store per microfrontend. Each microfrontend can use that store like a Redux store, and these stores are decoupled from each other. But, of course, here we don't have any communication between them and that's also problematic. So another solution that I've seen to maintain a state between microfrontends in a way that still keeps them quite decoupled is to allow microfrontends to subscribe or view stores of other microfrontends, but without the ability to mutate these stores. So in this example the support microfrontend can really, the product microfrontend, delivery microfrontends can view the store of the support microfrontend, but still there is some problem, there are some problems here, because any change in how the support store is implemented would affect the delivery and product microfrontends. So what we can do to make it even more secure is to come up with this global store. This global store manages the different stores of the different microfrontends, each microfrontend can subscribe to the store, it has its own store that it can manipulate, and other microfrontends can consume and subscribe to their own stores or other stores. Because this global store would expose some specific API, then each microfrontend will just have to implement that specific API and that will make everything much easier that will align this API across those different microfrontends.
Well, so that was like an overview of three common ways to maintain communication between microfrontends while keeping them quite isolated from each other. So I would like to summarize the talk and the first thing that we say that is to strive toward highly autonomous teams we should structure teams in alignment with business subdomains and that doesn't relate directly to microfrontends you can definitely have a monolith that is aligned with a business aspect. Although applying microfrontend architecture could enhance this idea by segregating business concerns in a better way. And by applying strategic DTD we can identify subdomains model them into bounded context and apply context mapping to find relationships between them. And we also say that the microfrontend is a technical implementation of the bounding context. So context mapping can help us identifying the spots, the touch points where microfrontend should communicate. And a one important rule is that communication between microfrontends should keep them isolated from each other. So that's the summary of the talk. I hope I gave you some inspiration about how to apply DDD in problems that we encounter in front end, and how to facilitate a communication between microfrontends in a way that will help you achieve goals like team's autonomy.
Comments