Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

N+1 queries in AWS AppSync

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?

like image 421
AlpacaGoesCrazy Avatar asked Aug 10 '18 13:08

AlpacaGoesCrazy


People also ask

How do I trigger lambda function from AppSync?

From the schema editor in the AWS AppSync console, on the right side, choose Attach Resolver for getPost(id:ID!): Post . Choose your Lambda data source. In the request mapping template section, choose Invoke And Forward Arguments. In the response mapping template section, choose Return Lambda Result.

How do I test AWS AppSync?

Testing ResolversIn the AWS AppSync console, go to the Schema page, and choose an existing resolver on the right to edit it. Or, choose Attach to add a new resolver. At the top of the page, choose Select test context, choose Create new context, and then enter a name.

What are available data source in AWS AppSync?

AWS AppSync has support for automatic provisioning and connections with certain data source types. AWS AppSync supports AWS Lambda, Amazon DynamoDB, relational databases (Amazon Aurora Serverless), Amazon OpenSearch Service, and HTTP endpoints as data sources.

What is schema in AppSync?

Schema files are text files, usually named schema.graphql . You can create this file and submit it to AWS AppSync by using the CLI or navigating to the console and adding the following under the Schema page: schema { } Every schema has this root for processing. This fails to process until you add a root query type.


1 Answers

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

like image 90
Lucasz Avatar answered Sep 30 '22 09:09

Lucasz