ruby 8 lines · 1 tab

API response compression with Rack::Deflater

Alex Kumar Jan 2026
1 tab
module MyApp
  class Application < Rails::Application
    config.load_defaults 6.1

    # Enable response compression
    config.middleware.use Rack::Deflater
  end
end
1 file · ruby Explain with highlit

Large JSON payloads consume bandwidth and increase latency, especially for mobile clients on slow connections. Rack::Deflater middleware automatically compresses responses using gzip when clients send Accept-Encoding: gzip headers. This typically reduces payload sizes by 70-90% for JSON, dramatically improving transfer times. The middleware is smart enough to skip compression for small responses where overhead exceeds benefit, and it respects already-compressed content types like images. The CPU cost of compression is negligible compared to network transfer time savings. I configure deflater early in the middleware stack so it wraps the entire response, and I verify compression with browser dev tools or curl with verbose output.


Related snips

Share this code

Here's the card — post it anywhere.

API response compression with Rack::Deflater — share card
Link copied