I am developing a chat app in which I am using firestore, functions & Cloud message. I planned to reduce cost to minimum if possible.
One thing I found that whenever user will send message or ask to receive message, He must need to be authenticated first. User can be verified using token that he will get after login. I save that token in firestore document. But, While we authenticate Firestore counts one read in the document for every message sent. So I am thinking to add Memory Store to it if it is possible. But Please tell me is it going to be wise decision or any cons.
You can also suggest me some innovative ways or some threads/articles related to this.
Thank You.
2
Answers
You can use any Memcache or Redis as a distributed cache system.
Reading from cache is a lot better than a database call. If you are having an application with high rps or thinking of building the scalable one then to reduce latency you can always go with the cache.
In your case, Cloud Firestore is a NoSQL cloud database and you can go with Memorystore from GCP which supports both Redis and Memcached under the same deployed vpc.
when you get a request, you can use Memorystore cache to check whether the user or auth info regarding that is present in the cache, if so then directly mark him authenticated and redirect to the next flow. If not make a call to Firestore, populate in the cache, and then continue with the flow.
if you are using LRU cache, you can store the recent authenticated users in cache and you can auto-expire for a certain period. Hence making fewer calls to Firestore
If you are using AWS, Amazon ElastiCache provides you the same feature.
In short, it’s always better to go with a cache system since it reduces latency.
If I resume your question :
You don’t need to store nor manage the ID token by yourself, so no for firestore nor Memory Store for this requirement. If the user is signed in, you can get its ID Token from the client (web, android, ios) using this method. The ID token has a validity of one hour, so the call will get you the current ID token if it has not expired. Otherwise, the token is refreshed and you get a new one.