---
title: "How we scaled the GitHub API with a sharded, replicated rate limiter in Redis"
description: "GitHub replaced a Memcached-based API rate limiter, which suffered evictions and could not survive a move to per-datacenter caches, with application-sharded Redis clusters (one primary, several read replicas) and atomic Lua scripts, rolled out behind a feature flag."
url: "https://swe.quest/articles/how-we-scaled-the-github-api-with-a-sharded-replicated-rate-limiter-in-redis-c2f9366d9d874889"
canonical: "https://github.blog/engineering/infrastructure/how-we-scaled-github-api-sharded-replicated-rate-limiter-redis/"
source: "GitHub Engineering"
published: "2021-04-05"
difficulty: "medium"
kind: "Case study"
reading_minutes: 9
tags: ["caching","replication","api-design","scalability","ruby"]
---

# How we scaled the GitHub API with a sharded, replicated rate limiter in Redis

A post on [GitHub Engineering](https://swe.quest/sources/github.md), published 2021-04-05, 9 min read, Case study. Original: [https://github.blog/engineering/infrastructure/how-we-scaled-github-api-sharded-replicated-rate-limiter-redis/](https://github.blog/engineering/infrastructure/how-we-scaled-github-api-sharded-replicated-rate-limiter-redis/)

## Summary

GitHub replaced a Memcached-based API rate limiter, which suffered evictions and could not survive a move to per-datacenter caches, with application-sharded Redis clusters (one primary, several read replicas) and atomic Lua scripts, rolled out behind a feature flag. Two bugs followed. Deriving X-RateLimit-Reset from Redis TTL plus Ruby's clock made the header 'wobble' by a second. Lazy key expiration on replicas made reads return stale windows, so clients were rejected while seeing a full remaining quota. The fix was to store the reset-at timestamp explicitly, treat expired data as absent in application logic, keep Redis TTL only as cleanup one second later, and reuse the first read's result for response headers instead of making a second call.

Written by swe.quest, not by the author.

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

## Topics

- [Caching](https://swe.quest/tags/caching.md) (Backend & APIs)
- [Replication](https://swe.quest/tags/replication.md) (Distributed systems)
- [API design](https://swe.quest/tags/api-design.md) (Backend & APIs)
- [Scaling](https://swe.quest/tags/scalability.md) (Distributed systems)
- [Ruby](https://swe.quest/tags/ruby.md) (Languages)

## Prerequisites

- Redis basics ([easy posts on Caching](https://swe.quest/tags/caching.md?difficulty=easy))
- rate limiting concepts
- primary/replica replication ([easy posts on Replication](https://swe.quest/tags/replication.md?difficulty=easy))

## Keywords

Redis, Lua, Memcached, GitHub API, X-RateLimit-Reset, Timecop

## Related posts

- [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.
- [How we improved availability through iterative simplification](https://swe.quest/articles/how-we-improved-availability-through-iterative-simplification-1aa63e0def5687e6.md): GitHub Engineering · 2024-07-23 · Medium · 6 min read · Performance engineering, MySQL, Ruby, Observability, Deployment
  GitHub cut timeouts in its Rails monolith with small, measured changes found through Datadog metrics, Splunk logs and custom MySQL slow-query monitors.
- [Interning at incident.io: rate limiting, resiliently](https://swe.quest/articles/interning-at-incident-io-rate-limiting-resiliently-703eb65d0ab42974.md): incident.io · 2026-09-02 · Medium · 10 min read · Resilience, Algorithms, Kubernetes, Engineering culture
  incident.io made its rate limiter keep working when its Valkey backing store (Google Cloud Memorystore) is unavailable, instead of failing open.
- [We replaced Redis with MySQL for inventory reservations—and it scaled](https://swe.quest/articles/we-replaced-redis-with-mysql-for-inventory-reservations-and-it-scaled-2f8fab4ab3565f02.md): Shopify Engineering · 2026-05-12 · Medium · 10 min read · MySQL, E-commerce, Concurrency, Scaling, Performance engineering
  Shopify moved checkout inventory reservations from Redis to MySQL by modeling one row per sellable unit and using SELECT ...
- [Partitioning GitHub’s relational databases to handle scale](https://swe.quest/articles/partitioning-github-s-relational-databases-to-handle-scale-7b5ea75acfc088c0.md): GitHub Engineering · 2021-09-27 · Medium · 9 min read · MySQL, Distributed databases, Scaling, Schema migrations, Ruby
  GitHub split its monolithic \`mysql1\` MySQL cluster by first virtually partitioning tables into schema domains (declared in YAML) and enforcing boundaries with query and transaction linters, then physically moving domains to new clusters via Vitess vertical sharding or a custom ProxySQL/GTID-based write-cutover script.

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)
