OOM in a Gin API Gateway: Caching Every Response Body
Reuse existing libraries; don't reinvent the wheel 😭 😭 😭 😭 😭 😭
Symptom: gateway memory spikes in minutes
We built an API gateway on top of Gin. After deploying it, we found that memory would surge in a short time and then the process would be OOM-killed. The memory trend looked like this:

Zooming in on one occurrence:

In the second chart you can see how fast memory grows: nearly 2 GB in a minute and a half.
For problems where memory spikes this fast, it's hard to catch the state in time with pprof — unless you write a script that samples pprof on a timer.
Root cause: every request cached the response body
Another pass over the code turned up the cause:
1 | package server |
At a glance, isn't this the standard way to capture the response body in Gin? After all, this is exactly how it is written on GitHub and Stack Overflow:
https://github.com/gin-gonic/gin/issues/1363
https://stackoverflow.com/questions/38501325/how-to-log-response-body-in-gin
So where is the problem?
Look at the code again: the logic keeps a complete copy of every response body in memory. For requests with large response bodies, that alone causes memory to spike — a log download, for example.
Fix: skip caching by request type
Once the cause was clear, the fix was simple: skip file-download requests based on the response headers, and skip SSE and WebSocket requests based on the request headers — streaming responses are of little value in the audit log, and they also break JSON serialization.
1 | package server |
Postmortem: when you copy code, mind your own use case
The real point of this post is not how the problem was fixed, but this:
- When you copy code, pay attention to whether it fits your context.
- Reuse existing libraries instead of rolling your own.
While hand-writing the API gateway, we also ran into the following:
- The first version of the network handling was hand-written too, which meant it handled the various Content-Types badly;
- Because we had to parse the body, and didn't have the capacity to support every compression scheme, compression had to be forcibly disabled at the gateway;
- Hand-written network handling produces strange problems
- For example: we ship a browser-based terminal that connects to a K8S cluster over WebSocket. Suppose the service behind that connection is service A (browser <----> gateway <----> service A <----> K8S cluster). Later, some requests going through the gateway would hit service A directly — skipping the gateway's API router entirely and reusing the WebSocket connection — even when those APIs did not belong to service A.
A sketch of the first hand-written version of the request handling:
1 | func proxyHttp(ctx context.Context, proxy_req *http.Request, domain string) { |
We eventually switched to the standard library's httputil.ReverseProxy for connection handling, and the problem went away.
This article is the English edition of a post originally published in Chinese.