EventJoin us at AWS re:Invent 2024! Learn how to use MongoDB for AI use cases. Learn more >>Join us at AWS re:Invent 2024! Learn how to use MongoDB for AI use cases. >>

What is Serverless Architecture?

What is serverless?

Serverless is a next-gen cloud-native development model that allows developers to build applications and run code without thinking about server provisioning, management and scaling. The term “serverless” does not imply the absence of servers but instead reflects the fact that server provisioning and management has been abstracted (or hidden) from the end-user.

With the serverless model, development teams can focus on writing and shipping code, and the underlying infrastructure will simply scale as needed - allowing developers to reduce operational overhead and speed time to market.

How serverless architecture works

A serverless architecture implies that applications are being built leveraging a collection of fully-managed, hosted cloud services. In a serverless architecture, a third party vendor, often a cloud provider, will take care of the server provisioning and scaling as your applications demand and will only charge you based on your usage. Typically, when nothing is being executed, you don't pay for anything.

In a traditional architecture, development teams have to worry about spinning up virtual machines, or purchasing the servers required to run their applications for a high cost upfront, which has some immediate drawbacks:

  • You're paying for the instance as long as you have it up, whether or not it's doing anything

  • Server scaling is typically manual - you need to keep an eye on usage and scale the instance up or down as needed

  • If you do need to scale, it's a stepwise function to the next tier - it may be 30% bigger (and 30% more expensive) when you only need 1% more in the short term

The serverless model offers a more cost effective way for developers to build and run applications while also allowing them to focus on business logic. Instead of spending your time worrying about managing servers or planning an instance tier upgrade in advance, you can just write your code and let it run at the necessary scale.

One of the most common forms of serverless architecture includes function as a service, or FaaS. Function as a service is a form of serverless cloud computing that enables developers to build, run, and manage business logic for their application where each logical function is an ephemeral, individual unit. Functions are event driven, meaning when a serverless function is called, the service instantiates it in a container where it runs as required producing the appropriate output. When there's no more demand, the container is torn down.

Because instances aren't always running, FaaS can have what's called the cold start problem. If a function hasn't been called for a while, a response can have an extra wait time while the instance is set up. As long as there's continued usage, the infrastructure stays up and the latency is shorter. However, if a long enough time goes without the function being used, the instance is removed and the next call will have a cold start.

Serverless is more than just FaaS

FaaS is oftentimes referred to as serverless computing, though the term is also used more broadly. AWS Lambda, is often credited to have first introduced serverless computing to the masses in 2014 and as a result many developers often think of serverless computing to be synonymous with Function as a service (FaaS). However there are a variety of technologies and services that now fit the serverless model - including BaaS (backend-as-a-service) solutions, application development platforms, and now even databases.

Backend as a Service (BaaS)

Where FaaS lets you run code in a serverless fashion, a Backend as a Service (BaaS) runs the full backend of an application using a serverless execution model. Backend as a service platforms replace server-side self-managed components with serverless, managed back-end services, and typically consist of functionality including database management, cloud storage, authentication, push notifications, APIs and more. In BaaS architectures, clients connect to the BaaS, and the BaaS connects to a database (or may have one built in). With BaaS, serverless functions are usually called via an API or API gateway.

By using serverless BaaS platforms, like Google Firebase or AWS Amplify, developers are able to build web and mobile applications without needing to worry about any of the behind-the-scenes aspects, and can instead focus solely on the frontend.

Some Backends as a Service platforms are fully serverless, scaling infrastructure and cost precisely with demand, while on the other hand others may still use the concept of instance size or an hourly charge even if there is no usage.

Serverless Application Development Platforms

Similar to BaaS, there are also frontend application development platforms, like Vercel and Netlify, that run on a serverless architecture. These platforms offer developers everything they need to build front-end applications and use serverless functions to provide the backend to run your server-side code. These platforms focus on ease of use by transforming difficult-to-reason functions into specific REST or GraphQL API endpoints, removing the need for a user to deploy and manage FaaS. They often require no orchestration and will scale automatically with application demand; however, it is still on the developer to integrate these platforms with a database to store their application data.

