Serverless Functions: Your Website's New Best Friend

Build a Website for Free with HubSpot CMS
Anna Fitzgerald
Anna Fitzgerald

Updated:

Published:

The capacity and functionality of your server directly impact the performance of your website. But keeping track of the server’s current capacity and upgrading it regularly is time-consuming and doesn’t mitigate the risk of poor performance — and that’s where serverless functions come into play.

woman creates serverless functions on her laptop

More than once over the years, I’ve had to deal with a website that is breaking continually or slowing down for users. Slow page load speeds, broken pages, and broken functionality typically require some investigation. I’ve found that many times, the current server set-up is totally overloaded and leading to these issues. As I’ve been busy building out new pages and content, the server is now straining to keep up.

Learn More About HubSpot's CMS Software

Serverless functions are services from cloud providers that enable me and the developers I’m working with to hand off that process completely, so I never have to worry about server capacity again. Let’s take a look at what a serverless function is, how it differs from Functions-as-a-Service (FaaS), and some of the biggest names in the serverless architecture market.

Table of Contents

Because these companies automatically add server capacity when I need it and then take it away when I don’t, I don’t have to worry about maintaining and scaling servers to fit the evolving needs of a website or application. Instead, I can spend more time creating newer and better products in less time.

HubSpot's Free Website Builder

Create and customize your own business website with an easy drag-and-drop website builder.

  • Build a website without any coding skills.
  • Pre-built themes and templates.
  • Built-in marketing tools and features.
  • And more!

Going serverless can also be cost-effective since most platforms have a pay-as-you-go model. That means I only pay for the server resources the site or app uses, rather than having to eat the cost of unused resources at the end of a billing cycle.

Before I take a look at the major cloud providers in the serverless architecture market, let’s take a closer look at how you might already be benefitting from serverless functions.

Benefits of Serverless Functions

Many platforms have begun extrapolating smaller pieces of their back-end code and moving it to serverless functions. This marks a huge shift in the software development community.

Historically, platforms have been monolithic. Meaning, they had large, unified codebases. That meant any time you added a new feature or even fixed a bug in a single line of code, you had to deploy the entire platform to ship the new code.

Then, microservices were introduced. Microservices are blocks of code that operate as smaller, individual services. Combined, they form a single, comprehensive application.

Serverless functions add another layer of modularity. With serverless functions, you can develop any new functionality or security patch as a completely isolated, single-purpose piece of code. All you have to do is upload your code and tell the platform how to provision it.

serverless functions microservices workflow

Image Source

Serverless functions not only make the deployment of new code quicker, simpler, and easier to automate — but they also significantly decrease the possibility of downtime during a deployment. In short, serverless functions improve the performance of your application and the experience of your developers and customers.

Recognizing the value not only to themselves but to their customers, content management systems like the Content Hub Enterprise have begun offering serverless functions. This allows you to add interactive elements, like event registrations and dynamic calculators, to your HubSpot-hosted content without needing to configure an external server.

Let’s say I’m a Content Hub Enterprise user and run a fitness site. Using serverless functions, I can allow visitors to view upcoming classes and register for them (or sign up for a waitlist) directly on my site. I don’t have to manually keep track of sign-ups or set a class to full capacity or anything — just write the back-end code and set it up to be triggered when a visitor fills out a registration form. HubSpot will do the rest.

How Do Serverless Functions Work?

Serverless functions take the work out of actually managing servers by abstracting the underlying infrastructure. This allows developers to focus purely on writing and deploying code, as providers handle the resources for a given function on an “as needed” basis. From triggers to execution, here’s a quick breakdown of how it works.

  • First, serverless functions use event-driven execution. File uploads, database changes, and HTTP requests trigger the server to automatically provide the resources needed for a function.
  • Functions also run in isolation in a stateless manner since, once the function is executed, the server is decommissioned and resources are released.
  • Serverless functions only provide the resources needed for functions to run, so they automatically scale up or down as needed. This means whether there’s one request or thousands, server capacity is dynamically allocated to meet demand.

For large websites or applications, I find serverless functions can be cost effective because they typically operate on a “pay-as-you-go” model, meaning you only ever pay for the resources actually used. For smaller websites or apps with low or steady usage, I find that this advanced level of server management isn’t necessary but something you might want to consider as you scale up.

As an example, the first time I came across the idea of serverless functions was for a large-scale ecommerce website. The brand took off unexpectedly. The website experienced huge spikes in traffic as a result, putting a strain on resources and causing some functionalities to break. I worked with the development team to implement serverless architecture that would enable the site to scale and protect against predicted seasonal spikes in usage.

Best Practices for Serverless Functions

There are many benefits when using serverless functions, but they still need to be managed carefully to maximize efficiency. Otherwise, you can run into issues with debugging and high costs. Here are some best practices for using serverless functions effectively.

