---
title: "Upgrading 3 Million Variables to Envelope Encryption"
description: "Railway replaced direct per-variable calls to GCP KMS with envelope encryption: a locally generated AES-256-GCM data key per environment, encrypted once by KMS and serialized alongside each ciphertext."
url: "https://swe.quest/articles/upgrading-3-million-variables-to-envelope-encryption-eb068fd9d30969a4"
canonical: "https://blog.railway.com/p/envelope-encryption"
source: "Railway"
published: "2024-04-16"
difficulty: "medium"
kind: "Case study"
reading_minutes: 6
tags: ["cryptography","schema-migrations","cloud","security"]
---

# Upgrading 3 Million Variables to Envelope Encryption

A post on [Railway](https://swe.quest/sources/railway.md), published 2024-04-16, 6 min read, Case study. Original: [https://blog.railway.com/p/envelope-encryption](https://blog.railway.com/p/envelope-encryption)

## Summary

Railway replaced direct per-variable calls to GCP KMS with envelope encryption: a locally generated AES-256-GCM data key per environment, encrypted once by KMS and serialized alongside each ciphertext. Storing the encrypted DEK next to the value enabled key rotation, decryption without passing environment context, and a gradual zero-downtime migration of over 3 million variables via fallback to legacy KMS decryption. KMS usage dropped 10x, worst-case batch decryption fell from ~10s to milliseconds, quota spikes disappeared, and per-environment keys limit the blast radius of a compromise.

Written by swe.quest, not by the author.

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

## Topics

- [Cryptography](https://swe.quest/tags/cryptography.md) (Security)
- [Schema migrations](https://swe.quest/tags/schema-migrations.md) (Data & databases)
- [Cloud platforms](https://swe.quest/tags/cloud.md) (Infrastructure)
- [Security](https://swe.quest/tags/security.md) (Security)

## Prerequisites

- symmetric encryption basics ([easy posts on Cryptography](https://swe.quest/tags/cryptography.md?difficulty=easy))
- cloud key management services ([easy posts on Cloud platforms](https://swe.quest/tags/cloud.md?difficulty=easy))

## Keywords

Google Cloud KMS, envelope encryption, AES-GCM, Railway, Postgres, Vault

## Related posts

- [Incident Report: Dec 13th, 2023](https://swe.quest/articles/incident-report-dec-13th-2023-798a501388715a4a.md): Railway · 2023-12-13 · Easy · 4 min read · Incidents & postmortems, Kubernetes, Cloud platforms
  An upgrade of Railway's dashboard GKE cluster to v1.25 pulled in a metadata server version with a known high-load bug, slowing the encrypt/decrypt calls used for service environment variables.
- [A few notes on AWS Nitro Enclaves: KMS integration](https://swe.quest/articles/a-few-notes-on-aws-nitro-enclaves-kms-integration-717dffbbed378c0a.md): Trail of Bits · 2026-08-05 · Hard · 17 min read · Cryptography, Sandboxing & isolation, Cloud platforms, Identity & auth, Security
  Integrating AWS Nitro Enclaves with KMS through PCR-based key policies and attestation-encrypted responses still leaves a range of passive and active attacks on the enclave-KMS channel.
- [Incident Report: October 21st Security Attack](https://swe.quest/articles/incident-report-october-21st-security-attack-5a78ac26d27c6de1.md): Railway · 2026-11-10 · Medium · 21 min read · Incidents & postmortems, Security, Identity & auth, Multi-tenancy, CI/CD
  An attacker partially escaped filesystem isolation in Railway's multi-tenant build infrastructure and stole a build credential.
- [Workflow SDK 5: built for production](https://swe.quest/articles/workflow-sdk-5-built-for-production-8a4967cc859bc936.md): Vercel · 2026-11-12 · Medium · 9 min read · Serverless, Resilience, Performance engineering, Cryptography, AI agents
  Workflow SDK 5 reworks Vercel's durable async JavaScript workflow engine around security, regional placement and performance.
- [Why and how GitHub encrypts sensitive database columns using ActiveRecord::Encryption](https://swe.quest/articles/why-and-how-github-encrypts-sensitive-database-columns-using-activerecord-4abc36e2a491f04c.md): GitHub Engineering · 2022-10-26 · Medium · 5 min read · Cryptography, Ruby, Databases, Security
  GitHub replaced its internal Encrypted Attributes library with Rails' ActiveRecord::Encryption so developers can encrypt a sensitive column with a single familiar \`encrypts\` call, without hand-provisioning a key per column.

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)
