SlideShare a Scribd company logo
1 of 26
Download to read offline
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
SRV301 - Best Practices for using
AWS Lambda with RDS-RDBMS
solutions
Mayank Thakkar
Global Life Sciences Solutions Architect
AWS
S R V 3 0 1
Emma Zhao
Software Development Manager
AWS
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Typical Solution Architecture
AWS Cloud
VPC
Availability zone 1
Availability zone 2
Private subnet
Private subnet
Private subnet
Private subnet
DB Instance
DB instance
standby
Elastic
network
interface
Elastic
network
interface
Lambda
Functions
Security group Security group
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Lambda with RDS / RDBMS - Best Practices
• Use subnets in multiple AZs for ENI configuration
• Protects against AZ failure and IP address exhaustion
• Lambda relies on ENIs (within your VPC) to connect
• Scalability depends on available free IP addresses and ENI limits
• ENI startup penalty
• Lambda in VPC mode does not have internet access by default.
• Add Network Address Translation (NAT) gateway inside the VPC to
enable internet access
• Avoid DNS resolution of public host names if possible
• Adds billable, time consuming resolution.
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Problem: Managing Database Connections
…
Lambda can scale, can your database connections?
Is Connection
pooling the answer?
Generic database
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Time
Container A
Container B
Container C
Container D
Lambda - Scaling
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
How does that affect Connection Pooling?
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Connection Pooling with Lambda
Use only one connection in a container, at a time
Set Connection Pool Size = 1
OR
Initialize the connection object “above the handler”
(in the global section)
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Connection Pooling with Lambda - Challenges
• No hook to indicate destruction of Lambda containers
• Can not explicitly close connections when the container is
recycled
• Fall back on Database TTL to clean up connections
• Can lead to session leakages
• No control over lifecycle of Lambda containers
• Can result many connections sitting idle
• Different containers for different Lambda functions
• No re use of connections across Lambda functions
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Solution 1a: Account Concurrency Limit
Use AWS Organizations to manage your accounts.
AWS Account
Lambda can help
with retries of
throttled
invocations*
* - Only for asynchronous and stream based event sources.
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Solution 1b: Function Concurrency Limit
Lambda can help
with retries of
throttled
invocations*
* - Only for asynchronous and stream based event sources.
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Concurrency Limits - Benefits
• Lambda Concurrency limit can be set at the
account level or at function level.
• Simple to set up
• Account level limit requires AWS support
• AWS Lambda manages retries for throttled
invocations*
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Concurrency Limits - Challenges
• Account Level limit:
• Covers Lambdas across the account – Difficult to isolate DB
interfacing functions
• Possibility of accounts sprawl.
• Function Level limit:
• Prior knowledge of DB accessing functions required.
• Limit should correspond to peak concurrency – prior
application behavior knowledge required
• Can not spread out connections dynamically, as load across
Lambdas varies
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Dynamic Connection Management
Helper
Maintain atomic counter
and max count
Open / close
connections
DB operations
Do something interesting
Generic database
Alarm
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Demo Code: https://tinyurl.com/lambdards
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Dynamic Connection Management - Benefits
• Scalable solution : depends on Lambda and Amazon DynamoDB
• Connections count managed from DB perspective
No limit on number of Lambda functions
• Can spread DB connections across Lambda functions @ runtime
Eliminate choke points and better serve hotspots
• Easy update : Control usable connections count through DynamoDB
• Use published CloudWatch metrics to react to out-of-connection
errors
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Dynamic Connection Management - Challenges
• No retries of throttled invocations: requires custom solution.
• More resources to manage.
• No connection re-use.
• Slightly increased latency.
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Summary
• Lambda Scaling Model and Connection pooling do not
mix
• Use a single connection, initialized in the global section
• Solutions and Best Practices:
• Account and function level concurrency Limits
• Custom Solution: Dynamic Connection Management
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
References
• AWS Organizations
https://aws.amazon.com/organizations/
• AWS Serverless :
https://aws.amazon.com/serverless/
• AWS Lambda Container Reuse:
https://aws.amazon.com/blogs/compute/container-reuse-in-lambda/
• Lambda - Retries of throttled invocations:
http://docs.aws.amazon.com/lambda/latest/dg/concurrent-
executions.html#concurrent-execution-safety-limit
• Dynamic Connections Manager Sample Code :
https://tinyurl.com/lambdards
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Questions?
Demo Code: https://tinyurl.com/lambdards
Thank you!
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Mayank Thakkar
Global Life Sciences Solutions Architect
AWS
Emma Zhao
Software Development Manager
AWS
Demo Code: https://tinyurl.com/lambdards
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

