---
title: "How to Write Fast Code in Ruby on Rails"
description: "Shopify’s guide presents performance advice for Ruby on Rails across Active Record, Rails, and Ruby, while treating speed as a feature rather than the first optimization priority. It recommends understanding Active Record’s lazy query execution, selecting fewer columns, avoiding unindexed queries, using safe indexing approaches for large tables, and treating query cache as short-lived rather than dependable. For Rails applications, it covers caching, throttling expensive or abusive operations, moving long-running work into Active Job-backed queues, and reducing dependency growth to limit boot time and memory use. Ruby-specific guidance includes limiting metaprogramming and indirection, choosing O(1) hash lookups over O(n) array searches when appropriate, and reducing allocations while avoiding harmful global mutation; benchmark figures show method-definition and invocation costs can differ."
---

# How to Write Fast Code in Ruby on Rails

[Shopify](https://yomu.fyi/company/shopify) · 2023-10-18 · Oct 8, 2019

**Type:** Tutorial

## Summary

Shopify’s guide presents performance advice for Ruby on Rails across Active Record, Rails, and Ruby, while treating speed as a feature rather than the first optimization priority. It recommends understanding Active Record’s lazy query execution, selecting fewer columns, avoiding unindexed queries, using safe indexing approaches for large tables, and treating query cache as short-lived rather than dependable. For Rails applications, it covers caching, throttling expensive or abusive operations, moving long-running work into Active Job-backed queues, and reducing dependency growth to limit boot time and memory use. Ruby-specific guidance includes limiting metaprogramming and indirection, choosing O(1) hash lookups over O(n) array searches when appropriate, and reducing allocations while avoiding harmful global mutation; benchmark figures show method-definition and invocation costs can differ.

## Context

Ruby on Rails and Ruby carry performance stigma, although Shopify reports using Rails at a scale of millions of requests per minute. The guide frames optimization as context-sensitive and cautions that speed should not take precedence over developer happiness, code correctness, or manageable technical debt.

## Approach / What changed

The guide organizes practical performance techniques across Active Record, Rails, and Ruby: inspect when SQL executes, select only needed data, index queried columns, cache stable results, throttle uncachable bottlenecks, defer long-running work to jobs, reduce dependencies, limit metaprogramming and indirection, choose suitable algorithmic complexity, and minimize allocations.

## Takeaways

- Active Record query cache can prevent repeated identical SQL within a request, but its in-memory, short-lived, and disableable nature makes it an unreliable performance foundation.
- Adding indexes to large tables can block writes; Shopify uses Large Hadron Migrator for scaling migration problems, while later PostgreSQL and MySQL versions support concurrent indexing.
- The benchmark reports normal def methods running 10.9 million times in five seconds, compared with 7.7 million for define\_method and 10.3 million for a class\_eval-defined method.

**Tags:** [Caching](https://yomu.fyi/topic/caching), [Performance](https://yomu.fyi/topic/performance), [Ruby](https://yomu.fyi/topic/ruby), [Ruby on Rails](https://yomu.fyi/topic/ruby-on-rails)

- Source: [Shopify](https://shopify.engineering/write-fast-code-ruby-rails)
- Source URL: https://shopify.engineering/write-fast-code-ruby-rails
- Ingested by Yomu: 2026-08-31T01:13:47.319Z

[Read original post](https://shopify.engineering/write-fast-code-ruby-rails)
