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 a simple API to manage semaphores and locks, enabling exclusive access to shared resources across processes. Useful for coordinating concurrent jobs, preventing race conditions, and protecting critical sections.

View on GitHub
Deep Wiki
Context7

The Symfony Semaphore component provides a simple, reliable way to coordinate concurrent work using semaphores—ensuring exclusive access to shared resources such as files, caches, or critical sections. It helps prevent race conditions by letting processes or threads safely acquire and release locks around sensitive operations.

Use it to gate background jobs, throttle access to limited resources, or serialize operations across multiple workers while keeping your code clean and predictable.

  • Exclusive access control for shared resources
  • Prevents race conditions in concurrent environments
  • Straightforward API to acquire/release permits
  • Integrates cleanly with the Symfony ecosystem
  • Suitable for workers, CLI commands, and long-running processes
Frequently asked questions about Semaphore
How do I integrate Symfony Semaphore into a Laravel application for queue job concurrency control?
Register the SemaphoreFactoryInterface in Laravel’s service container via a service provider, then inject it into queue jobs. Use `Semaphore::acquire()` in the `handle()` method to limit concurrent executions (e.g., `Semaphore::acquire('tenant:123:payments', 3)`). For global control, wrap jobs in middleware or use Laravel’s `dispatchSync()` with semaphore checks.
What Laravel versions and PHP requirements does Symfony Semaphore support?
Symfony Semaphore v8+ requires PHP 8.4+, so Laravel 10+ (PHP 8.1+) may need downgrading to v7.x. For Laravel 9 (PHP 8.0), use Semaphore v7.2.x. Check compatibility via `composer require symfony/semaphore:^7.2` or `^8.0` with PHP 8.4+.
Can I use Symfony Semaphore for API rate limiting in Laravel without Redis?
Yes, but Redis is recommended for production. Fallback to filesystem (dev/testing) or Doctrine DBAL via `Semaphore::createFactory()` with a custom backend. For API throttling, pair with Laravel’s `throttle` middleware and acquire semaphores per tenant/IP (e.g., `Semaphore::acquire('api:stripe:tenant_123', 10)`).
How do I prevent deadlocks when using Symfony Semaphore in Laravel queue workers?
Always call `Semaphore::release()` in a `finally` block to avoid leaks, even if exceptions occur. Laravel’s `try/catch` should wrap semaphore operations, and consider using `Semaphore::acquireWithTimeout()` to fail fast. Monitor unreleased locks via Redis `KEYS` scans or TTLs (e.g., `Semaphore::createFactory()->withTTL(3600)`).
What’s the performance impact of Symfony Semaphore in high-traffic Laravel APIs?
Redis-backed semaphores add ~10–50ms latency per operation. Benchmark under load (e.g., 1000 RPS) to validate impact. For low-latency needs, use filesystem backends (higher contention risk) or optimize semaphore granularity (e.g., namespace locks by tenant/resource).
How can I test Symfony Semaphore behavior in Laravel unit/integration tests?
Mock `SemaphoreFactoryInterface` in unit tests or use in-memory backends (e.g., `Semaphore::createFactory()->withBackend(new InMemoryBackend())`). For integration tests, spin up Redis containers (Docker) and verify lock acquisition/release. Test edge cases like concurrent requests, timeouts, and crashes.
Is there a Laravel-specific wrapper or package for Symfony Semaphore?
No official wrapper exists, but you can create a Laravel service provider to bind `SemaphoreFactoryInterface` to a Redis-backed implementation. Example: `app/Providers/SemaphoreServiceProvider.php` with `bind(SemaphoreFactoryInterface::class, fn() => Semaphore::createFactory()->withBackend(new RedisBackend()))`.
How do I handle semaphore failures in production (e.g., Redis downtime)?
Implement a fallback strategy: Use `Semaphore::createFactory()->withBackend(new FilesystemBackend())` or circuit breakers (e.g., `spatie/flysystem-circuit-breaker`). Log failures and alert on lock contention. For critical paths, design idempotent operations to tolerate transient semaphore unavailability.
Can Symfony Semaphore coordinate concurrency across Laravel and non-Laravel services?
Yes, if all services share the same backend (e.g., Redis). Use consistent semaphore names (e.g., `tenant:123:resource`) across Laravel APIs, queue workers, and external microservices. For multi-language support, expose semaphore operations via a shared API or gRPC.
What are the alternatives to Symfony Semaphore for Laravel concurrency control?
For Redis-based locking, consider `predis/predis` (low-level) or `spatie/laravel-queue-skeleton` (job-specific). For database locks, use Laravel’s `DB::transaction()` with `lock()` or `spatie/laravel-activitylog` for audit-based throttling. For CLI jobs, `symfony/process` with `Process::mustRun()` can enforce sequential execution.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport