Firebase, a serverless story.

Saad Bin Amjad
Monstar Lab Bangladesh Engineering
7 min readNov 16, 2018
“architectural photography of brown and gray castle” by Jonny Caspari on Unsplash

“There are no rules of architecture for a castle in the clouds.”

- Gilbert K. Chesterton

Prologue

Once upon a time, in a faraway land, there used to live a King. The Kingdom had everything one could ever dream of. Its land stretched from high up in the mountains to the mighty oceans. From innovative makers, hard working farmers to young and bold soldiers, everyone lived in peace and harmony. But there was one thing missing in that Kingdom.

The King never had a castle of his own!

While other kingdoms had big castles made of gold that glittered from space, so high and mighty that defined the power of the kings, this Kingdom had none.

The King, without a castle. Yet ruling the land with order and dignity? This question kept haunting other kingdoms.

Chapter Two

A serverless web application is like a King without a castle.

Chapter Three

Turns out the King was smart, he didn’t require a castle at all. Yes, he did live in a house somewhere in the Kingdom with his family, but it was never anywhere close to be called a castle. Instead of making his castle the biggest of them all, he invested all his time and money on ways to make his Kingdom happy. He just knew when the folks in Kingdom are happy, his castle shouldn’t be a concern, and focused on making his Kingdom great.

His humbleness was truly rewarding, as his folks, without his knowledge kept updating his small house with all the ammenties like any real castle would have in the first place from time to time.

And they all lived happily every after.

Chapter Four

As they say, small apps or big apps both scale from 0. So as developers we just need to start. Yeah, I can start coding and all, but who is going to manage the infrastructure?

Boom! Serverless is the answer for most of us. It is nothing but an abstraction of the infrastructure. We do not need to know the underlying implementation, but just choose the right services and we are good to go.

Like the King, we just need to now focus on coding the best possible app and features, instead of worrying how to host, accomodate/allocate resources for our servers that eventually will scale with time.

And lets talk about the elephant in the room. Serverless doesn’t mean that there are no servers at all, it means the servers are distributed and they are not our headaches. They are maintained and operated by third party cloud providers, like Google Cloud Platform, Amazon Web Services or Microsoft Azure.

Serverless architectures are application designs that incorporate third-party “Backend as a Service” (BaaS) services, and/or that include custom code run in managed, ephemeral containers on a “Functions as a Service” (FaaS) platform. By using these ideas, and related ones like single-page applications, such architectures remove much of the need for a traditional always-on server component.

It has been predicted that moving to cloud will become the second great digital migration, the first being companies going paperless.

Today we discuss in short, how quickly and easily we can use Firebase to deploy an app using some services of Firebase.

Chapter Five

Firebase provides more than a dozen of services for both mobile and web app developement. Lets shed some light on 5 basic services today to build an app.

Firebase Authentication:

For every project we require users to authenticate in the system, users credentials to be validated and secured in our database. In order to ease the flow of authentication, authentication using SNS, or social networking sites, is nowadays crucial to ensure easier first entry to the system. And we also need a login page to be created now and then for the same task.

Firebase Authentication not only provides mutiple platforms to log in including the general email login, it also provides a drag and drop ui to get you all started with the login page. Firebase authentication sdk generates unique userId for n number of users, and always maintains the state, so it just takes one promise to know if a user is logged in to the system or not. Features like Forget password, 2 Factor Authentication also come out of the box.

Here is a simple code snippet to highlight the ease of getting login done in Angular apps using @angular/fire:

Firebase Firestore:

Back in 2012, before Google acquired it, Firebase was very well known to developers for their real time database. One updates the store and instantly all the connected devices are in perfect sync. Google announced their Realtime Database back in 2016, and now we have a better version of it, called Firestore.

It is a NoSQL Database that supports query and provides offline support. If a user updates any data offline, it is persisted locally by the sdk and when the connection is up, the store is updated and the rest of the devices are in perfect sync. Firestore supports queries to ensure that the business logic to get the data in the front end is of much ease.

Here you can see how easy it is to subscribe to the user collections snapshot and immediately be notified of the change in the database.

Firebase Cloud Storage:

Now that you have authenticated users store their data in a real time database, you might also need to store contents like files, images etc.

Using Cloud Storage, storing in buckets have never been easier.

This storage securely stores user generated content, and ensures uploads over flaky internet connections and is immensly scaleable.

Firebase Hosting:

firebase deploy

And that is it, you will get your static site with dynamic content hosted with ssl certificate. Fast deployment to a global content delivery network (CDN), options for rollback if you think you need to deploy again, all make deployment just one command away. A series of questions will pop up in your cli regarding the deployment, choose the best answers that fit you and within minutes your production grade site is up.

Firebase CLI is really impressive to not only setup the project but also to deploy the contents.

Cloud Functions:

To simply put, cloud functions are your backend codes. They are dormant in nature, and will spring to life when any event is triggered, that may be in the form of Firebase features or HTTP requests.

“Cloud Functions lets you build and deploy services at the level of a single function, not at the level of entire applications, containers, or VMs.

Creating Google Cloud functions using Firebase SDK is simple, but for developers, you need to setup an account in Google Cloud Platform to get started.

For instance, the moment a user uploads a image in the storage, you can detect this change via functions.storage().onChange and then decide what to do. Cloud functions will definitely serve as an entry to Google Machine Learning Kit, called ML Kit.

Currently the cloud functions can be tested in local servers, and written only in Node.js and Python, and deploy it in firebase. Functions can be monitored in the Firebase console.

Chapter Six

Obviously severless is not a solution for all problems, it just solves a specifc set of problems and does them well. Firebase services are mostly ‘‘Pay as you go” basis. The biggest drawback of serverless is that they do have certain limitations like cold starts.

A cold start happens when you execute an inactive function. The delay comes from your cloud provider provisioning your selected runtime container and then running your function.

Cold starts refer to the fact that when a server is called on demand, it starts from scratch. Then it does its job, and destroys itself after emitting its result. So, often we can see lag when users are updating something and get the results a bit late.

There are also other drawbacks, and it is recommended to look at all serverless options out there to meet your specific business needs and cost. But overall, for many developers to be able to quickly build an app that will scale, the cloud based developement approach has its merit there for sure. And as the King focused on what is most important, the Kingdom became prosperous. So, when the developers save their time and efforts from intiating, load balancing and maintaining server instances for different purposes, they really shine in their own domains of just creating great functions.

And thus the developers lived happily ever after without the need to manage servers.

“sunset over the horizon” by Jordan Wozniak on Unsplash

The End

--

--

Saad Bin Amjad
Monstar Lab Bangladesh Engineering

Technical Lead at Monstar Lab Bangladesh. Prefers tea over coffee, and coffee over anything else. Loves to speak.