overhangio / tutor-mfe

This plugin makes it possible to easily add micro frontend (MFE) applications on top of an Open edX platform that runs with Tutor.
GNU Affero General Public License v3.0
22 stars 95 forks source link

[Spike/R&D] Cache usage in tutor-mfe build #210

Closed DawoudSheraz closed 4 months ago

DawoudSheraz commented 6 months ago

In Tutor Users meeting on May 20th, 2024, a large chunk of the meeting was focused on discussing the large build times of tutor-mfe (https://openedx.atlassian.net/wiki/spaces/COMM/pages/3583016961/Tutor+Users+Group#2024-05-20). When various MFEs are building in parallel, both install and build steps take quite a while, often leading to npm connection error. The only workaround is to limit buildx parallelism to 1/2/4, depending on the resources.

One of the items discussed in the meeting was the usage of cache during MFE build. There are various items we would want to test/verify:

Besides the above items' verification, identify what cache led (or other workarounds) can we do to improve/reduce MFE build times.

DawoudSheraz commented 6 months ago

@hinakhadim @FahadKhalid210 FYI

hinakhadim commented 5 months ago

My final insights after running everything:

First-time build or --no-cache build:

Second time build

Answers to the Questions:

How npm install work?

If anyone else want to add something, feel free to share.

If anyone else wants to add something, feel free to share. I have searched a few things to try on, prefer-offline, or if we cache npm packages of a mfe to a single place at our laptop/server, then other mfe install from that. (just a thought yet). It's for a first-time install as first-time image building is our biggest concern.

FahadKhalid210 commented 5 months ago

Docker cache is shared properly across layers. Docker Resources: CPU: 16 Memory: 32 GB Disk Limit: 136 GB MFE first time build takes ~32 min Second time from cache takes ~ 17.7 sec

Regarding npm cache, I tried building the images with the --no-cache option. However, I noticed that the npm cache is being hit multiple times during the build process. Additionally, I encountered network errors while attempting to build all the images.

image

hinakhadim commented 5 months ago

Updates:

With --no-cache flag: I tried building MFE image with the --no-cache on the sandbox server. There I got a cache hit which shows that the npm cache is being used by other MFEs npm installation as mentioned by @FahadKhalid210

kdmccormick commented 5 months ago

This is a great writeup. Thank you all!

hinakhadim commented 4 months ago

@tasawar-hussain suggested three ways to consider the cache usage for MFE build:

  1. multi stage builds This point says to use the multi-stage build feature of Docker that is already being used by MFE Dockerfile.

  2. npm cache with volume, but it would require additional configuration and management of the volume and it may be good for only one MFE. Something similar This is best for the case in which package.json changes frequently like version change that will bust docker's cache.

  3. using Docker Buildkit Here two solutions have been suggested. First is to use Docker buildkit that we're already using and it is by default provided by Docker Desktop. The second is to install dependencies before copying package.json by extracting dependencies in an extra file. But this only helps us when we have to increase the version or make changes in our package.json file other than deps change. In our case, we are not changing anything in package.json other than deps.

DawoudSheraz commented 4 months ago

We have good enough context on this, closing this for now.