---
title: "Caching Without Marshal Part 2: The Path to MessagePack"
description: "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."
url: "https://swe.quest/articles/caching-without-marshal-part-2-the-path-to-messagepack-dde2d14188f0b673"
canonical: "https://shopify.engineering/caching-without-marshal-part-two-messagepack"
source: "Shopify Engineering"
published: "2022-11-15"
difficulty: "medium"
kind: "Case study"
reading_minutes: 17
tags: ["ruby","caching","refactoring"]
---

# Caching Without Marshal Part 2: The Path to MessagePack

A post on [Shopify Engineering](https://swe.quest/sources/shopify.md), published 2022-11-15, 17 min read, Case study. Original: [https://shopify.engineering/caching-without-marshal-part-two-messagepack](https://shopify.engineering/caching-without-marshal-part-two-messagepack)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Ruby](https://swe.quest/tags/ruby.md) (Languages)
- [Caching](https://swe.quest/tags/caching.md) (Backend & APIs)
- [Refactoring & migrations](https://swe.quest/tags/refactoring.md) (Engineering practices)

## Prerequisites

- Ruby on Rails caching ([easy posts on Ruby](https://swe.quest/tags/ruby.md?difficulty=easy))
- ActiveRecord associations
- binary serialization basics

## Keywords

MessagePack, Marshal, Rails, ActiveRecord, memcached, ActiveRecordCoder

## Related posts

- [Caching Without Marshal Part 1: Marshal from the Inside Out](https://swe.quest/articles/caching-without-marshal-part-1-marshal-from-the-inside-out-57f413ca18174072.md): Shopify Engineering · 2022-11-11 · Medium · 11 min read · Ruby, Caching, Runtimes & VMs
  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.
- [Making Open Source Safer for Everyone with Shopify’s Bug Bounty Program](https://swe.quest/articles/making-open-source-safer-for-everyone-with-shopify-s-bug-bounty-program-48c56c4f3d09ee13.md): Shopify Engineering · 2022-06-15 · Medium · 5 min read · Application security, Ruby, Open source
  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.
- [Making Shopify’s Flagship App 20% Faster in 6 Weeks Using a Novel Caching Solution](https://swe.quest/articles/making-shopify-s-flagship-app-20-faster-in-6-weeks-using-a-novel-caching-100f1c4cb3fa31e6.md): Shopify Engineering · 2021-09-22 · Medium · 8 min read · Caching, Ruby, Scaling, Deployment, Performance engineering
  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.
- [The Case Against Monkey Patching, From a Rails Core Team Member](https://swe.quest/articles/the-case-against-monkey-patching-from-a-rails-core-team-member-b0bd43ce3a761b2b.md): Shopify Engineering · 2023-02-21 · Medium · 14 min read · Ruby, Refactoring & migrations, Open source
  Monkey patching Rails and other gems is brittle and should be avoided.
- [Changing a polymorphic\_type in Rails](https://swe.quest/articles/changing-a-polymorphic-type-in-rails-8b5924e77ba397de.md): Shopify Engineering · 2022-02-18 · Medium · 7 min read · Ruby, Refactoring & migrations, Schema migrations, Databases
  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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
