Decouple a monolith: How we did it for Open edX frontend using Strapi and Nextjs
Tags: strapi, nextjs, tailwindcss, openedx
Published: 2021-07-15 16:50:34

![monolith](https://media.giphy.com/media/eoZgD4aEku6cfxBUnJ/giphy.gif)
Original doc: https://strapi.io/blog/decouple-a-monolith-how-we-did-it-for-open-ed-x-frontend-using-strapi-and-nextjs
Open edx is a great open-source learning management system, but it's also an all-inclusive monolith. As such, there are some significant drawbacks to using this platform for your project needs. One of the biggest pain points comes when making small changes like changing text on landing pages or updating the logo can take a good amount of the developer's time due to needing code modifications and recompiling assets from servers with every little change made in place. 

We've created content types in Strapi for all of our UI components and stored their frontend data there. Some content gets created manually by CMS admins, while some is automatically generated when there's a new build on the frontend. We can now build our frontend using Next.js by fetching UI data from Strapi APIs. It's now possible to make changes in the UI without any technical skills, and our administration team can do it right from Strapi dashboard and when their changes is done with a click of button it shows up in our platform.

In this article, we'll be looking at how we decoupled the Open edX frontend by using Strapi and Nextjs and brought the joy back to our developers.

Before we start, let's answer some key questions to make sure we're on the right track.

## What is a monolith system?
A monolithic system is a software system with one code base and all the functionality included.

## Why monolith system is not ideal architecture anymore?
In recent years, with the rise of microservices and containerization, having a monolith system can turn into a nightmare for software development teams. For example, in the case of open edx we have a python/Django app in which all features are stuffed together. We never know what would break when refactoring some parts of the application or adding new ones.

If one piece breaks, we need to wait for the full or partial system to be deployed again via Ansible. This can take hours, even days if a big migration is involved.

## How does a monolith system influence developers' productivity?
A monolithic application will slow down the development process. We already mentioned that deploying the system can take hours or days. In this case, every change in the code takes a lot of time and requires more effort than usual to be deployed on production servers.

On top of all this, if we want to release a new feature, it is not possible to test it in a sandbox environment and roll back if we encounter any issue. We have to wait for the deployment process to finish (compile assets, migrations, deploy, etc.)

## How could we decouple the monolith?
Decoupling is when you separate an application into two or more parts that don't depend on each other. Each part has its own codebase

## What is a decoupled system?
A system architecture is said to be "decoupled" if each of its components may evolve independently of the others. The term decoupling generally refers to the removal of direct dependencies between two modules or components. In this sense, "backend" and "frontend" are loosely coupled.

In our case, we're trying to decouple the Frontend from the backend part of Open edx.

## Why?
The biggest benefit, you get by doing it, is The ability to update your front-end without touching the Server-side code. With this method, development becomes way faster and easier for core developers. When a new frontend developer joins the team, he or she can start working with minimum onboarding.   

## What does this mean for my users?
A decoupled system distributes the work of developing a web app between your client-side and server teams. The net result is that you have a faster feedback loop when building features. It also allows you to change the look and feel of your app without needing to rebuild it each time.

## What do we gain by decoupling ?
The decoupled architecture can give you a few advantages:

1. High performance

2. Better design, better onboarding & maintenance for front-end developers

3. Developers are no longer blocked by slow backend development cycles

4. Faster shipping and iteration time overall.

5. Easily scale the application by dividing it into multiple microservices.

## What do we need to do to decouple a monolith?
- Choose a client-side framework like Nextjs
- Create APIs with Strapi
- Build APIs in your Backend if needed
- Use these APIs to build your Frontend

## What we lose by decoupling a monolith?
There are some downsides to this approach:

1. It requires more initial work to set up the front-end and APIs, as well as understanding how they will interact with each other on a technical level

2. Teams must be cross-trained to maintain API quality and consistency

3. It can take time to identify which parts of your monolith are safe to decouple

Ok, enough with technical details, let's go through a quick example. As an example, I will show you how the frontend development looks before and after decoupling, we also take a quick look at the site performance.

## Open edX before decoupling

As I mentioned earlier, the project we are working on called open edx, which is an open-source learning management system. When you install it, you got something like the following out of the box.
![Screen Shot 2021-07-08 at 20.53.49](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uxorxy0ukm2syf929gi0.png)
I know it doesn't look too good, and the worst part comes when you want to start working on changes on UI. Having a development environment means having the monolith up and running in your local machine. The minimum hardware requirement is at least 4G-6G of RAM to have smooth development environment.

### Let's make a simple change in frontend
Ok, let's say we want to replace the logo placeholder with our actual logo. Here are the steps you need to follow:
1. Make change in your code repo
2. Pull them in the server
3. Run python paver command to compile assets (takes around 10-30 minutes)
4. Restart manually LMS services
5. wait for 2-5 minutes till the services are up and running
6. Check if the change applied to your site
7. If you don't see your change, repeat step 3 to 6 again
![openedx-coupled](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/su8zos8t7mrvpd8ff7o5.gif)
 
This took longer than what you see in the video, I needed to cut the video to make it lighter to upload :)

### How the performance looks like
Here is the lighthouse report. https://lighthouse-dot-webdotdevsite.appspot.com//lh/html?url=https%3A%2F%2Facademy.cubite.io
![Screen Shot 2021-07-08 at 21.31.05](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3tofqoik2oikwedhboe5.png)
Looks pretty bad :(

## Open edX decoupled
Now let's go to the fun part. If you are curious how we decoupled openedx theming, we used existing APIs like courses APIs and implemented new ones like registration and enrollment. The reason behind that is we want to create some records in Strapi automatically based on what we have in Open edX to keep our LMS as a single source of truth. So for example, our Next.js frontend makes a call to open edx courses API and create or update courses in Strapi, so we don't need to manually create them there, and we have a single source of truth.
### Strapi
We created all the frontend elements as content type in Strapi, we have for example navbar, hero, footer, course cards, tutors content types in Strapi. You can even change site color globally from Strapi with one click

### Nextjs
In Nextjs we have all of our React components, like hero, navbar, course etc. they are styled with Tailwindcss. Our site is pre-rendered using SSG and hosted on Vercel to bring blazing fast experience to our users. In our Nextjs app we have a GraphQL client that makes calls to Strapi, fetch data and during the build time we construct components based on what we receive from Strapi.

####How does it look like ? **Beautiful**.

{% youtube UQz2_tuS4cY %}

#### How is the performance ? **Fast**
https://lighthouse-dot-webdotdevsite.appspot.com//lh/html?url=https%3A%2F%2Fdemo.cubite.io
![Screen Shot 2021-07-08 at 22.04.53](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c1wrje2lseh203manu6a.png)

### Let's change the logo again
![openedx-decoupled-logo-change](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bbi4hcuq9umfn5g7mfcg.gif)
Also, the Vercel build time was pretty quick. 1m1s
![Screen Shot 2021-07-08 at 22.17.15](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ox9knhw7d1fkn64hrwba.png)


## Other benefits
By decoupling the frontend, we could integrate our platform with Auth0 and Stripe pretty easy. If we wanted to do this with previous design, it would take longer time for development as well as be more expensive to maintain because of how often open edx updates its source code on a regular basis. Fortunately, since these integrations live in our frontend now, all that's required is making sure the APIs we talked about earlier, still work. Something much easier than maintaining compatibility across different systems!
 
## Conclusion
Decoupling your monolith can bring a new life to your team. It allows faster iteration, less coupling between backend and frontend, better scalability and maintainability of the system. All this without slowing down the development process of adding new features for your users. We found Strapi and Nextjs a great mix to achieve decoupling for our use cases and greatly contribute to the maintainability, scalability, and performance of the frontend application.