Hi everyone! Between storing images in a database ...
# getting-started
e
Hi everyone! Between storing images in a database and storing them in a folder and storing their URLs in a database, which one is better?
m
It depends.. When you have all in a database, you have the data close together and do not by accident move the files to a different directory, or rename them. Also, you can access the bytes and metadata in the same transaction as guaranteed by the database system.
e
and considering things like scalability and performance?
m
On the other hand, if these don't sound like drawbacks to you, you keep your database tables smaller without images, potentially increasing query performance
If you mean scalability like in, you want to write the next big photo app where you'll serve thousands of images, then look into block storage. Or in general, in a seperate service that takes care of storing individual pieces of data
Another thing to consider, if you want to access the images in your server logic, e.g. to resize them, then putting them in the database (which you query anyways) might be not too bad. If you only want to build a website and put some <img src="foo"> in there and let some webserver take care of providing the raw image file, then the folder-approach looks more attractive
e
i have plans for it to one day serve millions of people.
m
And images like loose collections of photos that can be seen by a lot of people, or very specific images for individual users?
e
that can be seen by a lot of people
m
Sounds like you can start with a folder and urls, and when you hit scaling issues move to some bigger blob store
e
thank you
c
Note that many filesystems become extremely slow when many files are in the same directory
e
what can be done when it reaches that point?
c
Stop having too many files in a single directory.
I can't give you the exact numbers, but I've seen FSs where 1k files is too many, and others where 100k files is the limit
e
what if another directory is created when the existing directories reach a certain number in files? like having one major directory and sub directories inside that contain those files
c
that will depend on the FS but I guess that should be ok most of the time
what Git does to store commits is that it takes the first two characters of the commit hash and makes that a subdirectory (
tree -L 2 .git/objects
in any Git project to see what I mean)
e
i will apply this and check on the performance .