DEV Community

Cover image for What is a Serverless Function?
Adam Bavosa for PubNub

Posted on • Updated on • Originally published at

What is a Serverless Function?

Conventionally, serverless functions are single-purpose, programmatic functions that are hosted on managed infrastructure. These functions, which are invoked through the Internet, are hosted and maintained by cloud computing companies. The engineering teams within those companies ensure that the serverless functions have near-perfect uptime, redundant instances around the world, and scale to any incoming network request volume. So who creates serverless functions? It is not the cloud computing companies themselves; it's their customers. Software developers are moving their product code to serverless functions. This is quickly becoming a popular paradigm due to better code maintenance, record-low prices for hosting, and the peace of mind resulting from running them on managed infrastructure. In addition to those benefits, deployment of new code is quicker, simpler, and easily automated.

Monolithic Architecture vs. Microservice Architecture

Serverless functions can be considered microservices. In the software development community, there is growing excitement around the undertaking of breaking monolithic platforms into multiple, encapsulated microservices. Historically, monoliths have large, unified codebases, which require a full deploy of the entire platform for any shipment of new code. This includes new features, but also single line bug fixes. Monoliths can be practical in some situations, but when a platform’s codebase grows to the stage where the development team is large, some tasks become cumbersome. An architecture of microservices improves the maintainability of code bases and elevates the overall developer experience for software development teams. Microservices enable a large engineering organization to segment itself into loosely-coupled, autonomous teams. Each team can focus their attention on a few microservices that work independently from the rest of the platform. Note that while microservices can be maintained somewhat independently, they are still players on a unified team. With microservice architecture, new developers can be on-boarded to engineering projects rapidly, because they do not need to have a deep understanding of an entire monolith to begin making substantial contributions. Another advantage of microservices is that deployment of small code updates to single services results in little or no downtime for customers. When a monolith needs code updates, it might mean that all customers experience downtime for the duration of a deploy.

monolith compared to microservices

Functions-as-a-Service a.k.a. FaaS

The magic parts of serverless functions are autoscaling and redundant code deployments. This means that an app developer can concern themselves with one thing: writing great application code. The cloud hosting providers call this product offering Functions-as-a-Service or FaaS for short. With conventional app hosting, software developers need to repeatedly ask themselves these questions when they write their code:

  • Will my servers respond to all client requests, with very low latency, regardless of each client's physical location?
  • Am I creating room for human error in my code deployment process?
  • Can my servers handle sudden increases in request volume without overloading?
  • Can my servers handle sudden increases in request volume without wasting lots of money?
  • Do I need to constantly monitor my app's infrastructure? I sure do like sleeping 8 hours per night.

These questions do not apply when a software system is built upon a platform with Serverless Functions. Serverless Function code should have entirely stateless logic, so redundant instances will not cause inconsistencies for customers. Cloud hosting providers usually have many points-of-presence around the globe. This means that the servers in which an application runs are nearest to all possible end users. The cloud hosting provider will redundantly deploy a Serverless Function to data centers around the world, at the same time. This is good for a developer's customers because their client-side requests will be responded to with as little latency as possible. All of the networking logic is implemented by the cloud provider. Cloud hosting providers that offer Serverless Functions use industry standard best practices for automated code deployment. This means that there is no chance of human error breaking a service during deployment. It allows for fast shipment of new code, with little to no downtime for a web product. If there are 100+ global data centers that host a service, automation makes the whole deploy near-instant and more reliable. One of the most valuable features of Serverless Function hosting is out of the box autoscaling. Cloud hosting providers have made idle server costs a thing of the past for their customers. Thanks to software products like Kubernetes, services can programmatically scale their infrastructure in an automated fashion. Whether it be scaling up, or scaling down, this new kind of "elastic" infrastructure makes hosting more efficient. This results in big cost savings for companies that buy cloud hosting. Cloud hosting providers have internal engineering teams that monitor the Serverless infrastructure 24/7/365. This is included in the price developers pay for hosting. The 3am Pager Duty phone calls become a thing of the past after Serverless Functions are leveraged.

FaaS Providers

Cloud hosting companies are offering bleeding-edge FaaS platforms. Server costs are going down for consumers globally, as big tech companies build more and more server farms. However, not all FaaS services are created equal. FaaS platforms are not like the telephone company. Each platform has unique scenarios in which it shines.

AWS Lambda

AWS Lambda is a serverless function environment on Amazon Web Services. The supported programming languages are Java, Go, PowerShell, Node.js, C#, Python, and Ruby. Using Lambda functions is ideal in scenarios where on-demand compute is required e. g. provide compute for operations like file processing like file conversion.

Azure Functions

Azure Functions is a serverless function environment on Microsoft Azure. The supported programming languages are C#, JavaScript, F#, Java, Python, and TypeScript (transpiled JS). There are more languages that are currently experimental on Azure Functions. Using Lambda functions is ideal in scenarios where a single-purpose API needs to fill in functionality for a platform e. g. provide an HTTP endpoint for securely storing application data.

Google Cloud Functions

Google Cloud Functions is a serverless function environment on Google Cloud Platform. The supported programming languages are JavaScript, Python, and Go. Google Cloud Functions provide serverless compute that interacts succinctly with other Google Cloud Services and client applications. Using it is ideal in scenarios where data processing is key - like retrieving relevant data from images and videos.

PubNub Functions

PubNub Functions is a serverless environment to execute functions on the edge, transforming, enriching, and filtering messages as they route through the PubNub network. JavaScript is the only supported programing language. This serverless platform differs fundamentally from the others by way of execution. A PubNub Function's serverless event handler executes when a message is published with PubNub's Pub/Sub API. A developer can execute code on an in-transit PubNub message after its publish, but before the message reaches a subscriber. There are other execution configurations including non-blocking after publish, and a REST API Endpoint that can be accessed like a Node.js server. State can be managed using a built using KV-Store within the functions environment. There is also a catalog of open source, 3rd party integrations on the PubNub Blocks Catalog.

Which FaaS Provider should I use for my Service?

The answer to this question depends on the type of service that needs to be built. As you can see from the providers described above, there are different programming languages that are usable, depending on the cloud. The most important thing to consider when selecting a provider is: which attributes are mission-critical for my service? The largest cloud companies (AWS, Azure, Google) provide solutions that are meant to fit nearly every situation with generic cloud products. Companies like PubNub focus on specialization in addressing developer pain points so PubNub outshines generic serverless products exclusively in realtime use cases. PubNub Functions make in-transit message computation exhibit extremely low latency execution. Additionally, PubNub Functions are easy to build and easy to deploy. PubNub Functions are ideal for realtime applications like Chat, GPS location tracking, IoT signaling, multiplayer games, and more. If your needs are heavy in computation, heavy in languages that need OS access, and are not real-time critical, you are better off using any of the big cloud hosting companies. See the comparison of PubNub Functions and AWS Lambda on the PubNub blog for even more insights on the topic of choosing a serverless provider.

Top comments (0)