---
title: "Performance at GitHub: deferring stats with rack.after_reply"
description: "GitHub cut 30ms at p50 and 50ms+ at p99 from every request by buffering statsd metrics during a request and flushing them in one batch through `rack.after_reply`, a hook that runs callables after the full response reaches the client."
url: "https://swe.quest/articles/performance-at-github-deferring-stats-with-rack-after-reply-fed8f334f87dc4fe"
canonical: "https://github.blog/engineering/architecture-optimization/performance-at-github-deferring-stats-with-rack-after_reply/"
source: "GitHub Engineering"
published: "2022-04-11"
difficulty: "medium"
kind: "Case study"
reading_minutes: 3
tags: ["ruby","performance","observability"]
---

# Performance at GitHub: deferring stats with rack.after\_reply

A post on [GitHub Engineering](https://swe.quest/sources/github.md), published 2022-04-11, 3 min read, Case study. Original: [https://github.blog/engineering/architecture-optimization/performance-at-github-deferring-stats-with-rack-after\_reply/](https://github.blog/engineering/architecture-optimization/performance-at-github-deferring-stats-with-rack-after_reply/)

## Summary

GitHub cut 30ms at p50 and 50ms+ at p99 from every request by buffering statsd metrics during a request and flushing them in one batch through \`rack.after\_reply\`, a hook that runs callables after the full response reaches the client. Flamegraphs had shown statsd calls costing up to 65ms per request. Rack::Events was rejected because its callbacks blocked the response from closing. GitHub added \`rack.after\_reply\` support to Unicorn upstream and wrapped it in a small AfterResponse class. The tradeoff is that the Unicorn worker stays busy until the callables finish, which can increase HTTP queueing, so GitHub added timeouts to bound the deferred work.

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)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [Observability](https://swe.quest/tags/observability.md) (Reliability & operations)

## Prerequisites

- Rack middleware
- Ruby on Rails ([easy posts on Ruby](https://swe.quest/tags/ruby.md?difficulty=easy))
- statsd metrics ([easy posts on Observability](https://swe.quest/tags/observability.md?difficulty=easy))

## Keywords

rack.after\_reply, Unicorn, Puma, Rack::Events, statsd, maybe\_later

## Related posts

- [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.
- [Incident Report: November 20th, 2025](https://swe.quest/articles/incident-report-november-20th-2025-8a5b09711237e8e3.md): Railway · 2025-11-21 · Easy · 3 min read · Incidents & postmortems, Resilience, Deployment, Queues & messaging
  A dip in GitHub push webhooks followed by a 10x surge flooded Railway's asynchronous deployment task queue, delaying all new deployments for about two and a half hours on November 20th, 2025.
- [Node.js at scale, rebalanced: how we cut cost by 38%](https://swe.quest/articles/node-js-at-scale-rebalanced-how-we-cut-cost-by-38-1008b397ea498cc9.md): Booking.com Engineering · 2026-09-29 · Medium · 17 min read · JavaScript, Performance engineering, Cost optimization, Load balancing, Runtimes & VMs
  Booking.com cut compute costs for its largest Node.js React SSR service by 38% (30% fewer pods, 20% less memory per pod, up to 10% lower tail latency) without touching service code, by moving from pm2's cluster-based processes to wattpm worker threads and revisiting long-untouched configuration.
- [To Thread or Not to Thread: An In-Depth Look at Ruby’s Execution Models](https://swe.quest/articles/to-thread-or-not-to-thread-an-in-depth-look-at-ruby-s-execution-models-18ca691643d5cc44.md): Shopify Engineering · 2022-05-31 · Medium · 15 min read · Ruby, Concurrency, Memory management, Performance engineering, Runtimes & VMs
  Threaded Ruby servers like Puma and Sidekiq save memory over forking servers, but the gap is smaller than RSS suggests once copy-on-write sharing is measured properly with PSS and /proc/$PID/smaps\_rollup.
- [Incident Report: November 25th, 2025](https://swe.quest/articles/incident-report-november-25th-2025-0f5ac3667ca5f9ba.md): Railway · 2025-11-25 · Medium · 3 min read · Incidents & postmortems, Resilience, Scheduling
  Slow GitHub API calls (about 4x normal p95 latency) during peak hours backed up Railway's Temporal-based task queue, causing workers to run out of memory and crash in a cascading thundering-herd pattern as surviving and newly started workers were overwhelmed by the backlog.

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)
