File Uploads and Background Jobs
Uploads and slow tasks should not tie up your API. Production systems upload files straight to object storage with pre-signed URLs and move slow work such as emails, image processing and reports to background jobs.
What you will learn
- Compare multipart uploads through the server with pre-signed URLs
- Validate file type and size safely
- Move slow work to a job queue such as BullMQ
- Design retries and idempotent jobs
Key concepts
Pre-signed URL
The API creates a short-lived signed URL for S3 (or compatible storage) and the browser uploads directly to it. Your server never handles the bytes, so it stays fast and cheap.
Job queue
The API adds a job ({ type: "send-welcome-email", userId }) to a queue backed by Redis or a broker, and returns quickly. Separate worker processes pick jobs up, retry failures with backoff, and record results.
Idempotent job
Running the same job twice gives the same result (check "already sent" before sending). Queues deliver at least once, so duplicates will happen.
Step by step
Direct-to-storage upload flow
- Client asks POST /uploads with file name, type and size
- API checks the user, type allow-list and size limit, then returns a pre-signed PUT URL and a key
- Client uploads to storage directly
- Client calls POST /uploads/complete; API verifies the object exists and queues processing (virus scan, thumbnails)
Examples
Queue a job with BullMQ
import { Queue, Worker } from 'bullmq'
const connection = { url: process.env.REDIS_URL }
export const emailQueue = new Queue('email', { connection })
// in the API
await emailQueue.add('welcome', { userId: user.id }, { attempts: 5, backoff: { type: 'exponential', delay: 2000 }, jobId: 'welcome:' + user.id })
// in a separate worker process
new Worker('email', async (job) => {
const user = await db.user.findUnique({ where: { id: job.data.userId } })
if (user.welcomeSentAt) return // idempotent
await mailer.send(user.email, 'Welcome!')
await db.user.update({ where: { id: user.id }, data: { welcomeSentAt: new Date() } })
}, { connection, concurrency: 10 })Practise it
Try these without looking back. If you can explain each one out loud, you are ready for this question.
- Explain why checking the Content-Type header alone is not enough to validate an upload.
- Design a CSV import of 1 million rows that reports progress to the user.
Interview questions
What interviewers ask next on this topic. Answer out loud first, then open the model answer.
Why use a jobId when adding a job?
A fixed jobId de-duplicates: adding the same job twice while the first one exists is ignored. Combined with idempotent handlers, it protects against double emails or double charges.
What happens to a job that keeps failing?
After the retry limit it moves to a failed (dead-letter) state. You alert on it, inspect the error, fix the cause and replay it, instead of retrying forever.
Summary
Upload straight to object storage with pre-signed URLs, keep the API fast by queueing slow work, and make jobs idempotent with retries, backoff and a dead-letter state.
Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus