3

I'm new to AWS serverless applications and am looking for something like ocelot request aggregation on the AWS serverless stack.

Assume I have two AWS lambdas that return data needed by a SPA: A and B. They each take 1 second to run to load data from different sources. When the browser makes a call to the API I wish to run both concurrently and return the resulting JSON to the browser.

Two options I'm considering:

  1. A parallel state in a step function that runs A and B
  2. Have another lambda whose sole purpose is to run A and B concurrently and return the results

Testing both these I've found the step function takes an extra 220ms per call and the concurrent lambda takes an extra 500ms each call. These are both too slow for a SPA api. Is there a faster solution?

3
0

There are two potential solutions that come to mind.

  1. Make concurrent calls from the browser and aggregate the results there (credit to @RobertHarvey who has already mentioned this in the comments)
  2. Make concurrent calls from the Lambda and aggregate the results before sending a response

Using JavaScript's Promise.all for example would work for both options.

Both of these options limit the number of components and hops your request(s) makes, meaning you should see quicker responses.

Option 1 should be faster overall (assuming each request isn't fighting over the client's bandwidth), but will be more expensive as you are invoking two Lambdas and potentially two API Gateway call if you are set up that way. It also lets you expose each API individually in the future.

Option 2 may be ever so slightly slower than option 1, but it's only one call from the browser, so only one API Gateway call and Lambda invocation. If your Lambdas are grunt heavy (which I doubt if they are just calling to external sources) you could increase the memory allocation of your single lambda and still be saving money.

I should point out that option 2 will only be slower than option 1 if there is a significant amount of code or heavy dependencies that couldn't be shared, and even then it would probably only be noticeable in cold starts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.