More Related Content

What's hot

Silverlight - What Is It And How Can We Use It
Silverlight - What Is It And How Can We Use ItSilverlight - What Is It And How Can We Use It
Silverlight - What Is It And How Can We Use It
Venketash (Pat) Ramadass
 

What's hot (9)

Python
PythonPython
Python
 
Silverlight - What Is It And How Can We Use It
Silverlight - What Is It And How Can We Use ItSilverlight - What Is It And How Can We Use It
Silverlight - What Is It And How Can We Use It
 
Introduction to spaCy
Introduction to spaCyIntroduction to spaCy
Introduction to spaCy
 
PHP の GC の話
PHP の GC の話PHP の GC の話
PHP の GC の話
 
cpu scheduling in os
cpu scheduling in oscpu scheduling in os
cpu scheduling in os
 
Improving Customer Experience with Amazon Connect – The AI Driven Contact Cen...
Improving Customer Experience with Amazon Connect – The AI Driven Contact Cen...Improving Customer Experience with Amazon Connect – The AI Driven Contact Cen...
Improving Customer Experience with Amazon Connect – The AI Driven Contact Cen...
 
Monitoreo Atraves de GLpi
Monitoreo Atraves de GLpiMonitoreo Atraves de GLpi
Monitoreo Atraves de GLpi
 
React Native 入門
React Native 入門React Native 入門
React Native 入門
 
Machine Learning at the Edge
Machine Learning at the EdgeMachine Learning at the Edge
Machine Learning at the Edge
 

Similar to Best Practices for Using AWS Lambda with RDS/RDBMS Solutions (SRV301-R1) - AWS re:Invent 2018

Serverless Architecture - Design Patterns and Best Practices
Serverless Architecture - Design Patterns and Best PracticesServerless Architecture - Design Patterns and Best Practices
Serverless Architecture - Design Patterns and Best Practices
Amazon Web Services
 

Similar to Best Practices for Using AWS Lambda with RDS/RDBMS Solutions (SRV301-R1) - AWS re:Invent 2018 (20)

Best Practices for using AWS Lambda with RDS-RDBMS Solutions (SRV320)
Best Practices for using AWS Lambda with RDS-RDBMS Solutions (SRV320)Best Practices for using AWS Lambda with RDS-RDBMS Solutions (SRV320)
Best Practices for using AWS Lambda with RDS-RDBMS Solutions (SRV320)
 
Serverless use cases with AWS Lambda - More Serverless Event
Serverless use cases with AWS Lambda - More Serverless EventServerless use cases with AWS Lambda - More Serverless Event
Serverless use cases with AWS Lambda - More Serverless Event
 
Gluecon 2018 - The Best Practices and Hard Lessons Learned of Serverless Appl...
Gluecon 2018 - The Best Practices and Hard Lessons Learned of Serverless Appl...Gluecon 2018 - The Best Practices and Hard Lessons Learned of Serverless Appl...
Gluecon 2018 - The Best Practices and Hard Lessons Learned of Serverless Appl...
 
Build Modern Applications that Align with Twelve-Factor Methods (API303) - AW...
Build Modern Applications that Align with Twelve-Factor Methods (API303) - AW...Build Modern Applications that Align with Twelve-Factor Methods (API303) - AW...
Build Modern Applications that Align with Twelve-Factor Methods (API303) - AW...
 
Serverless Architectural Patterns: Collision 2018
Serverless Architectural Patterns: Collision 2018Serverless Architectural Patterns: Collision 2018
Serverless Architectural Patterns: Collision 2018
 
Serverless Architecture - Design Patterns and Best Practices
Serverless Architecture - Design Patterns and Best PracticesServerless Architecture - Design Patterns and Best Practices
Serverless Architecture - Design Patterns and Best Practices
 
Scale up a Web Application (ARC409-R) - AWS re:Invent 2018
Scale up a Web Application (ARC409-R) - AWS re:Invent 2018Scale up a Web Application (ARC409-R) - AWS re:Invent 2018
Scale up a Web Application (ARC409-R) - AWS re:Invent 2018
 
