Safe Avatar Replacement: Using Unique Storage Keys to Avoid Upload Race Overwrites
Replacing a profile avatar can fail badly when two requests write to the same object key, since the last write overwrites the earlier file with no way to recover it. The article recommends storing each new avatar under a unique key, such as one built from a UUID, rather than a per-user or filename-derived key. The database pointer to the current avatar is switched only after the upload is verified, and the previous object is deleted asynchronously. The write flow includes creating the key, uploading the bytes, checking the object exists, updating the database in one transaction, and queuing cleanup of the old key. The piece also includes a TypeScript example that uses an idempotency key and backs off on rate-limit responses, and it notes that the database, not the object store, determines which avatar is current.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in