Global file lock contention causing latency spikes under concurrent writes in Contao
0 reputation · 03 Jul 2022, 08:58 UTC
0 reputation · 03 Jul 2022, 08:58 UTC
The objective is to assess how the global file lock influences request latency when multiple concurrent requests write to the same database table in Contao.
Contao's global lock serializes all file operations, and because no per-request transaction isolation level can be set, concurrent writes may contend for the same table, leading to latency spikes and possible SQLSTATE[40001] deadlocks.
What impact does this lock have on overall request latency under high concurrency, and can the lock be tuned or replaced to reduce the observed delays, and how should deadlock handling be managed?
36775 reputation · 03 Jul 2022, 19:24 UTC
The global file lock is not, by itself, the cause of SQLSTATE[40001] deadlocks; that error is raised by the database when concurrent transactions cannot be serialized. If Contao holds an application file lock across a database transaction, it serializes otherwise independent requests and magnifies database lock-wait latency. Under high concurrency, overall request latency grows with lock-wait time, retry attempts, and backoff delays.
| Item | Status | Notes |
|---|---|---|
| Contao writes through Doctrine DBAL/PDO | Confirmed | Database concurrency control governs row and table locks. |
| SQLSTATE[40001] is a database serialization failure | Confirmed | It is not a PHP file-lock error. |
| Application file lock held across a transaction increases contention | Likely | Depends on Contao version and where the lock is acquired. |
| READ COMMITTED reduces gap-lock contention versus REPEATABLE READ | Likely, version-sensitive | Test before applying to production. |
SHOW ENGINE INNODB STATUS\GConfirm whether Contao's global file lock is acquired before or across the database transaction for the affected write path. If it spans the transaction, narrow or remove it; if it only guards cache writes outside the transaction, prioritize database indexing and transaction length instead.
Use comments to ask for clarification. Post a solution as an answer.
36,775 reputation · 03 Jul 2022, 13:58 UTC
Contao 4.4–4.8 used file-based locks in var/lock/, while 4.9+ switched to the tl_lock table — mitigation strategies differ significantly between these eras. For 4.9+, the first step is quantifying contention per lock name; run the monitoring query during a realistic load test to identify whether cache, search, or a custom lock dominates wait time. If a single lock name accounts for most latency, splitting it via a custom LockHandler (e.g., cache.page vs cache.layout) is lower-risk than disabling locks globally. For deadlocks (SQLSTATE[40001]), note that Contao's PDO wrapper lacks a built-in retry helper; implement idempotent retry with exponential backoff at the application layer (Doctrine DBAL middleware or a Messenger consumer) rather than relying on the framework.