· 1 min
How Multer actually handles file uploads
Multer doesn't buffer your file into memory the way you'd guess — here's what's really happening on the wire.
By Akshay Dhanraj Kurve, software engineer
When you write upload.single('file') in an Express route, it feels like magic — a file appears in req.file and you move on. Underneath, Node is doing real stream-parsing work, and understanding it saved me from an out-of-memory crash in production.
What's actually on the wire
POST /upload HTTP/1.1
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Length: 10485760
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="balance_sheet.pdf"
Content-Type: application/pdf
[Raw Binary Stream...]
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Busboy does the real work
Multer is a thin wrapper around Busboy, a fast writable-stream parser. Instead of waiting for the whole 10MB (or 1GB) body to arrive, Busboy scans incoming chunks for the boundary string as they land, and emits a file event with a readable stream the moment it finds the headers for one.
- It intercepts the raw
http.IncomingMessagestream - A small state machine looks for the boundary delimiter across chunk boundaries
- Once headers are parsed, it yields a distinct readable stream for that file
- With
diskStorage(), that stream pipes straight intofs.createWriteStream()
Memory storage will eventually bite you
// Can OOM the process under load — the whole file sits in RAM
const upload = multer({ storage: multer.memoryStorage() });
// Streams to disk with bounded memory overhead
const upload = multer({
storage: multer.diskStorage({
destination: "/tmp/uploads",
filename: (req, file, cb) => cb(null, `${Date.now()}-${file.originalname}`),
}),
limits: { fileSize: 25 * 1024 * 1024 }, // 25 MB
});
memoryStorage() is fine for small avatar uploads. The moment users can upload anything bigger, disk storage with an explicit size limit is the difference between a slow day and a downed server.