I have a Lambda that gets thousands of events sent to it at one time. Concurrency is left at the default, which means AWS will spin up several instances to handle the incoming events. The Lambda takes the data and inserts some data into a database if that data doesn't already exist. The Lambda is written in Node.js and uses Knex to connect to a Postgres database.
The Lambda essentially contains this logic:
Does a record with ID X exist?
a. Yes: do nothing
b. No: create a new record with ID X.
The problem is that when 50 Lambdas spin up at the same time, they'll enter a race condition where, say, 3 or 4 of them will check for the existing record at the same time (or within microseconds of each other) and not find it, therefore inserting multiple, duplicate records.
I know one way to solve this would be to create a unique constraint on the table to prevent multiple records with ID X. Then my logic would look like this:
Does a record with ID X exist?
a. Yes: do nothing
b. No: create a new record with ID X.
b.1. Did that succeed?
a. Yes: continue on.
b. No, it threw a unique constraint error: go back to line 1.
This seems a bit contrived, but should work. Is there a better option?
EDIT:
Here is the actual code:
let location = await Location.query().where({ external_id }).first();
if(!location){
location = await Location.query().insert({
name,
external_id
});
}