Mais conteúdo relacionado Semelhante a Best Practices for Using AWS Lambda with RDS/RDBMS Solutions (SRV301-R1) - AWS re:Invent 2018 (20) Mais de Amazon Web Services (20) Best Practices for Using AWS Lambda with RDS/RDBMS Solutions (SRV301-R1) - AWS re:Invent 20181. © 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.