Serverless Databases

The benefits of serverless computing and serverless architecture are now also being extended to databases, with the concept of serverless databases still being relatively new. With a traditional cloud Database as a Service (DBaaS), the database is fully managed and hosted for you and deployed as a specific pre-provisioned instance size with a specific price. A serverless architecture for a cloud database means that you don't choose an instance size at all - instead you simply set up a database and it grows as necessary with data size and throughput - and the cost scales accordingly.

In contrast to traditional serverless computing offerings like FaaS, serverless databases must still maintain state in order to securely store the data for your application and you will likely pay a small amount for data storage even in the absence of traffic to your app.

Serverless Architecture vs Container Architecture

Both serverless and container architectures can reduce infrastructure overhead by enabling developers to break application code into smaller components, or microservices, however they do have significant differences. Containers are ephemeral packages of both application code and their dependencies that run in an isolated environment and can be scaled up or down in size as needed.

In general, developers that manage containers must update the configurations and dependency of each deployment and maintain the container's system. Where maintenance on serverless services is entirely managed by the vendor (the cloud provider or service provider). Container architectures can be automated by using a container orchestration platform, like Kubernetes, an open-source system for automating the deployment, scaling, and management of containerized applications.

Some developers may opt to use a hybrid architecture, with serverless functions deployed in containers to allow for more control and in fact, there are even solutions built around this model, like Google Cloud Run and AWS Fargate. These platforms allow users to run containers on top of a serverless infrastructure - abstracting any of the underlying infrastructure management typically associated with building and hosting containerized applications.

The benefits of serverless computing

Serverless computing offers a number of benefits over traditional development approaches:

Faster time to market: Serverless architecture can help you build applications faster by removing the operational overhead of provisioning and managing your own infrastructure. With no infrastructure to set up and no dependencies on other teams, most serverless services allow you to start building instantly.

Elastic scalability: With serverless solutions, scaling is handled by a vendor without any work on your part. If you get unexpected peaks in usage, the service will automatically scale up to meet demand and back down when traffic slows, without any interruption to your users.

Increased developer productivity: When building apps with serverless architecture developers can focus on writing application code and business logic that sets their applications apart, instead of spending time on operational tasks like scaling servers.

Cost effectiveness: Serverless platforms are generally very cost effective in comparison to a traditional cloud platform where you are likely to pay for unused resources like CPU, and RAM. Serverless platforms leverage a usage-based model, where you're only paying for what you use and you're never charged for infrastructure sitting idle. It's important to note that specific pricing varies by service, so you should always make sure you understand the pricing model to avoid costs escalating quickly.

When should you use serverless computing?

Like with many development approaches, it's important to consider whether or not a serverless approach is the right fit for your application and your business. Generally speaking, you should consider a serverless architecture in any of these scenarios:

  • You don't know what scale of workload to expect

  • You don't want to pay for unused resources

  • You want to avoid infrastructure management as much as possible

  • Your application logic can be broken apart into discrete functions

  • You want to host your entire application backend without managing infrastructure

  • You need a small piece of custom functionality in a workflow of other services

  • You want to connect a large number of edge devices to a backend (for example, mobile or IoT)

In fact, these scenarios apply to most modern applications. So if you start by considering serverless options, when does it not make sense? If you're not already on board with the cloud, or you need to host your own infrastructure for regulatory reasons, serverless architecture obviously isn't a good choice. Applications that experience steady ongoing traffic, are mission-critical and require low latency, or have strict security or compliance requirements might also not be the best fit for serverless and likely would be better suited to run in a dedicated cloud environment where developers have more fine-grained controls.

As always, you should investigate a serverless offering carefully to confirm that it meets your requirements for latency (i.e. minimal cold start issues), security, and so on. And generally, most serverless technologies have specific patterns of development built in, so make sure you like this way of working before you commit.

Use cases for serverless applications

Serverless architecture or serverless computing can be used for a variety of use cases but is most often used for applications that either see infrequent or unpredictable surges in demand or are event-driven.  Some of the most common serverless application use cases include:

Rapid Application Development

