Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Lock Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Creates and manages locks, a mechanism to provide exclusive access to a shared resource

Frequently asked questions about Lock
How do I integrate Symfony Lock with Laravel’s queue system (e.g., Horizon or dispatch())?
Use the `Lock` facade in your job’s `handle()` method or wrap `dispatch()` calls with `Lock::acquire()`. For Horizon, inject the lock store into workers via Laravel’s service container. Example: `Lock::acquire('user:inventory:123', 30)->block(1000);`. Works natively with Laravel 8+.
Which storage backends does Symfony Lock support in Laravel, and how do I choose one?
Supported backends include Redis (low-latency), PDO (PostgreSQL/MySQL for persistence), Flock (filesystem), DynamoDB (serverless), and MongoDB. Choose Redis for high-throughput queues or PDO for ACID-compliant workflows. Configure via `LockFactory` in Laravel’s `config/app.php` or service provider.
Will Symfony Lock work with Laravel’s serverless deployments (e.g., AWS Lambda, Bref)?
Yes, but prioritize DynamoDB or Redis for serverless. Avoid Flock (NFS issues) and monitor cold-start latency. Use provisioned capacity for DynamoDB or Redis to mitigate delays. Test TTL settings (e.g., 60s) to avoid stale locks during cold starts.
How do I handle lock contention or failed acquisitions in production?
Implement exponential backoff in your lock acquisition logic (e.g., `Lock::acquire()->block(1000, 500)`). For critical paths, use a fallback store (e.g., Redis primary, PDO secondary). Monitor contention via Laravel Horizon events or custom metrics (e.g., Prometheus).
Does Symfony Lock support Laravel’s event system or console commands?
Yes, wrap event listeners or console commands with locks using the `Lock` facade. Example: `Lock::acquire('command:backup')->block(5000);`. Works seamlessly with Laravel’s service container and dependency injection, requiring no additional setup.
What’s the best TTL (time-to-live) for locks in Laravel queues or batch jobs?
Use 30–60 seconds for short-lived queue jobs to balance contention and stale locks. For batch jobs or long-running tasks, extend to 5–10 minutes. Always test with your workload’s maximum expected runtime. Symfony Lock auto-releases locks on TTL expiry or process crashes.
Can I use Symfony Lock with Laravel 7 or older versions?
Officially supported from Laravel 8+. For Laravel 7, use Symfony Lock v6.x with manual integration (e.g., `symfony/lock` + custom facade). Avoid Laravel <8.0 due to missing features like LockKeyNormalizer (v8.1+), which prevents key collision edge cases.
How do I test distributed locking failures (e.g., network partitions) in CI/CD?
Mock the lock store in PHPUnit using Symfony’s `TestLockStore`. Simulate failures with `Lock::acquire()->block(0)` (immediate timeout) or delay responses in tests. For Redis, use `Predis` mocks or Dockerized Redis clusters. Test TTL handling with `sleep()` in test jobs.
Are there performance differences between Redis and PDO backends in Laravel?
Redis offers sub-millisecond acquisition (ideal for queues), while PDO adds ~10–50ms per lock (due to SQL roundtrips). For high-frequency locks (e.g., inventory updates), Redis is 10x faster. Benchmark with `ab` or Laravel’s `queue:work --once` to measure impact on user-facing workflows.
How do I migrate from custom file-based locks or database tables to Symfony Lock?
Phase out old locks incrementally: replace one critical job/resource at a time. Use a feature flag to toggle between old and new locks. For database tables, drop them post-migration. Symfony Lock’s `LockKeyNormalizer` ensures backward-compatible key formats (e.g., `user:inventory:123`).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
ecotone/kafka
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata