How to Build Serverless Applications with SST and MongoDB Atlas
Rate this article
Serverless computing is now becoming the standard for how applications are developed in the cloud. Serverless at its core lets developers create small packages of code that are executed by a cloud provider to respond to events. These events can range from HTTP requests to cron jobs, or even file upload notifications. These packages of code are called functions or Lambda functions, named after AWS Lambda, the AWS service that powers them. This model allows serverless services to scale with ease and be incredibly cost effective, as you only pay for the exact number of milliseconds it takes to execute them.
However, working with Lambda functions locally can be tricky. You’ll need to either emulate the events locally or redeploy them to the cloud every time you make a change. On the other hand, due to the event-based execution of these functions, you’ll need to use services that support a similar model as well. For instance, a traditional database expects you to hold on to a connection and reuse it to make queries. This doesn’t work well in the serverless model, since Lambda functions are effectively stateless. Every invocation of a Lambda function creates a new environment. The state from all previous invocations is lost unless committed to persistent storage.
Over the years, there has been a steady stream of improvements from the community to address these challenges. The developer experience is now at a point where it’s incredibly easy to build full-stack applications with serverless. In this post, we’ll look at the new way for building serverless apps. This includes using:
- Serverless Stack (SST), a framework for building serverless apps with a great local developer experience.
“With MongoDB Atlas and SST, it’s now easier than ever to build full-stack serverless applications.” — Andrew Davidson, Vice President, Product Management, MongoDB
Let’s look at how using these tools together can make it easy to build serverless applications.
Lambda functions are packages of code that are executed in response to cloud events. This makes it a little tricky to work with them locally, since you want them to respond to events that happen in the cloud. You can work around this by emulating these events locally or by redeploying your functions to test them. Both these approaches don’t work well in practice.
SST is a framework for building serverless applications that allows developers to work on their Lambda functions locally. It features something called Live Lambda Development that proxies the requests from the cloud to your local machine, executes them locally, and sends the results back. This allows you to work on your functions locally without having to redeploy them or emulate the events.
Live Lambda development thus allows you to work on your serverless applications, just as you would with a traditional server-based application.
Traditional databases operate under the assumption that there is a consistent connection between the application and the database. It also assumes that you’ll be responsible for scaling the database capacity, just as you would with your server-based application.
However, in a serverless context, you are simply writing the code and the cloud provider is responsible for scaling and operating the application. You expect your database to behave similarly as well. You also expect it to handle new connections automatically.
To address this issue, MongoDB Atlas launched serverless instances, currently available in preview. This allows developers to use MongoDB’s world class developer experience without any setup, maintenance, or tuning. You simply pick the region and you’ll receive an on-demand database endpoint for your application.
You can then make queries to your database, just as you normally would, and everything else is taken care of by Atlas. Serverless instances automatically scale up or down depending on your usage, so you never have to worry about provisioning more than you need and you only pay for what you use.
In this post, we saw a quick overview of the current serverless landscape, and how serverless computing abstracts and automates away many of the lower level infrastructure decisions. So, you can focus on building features that matter to your business!
For help building your first serverless application with SST and MongoDB Atlas, check out our tutorial: How to use MongoDB Atlas in your serverless app.
"MongoDB Atlas’ serverless instances and SST allow developers to leverage MongoDB’s unparalleled developer experience to build full-stack serverless apps." — Jay V, CEO, SST
Also make sure to check out the quick start and join the community forums for more insights on building serverless apps with MongoDB Atlas.