One of the most common use cases for serverless technologies is application development and testing. When it comes to building and testing applications, there are often unknown factors and idle periods when paying for pre-provisioned infrastructure may not make sense. Leveraging serverless solutions not only can be cost effective but also removes barriers to entry making it easy to get started and iterate quickly without operational overhead. 

As serverless solutions continue to grow in popularity, it is only common that more developers will be looking to leverage them to develop new applications at a quicker pace.

Event-Based or Event-Driven Tasks/ Applications

Another common use case where serverless technologies are extremely valuable is for building event-driven applications or microservices. Event-driven microservices are designed to respond in real-time to events like user actions or system changes, and do so by using triggers to automatically execute serverless functions.  Due to their nature,  microservices can benefit from the instant, elastic scalability that only serverless can offer and the cost economics of only paying for resources used when there is activity.

Continuous Integration and Continuous Delivery (CI/CD)

CI/CD is a method to deliver applications and new code by introducing automations into the app development lifecycle, from integration and testing phases to delivery and deployment. 

Serverless architectures can also be used to automate many of the stages in CI/CD pipelines. For example, code commits can trigger a function to create a build, and pull requests can trigger automated tests.

Edge Computing

Edge computing is a distributed computing paradigm that brings application computation and data storage closer to the data sources (i.e. IoT devices) to help improve response time latency, and provide faster insights. 

Serverless solutions can be well suited for edge computing as they are highly scalable and lightweight to colocate or distribute closer to your end users. In the example of IoT, edge use cases can often involve intermittent spikes in usage and benefit from the on-demand scale serverless can provide.

While the above use cases highlight how to build serverless apps, serverless solutions can also be used to build specific components of your application code or architecture, like building RESTful APIs (leveraging serverless functions alongside an API gateway) or in the case of backend as a service, building your entire middleware and backend layers.

How to Build Serverless Applications with MongoDB Atlas

MongoDB Atlas is a fully-managed developer data platform designed to enable developers to build modern serverless applications. Built on the document model, Atlas offers developers the flexibility to work with a variety of data and workload types all within a single unified solution - helping reduce infrastructure complexity. With serverless and pre-provisioned deployment types and a secure, dependable, and scalable foundation, you can build applications of any size with confidence.

Unlike other solutions, Atlas also gives developers the flexibility to run on the cloud provider of their choice (available on AWS, Google Cloud and Azure) so you never have to worry about vendor lock-in.

MongoDB Atlas developer data platform

Atlas offers several services that abstract away the provisioning and management of servers to provide the benefits of a serverless architecture and seamlessly integrates with a variety of popular platforms and frameworks, including:

  • AWS Lambda, AWS Fargate, Amazon EventBridge, AWS AppSync

  • Azure Functions, Azure App Service

  • Google Cloud Functions, Google Cloud Run

  • Vercel

  • Netlify

  • and many others

Giving developers the building blocks to not only build best in class applications, but the flexibility to build them with the stack of their choice, using the tools and services they already know and love.

Below are some examples of how Atlas can be used to meet certain serverless application requirements but it's important to note that the capabilities mentioned can be leveraged independently or together depending on the specific needs of the user or organization.

Build serverless microservices

Serverless microservices are made up of serverless functions to perform specific roles within an application.  MongoDB Atlas provides the functionality to enable developers to easily build serverless microservices with fully managed cloud services such as cloud Functions, Triggers, and APIs (REST and GraphQL) available directly in our developer data platform.  

Serverless microservices are generally easy to manage and scale, making them well suited for complex, evolving applications and those which can be broken down into short, event-driven tasks.

Build an entirely serverless backend on Mongodb

On the other hand, many developers looking to build serverless apps may want to completely abstract the backend and will opt for a BaaS solution to help speed development. MongoDB Atlas provides all of the building blocks to enable developers to build a serverless backend for their web and mobile apps by using a combination of cloud Functions, Triggers, and APIs coupled with built-in authentication and role-based data access controls. For mobile apps, Atlas also has a fully-managed, device-to-cloud synchronization solution, Atlas Device Sync, to manage conflict resolution, keep your data up-to-date across devices, users, and your backend. Each service is built to independently auto scale to meet demand.

