symfony/lock
Symfony Lock component provides a unified API to create and manage locks, ensuring exclusive access to shared resources. Supports multiple backends (e.g., filesystem, Redis, PDO) to prevent race conditions in concurrent PHP apps.
Lock facade, Jobs, and Service Container, reducing friction in adoption. It aligns with Laravel’s component-based architecture, leveraging Symfony’s battle-tested patterns.LockFactory enables backend-agnostic locking (Redis, PDO, Flock, DynamoDB, etc.).bug #63975) prevent transaction aborts during lock contention, making it reliable for database-heavy workflows (e.g., financial systems, ERP).Lock facade abstracts lock creation/management, requiring only a single line (e.g., $lock = app(LockFactory::class)->createLock($key)).dispatch() and Horizon, enabling idempotent job execution (e.g., Lock::acquire($jobId) in job handles).bug #63230) ensure cross-DB reliability.evalSha() errors (e.g., bug #60238) may occur in edge cases. Use RedisStore with retry logic.bug #63975) can fail outer transactions. Test with PostgreSQL/MySQL in staging.RedisStore for cross-platform deployments.LockKeyNormalizer in v8.1). Validate keys in CI/CD.Cache, HttpFoundation). Major version upgrades (e.g., v7 → v8) may require PHP 8.4+ and dependency alignment.LockStore events).LockFactory to log events.Lock facade (Illuminate\Contracts\Cache\Lock) for zero-config integration with Laravel’s caching layer.Lock::acquire($jobId) in job handles or dispatchSync() with locks to prevent duplicate processing.PdoStore for persistent locks (e.g., inventory) or RedisStore for low-latency (e.g., payments).DynamoDBStore for AWS Lambda or RedisStore (ElastiCache) for cross-region consistency.RedisStore with sentinel failover for high availability.flock(), GET_LOCK() in MySQL) with FlockStore or PdoStore for consistency.DB::transaction() with Lock::acquire() for race-condition-prone operations.predis/predis or php-redis, configure RedisStore in config/cache.php.PdoStore with your DB connection (e.g., mysql:host=...).aws/aws-sdk-php; configure DynamoDBStore with IAM credentials.// config/lock.php
'default' => env('LOCK_STORE', 'redis'),
'stores' => [
'redis' => [
'connection' => 'cache',
],
'pdo' => [
'dsn' => 'mysql:host=...',
'username' => '...',
'password' => '...',
],
],
// app/Jobs/ProcessOrder.php
public function handle()
{
$lock = app(LockFactory::class)->createLock('order_' . $this->orderId, 30);
if (!$lock->acquire()) {
throw new RetryException('Could not acquire lock', 3);
}
// Critical section
$lock->release();
}
FlockStore as fallback.bug #63975).mongodb/mongodb; enforce readPreference=primary (see bug #61091).How can I help you explore Laravel packages today?