Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Shopify Engineering 14 min read Opinion mediumAdept

The Case Against Monkey Patching, From a Rails Core Team Member

Monkey patching Rails and other gems is brittle and should be avoided. Patches break silently on upgrades because private APIs carry no deprecation warnings, they can leave apps exposed after security releases, they pile up as tech debt, and they change behavior globally. Ruby refinements are not a safe alternative: at Shopify they added 13 seconds to monolith boot time by busting method caches and are now banned. Before patching, try upgrading, recheck usage, or fix the problem upstream. If a patch is unavoidable, keep it small and version-guarded in lib/patches, call super, document and test it, and plan its removal.

Related posts

  • How We Fixed the Dependency Confusion Vulnerability in Over 600 Ruby Applications

    Shopify Engineering mediumAdept

    Shopify closed a dependency confusion hole across roughly 600 Ruby applications by migrating each `Gemfile.lock` to Bundler's newer format, which pins every gem to an explicit source. A custom Bundler plugin did the upgrade without changing dependency versions: it allowed only patch updates during regeneration, then restored the original versions. A CLI running on top of CI gave each repository its own environment, ran the migrations in parallel and opened pull requests automatically. Shopify also failed CI on outdated Bundler versions and worked with the Bundler maintainers on changes that shipped in 2.2.21 and 2.2.22, the latter preventing lockfiles from regressing to the old format.

  • Making Open Source Safer for Everyone with Shopify’s Bug Bounty Program

    Shopify Engineering mediumAdept

    A Shopify bug bounty report showed that a leaked Rails development secret could be escalated to remote code execution because ActiveSupport's MessageEncryptor deserialized data with Marshal by default. Shopify patched its app to use JSON serialization, then contributed Rails changes making JSON the default serializer for MessageEncryptor and MessageVerifier starting in 7.1, with an upgrade path for existing apps. It also shares a patch that hooks Marshal.load and Marshal.dump in development and CI to flag unexpected calls from gems, tracking and deprecating Marshal use across an application.

  • Building GitHub with Ruby and Rails

    GitHub Engineering mediumAdept

    GitHub's ~2M-line Rails monolith upgrades to the latest Rails main-branch commit every week via a scheduled GitHub Actions workflow that opens an automated PR, runs all builds, and ships the next day. CI also runs a parallel build against the latest Ruby commit, which let GitHub test Ruby 3.2 release candidates on production traffic, catch regressions such as a keyword-argument allocation increase before release, and adopt Ruby 3.2.1 on release day. Benefits include dropping nearly all Rails patches, avoiding big-bang migrations, and better security posture. The approach depends on a thorough test suite and progressive rollout deploys.

  • Shopify-Made Patterns in Our Rails Apps

    Shopify Engineering easyNovice

    Shopify's Rails monolith relies on a set of in-house patterns: the dev CLI for consistent environment setup, a podded architecture that shards the full infrastructure stack by shop, and domain-driven components that share a single database with no database-level foreign keys. Migrations are periodically squashed into structure.sql, and quality is kept up as the org grows through a git-based translation platform, an open-source maintenance tasks engine built on Active Job, and Monorail, which enforces versioned Kafka event schemas with PII annotations. It is pitched as an onboarding overview for new Shopify developers rather than a detailed implementation guide.

  • Caching Without Marshal Part 2: The Path to MessagePack

    Shopify Engineering mediumAdept

    Shopify replaced Ruby's Marshal with MessagePack as the Rails cache format for its core monolith so that code changes would stop breaking cached payloads on deploy. MessagePack only encodes types it knows about. Ruby types are added through registered extension types such as Date, Time, BigDecimal and a custom ActiveRecordCoder, which serializes records with their loaded associations and uses an instance tracker to handle circular references. The migration ran both formats side by side for about six months: payloads were prefixed with a version byte, the client fell back to Marshal when MessagePack raised NoMethodError, and each failure was logged to find missing extension types. Record payloads shrank up to 13x, total memcached usage fell by more than 25%, and decode errors from renamed classes are now treated as cache misses.

The quest for software engineering knowledge.

Look:
Layout: