No More Servers - FaaS with Firebase Cloud Functions
Sameer SrivastavaSameer Srivastava
It’s easier than ever to “spin up” or deploy your own server to support your business. Of course, it’s not exactly your own server, the actual hardware and base configuration is controlled by the Amazons, Googles, Microsofts, and IBMs of the world.
However, you get to put your own software on there without worrying about where the server is located, the utility bills, replacement costs, etc. Now, we can even avoid configuring our servers and scaling our applications with Functions as a Service (FaaS).
So let’s break that down and go over a simple scenario of using a FaaS, in this case Google’s Firebase Cloud Functions, which we have been using at Leverege.
However, I don’t want to run a server all year round to listen for a sensor alert – rain is unpredictable and seasonal. Instead, I decide to use Google’s FaaS, Firebase Cloud Function, which will run only when needed to reduce costs.
So how does it work?
If the water level sensor detects the water is too high, then it can send an “event” to Cloud Functions. So then Cloud Functions receives the event, and starts up my serverless text message function. Notice that the text message function was not already running and was “event-triggered.” Also, Google started up my function and is executing it on their servers, so I don’t have to manage it. While FaaS is called “serverless,” it’s serverless for the consumer, not the FaaS vendor who still has to run servers.
Next, my text message function processes the event and sends Nicole the appropriate text message. After the function execution is completed (the text message was sent), my text message function is terminated.
All of this takes at most a tenth of a second from the time the event was sent from the water level sensor. This process happens every time the water level sensor sends an event, so the text message function is “ephemeral”, or only lasting for a short period of time.
A FaaS application can be thought of as a math function (see below). The input is an event, the output is the result of the function. Like a math function, a FaaS application is essentially “stateless”, it has no knowledge of anything except its inputs and is unable to hold an internal state.
[caption id="attachment_7738" align="aligncenter" width="448"]
Source (Wikipedia)[/caption]Below is a quick bulleted list of pros and cons of FaaS from Mike Roberts’ article. The pros and cons can give you a good idea of what type of applications are best used for FaaS. Afterwards, we'll go over more things that are specific to Google’s Firebase Cloud Functions.
Typescript is “used in almost four times as many pull requests as last year” (GitHub). Furthermore, deploying Cloud Functions is very easy, it only takes two terminal commands, firebase use <project name> and firebase deploy --only functions. Â
Firestore is Google’s new document database implementation which is currently in beta. Firestore allows for easier querying than Firebase and scales better. If you want to read about the differences between these two database implementations, check out Cloud FireStore, Firebase's More Scalable Older Brother.
This Cloud Function could be easily and quickly changed based off of promotions or campaigns your company is running. That type of ease and flexibility is really powerful. Cloud Functions and Google Cloud Platform products are only going to get more integrated with each other, which is really exciting.
FaaS is not for all applications, but it is definitely useful and here to stay. Whenever you are thinking of deploying a whole server, think of whether you could create a FaaS application instead. Personally, Firebase Cloud Functions have worked well for my team and me, so give that a try if you’re looking to use FaaS.
The Most Comprehensive IoT Newsletter for Enterprises
Showcasing the highest-quality content, resources, news, and insights from the world of the Internet of Things. Subscribe to remain informed and up-to-date.
New Podcast Episode
Recent Articles