SRV315 Building Enterprise-Grade Serverless Apps
 SRV315 Building Enterprise-Grade Serverless Apps SRV315 Building Enterprise-Grade Serverless Apps
SRV315 Building Enterprise-Grade Serverless Apps
 
Serverless SaaS apllications on AWS
Serverless SaaS apllications on AWSServerless SaaS apllications on AWS
Serverless SaaS apllications on AWS
 
Become a Serverless Black Belt - Optimizing Your Serverless Applications - AW...
Become a Serverless Black Belt - Optimizing Your Serverless Applications - AW...Become a Serverless Black Belt - Optimizing Your Serverless Applications - AW...
Become a Serverless Black Belt - Optimizing Your Serverless Applications - AW...
 
All the Ops you need to know to Dev Serverless
All the Ops you need to know to Dev ServerlessAll the Ops you need to know to Dev Serverless
All the Ops you need to know to Dev Serverless
 
AWS Lambda use cases and best practices - Builders Day Israel
AWS Lambda use cases and best practices - Builders Day IsraelAWS Lambda use cases and best practices - Builders Day Israel
AWS Lambda use cases and best practices - Builders Day Israel
 
Serverless computing - Build and run applications without thinking about servers
Serverless computing - Build and run applications without thinking about serversServerless computing - Build and run applications without thinking about servers
Serverless computing - Build and run applications without thinking about servers
 
Serverless Architectural Patterns
Serverless Architectural PatternsServerless Architectural Patterns
Serverless Architectural Patterns
 
Ci/CD for AWS Lambda Projects - JLM CTO Club
Ci/CD for AWS Lambda Projects - JLM CTO ClubCi/CD for AWS Lambda Projects - JLM CTO Club
Ci/CD for AWS Lambda Projects - JLM CTO Club
 
Serverless on AWS: Architectural Patterns and Best Practices
Serverless on AWS: Architectural Patterns and Best PracticesServerless on AWS: Architectural Patterns and Best Practices
Serverless on AWS: Architectural Patterns and Best Practices
 
Serverless Computing: build and run applications without thinking about servers
Serverless Computing: build and run applications without thinking about serversServerless Computing: build and run applications without thinking about servers
Serverless Computing: build and run applications without thinking about servers
 
Microservices & Data Design
Microservices & Data DesignMicroservices & Data Design
Microservices & Data Design
 
Microservices & Data Design: Database Week San Francisco
Microservices & Data Design: Database Week San FranciscoMicroservices & Data Design: Database Week San Francisco
Microservices & Data Design: Database Week San Francisco
 
Microservices & Data Design: Database Week SF
Microservices & Data Design: Database Week SFMicroservices & Data Design: Database Week SF
Microservices & Data Design: Database Week SF
 

More from Amazon Web Services

Tools for building your MVP on AWS
Tools for building your MVP on AWSTools for building your MVP on AWS
Tools for building your MVP on AWS
Amazon Web Services
 
How to Build a Winning Pitch Deck
How to Build a Winning Pitch DeckHow to Build a Winning Pitch Deck
How to Build a Winning Pitch Deck
Amazon Web Services
 
Building a web application without servers
Building a web application without serversBuilding a web application without servers
Building a web application without servers
Amazon Web Services
 
AWS_HK_StartupDay_Building Interactive websites while automating for efficien...
AWS_HK_StartupDay_Building Interactive websites while automating for efficien...AWS_HK_StartupDay_Building Interactive websites while automating for efficien...
AWS_HK_StartupDay_Building Interactive websites while automating for efficien...
Amazon Web Services
 

More from Amazon Web Services (20)

Come costruire servizi di Forecasting sfruttando algoritmi di ML e deep learn...
Come costruire servizi di Forecasting sfruttando algoritmi di ML e deep learn...Come costruire servizi di Forecasting sfruttando algoritmi di ML e deep learn...
Come costruire servizi di Forecasting sfruttando algoritmi di ML e deep learn...
 
Big Data per le Startup: come creare applicazioni Big Data in modalità Server...
Big Data per le Startup: come creare applicazioni Big Data in modalità Server...Big Data per le Startup: come creare applicazioni Big Data in modalità Server...
Big Data per le Startup: come creare applicazioni Big Data in modalità Server...
 
