Programming

Express API worker OOMKilled when receiving very large JSON body — body-parser buffers whole payload

Environment

Environment

Node.js 18.16 running Express 4.18 in Docker containers limited to 1 GiB memory. Route uses express.json() (body-parser) to parse incoming application/json payloads. Clients occasionally upload large JSON payloads (~150–300 MiB).

Symptoms

Symptoms

Process exits without application-level error and container restarts. Orchestration shows container OOMKilled. No stack traces from application-level code are present because Node/V8 is killed by the kernel OOM killer when the heap exceeds available memory.

Observations

Observations

docker logs show no uncaughtException; kernel dmesg contains OOM killer killing 'node'. Increasing container memory to 2 GiB temporarily eliminates the crash for the same payload size. Switching the endpoint to a streaming parser (tested in a sandbox) avoids peak memory usage.

Already attempted

Already attempted

Increased container memory limit and set node --max-old-space-size to larger value — mitigates but not viable for steady-state. Tried to chunk uploads client-side but cannot rely on clients being changed immediately. Replacing express.json() with body parsing middleware that streams was tested successfully in a staging app.

Question

Question

What's the safest approach to avoid OOM kills from very large JSON payloads in a Node/Express API, and how do I migrate without disrupting existing clients?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Use of body-parser (express.json()) which buffers entire JSON payload into memory, causing V8 heap exhaustion and kernel OOM in constrained containers. 90%
Container memory limits too low for peak payloads and Node not configured with sufficient --max-old-space-size causing OOM. 70%
Memory leak or retained references in application code that magnify memory usage with large payloads (less likely given reproducer behavior). 25%

Summary

The immediate cause is buffering the entire JSON body in memory (express.json/body-parser), which, combined with container cgroup limits and V8 heap limits, triggers kernel OOM. This is an application design issue (synchronous full-buffer parsing) rather than a Node.js bug. Increasing memory is a stopgap; robust solutions use streaming parsing, size limits, or offloading large payloads to object storage.

Suggested Fixes

Diagnostic step 1
Enforce a maximum request size at the ingress or reverse proxy (e.g., nginx client_max_body_size or API gateway limits) to reject overly large requests gracefully. This is reversible and low-risk.
Diagnostic step 2
Replace body-parser usage for large endpoints with a streaming JSON parser (e.g., JSONStream, stream-json) that processes the request body chunk-by-chunk and avoids buffering the entire payload in memory.
Diagnostic step 3
Implement an upload-to-storage pattern: require clients to upload large JSON blobs to object storage (presigned URL), then send a small reference to the API to process asynchronously. This avoids large synchronous processing and is safer for reliability.

Continue troubleshooting

Matched from the title, description and tags