Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Serverless loves mongodb

Aletheia
October 01, 2024

Serverless loves mongodb

In the Age of AI, we are still fighting to build CRUDs

Aletheia

October 01, 2024
Tweet

More Decks by Aletheia

Other Decks in Technology

Transcript

  1. In the Age of AI, we are still fi ghting

    to build CRUDs serverless ❤ mongodb
  2. 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
  3. 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 - …
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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.
  10. 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.
  11. 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.
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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.