Dealing with Concurrency with Locks
When a program runs concurrently, some part of code which modify shared resources should not be accessed by multiple processes at the same time. Symfony’s Lock component provides a locking mechanism to ensure that only one process is running the critical section of code at any point of time to prevent race conditions from happening.
The following example shows a typical usage of the lock:
$lock = $lockFactory->createLock('pdf-invoice-generation');
if (!$lock->acquire()) {
return;
}
// critical section of code
$service->method();
$lock->release();
Installation
In applications using Symfony Flex, run this command to install the Lock component:
$ composer require symfony/lock
Configuring Lock with FrameworkBundle
By default, Symfony provides a Semaphore
when available, or a Flock otherwise. You can configure
this behavior by using the lock key like:
New in version 6.1: The CSV support (e.g. zookeeper://localhost01,localhost02:2181) in
ZookeeperStore DSN was introduced in Symfony 6.1.
Locking a Resource
To lock the default resource, autowire the lock factory using
Symfony\Component\Lock\LockFactory (service id lock.factory):
// src/Controller/PdfController.php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Lock\LockFactory;
class PdfController extends AbstractController
{
/**
* @Route("/download/terms-of-use.pdf")
*/
public function downloadPdf(LockFactory $factory, MyPdfGeneratorService $pdf)
{
$lock = $factory->createLock('pdf-creation');
$lock->acquire(true);
// heavy computation
$myPdf = $pdf->getOrCreatePdf();
$lock->release();
// ...
}
}
Caution
The same instance of LockInterface won’t block when calling acquire
multiple times inside the same process. When several services use the
same lock, inject the LockFactory instead to create a separate lock
instance for each service.
Locking a Dynamic Resource
Sometimes the application is able to cut the resource into small pieces in order
to lock a small subset of processes and let others through. The previous example
showed how to lock the $pdf->getOrCreatePdf('terms-of-use') for everybody,
now let’s see how to lock $pdf->getOrCreatePdf($version) only for
processes asking for the same $version:
// src/Controller/PdfController.php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Lock\LockFactory;
class PdfController extends AbstractController
{
/**
* @Route("/download/{version}/terms-of-use.pdf")
*/
public function downloadPdf($version, LockFactory $lockFactory, MyPdfGeneratorService $pdf)
{
$lock = $lockFactory->createLock($version);
$lock->acquire(true);
// heavy computation
$myPdf = $pdf->getOrCreatePdf($version);
$lock->release();
// ...
}
}
Named Lock
If the application needs different kind of Stores alongside each other, Symfony provides named lock:
Each name becomes a service where the service id is part of the name of the
lock (e.g. lock.invoice.factory). An autowiring alias is also created for
each lock using the camel case version of its name suffixed by LockFactory
- e.g. invoice can be injected automatically by naming the argument
$invoiceLockFactory and type-hinting it with
Symfony\Component\Lock\LockFactory.