We have an aws serverless node project, and we’re using Sequelize to access our Postgres database.
There’s an endpoint that updates several rows, each with it’s own data (right now is limited to 50 rows, but we’re also refactoring everything, and we could do more, as long as it’s relatively fast). This endpoint is called inside another endpoint in a different project, that does a lot of different things, and we could risk timeouts, so we need some speed.
Right now each update looks something like this:
models.Package.update(
{
textfield1: value1,
// ...
textfield5: value5,
shippingCost: shippingValue
}, {
where: { id: someId },
returning: true
}
)
What’s the most efficient way to do these updates?
Is doing all the updates inside a Promise.all a good option? And what about using models.Package.bulkCreate
, with the updateOnDuplicate
option? These rows were previously created in the same endpoint, so they would all exist before the updates, so no accidental creations.
For additional context, I’m working on a refactor, the endpoint is currently doing this on production:
- Endpoint recieves data for each Package in the POST body
- Package.findAll to get existing Packages
- It calls a third party API for the new Packages (once for each Package… yeah, I know)
- Package.bulkCreate for these new Packages, using the body data, and the data retrieved from those calls.
But we had concurrency issues, with 2 or more simultanous requests trying to create the same packages (this is most likely an error of the apps consuming our endpoint, but we still have to guarantee consistency). So, some packages are being duplicated (this is easily fixed with a unique clause), but worst of all, that third party API is being called twice or more for the same package, which is bad.
So, what I’m working on right now is this:
- Check existing Packages
const createdPackages = Package.bulkCreate
to create Packages in a temporary state, without the third party data- createdPackages are only the Packages that were created just now, so it’s safe to use them
- Call the third party API only for createdPackages
- Update these packages with the data retrieved from the API
- In case of concurrency issues, the packages that were considered as new, but couldn’t be created, are returned as errors
There are some other things I have to consider, like making sure these temporary Packages are not used somewhere else.
This is just for context, but comments about this solution are also welcomed. The point of this question is to make sure this solution doens’t increase the execution time too much.