N+1 queries in AWS AppSync
Asked Answered
T

3

61

When using AWS AppSync with lambda data sources you can encounter N+1 query problem.

Basically when you have individual field resolver on your type and your query returns an array of those types you field resolver lambda will be called N times.

AWS introduces BatchInvoking lambdas in resolvers to combat this problem. Here you can read more about the problem and their solution: https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-lambda-resolvers.html#advanced-use-case-batching

However, their solution is not working. BatchInvoking lambdas are limited to only 5 events (this is not stated in documentation). It is a slight improvement to the N+1 problem (it makes it N/5+1), but I think it is not enough as more complex queries tend to execute for a very long time and require more lambda invocations.

So my question is how do you deal with this problem? Is there any better solution to this?

Tibetan answered 10/8, 2018 at 13:50 Comment(12)
I work on AWS AppSync team. I will be happy to pass this on as a feature request to customize the batch size for batch invoke.Pediatrics
This would be very helpful and will actually solve this problem in the best possible way, thank you!Tibetan
I hit the same problem. This is serious problem, because we have hundreds of resources with subresources, which are resolved in one betched call to external API (with current N/5 it would be ineffective to make such an amount of API calls). Too bad that it is NOT documented.Spyglass
Half a year passed and there's no workaround yet. @Karthik, is it in the roadmap?Caudillo
Any updates on this topic? We're looking for a solid GraphQL solution which should serve as a facet for dozens of REST services. Currently, this limitation doesn't seem to be a blocker on the initial stages but we don't want to face it in the middle of the way.Fimbria
@ArthurGurov I think it is better to go with Apollo GraphQL for now, you can host apollo server on a conventional aws ec2 instance or even on aws lambda if you preferTibetan
Any update on this problem? It is a serious limitationManagerial
Just tested this again with a clean setup and this undocumented and unchangeable behaviour is still the case. You would expect AWS to at least document this behaviour after 3 years of not adding it, but no.Impearl
Have you tried adding PerResolver caching? docs.aws.amazon.com/appsync/latest/devguide/…Coreen
@Coreen For request to be cached for later use it should fire at least once successfully. However problem in question in my case caused my request to time out.Tibetan
That's interesting. I know they offer Full Request caching where what you describe would be my expectation. For Resolver based caching I would expect each individual resolver would be cached even if the overall response fails for some other reason.Coreen
Is there perhaps a jira or other issue/case number so people can at least check the status of this at any time in the future?Oxtail
T
3

Until Appsync fixes their issues, we are using an Apollo server as a gateway in ECS to stitch the schemas made with Prisma and invokes lambdas directly where our logic is set.

For your request you can follow up on the feature request in their GitHub repo, which sadly does not have a lot of progress. https://github.com/aws/aws-appsync-community/issues/51

Tearful answered 19/9, 2021 at 7:46 Comment(0)
A
3

AWS just made batching size for AWS AppSync Lambda resolvers configurable (official blog post).

Now developers can easily enable batching on their Direct Lambda resolvers, and configure the maximum batching size (up to 2000 instead of the previous fixed default of 5) for their resolver. The same functionality is available for AppSync pipeline functions that use a Lambda function data source.

The official documentation has been updated with:

You can enable batching for your Direct Lambda Resolver by configuring the maxBatchSize on your resolver. When maxBatchSize is set to a value greater than 0 for a Direct Lambda resolver, AWS AppSync sends requests in batches to your Lambda function in sizes up to maxBatchSize.

Setting maxBatchSize to 0 on a Direct Lambda resolver turns off batching.

Using the console

You can configure it via AWS console for each resolver:

AWS AppSync Lambda resolvers configurable batching size in console

Using CLI

Using --max-batch-size parameter in AWS CLI V2 or AWS CLI V1

aws appsync update-resolver \
  --api-id <value> \
  --type-name <value> \
  --field-name <value> \
  --max-batch-size <value>

Using CloudFormation

The property name is MaxBatchSize and it can be set on AWS::AppSync::Resolver and AWS::AppSync::FunctionConfiguration.

MyResolver:
  Type: AWS::AppSync::Resolver
  Properties:
    ApiId: MyApiId
    DataSourceName: MyDataSourceName
    TypeName: MyTypeName
    FieldName: MyFieldName
    MaxBatchSize: 100

The official CloudFormation doc update will be out this week

Anatolian answered 12/1, 2022 at 16:29 Comment(0)
M
-1

AppSync released this enhancement today: https://aws.amazon.com/blogs/mobile/introducing-configurable-batching-size-for-aws-appsync-lambda-resolvers/

Morpheus answered 6/1, 2022 at 22:6 Comment(2)
Please don't post links as they can be deleted, copy the information from the link as well and provide the part of the answer they are looking forJohnjohna
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Helicline

© 2022 - 2024 — McMap. All rights reserved.