Skip to main content Accessibility Feedback

Functions as a service

Over on the Lean Web Club, I just launched a new course on Serverless and Cloudflare Workers.

I teach you how to create your own “backend functions” using nothing but vanilla JavaScript, how to write your own API endpoints, and how to store data in a database without actually having to manage a server.

Today, I wanted to share the first few lessons from the course. If you like them, you can get the whole thing for free over at the Lean Web Club.

Let’s dig in!

Functions as a Service

Serverless is a terrible name for an awesome thing.

It’s mostly just a silly marketing term for Functions-as-a-Service.

With serverless, you don’t have to worry about “the server” itself. You write some code and add it to your account. Then, you can make that code run in response to specific events, such as calling an API endpoint.

There are a few important under-the-hood details about serverless that are worth noting.

1. Different vendors support different languages.

Options include PHP, Node.js, Python, Ruby, and even vanilla JS. Some vendors even support multiple languages, allowing you to mix-and-match as desired.

2. Serverless functions only run when needed.

With traditional cloud-based server options, the server or virtual machine is always running. You pay a fixed amount of money for a fixed amount of memory, storage, CPUs, and data transfer, whether you use it all or not.

With serverless, a machine is spun up whenever a function is triggered, and shut down when its done. The vendor handles provisioning, scaling, and so on, and you only pay for the amount of server resources used (many vendors also have free options).

3. Serverless functions are stateless.

You can’t update a variable or value when a function runs, and then access that modified value the next time it runs. Because the server is spun up, runs the function, and then shuts down, any session data is lost each time it runs.

In the example below, if you run saveName(), then sometime later call getName(), it will return Merlin instead of the name you updated it to.

let wizard = 'Merlin';

// Update the wizard variable
function saveName (name) {
    wizard = name;
}

// This will ALWAYS return Merlin, even if saveName is run
function getName () {
    return name;
}

4. Many serverless vendors offer database services.

While serverless functions are stateless, many vendors offer databases that you can call from your serverless functions. This allows you to save data when functions run, and retrieve that data later.

But there are servers?

Why is it called serverless if there are servers involved?

Primarily marketing nonsense, honestly.

Serverless is supposed to describe your experience with the servers. You don’t have to think about them at all, and instead focus on the code you want to run and the tasks you’re trying to accomplish.

With traditional cloud services, even with managed ones, you still have to make choices around how much memory, storage, and bandwidth you need, and when to increase or decrease that. Serverless handles that stuff automagically for you.

Serverless vendors

One of the most popular serverless offerings, AWS Lambda, supports languages like Python, Ruby, Node.js, and Go. Netlify Functions support JavaScript and Go.

My personal favorite, though, is Cloudflare Workers.

I love CloudFlare Workers for two big reasons:

  1. There’s a super simple GUI you can use, so command line knowledge isn’t required (but is an option if you want).
  2. It uses vanilla JS.

Most of the other vendors I’ve looked at require you to push code to a GitHub account, or use a CLI package, or author your code in another language.

CloudFlare Workers are simple, fast, and let me write code in plain old JavaScript (they support a bunch of other languages, too). They have a bunch of other awesome features, too, like databases and scheduled tasks.

And they have a very generous free plan!

This all makes them a fantastic teaching tool, so we’ll be using Cloudflare Workers to learn about serverless for the rest of this guide.

Creating your first serverless function

Ready to get started?

Join the Lean Web Club for free, and get access to the rest of this course (and hundreds more!).