Speed up your CircleCI pipeline by using the RAM disk
During a presentation—held internally at Acast by my colleague Daniel Grefberg—I discovered a one-line change that reduced by 2 minutes the execution time of my current project’s CI pipeline.
- working_directory: ~/my-project-name
+ working_directory: /mnt/ramdisk/my-project-name
CircleCI allows us to mount the project on the RAM disk and use it as a ”temporary file storage paradigm.” That means, when caching a project’s dependencies, CircleCI will use the RAM disk instead of the disk drive.
When working on a Node.js project, it is common to cache the node_modules
folder. It allows us to re-use it in the other jobs to avoid running the install command every time. On paper, that should be faster than running npm ci
(or yarn install --frozen-lockfile
) for each job, but that’s not necessarily the case. It usually takes ~2 minutes to restore the cache of ~400MB. This time is mainly taken for decompressing the thousands of files from the folder on the disk. But, this step gets down to ~20 seconds by moving it to the RAM disk.
This change is a quick win to save a few minutes for running our pipeline. The only drawback is that we are limited to the RAM capacity—defined by the job’s resource_class attribute. By default, it runs on a 4GB RAM (medium resource class), so depending on our project and the dependencies’ size, we might need to increase the resource class.
version: 2.1
aliases:
- &default_image
working_directory: /mnt/ramdisk/my-project
docker:
- image: cimg/node:12.20
- &cache_key v1-cache-{{checksum ".circleci/config.yml"}}-{{checksum
"yarn.lock"}}
- &restore_cache
restore_cache:
keys:
- *cache_key
jobs:
initialize:
<<: *default_image
steps:
- checkout
- *restore_cache
- run: yarn install --frozen-lockfile
- save_cache:
name: Save cache
key: *cache_key
paths:
- node_modules
test_unit:
<<: *default_image
steps:
- checkout
- *restore_cache
- run: yarn test:unit --maxWorkers=2