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

Semaphore Laravel Package

symfony/semaphore

Symfony Semaphore Component provides semaphores for coordinating access to shared resources across processes and threads. Use it to enforce mutual exclusion, limit concurrency, and prevent race conditions via an easy, reusable API.

View on GitHub
Deep Wiki
Context7

Symfony Semaphore Component

Frequently asked questions about Semaphore
How do I install Symfony Semaphore in a Laravel project?
Use Composer to install the package with `composer require symfony/semaphore`. No additional Laravel-specific setup is required—it integrates directly with the Symfony service container. For Redis-backed semaphores, ensure the `predis/predis` or `phpredis` package is installed for optimal performance.
Can Symfony Semaphore work with Laravel Queues or Horizon?
Yes, it’s ideal for queue coordination. Wrap job execution in semaphores to prevent race conditions, e.g., `Semaphore::acquire('job-lock'); dispatch(new ProcessPaymentJob()); Semaphore::release('job-lock');`. Works seamlessly with Laravel’s queue workers and Horizon.
What Laravel versions does Symfony Semaphore support?
The component requires PHP 8.1+ (v7.x) or 8.4+ (v8.x), which aligns with Laravel 9+ and 10+. For Laravel 8.x, you’ll need to upgrade PHP or use filesystem/DBAL backends, though Redis is recommended for production.
How do I configure Symfony Semaphore for Redis in Laravel?
Configure Redis via Symfony’s `SemaphoreFactory` with a DSN like `redis://127.0.0.1:6379`. In Laravel, bind the factory to the container in `config/app.php` under `providers`. Example: `$factory = new SemaphoreFactory(new RedisClient($dsn));`
What happens if a semaphore lock times out or fails?
Locks auto-release after the configured timeout (default: 30 seconds). If the backing store fails (e.g., Redis downtime), locks may persist until recovery. Use `try-finally` blocks to ensure `release()` is called, or set a short timeout for critical sections.
Can I use Symfony Semaphore for tenant-aware rate limiting?
Yes, create unique semaphore keys per tenant (e.g., `tenant:{id}-stripe-calls`). Set a permit limit (e.g., 5) to enforce concurrent access. Example: `Semaphore::acquire('tenant:123-stripe', 5, 60)` for 5 concurrent calls with a 60-second window.
Is Symfony Semaphore thread-safe for Laravel’s CLI commands?
Yes, it’s designed for concurrent environments. Protect long-running CLI commands (e.g., `artisan migrate`) by acquiring a semaphore before execution. Example: `Semaphore::acquire('migration-lock');` to block concurrent migrations.
What are the alternatives to Symfony Semaphore in Laravel?
Alternatives include Redis `SETNX` (less reliable), database `SELECT ... FOR UPDATE`, or Laravel’s native `flock()` (filesystem-only). Symfony Semaphore stands out for its cross-process reliability, Redis/DBAL support, and built-in timeouts, reducing deadlock risks.
How do I test Symfony Semaphore in Laravel unit tests?
Use an in-memory backend (e.g., `SemaphoreFactory` with a mock store) or Redis for integration tests. Avoid filesystem backends in CI/CD due to race conditions. Example: `$factory = new SemaphoreFactory(new InMemoryStore());` for deterministic testing.
Can Symfony Semaphore handle distributed locks across multiple servers?
Yes, Redis-backed semaphores work across clusters. For high availability, use Redis Sentinel or Cluster mode (v7.2.4+). Validate DSN configurations (e.g., `redis+cluster://`) in staging to avoid connection failures. Fallback to filesystem/DBAL if Redis is unavailable.
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.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai