A Q&A-style introduction to the basics of serverless computing.
You are headed to a software developer conference on a short flight in one of those small regional jets. While you cannot wait to get to the conference, the flight is boring. Like most times, you’ll likely put on your headphones and listen to something mindless, just waiting for the plane to land.
This flight will be different, however. Because I'm a co-passenger and coincidentally, seated next to you. Not only am I a speaker at the conference, I'm known to tinker a lot with Xamarin and Azure technologies - it's what I do! This is your chance to pick my brain and learn as much as you can during this short flight. You formulate a plan. First, you’ll want to understand the much-hyped serverless computing, then dig into Azure Functions, and finally understand how to leverage such cloud services for your mobile apps. You take notes as you begin a quick Q&A session; clearly, I'm not getting any sleep on this flight.
I keep hearing the term, “serverless computing.” What does it mean?
For developers, serverless computing means you write highly compact individual functions that do one thing - and run in the cloud. These functions are triggered by some external event. That event could be a record being inserted into a database, a file uploaded into BLOB storage, a timer interval elapsed, or even a simple HTTP request.
OK, let’s step back for a second. Since we have some code running in the cloud and it is reacting to external events, it’s really not “server-less,” right?
Right, servers are still definitely involved! What has changed - from other types of cloud computing - is that the idea and ownership of the server has been abstracted away.
Wait, wait. What are the other types of cloud computing?
Glad you asked before I got too far ahead of myself!
You can think of cloud computing as an evolution of the on-premise datacenter. In the good old days, you had a server room stuffed full of powerful computers. You needed to account for the server’s software, keeping it up to date, backing it up, etc. You also needed to care for the server’s physical hardware, RAM, hard disk, CPU, and so forth. Not only that, you also needed to make sure that server rooms are secure and have proper ventillation, amongst a ton of other things I’m probably forgetting!
Then, cloud computing came along and abstracted the idea of on-premise data centers into the cloud.
This is where all the “as a service” things come into play, right?
First, there is Infrastructure as a Service (IaaS). Think of this as a virtual machine in the cloud. You’re still responsible for the server’s software, keeping it up to date, and its backups; as well as the hardware specifications of the VM. But now you’re free of any physical hardware maintenance. And of course, you still need to write/host your applications and install whatever supporting software on the server that’s needed.
Then there is Platform as a Service (PaaS). This is one level of abstraction higher than IaaS - the cloud provider takes care of the server, the supporting software, and the hardware. You host your applications on top of that pre-existing setup or platform. You do still take care of some basic server configuration and things like scaling.
Beyond that you get to Function as a Service (FaaS). This is serverless computing! It’s one level of abstraction higher than PaaS, now you don’t even worry about the platform or scaling. All you need to do is put together your application logic - and your code is going to be invoked in response to events - and the cloud provider takes care of everything else. You literally get to focus on only the business logic you need to run in response to something of interest - no worries about hosting.
The last level is Software as a Service (SaaS). Here, the app is already written for you and all you do is modify some app settings. Things like Google Docs or Office 365 are examples.
So, the cloud provider takes care of the hardware, OS, any supporting software and scaling - what else do they provide with FaaS?
First, you do not need to worry about wiring up the plumbing between the service that originates the event and the serverless runtime environment. The cloud provider will handle the mechanism to call your function in response to whatever event you chose to have the function react to.
Because the serverless functions are invoked in response to events, the thing that caused the event is automatically passed into the function. In other words, if a blob of data being inserted into a storage container causes a function to fire, you do not need to worry about the plumbing of getting the function to fire, nor do you need to worry about getting that blob passed into the function. That will all be handled for you. All you need to do is set some configuration values.
That sounds pretty cool, but what if I want to act on a completely different resource than the one that triggered the function?
Again, no problem! There are cloud providers that provide bindings to some of their other services that are not involved in the event that triggered the serverless function. References to those services are passed in as parameters to your serverless function.
In other words, you don’t need to concern yourself with the plumbing between the cloud provider’s services. As long as the cloud provider supports it, all you need to do is setup some config values.
But how much does this all cost?
That’s the other neat thing. You only pay for the time the serverless function is running. So, if you have a function that is triggered by an HTTP request, and you rarely get requests to your function, you would rarely pay. (Although that’s kinda not the point, right?)
In the PaaS model, where you have a REST service waiting for calls, you would be billed a monthly charge, no matter the usage. So serverless computing can make a lot of financial sense.
So what are some scenarios where I’d want to use serverless?
The first one that comes to mind is that of a simple web API. You can build out a suite of serverless functions that wait on HTTP requests to come in (and they can be set to listen for different HTTP verbs), and then via the power of binding, they can access the cloud provider’s other services such as a database.
Another would be a backend for mobile apps. Say a user snaps a photo on a mobile client app; the app could then invoke a serverless function to get a key to place the photo into cloud BLOB storage. Upon that BLOB being inserted into storage, another serverless function could invoked which resizes the photo into a thumbnail. Yet another function could be invoked to do push notifications to alert friends a new photo has been uploaded. You get the workflow.
Cool! How do I learn more?
Do a search; there is a wealth of internet information on serverless computing basics. Once you decide that you are ready to jump onboard, there are several big cloud providers offering serverless computing as a service:
I know you work for Microsoft. Tell me more about Azure Functions.
Sure thing! I can give you a quick introduction to developing serverless cloud functions with Azure Functions. You’ll see some of the events that trigger them as well as some of the bindings to other Azure services they have access to!
Oh, I see the flight attendant is coming down the isle with drinks. Let’s take a quick break, and then we'll be back with the rest of the series!