Dev.to WebDev 🛠 Dev 👁 0 📖 4 min read

The one-line fix that would have hidden 110 GB of videos again

TL;DR: A folder move made 207 videos (about 110 GB) disappear from a media library, even though every file was safe. A longer timeout would have hidden the bug, not fixed it. The real fix was choosing one source of truth

TL;DR: A folder move made 207 videos (about 110 GB) disappear from a media library, even though every file was safe. A longer timeout would have hidden the bug, not fixed it. The real fix was choosing one source of truth and designing for the moment a move is only half done.

What happened

I work on a client platform with a media library: photos, videos and documents in nested folders. The files live in an S3-compatible bucket, and a Postgres database (behind Hasura) describes every folder and file.

A user moved and renamed two folders full of videos. Both requests timed out. The folders now showed up in their new place, but empty.

Nothing had been deleted. Every video was still in storage. Nobody could see them.

Why the videos disappeared

A folder's path existed in two places:

Database:  folder row → path = "Projects/Launch/"
Storage:   objects    → "Projects/Launch/intro.mp4", ...

The old move code did three things in one request:

  1. Update the folder row to its new path.
  2. Copy every file to the new path in storage, then delete the original.
  3. Update each file row.

Step 1 finished. Step 2 started copying the first video and hit the function's 6-second timeout. The request died with the folder row pointing at the new path and every file still sitting under the old one.

That alone was recoverable. What made the files vanish was the listing code. It asked storage for objects under the folder's new path and dropped any file it couldn't find there. The database said the folder had moved, storage said the files hadn't, and the listing trusted storage. So the folder looked empty.

We repaired it by hand. Later that day, one folder broke again the same way.

The one-line fix

Raise the timeout. It's the first thing anyone would suggest, human or AI, and for a small folder it would even work.

It doesn't hold up:

  • A single 3.9 GB copy takes about 65 seconds. A folder of videos can take far longer than a request is allowed to run, and API gateways cap requests at around 29 seconds by default.
  • Even when a slow move finishes, it spends minutes half done, with some files copied and some not. Files are hidden for that whole window.
  • If it fails partway, we're back where we started.

A longer timeout makes the bug rarer and harder to reproduce. It doesn't remove it.

Where should the truth live?

The bug came from two systems holding the same fact, where is this file?, with nothing to keep them in agreement during a move.

So we picked one:

The database decides where a file belongs. Storage only records where its bytes are right now.

A file belongs to its parent folder, and a folder row always holds its current path. A file's storage key can lag behind for a while, and that's fine.

The design

1. The move becomes instant and database-only. The request validates, queues a background job, updates the folder paths (and any share links to them) in the database, and returns. It never touches the files themselves. The user sees the folder in its new place straight away, with every file listed.

2. A background worker moves the bytes. The job goes onto a queue (AWS SQS), and a separate worker picks it up:

copy each file to its new path  →  verify the copy (size and checksum)
→  switch the file rows together  →  delete the old copies

Rows switch only after every copy is verified. An old copy is deleted only when no row points to it and a verified copy exists at the new path.

3. Listings ask the database where to look. If a file's bytes haven't moved yet, the listing also checks the folder they're still in. A file is never hidden just because its bytes are in transit.

Designing for the half-done state

Background jobs crash eventually: a timeout, a deploy, a network blip. So everything rests on one rule: every file row always points to bytes that exist.

If the worker crashes... What's left behind What the next run does
While copying Rows at old paths, some copies made Skips finished copies, does the rest
Before switching rows Rows at old paths, all copies made Switches the rows
Before deleting old copies Rows at new paths, old copies remain Deletes them

There's no progress table to keep in sync. Each run works out what's left by comparing every file's storage path with its folder's path. A job that keeps failing ends up in a dead-letter queue (a holding queue for failed jobs) and raises an alarm, so nothing fails silently.

What it costs

  • Storage briefly holds two copies of every file being moved.
  • A folder can't be deleted while its files are still moving.
  • A browser tab opened before the switch may fail to open a file until it reloads.
  • One move has an estimated ceiling of 25,000 to 30,000 files. That's far above the largest folder today, but it's written down as a known limit.

Each of these is a deliberate trade for one guarantee: no file is hidden mid-move, and every row points to real bytes.

What I'd ask next time

When something "disappears" but wasn't deleted, look for two places that both think they own the same fact. Then ask:

  1. Which one is the source of truth? Make the other follow it.
  2. What does the user see while the change is half done? Design that state on purpose.
  3. What happens if it fails halfway? Make every step safe to run again.

The timeout was never the problem. It was where the problem showed up.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.