
Introducing Cloud Functions for Firebase - Andrex
https://firebase.googleblog.com/2017/03/introducing-cloud-functions-for-firebase.html
======
keithwinstein
This looks like a worthy competitor to AWS Lambda, but I'm disappointed by the
unalterable limit of 400 concurrent invocations of a function. (The "Quotas"
page says "Max concurrent functions" is 400, with "Can be increased" as "No."
[https://cloud.google.com/functions/quotas](https://cloud.google.com/functions/quotas))

One of the cool things about AWS Lambda is that you can use it as a short-term
supercomputer with thousands of threads. We (Stanford/UCSD ExCamera) are using
AWS Lambda to do massively parallel low-latency video encoding with 4,000+
concurrent threads. Our colleagues at U.C. Berkeley who built PyWren are doing
machine learning on Lambda with similar numbers of threads. AWS has been
encouraging to us for these kind of workloads, even if they aren't what Amazon
initially envisioned.

So a hard limit of 400 (that can't be increased) would be a real bummer. I
wonder if we can get around this by defining 20 identical functions and then
invoking each one 400 times in parallel...

~~~
metaphyze
Another disappointing limitation is being restricted to Node.js. AWS Lambda
lets you write your code in Node.js, Python, Java, or C#. This is particularly
useful for tuning your Lambdas based on their expected usage. If you have a
Lambda that is being constantly invoked, it might be better to write it in
Java. It has a longer start up time but has better performance after the
initial startup. However, if your Lambda is invoked infrequently (and thus is
frequently shutdown from inactivity), it would better to write it in Node.js
or Python to take advantage of the snappier start up time.

~~~
mbleigh
Firebase engineer here.

Additional languages will come, but we're starting with Node.js. We'd rather
take the approach of doing one language really well than a bunch of languages
with a poor developer experience.

~~~
rationalthug
How does Cloud Functions for Firebase handle node modules with native code
add-ons?

~~~
inlined
Image magick's binaries are already installed on the image. Any native binary
node module that has a preinstall script should work too. If you hit an
unsupported syscall error, contact firebase support at
firebase.google.com/contact/troubleshooting and we'll triage.

~~~
tokenizerrr
404\. That's an error.

The requested URL was not found on this server. That's all we know.

~~~
volent
[https://firebase.google.com/support/contact/troubleshooting/](https://firebase.google.com/support/contact/troubleshooting/)

------
brendanlim
(Cloud Functions for Firebase PM here)

We’ve been developing this product for years, and we’ve had it in private
alpha testing for well over a year, so we’re incredibly excited to finally
take the wrappings off and let all of you try it.

Firebase has always focused on empowering you all to build extraordinary
experiences for your users, without needing to worry about building common
infrastructure. While we’ve made big strides towards this vision in the past,
we always had one big hole: trusted code execution. Today, we’re completing
the story with Cloud Functions for Firebase so that you can easily run server-
side code in response to events from your Firebase app.

We think you’re going to love it, and we can’t wait to see what you build with
it!

~~~
drumttocs8
From an onboarding and testing perspective, it would be nice to be able to
integrate other services ("Outbound networking"?) in the free tier. Maybe
limit to one connection?

~~~
inlined
There's a bit of a nuance here. Even if you join the Blaze plan (pay as you
go) there's still a large free quota. The quota is actually much larger than
you get with Spark as well. We just can't allow arbitrary REST calls without a
credit card. This helps avoid Cloud Functions being used for abuse/malware.

------
Andrex
Thanks to the mods for un-duping this and linking to the blog post, which went
live like 7 minutes after I hit submit, haha.

Anyways, this was the last piece of the Firebase puzzle for me. Between this,
Authentication, Analytics, the DB (obviously) and Storage, Firebase has been
indispensable in getting my side project app up and running -- and most
importantly, feeling "real."

My only request at this point would be a bit more TLC given to how Firebase
integrates with React Native. I'm sure it's coming as it's likely a "still-
small-but-growing" subset of use cases. As an example, there's not an obvious
way to say, get Twitter auth working in a React Native app (last I checked a
few weeks ago.) There are workarounds, of course.

PS- The Firebase console and docs are some of the most slick and well-designed
examples of what they do I've seen. I really enjoy the web console and the CLI
tool. Just a real joy to use, and I'm not being sarcastic. :)

~~~
digitalmaster
+1 For React native support! In fact emailed tech support about just
yesterday. Got a really quick response in which they essentially said --
"....We're exploring potential solutions, but I can't share any details or
timelines at this time."

I'm optimistic. :)

~~~
Andrex
That's awesome! I might do the same thing in a bit. It's neat we can use the
Web SDK with RN (in a limited capacity), but an official (or even semi-
official) library would be the best.

------
subpixel
This was a long time coming, so congrats first.

I also recently read this from an experienced Firebase developer:
[https://medium.freecodecamp.com/firebase-the-great-the-
meh-a...](https://medium.freecodecamp.com/firebase-the-great-the-meh-and-the-
ugly-a07252fbcf15#.7duy5djwl)

Interestingly, he lists several things that he things Firebase needs in order
to be awesome, and server-functions is last on the list:

> 1\. Real querying capabilities. Search, joins, the whole enchilada.

> 2\. Some sort of references likes MongoDB or RethinkDB.

> 3\. Real offline persistence with Javascript.

> 4\. Give me moar analytics.

> 5\. A cache API.

> 6\. Some way of adding server side code without needing to resort to a VPS.

~~~
cyberferret
The extremely poor JOIN functionality is what turned me away from Firebase a
couple of years ago.

I come from 25+ years of SQL, and Firebase was my choice for essentially my
first 'real world' NoSQL project. I was enjoying it right up to the bit where
I had to extract data from 2, and 3 data tables at the same time in a single
query. Couldn't believe how verbose and painful it was and it made me drop
NoSQL for a while before more recently going back to it (with RethinkDB).

~~~
katowulf
A lot of the join functionality can now be simulated by using functions to
effectively create views or map reduce data to distilled output. We've got a
lot more to come on this front. Functions was just first on the list.

~~~
Liron
Do you think there will ever be some kind of materialized views / declarative
data denormalization?

------
chuinard
As a 2+ year user of Firebase, this is long overdue. I built my own cloud
function infrastructure using their Java SDK and Digital Ocean and I couldn't
have built my app (ProBooks.com) without it. It's good to see Cloud Functions
moving from Alpha to Beta!

~~~
kiliankoe
Just out of curiosity, what kind of functionality were you only able to build
this way?

~~~
chuinard
One feature of my app is the PDF creation of invoices and estimates. I use a
Java library to make these PDFs. Developing an iOS, web, and Android app that
syncs in real-time is much easier (or even possible) when your PDFs are made
on the backend.

------
boulos
Like many of you, I'm incredibly happy about how this:

> This has been the most requested feature since Firebase launched

is finally in place. Firebase is a great option for quickly getting something
together, and now that you can extend it with little compute, this really is a
massive expansion of what you can achieve.

Disclosure: I work on Google Cloud (and sit near the Firebase folks!).

------
aphextron
Quite exciting. I just recently used Firebase for the first time in years and
I was absolutely amazed at the level of productivity. The best part is how
seamless and brain-dead everything is compared to AWS. Also the client side JS
library for database storage makes handling application state a dream. Auth
and static hosting require almost no configuration, and the only thing I
couldn't use firebase for was server side compute based tasks.

This is the final missing piece will allow me to develop an entire app on the
google cloud with no more server admin. Can't wait.

------
drumttocs8
Oh man. This is great. Firebase is now a one-stop shop for the majority of app
infrastructure needs, and really is the closest thing to a "serverless"
infrastructure. Vendor lock-in aside, I can't think of a reason not to use it,
at the MVP level at least.

~~~
subpixel
Firebase offers a lot, but it's no longer the only game in town that offers a
sorta-serverless baas. Here are some compelling alternatives (I'd love to see
a complete list of the best Firebase alternatives):

\- [https://www.syncano.io](https://www.syncano.io)

\- [https://www.graph.cool](https://www.graph.cool)

\- [https://www.backand.com](https://www.backand.com)

I'm omitting things like feathers.js b/c the most attractive feature of
Firebase-like tools is not having to manage your own server/database.

~~~
pier25
We are moving from Firebase to Feathers js + PostgreSQL running on Heroku.
Zero server/database configuration or managing needed.

Being able to have data relationships and real querying is a big plus over
Firebase.

------
brilliantcode
This is a major coup against AWS. I might actually use Firebase now. If they
keep this up, they can put pressure on AWS.

------
Kiro
I don't really understand how to use this to build a real backend. Everything
seem to be revolving around reactions (new thing happening in the DB, file
uploaded etc) but isn't the more common use case that you actually want to
call the function directly?

I'm building a simple React Native/Expo app where I want users to join random
chat rooms so I obviously need some logic to delegate this process. Is
Firebase/Cloud Functions something I can consider?

My first thought was "oh, maybe I can let my app call the function directly to
add the user to a random chat room" but I don't see how that is possible.

~~~
mbleigh
You can definitely do this! There are two ways that readily come to mind:

1\. Use the Realtime Database as a communication channel to trigger a Cloud
Function that then sets up the thing you need.

2\. Create an HTTPS function and call it directly from your app, then do the
work you need.

~~~
inlined
When it hits YouTube, check out our talk "Cloud Functions and Firebase". We
have a much more complex chat room example than has been possible in the past

~~~
inlined
[https://youtu.be/GNR9El3XWYo](https://youtu.be/GNR9El3XWYo)

------
tedmiston
So it looks like very roughly AWS Lambda in JS only for Firebase with nice
user event hooks and Firebase Cloud Messaging.

I'm wondering how they handle:

1 - pricing

2 - support for long-running tasks (where Lambda does not)

3 - official tooling for packaging and deployment (perhaps the biggest
critique of Lambda)

It sounds like one could build an entire real app in this, which is quite
cool.

~~~
habosa
You can see pricing details here:
[https://firebase.google.com/pricing/](https://firebase.google.com/pricing/)

It's based on invocations, GB-seconds, CPU-seconds, and network egress. And
there's a free tier, so you should be able to try with your use case and
estimate your total future cost.

~~~
tedmiston
Ah, sadly no outbound networking requests on the free tier. In my opinion that
is a critical use case for a tool like this.

This could be a non-starter if there's really no way try it for outbound
requests without paying $25 / month. Hopefully I'm misunderstanding the
pricing page.

Edit: The fine print for the custom plan:

> 3 The Spark plan only allows outbound network requests to Google owned
> services. On the Blaze plan, Cloud Functions provides a perpetual free tier.
> The first 2,000,000 invocations, 400,000 GB-sec, 200,000 CPU-sec, and 5GB of
> Internet egress traffic is provided for free each month. You are only
> charged on usage past this free allotment. Pricing is based on total number
> of invocations, and compute time. Compute time is variable based on the
> amount of memory and CPU provisioned for a function. For more information,
> see Cloud Functions Pricing.

~~~
nivco
To clear things up: You Can use the Blaze plan which is pay-as-you-go and
starts at 0$ and has a large free tier. External requests are disabled on
projects that do not have billing enabled but you can have billing enabled and
still be within the free tier of usage. The Flame plan is the "fixed cost"
plan starting at 25$ and also includes external requests.

~~~
FlavorText
Oh, so if my app is currently under all the Free tier quotas for
DB/Hosting/Storage/transfer, I could sign up for Blaze and my cost would be
$0? I didn't realize that, the pricing page doesn't seem to make that clear to
me.

Edit: Actually, if I'm exploring this price calculator right, it appears that
there is no free tier for DB if you are on Blaze, only a free tier for
Functions. Which suggests I should leave my Free tier plan as is, set up a
second project that only has Functions that make external calls in it, sign
that up for Blaze, and call those functions from my free tier functions...
Convoluted, but hey, it'd be free.

------
flaviuspopan
I'm surprised by the progress and uptake of the node vm lib; it's really found
a ton of cool business models. From powering the Screeps game world, enabling
smart message routing for PubNub BLOCKS, to stuff like this. I'm excited to
see more advances with time.

------
nodesocket
I plan on doing a benchmark and blog post of a simple node http function that
just returns 200 and using Blitz.io to benchmark average startup latency of
Google Functions. Curious to see how it scales as the number of concurrent
requests increases and what the variance is.

~~~
inlined
Let us know what you find. Be keep an eye on your usage and quotas in the
Google Cloud console if you hit any limits.

------
primitivesuave
I have been waiting for this feature for a long time. This was the final
missing piece for building an entire software product entirely on Firebase.
Right now, I'm running servers on Google Cloud that use firebase-queue to
handle server-side requests like payments and restricted actions. Google Cloud
Functions would have been overkill because it would involve setting up a whole
new workflow for the team. This is perfect because it is integrated to the CLI
and easy for anyone to use.

All they need to work out now is educating their customers on the benefits of
using Firebase over building a backend from scratch, especially in the trade-
off between developer productivity and not owning your data.

------
batuhanicoz
We've been using this product in production for about 6 months now and it's
been great once we've figured out best implementations (for us) like database
structures.

------
systemz
Is there any protection for not spending fortune if your cloud function
endpoint is DDoS attacked?

------
rshm
No native python, java support like aws lambda. Are AWS 128MB instances
constrained to 200MHz cpus ?

~~~
inlined
One of Cloud Functions unique offerings is the ability to scale CPU and memory
separately.

------
bikamonki
Finally! This was the feature that made me go with parse.com when I started
making backend-less apps. For Firebase apps I have to spin-up tiny node.js
bots to capture events and do some cloud code magic.

Will start testing now!

------
timshim
Built our app, Wander, an IRC-like chat app, 100% with Firebase. We've been
using Firebase Queue to get server scripting abilities. Can't wait to migrate
to using Cloud Functions instead, just waiting for the (awesome) team at
Firebase to iron out any teething beta-related issues.

If only Realm made their server-side scripting available for free (or much
less than the $1.5k/mo they're asking for Enterprise). That would give
Firebase a run for their money.

------
phreack
This is just great, the two things missing from Firebase that we kept hacking
around were its very limited database querying abilities, and simple hooks
around database events. Now one is part of Firebase and the other is more
easily hackable than before, closing that little gap would make this the
ultimate platform tool.

------
skybrian
How does this compare to App Engine? The most obvious difference I see is that
App Engine lets you handle HTTP requests using a variety of languages, not
just JavaScript.

~~~
habosa
It's a pretty different model, so not really an apples-to-apples comparison.
Cloud Functions are bits of code that run in response to events, whether those
are events from Google Cloud / Firebase or HTTP triggers. These functions
scale up and down as the event load changes.

App Engine is a more traditional PaaS offering where you upload and entire web
app, something you might otherwise run on your own server or a bare VM.

~~~
camus2
Both can be used as a swarm of workers, that's basically what "functions as a
service" are.

The difference is usually in the pricing strategy. with "FAAS", you pay only
when the worker works, you're not paying when the server running them is idle,
as opposed to classic server instances.

App Engine standard has a few nice ideas in the way it implements push and
pull worker queues, but given its limitations it was never going to be a huge
success.

But "FAAS" is obviously not suitable to serve a "user facing" website, and
rely heavily on vendor locked in API that cannot be transferred to another
vendor.

Containerized worker jobs make more sense than "FAAS" IMHO. SAAS like iron.io
provide that.

~~~
katowulf
I'd argue there's a maintenance cost to consider as well. Functions are
"serverless" from the dev point of view, where the dev needs to maintain App
Engine services, scaling strategies, and so on. It's basically the next step
up in the abstraction chain: On prem (you build) > Compute > App Engine >
Functions.

------
quantumjs
After spending weeks getting to grip with AWS lambda I think I will try build
something with firebase to see how it compares.

------
mmedal
What version of Node.js is currently running in Cloud Functions for Firebase?
I assume it's LTS?

~~~
mbleigh
v6.9.1

------
randylubin
Very cool - congrats on the launch!

Is there any ability to set reoccurring functions or anything like a cron job?

~~~
jwngr
Not a built-in way per-se, but you do have a handful of options. You can use a
third party scheduling service like [https://cron-job.org/](https://cron-
job.org/). You could also use something like
[https://cloud.google.com/solutions/reliable-task-
scheduling-...](https://cloud.google.com/solutions/reliable-task-scheduling-
compute-engine) which uses Google App Engine and Cloud Pub/Sub. We actually
have a sample that shows just this (HTTPS function using an external cron job
service): [https://github.com/firebase/functions-
samples/tree/master/de...](https://github.com/firebase/functions-
samples/tree/master/delete-unused-accounts-cron)

~~~
randylubin
cool - thanks for the tip!

------
Dawny33
Wow! Great to see GCloud going the serverless way.

CloudFunctions vs Lambda would not be quite a clash now!

------
johngorse
Is it possible to setup a Stripe server side .js code on this?

------
intellegacy
is the cloud notifications something I could use for push Notifications in
Android and iOS app?

------
marknadal
Look, I'm going to be honest - this is exciting, and Firebase is one of the
best. But Google pushing this feature spells even more vendor-lock-in doom.

My disclosure is obvious, I'm an Open Source competitor, switch to us (
[https://github.com/amark/gun](https://github.com/amark/gun) ) and get "cloud
functions" for free which can deploy to any services ( Docker enabled, Heroku
1 click install, run on GCS, AWS, etc.).

And don't worry, we just load tested the system doing a sustained almost 2K
throughput messages/second across a distributed system on low end hardware. We
plan on getting this up to 10K inserts/second on a free Heroku box.

