I am using Laravel to make my project which requires extensice image uploading and image storings. Kindly tell me that what is the best way to store images so that images can Load faster.
3 Answers
The fastest possible way to store and load your images would be in main memory. However this probably isn't practical since images are large files, main memory is limited and isn't persistent between reboots. You should store them on disk.
I highly recommend against storing images as a blob in a database.

- 2,469
- 12
- 31
- 54
-
So I should store themin filesystem ? instead of storing them as blob in the database ? – Farhan Jun 29 '16 at 05:50
-
Explain your use case. What is it that you're building? – Jonathan Eustace Jun 29 '16 at 05:52
-
I am building an Application in which users would be able to make their profiles with cover photo and profile picture just like facebook. other then that users would be allowed to create a gallery of 3 images in which they can show their 3 pics. They can create as much gallery as they want. – Farhan Jun 29 '16 at 05:58
-
Then absolutely you want to store this in the file system. – Jonathan Eustace Jun 29 '16 at 06:00
-
but How does storing in the filesystem will get the images faster as compared to blob storage? as images from file system and blob will be coming from server. – Farhan Jun 29 '16 at 06:05
-
Take a look at this: http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay – Jonathan Eustace Jun 29 '16 at 06:07
Don't be afraid of putting the images on your filesystem. It's a bad practice to put it in your database directly. It'll hurt the database server. Instead, store the path of the image to the database.
Either cache it or just use CDN if you're really up for the speed. Also, be noted that CDN might not really speed up the loading in some places.
If a CDN server is on US only (for example just to get the thought) and you're on US well it'll be fast. Otherwise, it'll be slow.
It's only the location that affect speed also, the Disk speed of your server contributes to the speed too. If you're system is on an SSD, that'll be fast also.

- 218
- 4
- 13
What I would do is store them in disk and cache them in memory with an in-memory db like Redis when someone requests them and give the images in the db a really small lifetime before clearing the memory that gets renewed every time a request for that same image comes again (something easily done in redis) so that the most seen images are always cached and get to the user fast but the least used images are not using extra memory space.
You can also load them gradually if there are some long scrolls in the page so that initial feedback is a bit faster.

- 1
- 1
- 1
-
That really depends on the amount of images that are being stored and the amount of main memory on the server. Ram isn't cheap so rarely would this be a recommended solution. The ultimate question is, how important is that extra performance and how much are you willing to pay for it? – Jonathan Eustace Jun 29 '16 at 05:39
-
If he had 4GB of ram, and lets say he had no operating system or any other overhead in the ram he could only store 4000 1MB images. That shouldn't be considered a viable solution unless you're storing a very small set amount of images that will never increase. – Jonathan Eustace Jun 29 '16 at 05:41
-
thats why im saying he should give the keys in the db a lifetime, the chance of those 4000 images being seen on a regular basis is highly unlikely and if you have the amount of users that would be needed to keep them alive in memory constantly you can afford more ram. Keys would have small lifetimes so that only REALLY popular images are cached almost all the time. – Ignacio Torres Jun 29 '16 at 05:44