# How X02 Handles Large Uploads Without Running Out of Memory

> An engineering write-up of X02's upload path: streaming straight to Cloudflare R2 in multipart pieces, why we stopped buffering files in memory, and how upload rate limits work.

Source: https://x02.me/blog/high-performance-file-uploads-architecture · 2026-08-22 · X02 Team

Accepting uploads sounds simple until a few people send big files at the same moment. X02 runs on a server with 4 GB of RAM, and files can be up to 512 MB on Pro. This post explains how the upload path is built so that memory use stays small no matter how large the file is.

---

## 1. The Problem: Buffering a File Means Holding All of It

The easy way to receive an upload in Node.js is to let the framework collect the whole file in memory and hand it over when it is complete:

```javascript
// Anti-pattern: the whole file lives in RAM until the handler finishes
const upload = multer({ storage: multer.memoryStorage() });
```

That is fine for small files. It does not survive concurrency: eight people uploading 500 MB each need about 4 GB of buffers, and on a 4 GB server the process is killed and restarted, which interrupts everyone else's requests too. We hit exactly this, and it is why uploads no longer work this way.

---

## 2. What X02 Does Instead: Stream to R2

Every upload, whatever its size, is streamed through `multer` with a custom storage engine straight into Cloudflare R2. The server never holds a whole file.

- **Multipart to R2.** The stream is cut into **5 MB parts**, with **two parts in flight** at a time, and R2 joins them into one object when the last part lands. Files smaller than one part go up as a single request.
- **Flat memory.** The upload library only holds the parts it is currently sending, so each upload costs roughly **20–30 MB** of server memory, whether the file is 50 MB or 500 MB. Eight simultaneous 500 MB uploads need on the order of 150 MB rather than 4 GB.
- **Failed uploads clean up.** If the connection drops, the partial parts are discarded rather than left in storage.
- **Images are also copied to disk as they pass through**, so the NSFW scanner can check them after the upload completes. See [how the scan works](/blog/realtime-nsfw-detection-at-scale).

```typescript
import { Upload } from "@aws-sdk/lib-storage";

const upload = new Upload({
  client: s3Client,
  params: { Bucket, Key: key, Body: incomingStream, ContentType: mime },
  partSize: 5 * 1024 * 1024, // 5 MB parts
  queueSize: 2,              // two parts in flight
  leavePartsOnError: false,  // discard partial uploads
});

await upload.done();
```

A note on what this is **not**: the browser sends each file as one request. There is no resumable, chunk-by-chunk upload protocol, so if your connection drops partway through a large file, that upload has to be started again.

---

## 3. Browser Uploads Go Straight to `up.x02.me`

The main site sits behind Cloudflare, which refuses request bodies over 100 MB. Browser uploads therefore post to a dedicated upload hostname, `up.x02.me`, which is not subject to that cap. This is how Free (200 MB) and Pro (512 MB) uploads work from the website.

---

## 4. Rate Limits

Limits are counted per account (by API key) or per IP address for anonymous visitors, using fixed time windows stored in PostgreSQL:

| Who | Limit |
|---|---|
| Anonymous (website) | 30 uploads per hour |
| Free account | 250 uploads per 24-hour window |
| Pro account | 1,500+ uploads per 24-hour window |

The daily window starts at your first upload and runs for 24 hours, rather than resetting at midnight. Uploads that fail still count.

---

## Summary

The core idea is small: never hold a whole upload in memory. Streaming into R2 multipart uploads keeps memory flat, which lets a modest server accept large files from several people at once. We do not claim huge scale. We run one server process, and this design is what keeps it stable.
