Ever wondered how to manage connections to SQL databases from serverless applications, or how to rate limit and build serverless state machines? This presentation discusses patterns you can use to build the most complex serverless applications!
36. Most SQL Databases Limit Connections
Each DB connection is handled on an independent thread of
execution
Each thread of execution requires memory set aside for it
Switching execution between threads adds overhead
37. Wait, Didn’t AWS Aurora Add A Data API Last
Year?
Yes, But Data API Only Allows Atomic
Transactions, Not Locking
We Still Need A Persistent Network Connection
Solution
51. SQS Queues With Reserved Concurrency
1. To prevent overloading a system, each minute we add X
number of items to a queue (Here, web pages we intend to
scrape)
2. The Scraper function is limited to only Y concurrent
invocations (Gotcha: Y must be greater than 5 to work right)
3. Each Scraper invocation retrieves one message at a time and
processes it
57. Concurrency Coordination Example: Map / Reduce
1. Add a record to a database with count of concurrent invocations
2. Perform invocations
3. At completion, invocations atomically reduce concurrent count
4. If no more concurrent invocations, transition to next state
59. State Transitions Example: Custom Retry Logic
1. Attempt to execute function
2. On failure, check “retries” property of the input message
3. If retries > MAX_RETRIES, then report failure
4. Otherwise, increment “retries” property in message, then
reinvoke self
70. 1. Signup Invokes enqueueFunction, Enqueues Message To Queue
2. Enqueued Message Is Delayed 1 Minute
3. dequeueFunction Receives Messages; If More Than N Messages In
Queue (Or DeadLetter), Throws An Error To Send Messages To
DeadLetter, Sends Alert
4. Otherwise, dequeueFunction Sends Welcome Email
Normal Signup Process
71. 5. Human Invokes drainDeadLetter To Move All Caught Messages To
Holding, Unblocking dequeueFunction
6. Human Filters Out SPAM From Holding
7. Human Invokes approveAll To Move Messages From Holding To
Approved
8. dequeueFunction Processes All Approved Messages From Approved
Queue
Manual Approval Process