TL;DR: Object storage is the better home for the image bytes; a relational database should keep the recovery index, ownership rules, and lifecycle state. For an e-commerce system with one backup policy per tenant, that split makes retention and deletion auditable without forcing large binary values into transactional tables. The important design choice is not storage in isolation. It is keeping a deleted or expired image from becoming an undeletable orphan, while still allowing an operator to restore one known snapshot.
I would measure restore time, orphan rate, metadata query latency, and storage operations before copying this design. The trade-off is deliberate: the database gets more coordination work, while object storage handles the payload stream and immutable-ish version history.
That boundary matters.
Why is object storage a better fit than relational databases for storing generated images?
An AI-generated image is a binary object with a key, media type, size, checksum, and usually a generation prompt or model configuration. A relational database is excellent at constraints, transactions, joins, and small mutable records. It is a poor place to make every large binary value participate in the same access and backup path as carts, orders, and tenant settings.
That does not make a database blob universally wrong. A small, low-volume application can reasonably keep everything together. The boundary becomes useful when image reads are large, images have independent retention, or a tenant needs to restore one snapshot without replaying unrelated business tables. Object storage also gives the application a natural byte-addressable key, while the database can answer questions such as “which image belonged to this tenant at snapshot 2026-10-27T03:00:00Z?”
The relational row is the control plane. It should not pretend to be the archive.
What does a tenant-safe snapshot actually contain?
A snapshot needs a stable identity and an explicit lifecycle. Here is the smallest useful shape for a Node.js service; the storage client is intentionally abstract so the policy is not coupled to one provider.
type SnapshotState = "building" | "ready" | "deleting" | "deleted"; interface ImageSnapshot { snapshotId: string; tenantId: string; objectKey: string; contentType: string; byteLength: number; sha256: string; createdAt: string; expiresAt: string; state: SnapshotState; } interface ObjectStore { put(key: string, body: AsyncIterable<Uint8Array>, contentType: string): Promise<void>; get(key: string): Promise<AsyncIterable<Uint8Array>>; delete(key: string): Promise<void>; } The tenantId belongs in the database record and in the key namespace, even though neither is an authorization decision by itself. Every restore request still needs an authorization check against the tenant. The checksum is useful for detecting an incomplete upload or a key collision; it is not proof that a user is allowed to read the image.
A snapshot should become ready only after the object upload and metadata transaction have both succeeded. A common failure is creating the row first, crashing during upload, then treating the row as restorable. The opposite ordering has a different failure: an uploaded object may have no row after a process exit. That is why reconciliation is part of the design, not an emergency script.
How should retention and deletion behave?
Retention is a policy evaluation, not a single delete call. For each tenant, calculate the protected snapshots first, then mark expired snapshots as deleting in a transaction. A worker can delete the object and finally mark the row deleted. If the worker retries, the state transition and the object-delete operation must be safe to repeat.
Do not erase the database row before the payload is accounted for. The row is the evidence needed to investigate a failed deletion, and it lets a restore API reject a snapshot that is no longer available. On the other hand, do not keep rows forever just because the object deletion failed. Keep a terminal record or deletion ledger according to the audit requirement, and alert on records that exceed the allowed deletion window.
The policy should also distinguish a tenant pause, a legal hold, and ordinary expiry. They are different reasons to retain data. A scheduled job that only checks expiresAt will eventually delete something a separate control meant to protect.
Here is a compact policy decision function. It does not perform deletion; separating decision from effect makes it testable.
type RetentionDecision = "keep" | "delete"; function decideRetention(input: { now: Date; expiresAt: Date; isProtected: boolean; isReady: boolean; }): RetentionDecision { if (!input.isReady || input.isProtected) return "keep"; return input.expiresAt <= input.now ? "delete" : "keep"; } The test cases should include a snapshot that expires during a restore, a tenant with no retained snapshot, a duplicate worker execution, and an object that exists without a matching row. These are more valuable than a happy-path test that only asserts a successful upload.
What changes during a selected restore?
Restore should be a new write, not a destructive replacement. The operator selects a snapshotId; the service verifies tenant ownership, state, retention hold, and checksum metadata, then streams the object into a new working location. Only after verification should it update the tenant's active image reference. That gives the operation a clear failure boundary: an unusable snapshot cannot silently replace the current asset.
A restore record should include the requester, source snapshot, destination key, start and completion times, and result. The image itself can remain in object storage. The relational database records the workflow so an interrupted restore can be resumed or marked failed without guessing from filenames.
This arrangement also limits blast radius. A database transaction can change the active reference, but it cannot make a half-streamed image valid. The object checksum and an atomic application-level state change have to meet before the tenant sees the restored image.
The operational checks that decide if this survives production
The useful metrics are boring: bytes uploaded and deleted, restore duration by image size, objects without rows, rows without objects, expired snapshots still present, and deletion retries. Tag these metrics with tenant class and operation outcome, but avoid putting prompts or image content into logs.
Use a reconciliation job to compare the database index with object listings within a bounded prefix. Listing is not a substitute for the index, and the index is not proof that a payload still exists. The job should produce a reviewable discrepancy report before it is allowed to delete anything.
For testing, generate small deterministic fixtures and separately test large-stream behavior. Test authorization with two tenants that use similar snapshot IDs. Test expiration around the clock boundary. Test retries after each externally visible step. The system is ready for a broader rollout when these checks are observable, not when the first restore works.
Object storage wins this scenario because it separates large, independently retained bytes from transactional business state. The database still matters: it owns identity, tenant boundaries, policy decisions, and restore state. The decision rule is straightforward: store the image where byte delivery and lifecycle can scale independently, and store the facts required to find, protect, restore, and eventually delete it where constraints are strong.


