I thought that by storing base64 string images, it would be a lot faster, because you don't need to upload neither download anything, and also a string would occupy less storage than an actual image!
I searched about this and I don't get why but everyone is saying that this would be a bad decision for the opposite of everything that I've said and I don't understand why?
CodePudding user response:
it would be a lot faster
No, it would be slower (at scale) because you'd have to pay for the cost of the transformation to base64, and it would increase the amount of data to transfer.
you don't need to upload neither download anything
It's not clear to me why you'd say that. Any time data is transferred, it could be considered an "upload" or "download". When you load or save data in a cloud service, data transfer is always going to happen somehow. You can't avoid that.
a string would occupy less storage than an actual image!
A base64 encoding always increase the size of some data. I suggest reading about it to learn what it actually does to the data and why it's even used in the first place.
There are other reasons why specifically you would not want to store binary data in Firestore:
- You are limited to 1MB max per document, which is not really that much, especially for digital photography. That's in addition to the other fields in the document.
- Your code is forced to read the entire document any time it's read. So if you have an image in a document, but you need to read that document for reasons other than loading the image, you are paying for time and cost of data you don't need, and slowing down your app at the same time.
- By default, Firestore will try to index the field with the binary data, which means you are also paying for extra index storage that wouldn't help any queries at all. You'd have to remember to disable indexing on the field with the image to avoid this.
- Firestore simply costs more than Cloud Storage for combined storage and egress. Developers choose Cloud Storage because it really is more efficient in every way for large binary blobs of data - that's why it exists.
CodePudding user response:
Storing the image data as a base64 string in Firestore is going to be significantly more expensive than storing the same image data in a dedicated file/blob storage such as Cloud Storage.
If you look at the pricing pages for Cloud Storage and Firestore, you can see that storing the same amount of data in Firestore will be about 10x more expensive. Worse: encoding the image data as a base64 string increases its size ~25% meaning that it gets even worse. The same applies if you look at the bandwidth cost for reading the data: that's about 10x more expensive on Firestore than from Cloud Storage,
Now if you were to get benefits from the increased cost that might be a good trade-off, but the extra features that Firestore offers (queryability, realtime syntax, offline access) typically don't apply to non-structured binary data.