Optimize scaling for unpredicitable or spikey workloads

Many serverless applications are designed to handle unpredictable or large spikes in traffic and it's important that the database is equipped to do the same. Atlas offers elastic auto scaling in both dedicated clusters and serverless instances to take the burden off of development teams.

  • Atlas serverless instances provide an on-demand endpoint that scales automatically to meet application demand without any up front provisioning. They are based on an operations-based pricing model that charges only for the resources and storage used and will scale down to zero if there is no traffic.

  • Atlas dedicated clusters come in a range of cluster tiers based on storage and compute resource needs with auto-scaling enabled out of the box to scale up or down based on utilization. Unlike serverless instances which scale dynamically, dedicated clusters give developers more control with the option to set min and max scaling thresholds, or scale to another cluster tier manually if a spike is expected.

Improve costs for lower traffic applications or developement environments

For applications that don't experience steady traffic or development environments that aren't always in use, it may not make sense to pay for a pre-provisioned database. In this scenario, Atlas serverless instances can be used to optimize costs and avoid paying for unused resources while still ensuring your database can scale up instantly when there is traffic.

Use Mongodb Atlas as the data platform for your existing serverless app

If you’ve already started developing your serverless application, or have an existing serverless application in production already - MongoDB Atlas can help you take it to the next level. Our developer data platform gives you access to an extended set of data services, from search, data visualization, edge to cloud sync, data archival and more, to help you meet evolving application requirements without introducing added complexity, while also giving you the flexibility to deploy your application on the infrastructure that best suits your needs.  MongoDB Atlas integrates with a wide range of popular development tools and services allowing you to build and run serverless applications with the architecture and stack of your choice.

Severless reference architecture - MongoDB integrations

PLEASE NOTE: This diagram is not meant to be inclusive of all of the solutions that can be used alongside MongoDB Atlas. While it highlights the typical components you may find in a serverless architecture, it is not representative of the only way to build applications with MongoDB.

How to Get Started with MongoDB Atlas

Whether you're already using serverless technologies as a part of your stack or just getting started - MongoDB Atlas can help you take your apps to the next level.

Get Started with MongoDB

Sign up for MongoDB Atlas and start building serverless applications today.

FAQs

Is serverless computing secure?

A common misconception of serverless computing is that it is not secure or can impose security risks over self-hosted or traditional cloud computing offerings. However, in serverless architecture, the cloud provider or service provider manages the infrastructure security for you. So there is no need to worry about security misconfigurations and potential vulnerability issues. But the application security is still your responsibility.  With MongoDB Atlas, we ensure enterprise grade security controls are enforced out of the box, so you can build and deploy your applications with the confidence that your data is secure. Learn more about our security controls.

What is the role of cloud providers in serverless computing?

Similar to fully-managed cloud hosted solutions, in a serverless architecture, the cloud providers, or service providers, are responsible for the server provisioning and scaling as your applications demand. They are also responsible for managing security controls and version upgrades.

How is serverless different from PaaS?

Serverless and PaaS (platform as a service) solutions are similar in that the underlying infrastructure and servers are abstracted from the end user of the service. However, there are some notable differences that differentiate one from the other - most notably, being the scalability and the pricing model. Unlike serverless solutions, PaaS solutions are not capable of instantly auto scaling out of the box and typically require work on the developers part to forecast and configure the solution to scale automatically. Serverless solutions are also charged based on the precise amount of resources consumed, whereas PaaS solutions typically rely on a pre-provisioned price model and charge a flat fee based on the server resources (compute, storage, and so on) decided upon in advance.

Does serverless mean I don't need a database?

Despite a serverless model abstracting the backend completely from the end user, a database is still needed to run and host your application. While there are backend-as-a-service solutions that take care of the data storage for you, it’s important to choose a database that can best suit both your current and future application needs. Unlike other models, a document database can provide greater flexibility over traditional SQL, allowing you to build applications faster without being tasked with complex schema planning upfront or the need to re-architect your schema down the line with new application requirements.