Slide 1

Slide 1 text

In the Age of AI, we are still fi ghting to build CRUDs serverless ❤ mongodb

Slide 2

Slide 2 text

Luca Bianchi, PhD Who am I? Chief Technology O ff icer @ Neosperience a nd Neosperience He a lth, proud AWS Serverless Hero, p a ssion a te a bout softw a re a rchitectures, serverless, a nd m a chine le a rning. Serverless It a ly, [Gen]AI It a ly, a nd NextJS Meetup co-founder. ServerlessD a ys Mil a no a nd AWS Community D a y co-org a nizer. Big Daddy Little Elisa github.com/aletheia https://it.linkedin.com/in/lucabianchipavia https://speakerdeck.com/aletheia bianchiluca.com @bianchiluca

Slide 3

Slide 3 text

A not so easy de fi nition What is serverless? - pay-as-you-go - no pay for idle - managed - scales up and down e ff i ciently - …

Slide 4

Slide 4 text

— Anonymous CTO “serverless f ights complexity, serverless is servicefull.”

Slide 5

Slide 5 text

what do you mean by “serverless databases”?

Slide 6

Slide 6 text

Why? Serverless Databases Uncert a inity a nd unpredict a bility le a d to • Unpredict a ble worklo a ds • S aa S a pplic a tions • Infrequently used d a t a b a se • Purpose built d a t a b a ses

Slide 7

Slide 7 text

How? Serverless Databases M a n a gin uncert a inty brings a ddition a l requirements • Sc a l a bility • Reli a bility • E a se of m a n a gement • P a y- a s-you-go

Slide 8

Slide 8 text

What? Serverless Databases A serverless d a t a b a se is d a t a b a se technology where the underlying d a t a b a se servers a nd IT infr a structure a re fully m a n a ged. • on-dem a nd con f igur a tion • custom endpoint a nd re a d replic a • sc a l a bility by design

Slide 9

Slide 9 text

Fully managed database with serverless fl avor MongoDB Atlas • Fully M a n a ged Service • On-Dem a nd Sc a ling • No Prede f ined C a p a city • Ide a l for V a ri a ble Worklo a ds

Slide 10

Slide 10 text

calling serverless DB from serverless functions AWS Lambda with Mongo Atlas

Slide 11

Slide 11 text

calling Mongo Atlas DB from serverless functions AWS Lambda with Mongo Atlas

Slide 12

Slide 12 text

Thank you.

Slide 13

Slide 13 text

Thank you? …maybe not…

Slide 14

Slide 14 text

mongodb is more than this..

Slide 15

Slide 15 text

Atlas Functions serverless functions integrated within MongoDB’s cloud platform • executes logic within the database • javascript functions • event-driven • triggered by • database changes • App Services • external APIs

Slide 16

Slide 16 text

Atlas Triggers • Event-Driven: de fi ne actions that are automatically executed in response to certain database events (inserts, updates, or deletes) • Database, Authentication, and Scheduled triggers. • Real-Time Execution: eventually consistent immediate execution • Use Cases: task automating such as sending noti fi cations, updating related collections, logging changes, or initiating other work fl ows. Example When a new user document is inserted into the “users” collection, a trigger fi res and executes a function to send a welcome email or update a related “user_stats” collection.

Slide 17

Slide 17 text

Best Practices

Slide 18

Slide 18 text

Use MongoDB Atlas Connection Pooling with Lambda Best Practices 1 Issue AWS Lambda creates new instances (cold starts) for each invocation, which can lead to overhead in creating new database connections for every request. Best Practice Reuse the MongoDB Atlas connection across Lambda invocations by creating the MongoDB client outside of the main Lambda function handler. This prevents opening a new connection every time the function is invoked, which helps reduce latency and overhead.

Slide 19

Slide 19 text

Use VPC Peering or Private Endpoint for Secure Connectivity Best Practices 2 Issue Exposing your MongoDB Atlas cluster to the public internet could lead to security vulnerabilities. Best Practice Con fi gure VPC peering or Private Endpoints between your AWS Lambda’s VPC and MongoDB Atlas. This ensures that tra ff i c between Lambda and MongoDB is routed securely and privately, without traversing the public internet. • VPC Peering: Peers MongoDB Atlas with your AWS VPC. • Private Endpoint: Directly creates a private endpoint between MongoDB Atlas and AWS.

Slide 20

Slide 20 text

Environment Variables for Sensitive Data Best Practices 3 Issue Hardcoding MongoDB credentials and con fi guration details inside your Lambda function can lead to security risks Best Practice Store MongoDB Atlas connection strings, database names, and other sensitive con fi guration details in AWS Lambda environment variables or AWS Secrets Manager to keep your Lambda code secure

Slide 21

Slide 21 text

Minimize Cold Starts Best Practices 4 Issue Cold starts can add latency to your Lambda functions due to the initial overhead of creating new connections to MongoDB Atlas Best Practice Use Provisioned Concurrency to keep your Lambda functions warm, reducing the number of cold starts. This helps minimize latency and connection overhead. • Provisioned Concurrency pre-warms Lambda instances, ensuring they’re ready to handle requests instantly • Use Rust • Use LLRT

Slide 22

Slide 22 text

Timeouts and Retries Best Practices 5 Issue Unpredictable network conditions or temporary issues with MongoDB Atlas can lead to occasional timeouts Best Practice Set appropriate timeouts and use retry logic in your Lambda function to handle transient failures • Ensure MongoDB operations have a proper timeout to prevent Lambda executions from hanging • Implement retry logic, especially for reads and writes that can fail due to transient issues

Slide 23

Slide 23 text

Monitor and Optimize Performance Best Practices 6 Issue Without monitoring, it’s di ff i cult to understand performance bottlenecks or scaling issues Best Practice Use MongoDB Atlas Performance Advisor and AWS CloudWatch to monitor performance, identify slow queries, and get recommendations for improvements • Enable CloudWatch metrics for AWS Lambda to monitor invocation durations, errors, and resource utilization • Use MongoDB Atlas Performance Advisor to get insights into slow queries and recommendations on indexing

Slide 24

Slide 24 text

MongoDB Atlas ❤ AWS CDK

Slide 25

Slide 25 text

A L3 CDK construct mongodbatlas-awscdk/atlas-bootstrap Simpli fi es the process of preparing your environment to run the Atlas CDK by automating essential con fi gurations and resource provisioning • User Provisioning: creates a dedicated execution role • API Key Management: sets up an Amazon Secret Manager to securely store and manage API keys required for interacting with the Atlas services. • CloudFormation Extensions Activation: activation of CloudFormation public extensions essential for Atlas CDK.

Slide 26

Slide 26 text

MongoDB Atlas CDK construct

Slide 27

Slide 27 text

Thank you. now it’s true!