Optimized-min item creation can cause database deadlocks in a scaled environment on SXA sites


Description

Creating a custom theme in a scaled environment at the SXA site might cause a database deadlock, high CPU, and errors in the log records. The issue happens because instances in a scaled environment have no locking capability between each other and when simultaneous requests are made to the SXA site the optimized-min file might be duplicated and then deleted after publishing a theme.

One of the following errors appears in the log records:

ERROR Failed to add the item version. Item ID: {3897C3B8-A4E5-465F-95CA-0PHF9844136B}, database: web, language: de-DE
Exception: System.Exception
Message: Cannot insert duplicate key row in object 'dbo.VersionedFields' with unique index 'ndxUnique'.

or

ERROR Failed to save the item. Item ID: {0578ED5C-0F04-09B8-8814-6D05FE93AA34}, database: web
Exception: System.Exception
Message: Transaction (Process ID 265) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

Solution

To resolve the issue, consider one of the following options:

Alternatively, to address the issue, consider the following options: