Storage consistency checks
Verifies that file storage, database records, derivatives and quotas agree through upload, processing and failure.
storage-consistency-verification
Team members using this skill
When is it useful?
Use when interrupted uploads leave missing previews or incorrect quota usage.
What does it help you do?
- Checks operation ordering across object storage and database state.
- Tests repeated processing, malformed files and concurrent quota boundaries.
- Verifies authorized preview and download across tenant boundaries.
Try asking this
Test two parallel uploads when only one fits, then verify quota and derivatives after one interrupted upload. Reserve the final quota concurrently, interrupt the winning upload and reconcile storage. No ready record may point to missing bytes, and reserved capacity must remain explainable.
Detailed documentation
Read the skill’s original instructions, examples and checks in their authored language.
Show less
Storage Consistency & Verification
Object storage (S3, GCS, Azure Blob, local disk), the relational database (PostgreSQL, SQLite, MySQL) and background job queues do not share an atomic transaction. Designing for partial failure prevents "ghost metadata" (DB row exists, file missing in storage) and "dark/orphan objects" (bytes sitting in storage forever with no DB row).
1. Ordering Operations Across Storage & Database
Follow Pat Helland's and Martin Kleppmann's patterns for cross-system mutations:
- Upload sequence (Write Metadata Intent First):
- Insert the
filesrow in state'uploading'(recording the targetstorage_keyand reserving quota) before uploading bytes to object storage. - Upload bytes to
storage_key. - Verify the object exists in storage (
HeadObject: check actual byte size, checksum and storage metadata) and transition the DB row to'uploaded'/'processing'->'ready'.
- Why: if the process crashes during step 2, the DB has a traceable
'uploading'record with the exactstorage_keyso the cleanup worker can delete the orphaned object and release the quota. If you uploaded to storage before writing to the DB, a crash leaves an invisible, untracked object in the bucket forever.
- Insert the
- Deletion sequence (Soft-Delete DB First, Purge Storage Second):
- In a DB transaction, mark the file row
status = 'soft_deleted'(or'purging'), revoke access immediately, and enqueue a durable storage-cleanup job. - The cleanup worker deletes all derived variants, multipart uploads and the primary storage object (
DeleteObjectis idempotent on404 NoSuchKey), then transitions the DB row to'purged'(or deletes it) and decrementsused_bytes.
- In a DB transaction, mark the file row
2. Idempotent Background Media Processing
Background workers can crash mid-transcode or receive duplicate job deliveries:
- Deterministic derivative keys: write derived outputs to deterministic keys (
{file_id}/derivatives/{variant}_{version}.webp) or overwrite atomically so retrying a failed job does not accumulate duplicate orphan files. - Independent step tracking: record which derivatives succeeded (
thumb_sm: ready,preview_pdf: failed) so a retry only re-runs the failed step and the user can still download the original file if a non-critical preview fails. - Dead-letter handling: after max retries on a corrupt or unsupported file, transition status to
'failed'with a clear diagnostic code rather than looping infinitely.
3. Mandatory Verification Matrix
Before completing any file or media task, verify these concrete scenarios:
- Happy path end-to-end: upload -> server verification -> derivative generation -> authorized preview/download -> deletion (confirming primary + derivatives are removed and quota is restored).
- Interrupted / abandoned upload: start an upload and abort mid-stream; verify state handling, retry/resume behavior and quota release.
- Spoofed & malformed files: upload a
.exeor<svg onload=...>renamed toavatar.png, a zero-byte file, and a truncated/corrupt image; verify magic-byte rejection and clean error reporting. - Quota boundary & concurrency: trigger parallel uploads when remaining quota only fits one file; verify atomic reservation prevents over-allocation.
- Cross-tenant access denial: attempt to finalize, download, preview or delete Tenant A's file using Tenant B's credentials; verify
403/404rejection.
Included files 1
SKILL.md
