N+1 queries in AWS AppSync

Amazon Web-ServicesAws LambdaGraphqlAws Appsync

Amazon Web-Services Problem Overview


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?

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

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

Solution 2 - Amazon Web-Services

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

Solution 3 - Amazon Web-Services

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlpacaGoesCrazyView Question on Stackoverflow
Solution 1 - Amazon Web-ServicesLucaszView Answer on Stackoverflow
Solution 2 - Amazon Web-ServicesYves M.View Answer on Stackoverflow
Solution 3 - Amazon Web-ServicesNicholas DeJacoView Answer on Stackoverflow