Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it faster to upload and download images into a MongoDB than on disk

Say we want to develop a photo site.

Would it be faster to upload or download images to or from MongoDB than store or download images from disk... Since mongoDB can save images and files in chunks and save metadata.

So for a photosharing website, would it be better (faster) to store the images on a mongodb or on a typical server harddisk. etc.

im thinking of using php, codeigniter btw if that changes the performance issues regarding the question.

like image 628
Sirwan Qutbi Avatar asked Jun 29 '11 21:06

Sirwan Qutbi


People also ask

Is it good idea to store images in MongoDB?

No, MongoDB is not a good place for storing files. If you want to store files, you should use storages like Amazon S3 or Google Could Storage. The good practice is to store the files in a storage and then to just save the URL of the uploaded image in the MongoDB.

What is the best way to store images in MongoDB?

You can store the images in the MongoDB database by creating a schema with Mongoose. The schema is defined by creating a file, model. js . The data type Buffer is used for storing the images in the database form of arrays.

Is MongoDB good for storing images and videos?

GridFS is the MongoDB specification for storing and retrieving large files such as images, audio files, video files, etc. It is kind of a file system to store files but its data is stored within MongoDB collections. GridFS has the capability to store files even greater than its document size limit of 16MB.


1 Answers

Lightweight web servers (lighttpd, nginx) do a pretty good job of serving content from the filesystem. Since the OS acts as a caching layer they typically serve content from memory which is very fast.

If you want to serve images from mongodb the web server has to run some sort of script (python, php, ruby... of course FCGI, you can't start a new process for each image), which has to fetch data from mongodb each time the image is requested. So it's going to be slow? The benefits are automatic replication and failover if you use replica sets. If you need this and clever enough to know to achieve it with FS then go with that option... If you need a very quick implementation that's reliable then mongodb might be a faster way to do that. But if your site is going to be popular sooner or later you have to switch to the FS implementation.

BTW: you can mix these two approaches, store the image in mongodb to get instant reliability and then replicate it to the FS of a couple of servers to gain speed.

Some test results.

Oh one more thing.. coupling the metadata with the image seems to be nice until you realize the generated HTML and the image download is going to be two separate HTTP requests, so you have to query mongo twice, once for the metadata and once for the image.

like image 194
Karoly Horvath Avatar answered Oct 12 '22 23:10

Karoly Horvath