{
  "id": 20637471,
  "title": "Avatar Upload Replacement: A Safe Unique-Key Strategy for Object Storage Races",
  "url": "https://urgent.news/2026/11/18/avatar-upload-replacement-a-safe-unique-key-strategy-for-object",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-18T18:41:15.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/tatefletcher6754/avatar-upload-replacement-a-safe-unique-key-strategy-for-object-storage-races-2448"
  },
  "original_language": "en",
  "account": "Replacing an avatar in object storage can be tricky due to the failure mode where writing the same object key results in the last write winning without any versioning to recover previous data. To avoid this, upload every new avatar under a unique key, update the database pointer only after verifying the upload, and delete the old key asynchronously in the background. By treating the database as the owner of identity and each object key as immutable application history, a profile read follows the current pointer while an audit job can still locate the exact object selected at that time.\n\nTo prevent object storage overwrite races, ensure the key is unique per upload, not merely unique per user. Using a UUID as the key, such as avatars/{userId}/{uuid}.jpg, helps avoid filename collisions and retry issues. The write sequence should be straightforward: accept the upload, create a new key, store the bytes, and optionally check if the object exists. Set the users.avatar_key in a single database transaction, queue the deletion of the previous key, and ensure a failed cleanup doesn't roll back a successful profile update.\n\nInfrai offers a simple way to handle storage calls over plain HTTP, providing request and response schemas for a small upload worker to inspect the contract without needing an SDK. A TypeScript client can be created to perform the upload steps, using an explicit method, fetching the key from the environment, allowing retries with an idempotency key, and implementing exponential backoff on 429 responses instead of overloading the service.",
  "summary": "Replacing an avatar is a small feature with a surprisingly sharp failure mode. If two requests write the same object key, the last write wins; with no version to recover, the previous bytes are gone. Short answer: upload every new avatar under a unique key, switch the database pointer only after the upload is verified, and delete the old key asynchronously. That is the useful before/after mental…",
  "key_points": [
    "Replace avatars with unique keys to avoid overwrite races",
    "Use UUIDs like avatars/{userId}/{uuid}.jpg for unique keys",
    "Update database pointer after verifying upload, delete old key asynchronously"
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}