If you have a platform where users can have profiles -like a social network-, you're most likely finding yourself wanting to store their profile pictures somewhere. let's iterate through some of options you have to do this.
First option
Store them as binary blobs in your database. Don't do it! noone wants bulky binary blobs in their databases, they perform poorly and don't scale well.
Second option
Use a storage service like S3 and make a directory with the ID of the user, so the path will always be something like /profilepictures/{userId}
and it would be really easy to reference it in the client side. until you need to use a CDN, it won't know when to revoke the cached version because it's always the same name so the cached version will always be returned until its TTL expires (unless of course you purge it).
Third option
To be able to use the power of CDNs and also keep them updated (by updating them when a user changes their profile picture and leave them cached otherwise), what you can do is, when a user changes their profile picture
- generate a unique timestamped name for the picture by generating a UUID or something similar (which might be just a unix timestamp) and appending it to the user ID as a string before saving the file so you have something like
{userID}-70bb1914-e0bf-4c10-8a85-92157acdbf42
. - then save that generated name (or the full path if you will) in the database.
- upload the file like normal to the blob storage service (S3) at
/profilepictures/{generatedName}
. You can also delete the old picture to save up some cloud space.
This will help CDNs as well as browsers cache the files as long as they don't change.
This is it. This is the problem I have faced and how I've solved it. As always their are indeed other and better ways to do it, and as always feel free to discuss them in the comments.
No comments:
Post a Comment