Highly concurrent AWS Express Step Functions - amazon-web-services

I have a system the receives records from Kinesis stream, Lambda is consuming the stream and invokes one function per shard, this function takes a batch of records and invokes an Async Express Step Function to process each record. The Step Function contains a Task relies on a third party. I have the timeout for this task set but this still can cause high number of concurrent step functions to start executing, when the task is taking longer, as the step functions are not completing quickly enough, causing throttling on Lambda executions further down the line.
To mitigate the issue I am thinking of implementing a "Semaphore" for concurrent Express function executions. There isn't too much out there in terms of similar approach, I found this article but the approach of checking how many active executions there are at a time would only work with Standard Step Function. If it would work with Express I can imagine I could throw error in the function that receives Kinesis record if the arbitrary Step Function execution limit is exceeded, causing Kinesis+Lambda to retry until capacity is available. But as I am using Express workflow, calling ListExecutions is not really an option.
Is there a solution for limiting number of parallel Async Express Step Function executions out there or do you see how I could alternatively implement the "Semaphore" approach?

Have you considered triggering on step function per lambda invoke and using a map state to do the multiple records per batch? The map state allows you to limit the number of concurrent executions. This doesn’t address multiple executions of the step function, and could lead to issues with timeouts if you are pushing the boundary of the five minute limits for express functions.
I think if you find that you need to throttle something across partitions you are going to be in a world of complex solutions. One could imagine a two phase commit system of tracking concurrent executions and handling timeouts, but these solutions are often more complicated than they are worth.
Perhaps the solution is to make adjustments downstream to reduce the concurrency there? If you end up with other lambdas being invoked too many times at once you can put SQS in front of them and enable batching as well as manage throttling there. In general you should use something like SQS to trigger lambdas at the point where high concurrency is a problem, and less so at points that feed into it. In other words if your current step functions can handle the high concurrency you should let them, and anything has issues as a result of it should be managed at that point.

Related

How to exit running event-triggered Cloud Function from within itself?

I want to terminate and exit running cloud function. Function was triggered by Firestore event. What are some ways to do this?
There are some reasons why you want a Cloud Function to terminate itself, for example, to avoid an infinite loop or infinite retries.
To avoid infinite retry loops, set an end condition. You can do this by including a well-defined end condition, before the function begins processing.
A simple yet effective approach is to discard events with timestamps older than a certain time. This helps to avoid excessive executions when failures are either persistent or longer-lived than expected.
Events are delivered at least once, but a single event may result in multiple function invocations. Avoid depending on exactly-once mechanics and write idempotent functions.
Note that updating the function-triggering Firestore document may create subsequent update events, which may cascade into an infinite loop within your function. To solve this problem, use trigger types that ignore updates (such as document.create), or configure your function to only write to Firestore if the underlying value has changed.
Also, note the limitations for Firestore triggers for Cloud Functions.
You might also want to check this example about Cloud Function Termination.
Do not manually exit a Function; it can cause unexpected behavior.

Check if Lambda function is available boto3

I have a lambda function that I'm calling using boto3. There is a high chance that there will be many concurrent executions and I know that Lambda throttles you if you make too many requests. I am doing this in an synchronous manner, so there are no retries. I want to make sure I know when this will happen, so that I can push requests onto a queue, and try them again at a later time.
Boto3 will return an error if there are too many requests, but I would rather not use try and catch for this. From the boto3 docs:
For example, Lambda returns TooManyRequestsException if executing the function would cause you to exceed a concurrency limit at either the account level (ConcurrentInvocationLimitExceeded ) or function level (ReservedFunctionConcurrentInvocationLimitExceeded ).
Does anyone know of a way to check if the function is available for execution before hand?
Thanks.
Does anyone know of a way to check if the function is available for execution before hand?
No, there isn't a way unless you maintain a counter yourself, which would also be a rough estimate.
Use a try catch statement as this is where it is meant to be used at a code level, use asynchronous invocation or retry your synchronous invocation using exponential backoff (increasing the duration between retries every time).

What is the best way to know all concurrent running AWS Lambdas have ended?

I am running multiple lambdas in parallel based on the s3 trigger. I want to get the end time to send back to the user when all the lambdas have ended their execution.
If you need coordination between multiple lambdas, and something to happen when all of them are completed, your best bet is to use a Parallel Task in Step Functions, to run them in parallel and have an additional lambda as the next task after the Parallel one. This is pretty much the standard use case for Step Functions/State Machines - maintaining "state" between Lambdas (including and beyond knowing when the others are complete).
This also gives you only a single entry point for your process as opposed to trying to replicate the data yourself to multiple lambdas.

Redshift stv_wlm_query_state.state: QUEUED vs QUEUEDWAITING

In redshift stv_wlm_query_state system table, what are the differences between QUEUED state and QUEUEDWAITING state?
I've not seen an exact and authoritative set of definitions for queue states published but I have a general understanding that has been useful to me. When a query is submitted it needs to be processed through many steps like compiling, running and returning data. These are all reflected in queue states but there is also time before and between these steps as the query progresses. QUEUED just means that the query is in the queue process but not in another defined state.
Since parallel execution of queries is limited by the WLM and the number of slots available there is a defined state for queries that are waiting on other queries to finish before they can be executed. This specific waiting-for-an-execution-slot state is QUEUEDWAITING. This is generally the most common place for significant waiting to occur and is directly optimizable through the WLM (but possibly not fixed). Delays caused by a flurry of very complex queries needing to be compiled and optimized by the leader would not create QUEUEDWAITING states but these could just show up as QUEUED state.
This is my working understanding based on experience. If someone posts an authoritative set of definitions for queue states I'll be as interested as you are.

Best way to parallelize AWS Lambda

I have a large file being uploaded on S3, and for each line in the file I need to make a long running rest API call. I'm trying to figure out the best way to break up the work. My current flow idea is
Lambda (break up file by line) -> SNS (notification per line) -> Lambda (separate per line/notification)
This seems like it is a common use case, but I can't find many references to it, am I missing something? Is there a better option to break up my work and get it done in a reasonable amount of time?
The Best way is going to be subjective. The method you are using currently, Lambda->SNS->Lambda, is one possible method. As JohnAllen pointed out, you could simply do Lambda->Lambda.
Your scenario reminds me of this project, which has a single Lambda function adding items to a Kinesis stream, which then triggers many parallel Lambda functions.
I think Lambda->Kinesis->Lambda might be a better fit for your use case than Lambda->SNS->Lambda if you are generating a very large number of Lambda tasks. I would be worried that the SNS implementation would run up against the maximum number of concurrent Lambda functions, while the Kinesis implementation would queue them up and handle that gracefully.