Building a faster CI pipeline with Turborepo and pnpm
Tags: typescript, webdev, programming, devops
Published: 2023-04-11 16:01:46

In the last 8 months, the frontend team at Tinybird has revamped the way we manage our code base. We made 3 big changes (plus a bunch of little ones) that have shortened our frontend continuous integration (CI) execution time despite a massive increase in code size and test coverage.

In this post, I’ll explain how we used a combination of a monorepo, Turborepo, and pnpm to modernize our frontend stack to ship more code, faster.

## What is Tinybird?
[Tinybird](https://www.tinybird.co/) is a serverless real-time database for developers and data engineers. With Tinybird, you can ingest data from multiple sources at scale, query it with SQL, and instantly publish your queries as low-latency, parameterized APIs to power your app dev. You can [sign up for free](https://ui.tinybird.co/signup) if you want to try that out.

Behind all the magic is a devoted engineering team that’s shipping code as fast as we can. We follow continuous delivery practices; in a good week, we’ll push over 100 changes to our production codebase. Fast CI times matter. #speedwins.

Our backend team recently detailed [how they cut their CI pipeline execution time in half](https://dev.to/tinybirdco/how-we-cut-our-ci-pipeline-execution-time-in-half-l3m). This was a big win for us. Tinybird is a data platform, so the backend is the biggest piece of our product and the lion’s share of the codebase. It’s what makes the engine roar.

But while Tinybird might be one of the most powerful analytics tools on the planet, the special sauce in the recipe is undoubtedly the developer experience. Developers choose Tinybird because, as we’ve been told, we “make OLAP feel like Postgres.”

<figure><img src="https://global-uploads.webflow.com/604a2f667b2fcefbc095bfc8/6434384c12954fbb6cd55798_miUy5eOqdpRHqufVoJUED6R1d_DGDUdFkXDflV_8Tro2NW2bdp4OpWuLX_gi-ah6mh7bcNo6Mi3zHNhXECvLnLDleidrG0fcC2DlZWMoEcHA1jaO9tOQ3wrTYp02E0v0i1Vcztpywg4d3VOudgutNjs.jpeg" alt="Screenshots showing various interfaces in the Tinybird browser UI."><figcaption>We put a lot of thought and effort into our browser UI to optimize developer experience.</figcaption></figure>

The frontend UI takes center stage in the Tinybird developer experience, and we needed to make similar changes to our frontend CI to keep up with the backenders. Here’s how we did it.

## Stage 0: Simple code, no type safety, no e2e tests
**June 2022**

When we started building the Tinybird UI, things were simple. We had 3 pieces of code:

1. A main UI app
2. A styles library
3. An API wrapper library

…all written in Javascript using React. We had a few basic unit tests, but zero end-to-end (e2e) testing. Don’t judge; there were some solid benefits:

- Very fast build times
- Zero time-wasting boilerplate
- We could be hacky and agile and get away with it

Of course, the project grew. More features, more UI paths. We ended up with a lot of duplicated components and very little standardization.

Plus, the team grew. Fresh eyes on messy code led to a lot of head-scratching and Slack threading. We didn’t have any type safety, and we lacked good test coverage. It was easy to make mistakes and introduce errors.

Dev speed slowed down since our team couldn’t rely on guardrails from type safety or CI. Shipping to production felt… risky.

To make matters worse, our code was split across multiple repositories. This gave us a number of fits:

1. **No ability to track issues in a central location**. Team members had to track their tasks across multiple GitLab projects, which created frustration. 
2. **Minor changes to multiple packages required changes across multiple repos**. This meant multiple merge requests that needed their own approvals.
3. **Duplicated code**. Merging to multiple repos was annoying, so we cut some corners to avoid it, including adding the same code to multiple places.

All in all, our code and process may have worked for a seed-stage startup, but it was time for us to grow up a bit and bring the consistency and safety expected from an enterprise-grade data platform.

<figure><img src="https://global-uploads.webflow.com/604a2f667b2fcefbc095bfc8/6434384c672b510e4880ff15_t9QIBjhlaLSAS5kz_erkpRfLshlZsfIUdDn7mTt7pOHAOD_mVJcThXmPy_i-JIuAz0lETAcjb14U5Pp4D8HeyVD5cULKSThEQfnbzKbUv38Z-x-iEtdCQj7C1tTp0ZBXkZE2lORiXHxJgmbHFUjObag.png" alt="A list of stats for the first stage of the project. There were 120,000 lines of code and 1,800 unit tests. Unit tests took 5 minutes and 30 seconds, npm install took 2 minutes, there were no e2e tests, and the production build took 1 minute and 30 seconds for a total CI execution time of 9 minutes in this stage. "></figure>

## Stage 1: Type safety, standardization, increased test coverage
**September 2022**

After finding ourselves in a classic startup code-mire, we decided to fix some things:

1. **We started using [TypeScript]**(https://www.typescriptlang.org/), migrating our packages and libraries (but not yet the main app). From this point on, every new package has been fully written in Typescript. At the same time, we started typing old parts of the codebase progressively.
2. **We created a standardized components library** so we could start reusing code and enforcing more consistency.
3. **We increased unit test coverage**. We ended up with about 2,200 unit tests over nearly 160,000 LOC.
4. **We added some e2e tests**. Specifically, we started testing critical paths, sign-up and login flows, and some of the fundamental aspects of Tinybird usage like creating Workspaces, Data Sources, and Pipes. We rely on [Cypress](https://www.cypress.io/) to create our e2e tests.

At this point, we ended up with a much larger code base and many more tests. Critically, we weren’t caching any tests in our CI, and only a few in our local dev. Every new production build would run all the tests.

So, while we had added the guardrails to keep us from careening off course, our CI execution times slowed waaayyyy down. In fact, they more than doubled, from 9 minutes to almost 20.

<figure><img src="https://global-uploads.webflow.com/604a2f667b2fcefbc095bfc8/6434384c088ae0c0d326ff5c_PnI7TqauuB2VFc1Y28r49ji2_NYPP28PmrX6E9BwII-cPTiSAhsIBnLUuwdJEzV3Njom3CheoeK49q81FFvhu9LMqnLZ9NZhHek4EmRs14oOYNPiUVBPP1sx1SZCkKZrYmO4eiitZDZL9PDv2Ihc0l4.png"></figure>

## Stage 2: Monorepo, Turborepo, pnpm
**February 2023**

Stage 1 brought a big quality boost to the project at the expense of our CI execution times. We knew the project was only going to keep growing, and we were pursuing even higher test coverage. That left us staring down the barrel of 20-30+ minutes of CI time. At that rate, we literally wouldn’t have enough minutes in a day to ship all we wanted to ship.

So, we made 3 big changes to our stack:

1. We consolidated our code into a monorepo.
2. We used Turborepo to handle tasks and cache test results.
3. We used pnpm to install our packages.

### Monorepo
Before these changes, our libraries and packages were all in individual repositories. So we had many different build processes to maintain, each individually tested, and we couldn’t cache unit test results across different repos.

We switched to a monorepo and brought all the frontend code - the app, the libraries, and the packages - into a single repository. This consolidated our code and issue tracking, removed code duplicates, and simplified the merge request process.

But, a monorepo can come with its own set of problems. Without a smart approach, it won’t scale. We could end up with a load of CI tasks and actually go backward in terms of CI pipeline time.

That’s why we used Turborepo.

### Turborepo
[Turborepo](https://turbo.build/repo) is an incredible, high-performance build system for JavaScript/TypeScript monorepos. It handles every package task and caches the logs and the outputs from them. With Turborepo, we can intelligently build only the pieces of code that have changed, and aggressively cache unit tests results across the different code bases that aren’t impacted by the changes we’ve recently committed.

We’ve started using Turborepo just to handle all the CI tasks (test, build, and lint). Turborepo’s smart protocol made this really easy, and it made job management much more efficient. There was almost zero configuration, and the default templates were almost perfect out of the box.

At first, we only used cache in local development, but shortly thereafter we implemented cache in the CI.

Right now, we use [GitLab CI](https://docs.gitlab.com/ee/ci/) for the cache. Turborepo generates some artifacts (builds & meta), and our GitLab CI saves these artifacts into Google Cloud Storage. We generate a key associated with the cache files based on the current task and step in the CI, and we use that key in future deployments. In this way, we can incrementally build the app, only rebuilding the parts that change.

This process isn’t perfect out of the box, and it took some work to get the config right. Notably, we currently aren’t using Vercel remote cache (which would work out of the box). A few of our projects do run on Vercel, so in the future, we think we can make some efficiency gains by switching to the Vercel remote cache.

### pnpm
As its name suggests, [pnpm](https://pnpm.io/) is a performant package manager. And it works great with our monorepo structure.

When we switched to a monorepo and had multiple apps in one place, some apps had the same dependencies but were pinned to different versions. So npm became really slow, as it was prone to errors while trying to resolve complex dependency trees. We tried implementing npm cache without any benefit; it didn’t improve our total build time at all.

We also tried yarn, but it too was running into dependency resolution conflicts.

pnpm has a much faster and more consistent dependency resolution algorithm than standard npm and yarn, so it works much more quickly and efficiently in re-using dependencies across the many projects in a monorepo. In our implementation, pnpm never hit any resolution errors. 

We have a total of five CI pipelines. With npm install, each pipe took 2+ minutes to run even with caching. With pnpm and its caching, we cut those build times down to 10 seconds. That means that, across our five pipelines, we’ve cut our times down from 10+ minutes to just 50 seconds with pnpm.

Along with these 3 major changes, we further migrated our code to Typescript and added over 600 additional unit tests to support multiple major new features and some internal applications that also live in the repo. We now have over 400 tests in our new packages and 3,000 in the main app. Turborepo handles both.

Even with all these new additions, the uncached CI time clocked in at just 15 minutes, a 25% improvement over the prior stage despite a big increase in test coverage and LOC.

But with caching and our trifecta of updates, we shaved off another ~8 minutes for an average total CI time of about ~7 minutes (depending on what’s cached).

<figure><img src="https://global-uploads.webflow.com/604a2f667b2fcefbc095bfc8/6434384cff079696e346c7bf_okwszcF-yfbWqWMd-v5AKQS7ePBefX0rR7YfRcFMlysSj52bnds11qVBgy-BPbL99dUbDKaqCkiIfj9e3V5M5FqJ5KiiVSuZ_WThoOU8dBikhqMudzR0vUbpOk3GI3mju-4gflNvCi-wx27jhwUPrT4.png"></figure>

All told we’ve eliminated about 5 hours of CI time every week. With the combination of Typescript, a monorepo structure, Turborepo, and pnpm, we ship faster and with more confidence. Faster development times lead to faster feedback from our users which leads to even faster development times for new features as the development flywheel accelerates.

<figure><img src="https://global-uploads.webflow.com/604a2f667b2fcefbc095bfc8/643440ac6d9690a32f962e81_Lines%20of%20Code%20and%20Unit%20Tests.png"><figcaption>Over the last 10 months, our LOC has increased by ~70% and our unit tests have increased by ~190%.</figcaption></figure>

<figure><img src="https://global-uploads.webflow.com/604a2f667b2fcefbc095bfc8/643440d268423313185d9a1a_CI%20Pipeline%20Execution%20Time.png" loading="lazy"><figcaption>When we moved to Typescript and added e2e testing, our CI execution time more than doubled. With Turborepo and pnpm, we actually ended up with a faster CI despite the increases in LOC and test coverage.</figcaption></figure>

## Next Steps
Of course, good engineering never sleeps. In the future, we will focus on adding even more tests covering more features. And we’ll make some additional changes to our codebase, namely:

- Migrating everything to TypeScript
- Splitting our frontend from our backend repo to simplify the CI process so that frontend changes don’t impact backend deployments
- Migrating from vanilla React to Next.js
As we make improvements, we’ll write about them.

## Summary
We significantly improved our CI execution time and code management of our frontend code base by implementing a combination of a monorepo, Turborepo, and pnpm. These changes allowed us to ship code faster and with more confidence, despite a massive increase in our code size and test coverage.

You can apply the lessons learned from us by evaluating your own codebases and considering the adoption of a monorepo structure, leveraging Turborepo for efficient task handling and caching, and using pnpm for faster package management. By doing so, you can also optimize your CI pipelines and enhance your overall development experience.

For those interested in learning more about building better CI pipelines for your front ends, we recommend you check out our other blog post on this subject: [Better CI Pipeline with Data](https://www.tinybird.co/blog-posts/better-ci-pipeline-with-data). Also, you can explore these resources:

**Monorepo Best Practices:**

1. [Scaling a Monorepo at Google](https://cacm.acm.org/magazines/2016/7/204032-why-google-stores-billions-of-lines-of-code-in-a-single-repository/fulltext)
2. [Monorepos: Please don't!](https://medium.com/@mattklein123/monorepos-please-dont-e9a279be011b)
3. [Awesome Monorepo: A curated list of resources related to monorepos](https://github.com/korfuri/awesome-monorepo)
4. [Benefits and challenges of monorepo development practices](https://circleci.com/blog/monorepo-dev-practices/)

**Turborepo Documentation:**
1. [Turborepo Official Documentation](https://turborepo.org/docs)
2. [Building a Design System Monorepo with Turborepo](https://leerob.io/blog/turborepo-design-system-monorepo)

**pnpm Guides:**
1. [pnpm Official Documentation](https://pnpm.io/)
2. [Why should we use pnpm?](https://medium.com/pnpm/why-should-we-use-pnpm-75ca4bfe7d93)
3. [Benchmarks of JavaScript Package Managers](https://pnpm.io/benchmarks)

## More about Tinybird
Interested in learning more about Tinybird, the real-time database for developers? Follow us on [Twitter](https://twitter.com/tinybirdco) or [LinkedIn](https://www.linkedin.com/company/tinybird-co), join our growing [Slack community](https://www.tinybird.co/join-our-slack-community), or check out our [blog](https://www.tinybird.co/blog).