Skip to content

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

Shopify Engineering 17 min read Case study mediumAdept

Caching Without Marshal Part 2: The Path to MessagePack

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.

Assumes: Ruby on Rails cachingActiveRecord associationsbinary serialization basics

Related posts

  • Caching Without Marshal Part 1: Marshal from the Inside Out

    Shopify Engineering mediumAdept

    Ruby's Marshal serializes an object's full class graph into cache entries, so a refactor that renamed classes caused old and new code to choke on each other's cached beta-flag objects mid-deploy and triggered an incident at Shopify. Walking through marshal.c and real payloads, the post explains how Marshal encodes objects (TYPE_OBJECT), instance variables on core types (TYPE_IVAR), circular references (TYPE_LINK) and subclasses of String/Array/Hash/Regex (TYPE_UCLASS). These are exactly the features that break when replacing Marshal, setting up Shopify's migration of Rails cache serialization to MessagePack in part two.

  • 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.

  • Making Shopify’s Flagship App 20% Faster in 6 Weeks Using a Novel Caching Solution

    Shopify Engineering mediumAdept

    Shop's home feed, which aggregated orders and carrier tracking data and accounted for 30% of database load, was moved behind a custom write-through cache in Memcached, cutting database load by 15%, overall GraphQL latency by about 20% and CPU usage by a double-digit percentage. To handle concurrent order updates without serving stale data, each user has a pending-writes counter that workers increment before a database write and decrement after it. A short-expiry key detects crashed writers so the cache can be safely rewarmed. The logic was mixed into Active Record models as concerns, so the ORM API was unchanged. Correctness was validated in a shadow rollout that compared cache and database reads before serving from the cache.

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

    Shopify Engineering mediumAdept

    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.

  • Changing a polymorphic_type in Rails

    Shopify Engineering mediumAdept

    Rails stores the parent's class name in a polymorphic association's type column, so moving models into a new namespace (e.g. Car to Garage::Car) leaves mixed values like "Car" and "Garage::Car" and breaks lookups in both directions. Shopify's Payment Flexibility team decoupled the database from class names by overriding polymorphic_name to write arbitrary strings such as "car", then used an unscoped association scope to read both old and new values during the transition. After a backfill of existing rows, the temporary scope is removed and future renames only require updating a class-mapping hash. The approach adds complexity and is mainly worth it when module and class names are expected to change.

The quest for software engineering knowledge.

Look:
Layout: