DynamoDB To ElasticSearch Function (DdbToEsFn-*) is VERY Slow
Describe the bug
The nature of my solution is that I essentially write to DynamoDB and read from ElasticSearch - so its critical that writes to DynamoDB are written to ElasticSearch nearly instantaneously.
I have a utility to load data from a CSV file that calls the AppSync mutation and loads the data into DynamoDB. The DdbToEsFn then fires to send that data to ElasticSearch (provided the table is @searchable).
When looking at larger imports it was taking a very long time to get the data loaded where I could read it from ElasticSearch. I had just put this down to "AppSync must be slow". But I just noticed something when I went I loaded 40,437 records with an AppSync create mutation - these records loaded into DynamoDB VERY quickly. I didn't time it, but it was probably 90 seconds or so.
However, its now about 30 minutes later and only 3/4 of the records have made their way to ElasticSearch. So from this I can say that the AppSync create mutation is working really well and really performant - but the problem is with the Lambda function to stream the data to ElasticSearch.
Amplify CLI Version
4.21.3
To Reproduce
Bulk load a lot of data with an AppSync mutation at a searchable table and measure the time taken to load the data into DynamoDB and the time for that data to appear in ElasticSearch.
Expected behavior
I expect that data is streamed into ElasticSearch in almost the same time as it can be loaded into DynamoDB. That DynamoDB is nearly instantaneously streamed to ElasticSearch.
@sacrampton I am trying to replicate what you're doing to see what is causing the lag in indexing the data. Could you clarify the following
- When you load the CSV data do you run concurrent mutation in an thread pool
- How long does it take for your script to load the data to AppSync using mutation
- How long do you have to wait for Elastic search to catchup after the data is loaded using mutation
- Could you share details how complex your types are so I can get some idea on the row size for each type that is being indexed
Hi @yuth - thanks for taking a look at this. To answer your questions:
- I believe we are loading via concurrent mutations (I'll double check with the developer)
- As I mentioned I was seeing 40K records loading in under 2 minutes using the AppSync mutation. That is that the data had been loaded from the CSV file into DynamoDB.
- I am waiting upwards of 30 minutes for those 40K records to appear in ElasticSearch.
- My smallest type probably has 20 attributes and the largest has several hundred attributes. I've attached a sample of the one that I loaded with 40K records that I refer to here.
Thanks
@sacrampton thanks for the update. I was able to reproduce this. This is happening because the BatchSize for Lambda EventSourceMapping is set to 1, and the lambda execution is not able to catch up with the bulk write. Increasing this value to an higher number will make the indexing faster.
Unfortunately this is not something that is configurable. We will have to expose this as a configuration.
Hi @yuth - great news you could reproduce this. In the interim is there a way in the console that we can change this setting outside of AppSync to get moving whilst you are exposing this configuration?
@sacrampton I am afraid making changes using the console might cause issues when you try to update the resource using cloudformation. But in the mean time the work around would be to go to the and edit the generated cloudformation file amplify/backend/api/<api-name>/build/stacks/SearchableStack.json and adding BatchSize and MaximumBatchingWindowInSeconds properties there similar to what I have below.
Searchable<TypeName>LambdaMapping": {
"Type": "AWS::Lambda::EventSourceMapping",
"Properties": {
"BatchSize": 100,
"MaximumBatchingWindowInSeconds": 2,
"Enabled": true,
"EventSourceArn": {
"Fn::ImportValue": {
"Fn::Join": [
":",
[
{
"Ref": "AppSyncApiId"
},
"GetAtt",
"<TableName>",
"StreamArn"
]
]
}
},
"FunctionName": {
"Fn::GetAtt": ["ElasticSearchStreamingLambdaFunction", "Arn"]
},
"StartingPosition": "LATEST"
},
"DependsOn": ["ElasticSearchStreamingLambdaFunction"]
}
Once this change is made, you will have to push the changes with --no-gql-override flag to prevent transformer recreating the stack. So when you have an actual change in the schema, you will have to first compile those changes using
amplify api gql-compile and then would have to do the changes to BatchSize again and push using --no-gql-override. I apologies for all the hoops that you would have to go through to make this work
$ amplify push --force --yes --no-gql-override
Hi @yuth - I realize that you guys can't put out a specific release schedule of fixes/enhancements - but can I get an approximate idea of when this fix might be implemented. By the way, I see this is classified as an enhancement - its actually a bug because an existing feature is not working properly. I believe bugs get fixed faster than enhancements.
You are correct that the workaround you proposed above is very cumbersome and not recommended. And if its only a couple of weeks till you get a fix out then I can continue to limp by. But if its going to be longer than that I'm going to have to try to come up with a workflow where I can reliably implement your work around (which I'd prefer not to have to do).
So if you can give me an order of magnitude idea of when a fix for this bug might be implemented it would be really helpful.
Thanks
Hi @sacrampton - we are looking at this as an enhancement as the underlying ElasticSearch functionality does work as expected and designed. We will not be able to address this in the near future but we'll keep this in mind as we're making some architectural changes to the GraphQL transformer to help support scenarios like this. In the mean time, the workaround that @yuth recommended is the best path forward.
In case this helps anyone else.... Given stack size limitations, need to specify minify when compiling.
$ amplify api gql-compile --minify
Then open amplify/backend/api/
Source -> "BatchSize":1 Replace -> "BatchSize":100,"MaximumBatchingWindowInSeconds":2
This will quickly replace everything in the SearchableStack.json. Then just push to cloud.
$ amplify push --no-gql-override --yes
Hi, @sacrampton thanks for the details how to fix this ... @yuth some progress here ? We have the same problem... pushing 20000 entries in DynamoDB via GlueSpark. The lambda event handler then needs 1h to synchronize them. In the meantime GraphQL queries via AppSync lead to inconsistend results... get*/list* with new, search* with old data ! This is definetly a bug! Hoping the parameter can be configured in amplify soon....
+1
We have been struggling with deploying external data using amplify and BatchSize=1 is a huge pain for us
Just changed in the UI to itemBatch of 100. it's done the work good.
Amplify developers please make autoBatch of 100! I will try maybe later to make PR to change it. it big pain.
Hi folks - we're planning on addressing this as part of our new GraphQL transformer. The approach is captured here in our RFC: https://github.com/aws-amplify/amplify-cli/issues/7546 love to hear your feedback!
This is now available with the new GraphQL Transformer!
This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.
Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.