Mongo Db is a great tool to store Data as a key value pair in no SQL format , we can even put files in binary format like Images , Videos , Docs etc but there is an limit of 16 mb file size that we can put into the single mongo Db document . MongoDb Gridfs solves that problem by distributing the file into smaller chunks and linking it together . How does it perform that action can be checked here or the topic for another blog post . Here we will discuss its comparison with Amazon S3 file storage .
So which one is better: MongoDb GridFS or Amazon S3?
“Well depends on the Use Case“
Hmm , I know it might seems tempting but really it depends on the Use case of yours , what we can do is dive into various point wise comparison which will help you taking a good decision
- Storage Size :- As you already might have guessed it , in Mongo db GridFs Any file would take a larger amount of size to be stored into the database . So what is the reason for the same ? Well it is because of how Mongodb GridFs works . It basically divides a file into smaller chunks with a default size of 256KB and stores it into the fs.chunks and its metafile data into fs.files collection . So each chunk would need some extra memory to store metadata along with the whole file metadata with various chunks information in fs.files. On the other Hand Amazon S3 works in a different way which brings the file size difference . As a result Any file stored in MongoDb GridFs would Consume more memory as that of Amazon S3
Image Src: – Full Stack Academy
- Cost :- If we assume that we will use amazon for both i.e hosting our MongoDb server on AWS and using AWS S3 then definitely AWS s3 is a clear win here. AWS EBS can cost as much as double as that of Amazon S3 and bandwidth would be of the same price . However if you are Hosting on dedicated servers or somewhere else It can be a game changing as in that case using Mongodb GridFs can be quite cheap both Hosting wise and Bandwidth cost wise . However that is mainly due to the fact of HIGH AWS costs compared to dedicated servers (as much as 1000 times if we compare bandwidth ), if you know how to set up and manage those . If you want we can do the same for you . If you are thinking I am doing self promotion !! Yesss I am ha ha . Now move to the next point.
- Scalability :- AWS s3 is a managed service which is build for scalability under the hood scalable infrastructure but in case of mongodb as it would be a self hosted / managed solution then we would be responsible for scaling the same as it would be in the case of normally scaling a nosql database via load balancers , proper collections arrangement and partitioning .
Conclusion :- AWS S3 is built for file storage and performs better in almost all aspects but if you are worried about High AWS bandwidth costs Mongo Db Grid FS on self managed servers would be a way to go . If you need any help from us we can do the same for you . However there are other S3 protocol supported open source solutions also which can be self managed and very much cost effective .