Using a Cloud Function as proxy for Firestore...but also with caching...?

1,453 views
Skip to first unread message

Bradley Coffield

unread,
Nov 5, 2019, 12:42:48 AM11/5/19
to Firebase Google Group
Hi all,

I was going to create a little proxy server thing using cloud functions to make the read/write access to firestore a little more secure but also, and more important for me, to use the cloud function to cache API call results for some period of time. I'd like to cut down on the number of reads from the database which are currently, despite very small scale, in the thousands. The database doesn't change frequently.

I'd built something in the past to do something similar with a different database/api calls. I built a little node/express thing and hosted it on Heroku and used the node-cache package to achieve what I was looking for. I'm hoping to keep everything in the firebase silo with this project but I'm not thinking there's a way to do what I want.

So... can you cache the results from firestore calls in a cloud function?

Thanks!

Brad

Sam Stern

unread,
Nov 5, 2019, 1:10:31 AM11/5/19
to Firebase Google Group
Hi Bradley,

If these are HTTP functions you can integrate them with Firebase Hosting:

Then you can use Hosting's CDN to get configurable and performant caching:

- Sam

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/ca58659b-f5db-4121-90e3-f16fc00330ab%40googlegroups.com.

Bradley Coffield

unread,
Nov 5, 2019, 11:10:25 AM11/5/19
to fireba...@googlegroups.com
Interesting! Thank you, I'll take a look.

Doug Stevenson

unread,
Nov 5, 2019, 3:56:13 PM11/5/19
to Firebase Google Group
Bear in mind that if you put a function in front of Firestore, you'll be missing out on Firestore's own client side caching for web and mobile SDKs.  For a full discussion, read this.  If you can express your security concerns as security rules, you're probably better off using those instead of incurring the cost of both a Cloud Firestore query in addition to a Cloud Functions invocation.  But without knowing all your details, it's not possible to say for sure.

Doug

On Tuesday, November 5, 2019 at 5:10:25 PM UTC+1, Bradley Coffield wrote:
Interesting! Thank you, I'll take a look.

On Mon, Nov 4, 2019, 11:10 PM 'Sam Stern' via Firebase Google Group <fireba...@googlegroups.com> wrote:
Hi Bradley,

If these are HTTP functions you can integrate them with Firebase Hosting:

Then you can use Hosting's CDN to get configurable and performant caching:

- Sam

On Mon, Nov 4, 2019, 9:42 PM Bradley Coffield <bcof...@gmail.com> wrote:
Hi all,

I was going to create a little proxy server thing using cloud functions to make the read/write access to firestore a little more secure but also, and more important for me, to use the cloud function to cache API call results for some period of time. I'd like to cut down on the number of reads from the database which are currently, despite very small scale, in the thousands. The database doesn't change frequently.

I'd built something in the past to do something similar with a different database/api calls. I built a little node/express thing and hosted it on Heroku and used the node-cache package to achieve what I was looking for. I'm hoping to keep everything in the firebase silo with this project but I'm not thinking there's a way to do what I want.

So... can you cache the results from firestore calls in a cloud function?

Thanks!

Brad

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fireba...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fireba...@googlegroups.com.

Bradley Coffield

unread,
Nov 6, 2019, 1:29:34 AM11/6/19
to fireba...@googlegroups.com
I read that article! It was very compelling and made me worry setting something up to cache myself was a bad idea (and it likely is). I guess beyond cutting down on calls to the database (though you make a fair point about cloud functions invocations) I was also thinking about security. The pages that want the data from my database are completely public without users needing to sign in etc. There doesn't seem to be a way to write a rule that helps secure the database in an instance like that or am I incorrect? Currently I have writes secured to just a few logged in users (from a crud app I built for managing our data) but have the security rules set to anyone can read the database.... Thanks for your help.

To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/103d88eb-aed3-4fa7-8d51-50b2b3a579db%40googlegroups.com.

Doug Stevenson

unread,
Nov 6, 2019, 5:50:03 AM11/6/19
to fireba...@googlegroups.com
It's not necessarily a bad idea to put a function in front of Firestore.  It just requires some thought to figure out if there is any benefit to doing so.

If your data must be public, I don't think there's anything security rules can do to help.  Anyone could effectively bypass the client SDK cache, as well as use the REST API to query directly and incur document reads.  This situation improves with a Firebase Hosting cache, though I'm not sure if clients can send headers to tell Hosting not to serve from its cache.

Doug

You received this message because you are subscribed to a topic in the Google Groups "Firebase Google Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/firebase-talk/m3jZ9o9jhgk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/CAGxZ1ycvp6M_X0z7r79PuUJb08kevLjLWxJtGi1rxc%3DgVh6AAA%40mail.gmail.com.

Bradley Coffield

unread,
Nov 6, 2019, 1:25:41 PM11/6/19
to fireba...@googlegroups.com
Thank you very much for your input. It's helpful and appreciated!

Reply all
Reply to author
Forward
0 new messages