- Published on
What and why micro frontend?
- Pramod Kumar
More than showing code to build micro frontends, this post focuses on the idea behind micro frontends. It lets you understand the core idea and current solutions around it.
yarn commands, you have a running frontend application!
Sure, we are in a better place. But then, there come new problems. I have personally worked end to end throughout the stack. In the backend, the crazy world of APIs, which drive the core business logic is very simple to start with. By now, there are more than a couple of dozens of frameworks across the technologies, to get you started with quickly building the backend application.
To start, that would be helpful. Once your application starts growing, your team starts getting more headcount, and more features and your backend application grows to a level where it will be a nightmare to manage.
Let’s take an example. You are building an e-commerce application. You start with two member tech team. For a month, both work left and right and bring it to live. Celebration moment! You start getting orders. Awesome!
As the owner of the business, you would plan to expand the business which is directly proportional to the features and the scope of the app itself. It is not enough to just have two people working on it, you hire more, and by no time, you would be 30 member team.
If you have experience in managing people and work, you understand the importance of distributing the work for the team to reach the goal. The same applies here. You need a better way to break the work and distribute it. The best way to do this is to break the tech team into sub-teams where each team works on one part of the business. So you would have an Inventory Team, Orders Team, Fulfillment Team, etc.
Each team owns its backend applications, its way of hosting the code, its standards, its release cycles, and others. Here evolves the solution for monoliths
It’s not something new by the way. It’s been quite a time since people started adopting microservices. People started moving against microservices because of their downside. Anyways, let us not focus on it this topic though I believe it’s because of poor execution of microservices.
To be short, every team that we discussed above would have a backend application that will have APIs for other teams to interact with it. It is that team’s wish to what technology they use, where they host the code, dependencies, data stores, etc. Till the time the APIs are in place, the implementation does not matter at a global level.
This makes the teams independent and lets them work on their terms and at their own pace. I will stop about microservices here or else it becomes lengthy. A step back, this idea of independent teams is so beautiful and makes sense in reality. Why should it be just for backend applications? Why not the frontend team also?
Why should there be a single frontend application, let be React, Vue, Angular or Svelte? Why should the entire code be in the same repository? Why should everywhen use the same framework? Why should the deployment pipeline be blocked when there is a bug only in the fulfillment part of the application?
In other words, it is about making the frontend applications functional. It may not be as easy to implement this as it sounds. Let us check a few of the problems with this setup
- We set up multiple frontend applications where each can be independently built and deployed would be a handy solution.
- We may want to have some common components which will be shared across the sub-projects
This is called monorepo. One repository of the code with multiple sub-applications where code can be shared and each sub-application can be built separately. This solves the problem of building multiple frontend applications.
Our problems do not solve with just a monorepo setup. We need more. If you see the above picture, imagine we are building the browse product page. We would show
- The list of products to buy
- Items in the shopping cart
- The page itself
If you break it down, there are 2 teams involved in it, Inventory, and Cart teams respectively. Now the problem is, how does the page render the products section which is built by the Inventory team, and the shopping cart section which is built by the Cart team on the page? They are completely different applications serving on different hosts.
How does the container app render the child apps? How do the child apps communicate with the container app? How does the data/state of the app gets exchanged? If you observe the above image, the “Add to cart” button is from the Cart team. How do render multiple types of screens from children’s apps? Lot of questions to answer. Let’s go one by one
There is a container app and a child app that is being served from a remote host. The requirement is, to mount the children’s app onto the container app. This should be framework agnostic. Once it is mounted it should work and blend with the container app seamlessly. Both in terms of UI and the control part.
Data sharing and communication
The simplest requirement would be, that the child apps need to know who is the logged-in user. There should be a shared data store in between the container and child apps. It should be reactive too. There should be a communication channel too between the container and child apps.
As I was saying above, from a concept point of view this looks interesting but implementing this is not easy. It is strongly not recommended for small teams. More than solving problems, it creates more problems if it is executed poorly.
I want to discuss only the concept because there can be multiple ways and variations of implementation possible, but the idea is the same. So, if we understand the concept we can work and implement what works for us.
Some of the resources I recommend if you want to see it in action
If you like this post follow me here and on Twitter — (@pramodk73)[https://twitter.com/pramodk73]. I keep posting about Software Engineering, SaaS, and building in public. Cheers!