Browse Chapters

Serverless Functions and Serverless Applications

In the previous chapter we took a close look at the term “serverless” as it is used in cloud computing. While we enumerated three different definitions of the term “serverless,” we said that we were really only interested in the third definition. In that definition, a serverless application is one where you (the developer) do not have to write a software server. Instead, you focus only on writing a request handler. Let’s spend some time talking about this programming model; easily creating serverless functions and serverless applications.

Your program is started when a request is received. The request object is passed into a function in your code. That function is expected to run to completion, possibly handing back a response. Once the function has been completed, the program exits.

There are three characteristics of this sort of program:

  • It is short running, often running for only milliseconds.
  • It is triggered by an event or a request.
  • It is responsible merely for dealing with that request (often returning a response).

For the sake of clarity, let’s look at a simple example of this kind of program. We will use the world’s most popular programming language, JavaScript, for this example. But the pattern is similar across languages. Also, we will write an example of a serverless function that handles an HTTP request.

const encoder = new TextEncoder()

// Declare a function that handles a request (in this case, an HTTP request)
export async function handleRequest(request) {

    // Send back an object that describes a response (in this case, an HTTP response)
    return {
        status: 200,
        body: encoder.encode("I'm a Serverless Function").buffer

There are three things to note about the example above:

  1. We do not set up a server of any sort (we don’t even import any libraries).
  2. There is a function called handleRequest() that takes a request object. This function is called when an inbound HTTP request occurs.
  3. The function returns a response. In this case, it’s an HTTP response with a 200 response code (which means no error occurred) and the content that will be displayed in the web browser.

If you want to dive into creating JavaScript serverless apps, you can jump on over the Fermyon technical documentation and read Building Spin Components in JavaScript. There are similar guides for Rust, Go, Python, C# and .NET, and many other languages.

That is all there is to it! We don’t start a server, map ports, handle interrupts, declare SSL/TLS certificates, or anything like that. The serverless app platform does all that stuff on our behalf outside of our code. When a request comes in, this app is started, the handleRequest function is called, and then the app exits. And how fast is this? With Spin, the handler can be started in under a millisecond. That is why there is no reason to run a server. If we can start this fast, it’s much more efficient (and much cheaper) to not be running idle servers.

The above is an example of a serverless function. And when we package that up and send it off to a server, we have built a simple serverless app.

Browse Chapters

Quickstart Your Serveless Apps with Spin

Get Started