How we scaled the GitHub API with a sharded, replicated rate limiter in Redis
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.
Related posts
- Making Shopify’s Flagship App 20% Faster in 6 Weeks Using a Novel Caching Solution
Shopify Engineering mediumAdeptShop'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.
- How we improved availability through iterative simplification
GitHub Engineering mediumAdeptGitHub cut timeouts in its Rails monolith with small, measured changes found through Datadog metrics, Splunk logs and custom MySQL slow-query monitors. Each change was validated with Scientist experiments or rolled out gradually behind Flipper feature flags. Reordering a Command Palette query to look up a user's contributions before filtering by org removed a large IN clause and was 80-90% faster, and batching access checks added another 20-80% and 30-40%. Skipping unneeded authorization filters on a busy redirect endpoint cut tail latency and timeouts. Not every idea paid off: moving sorting into the application was 40-80% slower, so that experiment was dropped.
- Interning at incident.io: rate limiting, resiliently
incident.io mediumAdeptincident.io made its rate limiter keep working when its Valkey backing store (Google Cloud Memorystore) is unavailable, instead of failing open. Each pod tracks the top-k busiest traffic gates in local memory and keeps an in-memory budget buffer for them, so it can grant up to its local budget and then deny. A simulation benchmark chose HeavyKeeper over Space-saving because Space-saving lost precision under high-cardinality traffic. The change was validated by killing a local Valkey cluster mid-test and was rolled out gradually by gate risk; a follow-up used a Kubernetes informer to divide the limit by the live pod count so the effective limit didn't become limit × pods. The post is framed as an intern's experience and recruiting pitch.
- We replaced Redis with MySQL for inventory reservations—and it scaled
Shopify Engineering mediumAdeptShopify moved checkout inventory reservations from Redis to MySQL by modeling one row per sellable unit and using SELECT ... FOR UPDATE SKIP LOCKED, so concurrent checkouts grab different rows without contention while reservations and the inventory ledger share ACID transactions. Key fixes included a bounded pool of 1,000 rows per item/location with locked inline replenishment, a composite primary key that halved row locks, READ COMMITTED to avoid gap/supremum locks, consistent lock ordering to prevent deadlocks, and UNION ALL batching. The real throughput ceiling turned out to be database connection exhaustion from other checkout code, found by tagging SQL with caller comments and measuring hold time in ProxySQL; cleanup cut 50% of reads and 33% of transactions, and raising InnoDB thread concurrency removed the limit. Cutover used dual-write shadow mode with a kill switch and gradual pod-by-pod rollout.
- Partitioning GitHub’s relational databases to handle scale
GitHub Engineering mediumAdeptGitHub 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. Cross-domain joins were eliminated with techniques like preload, Rails' new `disable_joins` option and application-side joins, validated with Scientist experiments. The write-cutover moved 130 of the busiest tables in tens of milliseconds with only a handful of failed writes. Query volume grew from 950k to 1.2M queries/s while average per-host load halved, reducing database-related incidents.