What is FaaS?

Definition

Function as a service (FaaS) is a cloud computing service that executes code in a serverless manner in which the developer does not need to maintain a server.

Overview

FaaS is part of a wider concept called serverless computing. Servers actually exist but they are abstracted as much as possible. You don’t have to maintain them, secure them, or worry about elasticity. Everything scales as it needs to based on the amount requests coming in.

The only thing you need to do is write your function and set a trigger for it. When triggered, the serverless computing provider creates a container in a matter of milliseconds that runs your code and returns a desired result. The result can be a computational output or an output of JavaScript, among other things.

Even though serverless is a relatively new concept, all major cloud platforms have a FaaS offering. There are even edge providers like StackPath that offer FaaS and reduce latency by executing functions as close as possible to the end user.

In addition to using FaaS by cloud providers you can deploy your own serverless framework with many different open source solutions including Knative, Kubeless and OpenWhisk. All of these run on a Kubernetes cluster that you can deploy on any public or private cloud, or on bare metal in your own datacenter.

Another great thing about FaaS is that high-availability is built into it. It can only go down in two cases: 1) if your cloud provider has an outage, or 2) if you modify the service in a way that breaks it and send it to production before testing. In that case, you can still roll back pretty quickly. But in first case you can only mitigate issues by using a multi-cloud solution.

How FaaS Works

To understand how FaaS works, consider this simple Go function executed on Google cloud that prints hello world:

After enabling Google Cloud functions for your project, as well as billing for that project, start a terminal on gcloud and run these commands.

git clone https://github.com/GoogleCloudPlatform/golang-samples.git
cd golang-samples/functions/helloworld/
gcloud functions deploy HelloGet --runtime go111 --trigger-http
gcloud functions describe HelloGet | grep url:

The last one will give you the link to the function output.

Use command…

cat hello_world.go

…to see the content of the function.

Step by step, here is what’s happening:

  1. To facilitate the execution of this function the cloud provider has to provide an endpoint which will receive an http request.
  2. During the creation of the function, the service creates a container in the background that is loaded with the GoLang runtime.
  3. When that endpoint is hit, it triggers the execution of the function.
  4. The function is executed on a container that was waiting for traffic to be routed to it.
  5. If the endpoint gets a lot of traffic and one container can not execute the function as fast as traffic is arriving, more containers are spawned and the http endpoint routes traffic to each of them in a round robin fashion.

When executed, the function in this case says print hello world.

Examples of FaaS

To get familiar with StackPath serverless scripting consider a simple function that returns different messages depending on the country code of your request. To apply this function to your project on StackPath you need CDN and serverless scripting purchased and enabled for your stack.

To learn how to enable it, follow this tutorial.

addEventListener('fetch', event => {
  event.respondWith(fetchAndApply(event.request))
})

async function fetchAndApply(request) {
  if (request.headers.get('x-sp-server-countrycode') == 'US') {
    return new Response('Hello, welcome to our service',
        { status: 200 })
      
  }
  else {
    return new Response('Sorry, we do not offer service outside the USA at this time',
        { status: 200 })
  }

return fetch(request)
}

What does this function do?

If your request comes from the US, it tells you that the service is available in the US. And if your request comes from anywhere outside the US, it tells you that service is not available outside the US at this time. Based on this function, you can replace the part that shows a message saying the service is unavailable and redirect to your site. When a request comes from outside the US you may redirect the user to a landing page that allows them to subscribe to a newsletter that will notify them when service becomes available in their region.

For another example, consider a website that has an English and German version.  You would typically want visitors from Germany and Austria to be redirected to the German version and visitors from the UK and US (and probably rest of the world) to be redirected to the English version.

Changing this function a little bit would do the trick. Instead of displaying messages based on country codes it could redirect to links that resolve to your site version.

Key Takeaways

  • With FaaS you don’t need to manage server infrastructure.
  • If using FaaS from a public cloud provider you only need to write compatible code (i.e. a function) and point it to a serverless endpoint.
  • You pay only for traffic you use, per request.
  • Scaling and life-cycle management is done automatically.
  • High availability is built in; your function is unavailable only if the cloud provider has an outage.