Scalability Tip: Move business logic out of DB

Peter Lin
tech-at-instacart
Published in
2 min readMay 8, 2018

--

Instacart has been growing tremendously. Growth means more traffic to our apps, putting a considerable load on our API, and of course, our database. In our effort to scale, we have found that moving business logic out of the database and, instead, running the logic through code worked well.

Here’s a simple example demonstrating this tip: Instacart offers an annual delivery subscription called Express. Customers can save money by buying an Express subscription, offered in multiple pricing tiers, including regular, business, and trial. Naturally, we have a Subscription model, which looks something like this:

Since there are so many subscription tiers, over time we ended up with lots of helper methods defined on the User class, like so:

As it turns out, it is not uncommon for us to call many of these methods in one request, and unfortunately, it meant that we were doing multiple DB queries on the subscriptions table per request. Since the query itself is different for each of these methods, query caching isn’t able to help us out. Doing multiple queries slows down the request and increases load on the DB, so this is bad for us and bad for our customers.

Luckily, one thing we do know is that customers are very unlikely to have multiple active subscriptions at a time. We can take advantage of this fact by loading all current subscriptions and filter run the filtering logic in Ruby, which looks something like this:

With this quick change, we have reduced n queries down to one. Even as we add more tiers in the future, we won’t have to do more than one query. As a bonus, all current subscriptions are loaded should you need to access them.

I hope you have found this post interesting. For more posts like this, check out Instacart’s tech blog. We are also scaling our engineering team: if you are interested in building great APIs, creating magical consumer app experiences, scale infra, or anything engineering, we’re hiring :)

My thanks to Kaushik Gopal, Michael Scheibe, Dan Loman, and Zain Ali for reading a draft of this post.

--

--