LAUNCHMongoDB 8.3 is built for the sub-100ms retrieval & zero downtime AI demands. Read blog >
AI DATAStop fighting your data layer. Get the memory & retrieval agents need to scale. Read blog >

What is FaaS and How Does It Work?

Get Started Free

Function as a service (FaaS), also known as serverless functions, represents a paradigm shift in cloud computing services, where developers can deploy individual functions to the cloud without managing the underlying cloud infrastructure. FaaS platforms like AWS Lambda, Google Cloud Functions, Microsoft Azure Functions, IBM Cloud Code Engine, MongoDB Atlas App Services Functions, and other cloud computing services handle the deployment, execution, and scaling of these functions.

FaaS simplifies the deployment and management of application code by utilizing a consumption-based pricing model, which charges based on actual resource usage. Such platforms allow developers to focus solely on writing and executing FaaS code using application programming interfaces (APIs) while the underlying infrastructure and network resources are managed by the service provider. This approach is ideal for breaking away from monolithic architecture, enabling the development of scalable, event-driven applications.

The key benefit to developers is FaaS gives teams the ability to run individual functions in response to events without managing servers. It’s ideal for mobile apps, scheduled tasks, and horizontally scalable scenarios. Developers write code in discrete functions, which FaaS platforms execute in response to triggers. This approach simplifies the development process, enabling rapid deployment of applications.

Table of contents

The benefits of FaaS

The core principle of function as a service is to decouple the deployment and execution of application code from the underlying infrastructure. This is achieved by hosting functions on public cloud services like AWS Lambda, Google Cloud Functions, MongoDB App Services, and other cloud computing models. These public cloud platforms abstract the hardware and automatically manage the scaling and execution of functions based on incoming requests.

Key advantages of FaaS

The key advantages of FaaS are:

  • Cost efficiency
  • Scalability
  • Support for a variety of programming languages
  • Seamless integration with cloud services
  • Robust monitoring and logging controls

For organizations, the key advantage of FaaS is its cost efficiency. Since billing is based on actual execution time, FaaS differs from other models because developers are not charged for idle resources. This model is particularly advantageous for applications with intermittent or unpredictable workloads, such as scheduled tasks, data processing pipelines, and real-time data analytics.

FaaS supports a range of major programming languages and runtime environments, providing many organizations with the flexibility to choose the best tools for their tasks. Each FaaS provider has its own set of supported languages and APIs, which ensure developers can use familiar technologies and frameworks, further streamlining the development process.

FaaS can integrate seamlessly with your cloud computing service, enabling the creation of complex, event-driven architectures. Events include HTTP requests, database changes, and message queue triggers that can automatically invoke functions, facilitating real-time processing and responsiveness.

FaaS and cloud computing services

There is a range of FaaS providers and each supports a unique set of languages for software developers writing functions with different characteristics and limitations. Choosing a cloud service that aligns with your development environment and ecosystem is essential.

MongoDB Atlas App Services Functions

Mongo DB Atlas App Services Functions are ideal for developers who want tight integration with MongoDB without relying heavily on external cloud services. This platform supports features like git repository integration for automated deployments, direct function editing in the browser, and robust security mechanisms. By leveraging MongoDB Atlas App Services Functions, developers can build highly responsive, data-driven web apps with minimal configuration and maintenance overhead.

MongoDB's Atlas Functions allow you to write JavaScript functions, with automatic integration with your MongoDB database cluster. These functions are automatically integrated with MongoDB Atlas databases, offering a seamless development experience. These functions can be triggered in various ways, including:

AWS Lambda

Amazon was the first public cloud provider to release a FaaS product. AWS Lambda natively supports functions written in Java, Go, PowerShell, Node.js, C#, Python, and Ruby. They also provide an API that allows you to write your functions in any programming language you prefer.

Lambda's integration with MongoDB Atlas is seamless, particularly since MongoDB Atlas clusters, including serverless instances, can be hosted on AWS, and a connection between both can be established through VPC peering.

This tight integration allows developers to build scalable and high-performance applications with minimal operational overhead. For more details, read about AWS Lambda functions with MongoDB.

Google Cloud Functions

Google's Cloud Function offers robust support for functions written in Node.js, Python, Go, Java, C#, Ruby, and PHP. As a part of the Google Cloud ecosystem, it provides deep integration with other Google services like BigQuery, Cloud Pub/Sub, and Firebase, making it a versatile choice for developers.

MongoDB clusters can be hosted on Google Cloud, facilitating smooth integration with Google Cloud Functions.

By using Google Cloud Functions with MongoDB Atlas, developers can create event-driven architectures where functions are triggered by changes in the database or other Google services, ensuring efficient and responsive application performance.

Microsoft Azure Functions

Microsoft's Azure Functions allows you to write functions in C#, JavaScript, F#, Java, PowerShell, Python, and TypeScript. Azure Functions are designed to integrate seamlessly with other Azure services such as Azure Cosmos DB, Azure Storage, and Azure Event Grid.

MongoDB Atlas supports deployment on Azure, making Azure Functions a suitable option for applications that leverage Microsoft's cloud ecosystem.

For example, developers can use Azure Functions to process real-time data streams, handle scheduled tasks, or integrate with other Azure services, while using MongoDB Atlas as the primary database.

How exactly does FaaS work?

FaaS platforms package function code into containers. The host platform then manages the routing of events to the function containers and scales them up or down as demand requires. This setup abstracts away the need for developers to manage infrastructure, allowing them to focus solely on writing application logic.

FaaS vs serverless

FaaS is a specialized subset of serverless computing. It refers to platforms that abstract away the hardware, enabling developers to focus on code. These platforms usually charge on a per-usage basis, and ideally they scale to zero, meaning minimum cost for applications when they are not in active use. FaaS specifically involves running discrete functions in response to events, providing an efficient and scalable solution for executing code without managing servers.

Pros and cons of FaaS

The main advantages of FaaS are:

  • Ease of deployment.
  • Pay-as-you-go cost model.
  • Automatic scaling of functions.

The primary disadvantages of FaaS include the following:

  • There are proprietary APIs for writing functions, leading to potential vendor lock-in.
  • Some applications don’t easily decompose into separate functions without a shared state.
  • Running the code locally requires a complex development environment.
  • They are difficult to maintain.

What to consider when using FaaS

FaaS architectures are event-based. Your functions are triggered by events such as HTTPS requests, messages from a message queue, scheduled triggers, or calls from another hosted function.

In a FaaS environment, functions are ideally stateless, which allows independent scaling by the host platform. While each function may be hosted in a separate VM or server, leading to limited persistent or shared state, this design encourages functional programming best practices. Stateless functions promote a functional programming approach, which enhances modularity, reusability, and maintainability for modern applications.

Functions are charged for the amount of time they execute. For this reason, it can be a good idea to optimize to keep the startup time and running time as low as possible. Each platform will have a time limitation after which functions will be forcibly shut down, so for this reason, it's important to ensure that functions at least execute in a reasonable amount of time.

Functions can scale more or less linearly, providing you can store your functions' data in a suitably scalable database (like MongoDB!).

In applications where the application builds up significant state in memory, a FaaS solution may not be suitable.

MongoDB and FaaS

Because MongoDB Atlas can deploy and manage clusters on all the three major cloud platforms (or even across two or more platforms!), choosing a particular cloud provider's function offering comes down more to the other services offered by that cloud platform. If you're integrating between your cloud provider's function offering and MongoDB, you have two choices. The first is to use the MongoDB driver for your language (such as PyMongo for Python functions).

When using the native MongoDB driver, ensure that the driver is cached between function calls. There's a cost to initializing the MongoDB driver, and so if you can share instances between different function calls, that will reduce your functions' run time. Take a look at the Developer Center for more best practices around MongoDB and AWS Lambda.

Another choice would be the MongoDB Atlas App Services platform, which provides functions as a service. App Services supports functions written in JavaScript and provides git repository integration for automated deployment, as well as the ability to write functions directly in the browser.

FAQs

Get started with Atlas today

Get started in seconds. Our free clusters come with 512 MB of storage so you can play around with sample data and get oriented with our platform.
Try FreeContact sales
GET STARTED WITH:
  • 125+ regions worldwide
  • Sample data sets
  • Always-on authentication
  • End-to-end encryption
  • Command line tools