Hey everyone,
I have an API call that gets called from my frontend.
It does the following (in that order):
a) call an external API which results in a list of records ('allEntries' in screenshot)
b) check for each result ('entryToBeAdded') if it's already existing in a database ('wikiEntries'), and if not, add it to the database)
Today I found that during a certain period a lot of duplicate entries were added to the database at the same time - and after some digging I assume this is because the API call was fired twice nearly at the same time (with same inputs for the external API call resulting in the same list of 'allEntries'). So I assume they both - at the same time - came to the conclusion that the record doesnt already exist, and thus were adding it to the database.
Is there a smart way to avoid this? Note: Also, I don't want to 'lock up' the whole database for a single API request (in case this is even possible), because I plan to have a lot of parallel API requests in the future - enforcing synchronous processing would be a kill in my app...
Thanks!
Johannes