Esegui pod serverless con Amazon EKS e AWS Fargate
Esegui pod serverless con Amazon EKS e AWS FargateEsegui pod serverless con Amazon EKS e AWS Fargate
Esegui pod serverless con Amazon EKS e AWS Fargate
 
Costruire Applicazioni Moderne con AWS
Costruire Applicazioni Moderne con AWSCostruire Applicazioni Moderne con AWS
Costruire Applicazioni Moderne con AWS
 
Come spendere fino al 90% in meno con i container e le istanze spot
Come spendere fino al 90% in meno con i container e le istanze spot Come spendere fino al 90% in meno con i container e le istanze spot
Come spendere fino al 90% in meno con i container e le istanze spot
 
Open banking as a service
Open banking as a serviceOpen banking as a service
Open banking as a service
 
Rendi unica l’offerta della tua startup sul mercato con i servizi Machine Lea...
Rendi unica l’offerta della tua startup sul mercato con i servizi Machine Lea...Rendi unica l’offerta della tua startup sul mercato con i servizi Machine Lea...
Rendi unica l’offerta della tua startup sul mercato con i servizi Machine Lea...
 
OpsWorks Configuration Management: automatizza la gestione e i deployment del...
OpsWorks Configuration Management: automatizza la gestione e i deployment del...OpsWorks Configuration Management: automatizza la gestione e i deployment del...
OpsWorks Configuration Management: automatizza la gestione e i deployment del...
 
Microsoft Active Directory su AWS per supportare i tuoi Windows Workloads
Microsoft Active Directory su AWS per supportare i tuoi Windows WorkloadsMicrosoft Active Directory su AWS per supportare i tuoi Windows Workloads
Microsoft Active Directory su AWS per supportare i tuoi Windows Workloads
 
Computer Vision con AWS
Computer Vision con AWSComputer Vision con AWS
Computer Vision con AWS
 
Database Oracle e VMware Cloud on AWS i miti da sfatare
Database Oracle e VMware Cloud on AWS i miti da sfatareDatabase Oracle e VMware Cloud on AWS i miti da sfatare
Database Oracle e VMware Cloud on AWS i miti da sfatare
 
Crea la tua prima serverless ledger-based app con QLDB e NodeJS
Crea la tua prima serverless ledger-based app con QLDB e NodeJSCrea la tua prima serverless ledger-based app con QLDB e NodeJS
Crea la tua prima serverless ledger-based app con QLDB e NodeJS
 
API moderne real-time per applicazioni mobili e web
API moderne real-time per applicazioni mobili e webAPI moderne real-time per applicazioni mobili e web
API moderne real-time per applicazioni mobili e web
 
Database Oracle e VMware Cloud™ on AWS: i miti da sfatare
Database Oracle e VMware Cloud™ on AWS: i miti da sfatareDatabase Oracle e VMware Cloud™ on AWS: i miti da sfatare
Database Oracle e VMware Cloud™ on AWS: i miti da sfatare
 
Tools for building your MVP on AWS
Tools for building your MVP on AWSTools for building your MVP on AWS
Tools for building your MVP on AWS
 
How to Build a Winning Pitch Deck
How to Build a Winning Pitch DeckHow to Build a Winning Pitch Deck
How to Build a Winning Pitch Deck
 
Building a web application without servers
Building a web application without serversBuilding a web application without servers
Building a web application without servers
 
Fundraising Essentials
Fundraising EssentialsFundraising Essentials
Fundraising Essentials
 
AWS_HK_StartupDay_Building Interactive websites while automating for efficien...
AWS_HK_StartupDay_Building Interactive websites while automating for efficien...AWS_HK_StartupDay_Building Interactive websites while automating for efficien...
AWS_HK_StartupDay_Building Interactive websites while automating for efficien...
 
Introduzione a Amazon Elastic Container Service
Introduzione a Amazon Elastic Container ServiceIntroduzione a Amazon Elastic Container Service
Introduzione a Amazon Elastic Container Service
 

