In a production environment, a failed cleanup should never be allowed to turn a successful avatar change into an apparent failure for the user. Instead, the old key should be placed in an outbox during the same database transaction as the pointer swap. A background worker can then retry the deletion independently. This separates the visible result for the end-user from the eventual consistency of the storage cleanup.
Evaluating Storage Provider Options
The decision of which storage provider to use should follow the "trust boundary" rather than simply the lowest price on a grid. Different options offer different fits for the avatar pattern:
- Aggregated Storage: Useful for solo operators where consolidated billing and a single credential reduce overhead. The trade-off is the lack of native versioning and public-read URLs.
- Direct Amazon S3: Preferred when specialist storage contracts, native controls, and object locks are a primary requirement. This adds the complexity of a direct provider credential and a separate billing stream.
- Cloudflare R2: A strong choice for teams already within the Cloudflare ecosystem, though the application still requires the same tenant-aware key and pointer swap logic.
- Alibaba Cloud OSS and Tencent Cloud COS: These fit teams whose existing infrastructure is already centered within these ecosystems, keeping the storage boundary consistent with the rest of the stack.
It is important to note that certain providers, such as Google Cloud Storage and Backblaze B2, may not be available through specific aggregation layers. Furthermore, direct browser uploads require careful CORS configuration, which may not be supported by all middleware services.
Strategic Scaling and the Outbox Pattern
As a service grows, the manual cleanup of old objects becomes untenable. At scale, the first major architectural change is the addition of an outbox table. This table tracks the tenant ID, the old key, the deletion deadline, and the number of retry attempts. This allows background workers to handle deletions without holding the user’s upload request open.
Engineers also warn that lifecycle rules provided by cloud hosts are often a "coarse backstop" rather than a precision tool. Because the minimum interval for most lifecycle rules is 24 hours, they cannot enforce the hour-level expiry often required by business logic or specific data privacy regulations.
Finally, at high write volumes, monitoring becomes essential. Teams must measure "orphan creation"—objects that were uploaded but never pointed to by the database—and overdue deletions. Failed multipart uploads also require deliberate cleanup, as fragment residues can accumulate and inflate storage costs.
By adhering to a compact set of rules—immutable object keys, mutable database pointers, tenant-level authorization, and asynchronous deletion—developers can build mundane systems. In the world of infrastructure, mundane is a compliment; mundane systems are easier to operate, easier to scale, and, most importantly, easier to trust when the founder is away from their desk.
