☄️ Pufferfish, please scale the site!
Published: 2021-07-22 07:43:08

---
canonical_url: https://blog.noredink.com/post/657392972659310592/pufferfish-please-scale-the-site
---

_This post was co-authored by Michael Glass, Stöffel, and myself. It first first appeared on [the NoRedInk blog](https://blog.noredink.com/post/657392972659310592/pufferfish-please-scale-the-site)._

We created Team Pufferfish about a year ago with a specific goal: **_to avert the MySQL apocalypse_**! The MySQL apocalypse would occur when so many students would work on quizzes simultaneously that even the largest MySQL database AWS has on offer would not be able to cope with the load, bringing the site to a halt.

A little over a year ago, we forecasted our growth and load-tested MySQL to find out how much wiggle room we had. In the worst case (because we dislike apocalypses), or in the best case (because we like growing), we would have about a year’s time. This meant we needed to get going!

Looking back on our work now, the most important lesson we learned was the importance of timely and precise feedback at every step of the way. At times we built short-lived tooling and process to support a particular step forward. This made us so much faster in the long run.

## 🏔 Climbing the Legacy Code Mountain

Clear from the start, Team Pufferfish would need to make some pretty fundamental changes to the Quiz Engine, the component responsible for most of the MySQL load. Somehow the Quiz Engine would need to significantly reduce its load on MySQL.

Most of NoRedInk runs on a Rails monolith, including the Quiz Engine. The Quiz Engine is big! It’s got lots of features! It supports our teachers & students to do lots of great work together! Yay!

But the Quiz Engine has some problems, too. A mix of complexity and performance-sensitivity has made engineers afraid to touch it. Previous attempts at big structural change in the Quiz Engine failed and had to be rolled back. If Pufferfish was going make significant structural changes, we would need to ensure our ability to be productive in the Quiz Engine codebase. Thinking we could _just do it_ without a new approach would be foolhardy.

### ⚡ ️The Vengeful God of Tests

We have mixed feelings about our test suite. It’s nice that it covers a lot of code. Less nice is that we don’t really know what each test is intended to check. These tests have evolved into complex bits of code by themselves with a lot of supporting logic, and in many cases, tight coupling to the implementation. Diving deep into some of these tests has uncovered tests _no longer covering any production logic at all_. The test suite is large and we didn’t have time to dive deep into each test, but we were also reluctant to delete test cases without being sure they weren’t adding value.

Our relationship with the Quiz Engine test suite was and still is a bit like one might have with an angry Greek god. We’re continuously investing effort to keep it happy (i.e. green), but we don’t always understand what we’re doing or why. Please don’t spoil our harvest and protect us from (production) fires, oh mighty RSpec!

The ultimate goal wasn’t to change Quiz Engine functionality, but rather to reduce its load on MySQL. This is the perfect scenario for tests to help us! The test suite we want is:

- fast
- comprehensive, and
- not dependent on implementation
- includes performance testing

Unfortunately, that’s not the hand we were given:

- The suite takes about 30 minutes to run in CI and even longer locally.
- Our QA team finds bugs that sneaked past CI in PRs with Quiz Engine changes relatively frequently.
- Many tests ensure that _specific queries_ are performed in a _specific order_. Considering we might replace MySQL wholesale, these tests provide little value.
- And because a lot of Quiz Engine code is extremely performance-sensitive, there’s an increased risk of performance regressions only surfacing with real production load.

Fighting with our tests meant that even small changes would take hours to verify in tests, and then, because of unforeseen regressions not covered by the tests, take multiple attempts to fix, resulting in multiple-day roll-outs for small changes.

Our clock is ticking! **_We needed_** **_to_** **_iterate faster than that if we were going to_** **_avert the apocalypse._**

### 🐶 I have no idea what I’m doing 🧪

Reading complicated legacy Rails code often raises questions that take surprising amounts of effort to answer.

- Is this method dead code? If not, who is calling this?
- Are we ever entering this conditional? When?
- Is this function talking to the database?
- Is this function _intentionally_ talking to the database?
- Is this function only _reading_ from the database or also _writing_ to it?

It isn’t even clear _what_ code was running. There are a few features of Ruby (and Rails) which optimize for _writing_ code over _reading_ it. We did our best to unwrap this type of code:

Rails provides devs the ability to wrap functionality in hooks. `before_` and `after_` hooks let devs write setup and tear-down code once, then forget it. However, the existence of these hooks means calling a method might _also_ evaluate code defined in a different file, and you won’t know about it unless you explicitly look for it. _Hard to read!_

Complicating things further is Ruby’s dynamic dispatch based on subclassing and polymorphic associations. Which `load_students` am I calling? The one for `Quiz` or the one for `Practice`? They each implement the `Assignment` interface but have pretty different behavior! And: they each have their own set of hooks🤦. Maybe it’s something completely different!

And then there’s `ActiveRecord`. `ActiveRecord` makes it easy to write queries — a little too easy. It _doesn’t_ make it easy to know where queries are happening. It’s ergonomic that we can tell `ActiveRecord` what we need, and let it figure how to fetch the data. It’s less nice when you’re trying to find out where in the code your queries are happening and the answer to that question is, “absolutely anywhere”. We want to know _exactly what queries_ are happening on these code paths. `ActiveRecord` doesn’t help.

### 🧵A rich history

A final factor that makes working in Quiz Engine code daunting is the sheer size of the beast. The Quiz Engine has grown organically over many years, so there’s a lot of functionality to be aware of.

Because the Quiz Engine itself has been hard to change for a while, APIs defined between bits of Quiz Engine code often haven’t evolved to match our latest understanding. This means understanding the Quiz Engine code requires not just understanding what it does today, but also how we thought about it in the past, and what (partial) attempts were made to change it. This increases the sum of Quiz Engine knowledge even further.

For example, we might try to refactor a bit of code, leading to tests failing. But is this conditional branch ever reached in production? 🤷

## Enough complaining. What did we do about it?

We knew this was going to be a huge project, and huge projects, in the best case, are shipped late, and in the average case don’t ever ship. The only way we were going to have confidence that our work would ever see the light of day was by doing the riskiest, hardest, scariest stuff first. That way, if one approach wasn’t going to work, we would find out about it sooner and could try something new before we’d over-invested in a direction.

So: where is the risk? What’s the scariest problem we have to solve? History dictates: The more we change the legacy system, the more likely we’re going to cause regressions.

So our first task: cut away the part of the Quiz Engine that performs database queries and port this logic to a separate service. Henceforth when Rails needs to read or change Quiz Engine data, it will talk to the new service instead of going to the database directly.

Once the legacy-code risk has been minimized, we would be able to focus on the (still challenging) task of changing where we store Quiz Engine data from single-database MySQL to something horizontally scalable.

### ⛏️ Phase 1: Extracting queries from Rails

#### 🔪 Finding out where to cut

Before extracting Quiz Engine MySQL queries from our Rails service, we first needed to know where those queries were being made. As we discussed above this wasn’t obvious from reading the code.

To find the MySQL queries themself, we built some tooling: we monkey-patched `ActiveRecord` to warn whenever an unknown read or write was made against one of the tables containing Quiz Engine data. We ran our monkey-patched code first in CI and later in production, letting the warnings tell us where those queries were happening. Using this information we decorated our code by marking all the reads and writes. Once code was decorated, it would no longer emit warnings. As soon as all the writes & reads were decorated, we changed our monkey-patch to not just warn but fail when making a query against one of those tables, to ensure we wouldn’t accidentally introduce new queries touching Quiz Engine data.

#### 🚛 Offloading logic: **Our first approach**

Now we knew where to cut, we decided our place of greatest risk was moving a single MySQL query out of our rails app. If we could move a single query, we could move all of them. There was one rub: if we _did_ move all queries to our new app, we would add a lot of network latency. because of the number of round trips needed for a single request. Now we have a constraint: Move a single query into a new service, but with very little latency.

How did we reduce latency?

1. Get rid of network latency by getting rid of the network — we hosted the service in the same hardware as our Rails app.
2. Get rid of protocol latency by using a dead-simple protocol: socket communication.

We ended up building a socket server in Haskell that took data requests from Rails, and transformed them into a series of MySQL queries, which rails would use to fetch the data itself.

#### 🛸 Leaving the Mothership: Fewer Round Trips

Although co-locating our service with rails got us off the ground, it required significant duct tape. We had invested a lot of work building nice deployment systems for HTTP services and we didn’t want to re-invent that tooling for socket-based side-car apps. The thing that was preventing the migration was having too many round-trip requests to the Rails app. How could we reduce the number of round trips?

As we moved MySQL query generation to our new service, we started to see this pattern in our routes:

    MySQL Read some data       ┐
    Ruby  Do some processing   │ candidate 1 for
    MySQL Read some more data  ┘ extraction
    Ruby  More processing
    MySQL Write some data      ┐
    Ruby  Processing again!    │ candidate 2 for
    MySQL Write more data      ┘ extraction

To reduce latency, we’d have to bundle reads and writes: In addition to porting reads & writes to the new service, we’d have to port the ruby logic _between_ reads and writes, which would be a lot of work.

What if instead, we could _change_ the order of operations and make it look like this?

    MySQL Read some data       ┐ candidate 1 for
    MySQL Read some more data  ┘ extraction
    Ruby  Do some processing
    Ruby  More processing
    Ruby  Processing again!
    MySQL Write some data      ┐ candidate 2 for
    MySQL Write more data      ┘ extraction

Then we’d be able to extract batches of queries to Haskell and leave the logic behind in Rails.

One concern we had with changing the order of operations like this was the possibility of a request handler _first_ writing some data to the database, then reading it back again later. Changing the order of read and write queries would result in such code failing. However, since we now had a complete and accurate picture of _all_ the queries the Rails code was making, we knew (luckily!) we didn’t need to worry about this.

Another concern was the risk of a large refactor like this resulting in regressions causing long feedback cycles and breaking the Quiz Engine. To avoid this we tried to keep our refactors as dumb as possible: Specifically: we mostly did a lot of inlining. We would start with something like this

    class QuizzesControllller < ActionController
      def show
        quiz = load_quiz! # here are queries sometimes
        quiz_type = which_quiz(quiz) # and here other times
      end
    end

and we would aggressively inline functions to surface where and why we were querying

    class QuizzesControllller < ActionController
      def show
        quiz = Quiz.find(quiz_id_param)
        quiz_type =
          if quiz.for_credit?
            :for_credit
          else
            load_practice_quiz_type
          end
      end
    end

and again, and again

    class QuizzesControllller < ActionController
      def show
        quiz = Quiz.find(quiz_id_param)
        quiz_type =
          if quiz.for_credit?
            :for_credit
          else
            how_much_fun = QuizForFun.find(quiz_id_param)
            if how_much_fun > 9000
              :super_saiyan
            else
              load_sub_syan_fun_type # TODO: inline me
            end
          end
      end
    end

These are refactors with a relatively small chance of changing behavior or causing regressions.

Once the query was at the top level of the code it became clear when we needed data, and that understanding allowed us to push those queries to happen first.

e.g. from above, we could easily push the previously obscured `QuizForFun` query to the beginning:

    class QuizzesControllller < ActionController
      def show
        quiz = Quiz.find(quiz_id_param)
        how_much_fun =
          if quiz.for_credit?
            nil
          else
            QuizForFun.find(quiz_id_param)
          end
        quiz_type = if quiz.for_credit?
            :for_credit
          elsif how_much_fun > 9000
            :super_saiyan
          else
            load_sub_syan_fun_type # TODO: inline me
          end
      end
    end

You might expect our bout of inlining to introduce a ton of duplication in our code, but in practice, it surfaced a lot of dead code and made it clearer what the functions we left behind were doing. That wasn’t what we set out to do, but still, nice!

## 👛 Phase 2: Changing the Quiz Engine datastore

At this point all interactions with the Quiz Engine datastore were going through this **_new_** Quiz Engine service. Excellent! This means for the second part of this project, the part where we were actually going to avert the MySQL apocalypse, we wouldn’t need to worry about our legacy Rails code.

To facilitate easy refactoring, we built this new service in Haskell. The effect was immediately noticeable. Like an embargo had been lifted, from this point forward we saw a constant trickle of small productive refactors get mixed in the work we were doing, slowly reshaping types to reflect our latest understanding. Changes we wouldn’t have made on the Rails side unless we’d have set aside months of dedicated time. Haskell is a great tool to use to manage complexity!

The centerpiece of this phase was the architectural change we were planning to make: switching from MySQL to a horizontally scalable storage solution. But honestly, figuring the architecture details here wasn’t the most interesting or challenging portion of the work, so we’re just putting that aside for now. Maybe we’ll return to it in a future blog post (sneak peek: we ended up using Redis and Kafka). Like in step 1, the biggest question we had to solve was **_“how are we going to make it safe to move forward quickly?”_**

One challenge was that we had left most of our test suite behind in Rails in phase one, so we were not doing too well on that front. We added Haskell test coverage of course, including many **_golden result tests_** which are worth a post on their own. Together with our QA team we also invested in our Cypress integration test suite which runs tests from the browser, thus integration-testing the combination of our Rails and Haskell code.

Our most useful tool in making safe changes in this phase however was our production traffic. We started building up what was effectively a parallel Haskell service talking to Redis next to the existing one talking to MySQL. Both received production load from the start, but until the very end of the project only the MySQL code paths’ response values were used. When the Redis code path didn’t match the MySQL, we’d log a bug. Using these bug reports, we slowly massaged the Redis code path to return identical data to MySQL.

Because we weren’t relying on the output of the Redis code path in production, we could deploy changes to it many times a day, without fear of breaking the site for students or teachers. These deploys provided frequent and fast feedback. Deploying frequently was made possible by the Haskell Quiz Engine code living in its own service, which meant deploys contained only changes by our team, without work from other teams with a different risk profile.

## 🥁 So, did it work?

It’s been about a month since we’ve switched entirely to the new architecture and it’s been humming along happily. By the time we did the official switch-over to the new datastore it had been running at full-load (but with bugs) for a couple of months already. Still, we were standing ready with buckets of water in case we overlooked something. Our anxiety was in vain: the roll-out was a non-event.

Architecture, plans, goals, were all important to making this a success. Still, we think the thing most crucial to our success was continuously improving our feedback loops. Fast feedback (lots of deploys), accurate feedback (knowing **all** the MySQL queries Rails is making), detailed feedback (lots of context in error reports), high signal/noise ratio (removing errors we were not planning to act on), lots of coverage (many students doing quizzes). Getting this feedback required us to constantly tweak and create tooling and new processes. But even if these processes were sometimes short-lived, they've never been an overhead, allowing us to move so much faster.