I have application that receives multiple requests from external sources (invoices from point-of-sale units). It gets tens of requests per second, and some of those requests are the same (have same request body).
request data is transformed and saved to two associated tables (foreign key). if record already exists (queried by unique composite key), record is updated, otherwise record is added.
the problem is that sometimes if two requests with same body are received at the same time app throws exception that unique key already exists and it can't insert it:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
It's probably some kind of race condition in MySQL but can't figure it out
Whole process is wrapped in Laravel transaction. I tried setting different isolation levels. SERIALIZABLE resolves that issue but then I get lot of deadlock exceptions, and as I can see sometimes record is not saved at all.