Keep functions small and focused.

If a function tries to do too much, you can increase your pay-as-you-go bill unnecessarily since more resources will be deployed than needed. Instead, breaking down functions into smaller modular pieces keeps costs lower and also makes it easier for you to debug and maintain.

Reduce cold starts as much as possible.

If a function has been idle for a while, the server needs to “warm up” to execute it, which can mean slower response times. You can avoid these cold starts as much as possible by reducing dependencies for loading functions and keeping them as lightweight as possible. Some providers also let you set functions to “keep warm” to avoid this. But, you should be aware that this can increase costs significantly depending on the size and number of functions you keep in this state.

Cold starts can trip developers up, even in unexpected ways. I spoke with Alex Vasylenko, founder of The Frontend Company, to hear about his experiences with cold starts. “I came to realize how cold starts can be very unforgiving, especially on applications relying on low latency,” he says. “The best way to handle this would be to keep the deployment package of your function as small as possible.”

Vasylenko recommends fewer dependencies, as this equates to faster cold starts. “I also like the application of techniques such as prewarming functions, whereby a schedule of frequent invocations is set up, which serves the purpose of keeping functions warm and responsive.”

Monitor and log everything.

Serverless functions are event-driven and stateless — once a function has finished executing, it’s gone, leaving no way to check or trace what happened. That’s why monitoring and logging for every execution is so important. Without it, you’ll be almost totally in the dark if something goes wrong.

Vasylenko agrees that monitoring and logging are critically important in a serverless environment, saying, “Your serverless function is so transient that it‘s very easy to miss out on what happens if you don’t have proper logging set up in advance. A good monitoring solution tracks execution metrics, usage patterns, and error rates. This will be super useful for making data-driven decisions and further optimizations, not only for troubleshooting.”

Most cloud providers, especially popular ones like AWS or Azure, have monitoring and logging functionality built into their service. You can also integrate a third-party tool if needed, particularly if you want something more detailed. Either way, comprehensive monitoring and logging will help you track performance and diagnose issues.

Optimize resource usage.

With usage and capacity automated, I’ve found it’s easy for inefficiencies to creep in over time as you use serverless functions. So it’s still important to carefully review memory, CPU, and execution time and optimize your code so it’s only using the resources it needs. This could mean analyzing execution frequency and combining or reorganizing tasks accordingly, for example.

Before we take a look at popular vendors, let's look at some examples of serverless functions.

HubSpot's Free Website Builder

Create and customize your own business website with an easy drag-and-drop website builder.

  • Build a website without any coding skills.
  • Pre-built themes and templates.
  • Built-in marketing tools and features.
  • And more!

Serverless Functions Examples

Below are case studies of brands using serverless functions to reduce costs and improve efficiency.

1. Walker Manufacturing

Walker Manufacturing uses serverless functions to collect, store, and dynamically display data from HubDB. This enables customers to select different options from a dropdown menu on product pages and view the most up-to-date details about that specific product, including images and pricing. This data dynamically displays without the web page reloading.

Here's a video that recaps how they use serverless functions and other features of Content Hub to optimize their website.

2. Coca Cola

Coca-Cola uses serverless functions to reduce the costs of managing vending machines. When a beverage is purchased, the payment gateway makes a call to the AWS API Gateway. This triggers an AWS Lambda function to complete the transaction (which might include sending the customer a push notification on Apple or Android Pay if they initiated the transaction on their phone). Being able to pay per request instead of operating at full capacity enables Coca-Cola to stay on top of inventory while keeping costs low.

Here's an awesome illustration of the process below:

pay per request serverless functions process

Image Source

3. Netflix

Netflix uses serverless functions to improve UX and streamline its operations. They expanded their use of Amazon Lambda in 2022 to improve personalization for users and streaming performance using Amazon Rekognition. When users interact with the platform, serverless functions are triggered to analyze things like viewing history and dynamically generate personalized recommendations. For a platform the size of Netflix, this means handling millions of requests per second, so serverless functions make the best use of resources.

Before we move on to serverless architectures, I first have to clarify the difference between serverless functions and Functions-as-a-Service.

Although serverless functions and FaaS are sometimes used interchangeably, FaaS is actually a subset of serverless functions.

Serverless functions can fall into any service category where the configuration and management of servers are invisible to the end user. That includes storage, database, messaging, computing, API gateways, and more. FaaS, on the other hand, refers exclusively to event-driven computing wherein the code only runs in response to specific events or requests.

So, if I set a function to be automatically triggered by an event such as in-app activity, website clicks, or image uploads, I’m using FaaS.

Now, let’s look at some public cloud hosting companies that have serverless platforms.

Serverless Architectures