Best Practices for Using AWS Lambda with RDS/RDBMS Solutions (SRV301-R1) - AWS re:Invent 2018

  • 1. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. SRV301 - Best Practices for using AWS Lambda with RDS-RDBMS solutions Mayank Thakkar Global Life Sciences Solutions Architect AWS S R V 3 0 1 Emma Zhao Software Development Manager AWS
  • 2. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
  • 3. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. Typical Solution Architecture AWS Cloud VPC Availability zone 1 Availability zone 2 Private subnet Private subnet Private subnet Private subnet DB Instance DB instance standby Elastic network interface Elastic network interface Lambda Functions Security group Security group
  • 4. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda with RDS / RDBMS - Best Practices • Use subnets in multiple AZs for ENI configuration • Protects against AZ failure and IP address exhaustion • Lambda relies on ENIs (within your VPC) to connect • Scalability depends on available free IP addresses and ENI limits • ENI startup penalty • Lambda in VPC mode does not have internet access by default. • Add Network Address Translation (NAT) gateway inside the VPC to enable internet access • Avoid DNS resolution of public host names if possible • Adds billable, time consuming resolution.
  • 5. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. Problem: Managing Database Connections … Lambda can scale, can your database connections? Is Connection pooling the answer? Generic database
  • 6. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
  • 7. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. Time Container A Container B Container C Container D Lambda - Scaling
  • 8. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. How does that affect Connection Pooling?
  • 9. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. Connection Pooling with Lambda Use only one connection in a container, at a time Set Connection Pool Size = 1 OR Initialize the connection object “above the handler” (in the global section)
  • 10. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. Connection Pooling with Lambda - Challenges • No hook to indicate destruction of Lambda containers • Can not explicitly close connections when the container is recycled • Fall back on Database TTL to clean up connections • Can lead to session leakages • No control over lifecycle of Lambda containers • Can result many connections sitting idle • Different containers for different Lambda functions • No re use of connections across Lambda functions
  • 11. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
  • 12. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
  • 13. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. Solution 1a: Account Concurrency Limit Use AWS Organizations to manage your accounts. AWS Account Lambda can help with retries of throttled invocations* * - Only for asynchronous and stream based event sources.
  • 14. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. Solution 1b: Function Concurrency Limit Lambda can help with retries of throttled invocations* * - Only for asynchronous and stream based event sources.
  • 15. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. Concurrency Limits - Benefits • Lambda Concurrency limit can be set at the account level or at function level. • Simple to set up • Account level limit requires AWS support • AWS Lambda manages retries for throttled invocations*
  • 16. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. Concurrency Limits - Challenges • Account Level limit: • Covers Lambdas across the account – Difficult to isolate DB interfacing functions • Possibility of accounts sprawl. • Function Level limit: • Prior knowledge of DB accessing functions required. • Limit should correspond to peak concurrency – prior application behavior knowledge required • Can not spread out connections dynamically, as load across Lambdas varies
  • 17. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
  • 18. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. Dynamic Connection Management Helper Maintain atomic counter and max count Open / close connections DB operations Do something interesting Generic database Alarm
  • 19. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. Demo Code: https://tinyurl.com/lambdards
  • 20. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. Dynamic Connection Management - Benefits • Scalable solution : depends on Lambda and Amazon DynamoDB • Connections count managed from DB perspective No limit on number of Lambda functions • Can spread DB connections across Lambda functions @ runtime Eliminate choke points and better serve hotspots • Easy update : Control usable connections count through DynamoDB • Use published CloudWatch metrics to react to out-of-connection errors
  • 21. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. Dynamic Connection Management - Challenges • No retries of throttled invocations: requires custom solution. • More resources to manage. • No connection re-use. • Slightly increased latency.
  • 22. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. Summary • Lambda Scaling Model and Connection pooling do not mix • Use a single connection, initialized in the global section • Solutions and Best Practices: • Account and function level concurrency Limits • Custom Solution: Dynamic Connection Management
  • 23. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. References • AWS Organizations https://aws.amazon.com/organizations/ • AWS Serverless : https://aws.amazon.com/serverless/ • AWS Lambda Container Reuse: https://aws.amazon.com/blogs/compute/container-reuse-in-lambda/ • Lambda - Retries of throttled invocations: http://docs.aws.amazon.com/lambda/latest/dg/concurrent- executions.html#concurrent-execution-safety-limit • Dynamic Connections Manager Sample Code : https://tinyurl.com/lambdards
  • 24. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. Questions? Demo Code: https://tinyurl.com/lambdards
  • 25. Thank you! © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. Mayank Thakkar Global Life Sciences Solutions Architect AWS Emma Zhao Software Development Manager AWS Demo Code: https://tinyurl.com/lambdards
  • 26. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.