The global serverless architecture market is expected to be worth $44.7 billion by 2029, according to a new report by MarketsandMarkets. Below I’ll show you the serverless portfolios of some of the biggest vendors in the market.

1. Lambda Serverless

Introduced in 2014, Lambda from Amazon Web Services is an event-driven, serverless computing platform that allows users to run code without provisioning or managing servers.

Users can run code for virtually any type of application or backend service with zero administration. Once they upload their code and set it up to automatically trigger from either other AWS services or directly from their website or mobile app, AWS Lambda takes care of everything required to run and scale the code with high availability.

amazon web services lambda serverless function

Image Source

2. Google Cloud Functions

Released in 2018, Google Cloud Functions is a serverless function environment on the Google Cloud Platform. As with AWS Lambda, users simply have to write their code and set it up to trigger from other Google Cloud Services or from their application. Google will handle the rest from there.

I think this platform is ideal for setting up functions for data processing, like retrieving relevant data from images and videos.

https://youtu.be/1r3vMYywNLk

3. Microsoft Azure Functions

Introduced in 2016, Azure Functions is the serverless computing service hosted on the Microsoft Azure public cloud. In my opinion, what sets Azure apart from the other platforms is that it provides an easy-to-use web interface where users can write code. Azure Functions is ideal for tasks like image processing, order processing, and file maintenance.

microsoft azure functions serverless computing

Image Source

4. IBM Cloud Functions

Released in 2019, IBM Cloud Functions is a FaaS programming platform based on Apache OpenWhisk that enables users to develop lightweight code that scalably executes on demand. IBM Cloud Functions is ideal for deploying serverless apps across multiple regions and building a serverless back end for a mobile application.

What’s unique about this serverless platform is that it can work in combination with other IBM solutions, like the IBM Watson IoT platform and IBM Event Streams, to react to and process IoT sensor data and incoming messages.

serverless functions: IBM cloud functions

Image Source

5. Alibaba Cloud Function Compute

Introduced in 2017, Alibaba Cloud Function Compute is a serverless platform that provides the compute resources required to run code flexibly and reliably. This allows users to focus on writing and uploading code instead of having to manage servers and other infrastructure.

I like that Function Compute is designed to reduce computing costs and improve computing efficiency when building serverless web applications, processing data in real time, or creating AI reasoning services and serverless video processing systems.

alibaba serverless computing service

Image Source

When to Use Serverless Functions

Trying to decide whether serverless is right for you? Consider whether the reasons below apply to you.

1. You want to reduce server costs.

Going serverless will significantly reduce server costs. That‘s because cloud providers only charge for the time that serverless functions are running so you’re not paying for unused resources.

2. You want to free up developer resources.

Since no one has to deal with infrastructure management when going serverless, developers and engineers can focus solely on building out new features and improving the product.

3. You want to scale your web application.

Switching to serverless can help your web application scale with no operational concerns. Concert.ua, for example, switched to serverless architecture with AWS Fargate in 2022. Due to huge spikes in traffic, the website was not able to process customer payments so they could scale resources automatically in response to demand spikes.

4. You want to process batch jobs.

Some jobs only need to be run periodically, but planning server resources for them to occur can be challenging. From generating reports to scheduled tasks or data processing, serverless functions can handle these, so you don’t need to worry about managing servers for these routine batch jobs.

5. You’re building a chatbot.

If you want to deploy chatbots that respond to users in real time, serverless functions ensure that the bot can process the input and deliver a response using the user’s bot interaction as a trigger. This means you don’t need to run a server 24/7 to cover your interactive chatbot.

6. You want to automate file management.

Many applications process files as part of their functionality. Examples include resizing images, converting or compressing audio and video files, or generating PDFs. Serverless functions mean you can use a user’s file upload to trigger these functions and save on resources.

Going Serverless

At first glance, serverless environments can seem like a no brainer. But as I’ve learned, there are considerations to take into account before making the switch, and continuous optimizations are still needed to ensure your serverless functions and resource usage are optimal.

I think serverless is the future of software development. By using a platform that offers serverless functions like Content Hub, you’ll reap the benefits of reduced server expenses, auto-scaling, and improved uptime.

Editor's note: This post was originally published in February 2020 and has been updated for comprehensiveness.

HubSpot's Free Website Builder

Create and customize your own business website with an easy drag-and-drop website builder.

  • Build a website without any coding skills.
  • Pre-built themes and templates.
  • Built-in marketing tools and features.
  • And more!
Topics: Web Hosting

Related Articles

Create and customize your own business website with an easy drag-and-drop website builder.

BUILD A WEBSITE

CMS Hub is flexible for marketers, powerful for developers, and gives customers a personalized, secure experience

START FREE OR GET A DEMO