Download the PHP package kefyusuf/laravel-shard without Composer
On this page you can find all versions of the php package kefyusuf/laravel-shard. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kefyusuf/laravel-shard
More information about kefyusuf/laravel-shard
Files in kefyusuf/laravel-shard
Package laravel-shard
Short Description Modular database sharding for Laravel: deterministic shard routing with optional Redis map and shard-aware queues.
License MIT
Informations about the package laravel-shard
Laravel Shard
A modular shard locator for Laravel applications that provides deterministic shard routing for shard-key-aware Eloquent workflows. Redis-backed maps and shard-aware queues are optional modules.
β¨ Features
- Deterministic Shard Routing: Route shard-key-aware reads and writes to the correct shard
- Redis-Based Lookup: Persistent key-to-shard mappings stored in Redis
- Multiple Strategies: Support for Modulo, Consistent Hashing, Range-based, and Virtual Bucket sharding
- Request Routing: Route middleware and shard-aware builders for single-shard operations
- Model Integration: Easy integration using the
Shardabletrait - Performance Monitoring: Built-in monitoring and health checks
- Cross-Shard Operations: Support for cross-shard queries and aggregations
- Laravel Integration: Native Laravel service provider with Artisan commands
- Octane Compatible: Locator state is flushed between Octane worker requests automatically
- Laravel Pulse Ready: Shard routing distribution recorded and visualized on the Pulse dashboard when Pulse is installed
- Tenancy Bridge: Use
stancl/tenancyorspatie/laravel-multitenancywith the tenant id as shard key (REDIS_SHARD_TENANCY_DRIVER) - Read Replicas: Map per-shard replica connections β reads go to the replica, writes stay on the shard
- Cross-Shard Transactions:
ShardManager::transaction()coordinates begin/commit/rollback across shards (best-effort, documented) - Write-Fenced Rebalancing: idempotent rebalance with write fencing β a concurrent write can never be lost; virtual buckets make resharding move ~1/N of the keys
π Requirements
- PHP 8.2, 8.3, or 8.4
- Laravel 10.x, 11.x, 12.x, or 13.x
- Multiple database connections configured
Optional modules
| Module | Default | Needs | Provides |
|---|---|---|---|
core |
always on | β | strategies, Shardable, builders, CLI |
redis |
on | predis + illuminate/redis |
persistent keyβshard map (RedisShardLocator) |
queue |
off | illuminate/queue |
shard-aware jobs (ShardAwareJob, RestoreShardContext) |
With redis disabled the package falls back to ArrayShardLocator (process-local map) and deterministic strategy routing. No Redis server is required.
π Documentation
- Quick Start Guide - Get started in 5 minutes
- API Reference - Complete API documentation
- Rebalance Operations - Dry-run, gradual moves, idempotent rebalance, write fencing, virtual buckets
- Cross-Shard Transactions -
ShardManager::transaction()semantics and reconciliation patterns - Migration Guide - Migrate existing applications
- Performance Guide - Optimization and benchmarks
- Release Runbook - Package + consumer release checklist
- Example Application - Multi-tenant SaaS example
- Load Testing - Performance testing tools
π Installation
Install the package via Composer:
The package will automatically register its service provider thanks to Laravel's package auto-discovery.
Publish the configuration file:
Run the migrations:
π³ Local Development (Docker)
The repository ships with a Docker stack that bundles PHP (with the redis extension via PECL), Composer, and a Redis 7 service so you can run the full test suite without installing anything on the host.
Override the PHP version via PHP_VERSION (e.g. make PHP_VERSION=8.4 test).
βοΈ Configuration
After publishing the config file, configure your shards in config/redis_sharding.php:
Important Constraints
- Shard-aware query routing is deterministic only when the builder can resolve a single shard from the shard key, currently via explicit shard connection binding,
find, shard-key=predicates, and shard-keywhereIn(...)predicates. - Queries that cannot be routed safely now fail fast instead of silently falling back to the default connection.
- Authoritative key-to-shard mappings are persisted in Redis and no longer expire automatically.
- Auto-increment primary keys require an explicit shard key value, request shard binding, or explicit connection before insert.
modulois suitable only for fixed shard topologies. Adding or removing shards withmodulorequires planned data migration.
Supported Query Patterns
| Pattern | Behavior |
|---|---|
Model::find($id), findMany([...]) |
Routes by primary key when the shard key is the primary key |
where($shardKey, '=', $value) |
Routes to one shard |
whereIn($shardKey, [...]) |
Routes to one or more shards and merges results when needed |
get, first, count, exists, value, pluck |
Shard-aware when the query is deterministic |
paginate, simplePaginate, chunk |
Shard-aware when the query is deterministic |
update, delete, touch, increment, decrement |
Shard-aware when the query is deterministic |
upsert |
Shard-aware when every row contains a resolvable shard key value |
Fail-Fast Query Patterns
- Queries without an explicit shard connection or shard-key predicate
- Shard-key predicates using unsupported operators such as
whereBetween(...) - Queries that mix shard-key routing with
orWhere(...) upsert(...)payloads that omit the shard key or contain unresolvable shard-key values
These paths now throw ShardingException instead of silently using the default connection.
Locator Fallback Store
If you want shard lookups to survive a Redis outage beyond the current PHP process, configure redis_sharding.locator.fallback_store with a Laravel cache store that is independent from Redis.
- Recommended:
databaseorfile - Acceptable: any shared non-Redis cache backend with separate failure characteristics
- Not useful for outage isolation: a cache store backed by the same Redis cluster
- Test-only:
array, because it is process-local
Example:
Health / metrics endpoint
Enable a JSON health probe for load balancers and uptime checks (no Redis required):
Status is ok (all shards reachable), degraded (partial), or down (none reachable).
Add ?detail=1 for a full diagnostics payload (latency, distribution balance, modules, issues), or use the console:
Programmatic access:
π― Quick Start
The steps below match the package consumer smoke test (examples/smoke.php), which installs this package into a fresh Laravel app on every CI run.
1. Install
2. Enable only the modules you need
With redis disabled the package uses ArrayShardLocator and deterministic strategy routing β no Redis server required.
3. Add sharding to your models
Use the Shardable trait and override getShardKeyName() (do not redeclare $shardKey; the trait already defines it):
4. Use shard-aware queries
5. Dispatch shard-aware jobs (queue module)
RestoreShardContext job middleware rebinds the shard connection on the worker before handle() runs.
6. Use middleware for request routing
7. Verify the wiring
π§ Sharding Strategies
The package supports four different sharding strategies:
1. Modulo Strategy
Simple modulo-based distribution. Use only when the shard list is effectively static.
Pros: Simple, predictable distribution Cons: Adding/removing shards remaps most keys and requires explicit data migration
2. Consistent Hashing Strategy
Uses consistent hashing algorithm for better distribution when shards are added/removed.
Pros: Minimal data movement when scaling, good distribution Cons: A rehash still displaces ~half the keys; resharding is all-or-nothing per run
3. Range-Based Strategy
Distributes data based on key ranges.
Pros: Good for time-series or sequential data Cons: Can create hotspots if data isn't evenly distributed
4. Virtual Bucket Strategy (Recommended for resharding)
A key hashes into a fixed bucket (crc32 % N, default 1024) and a persistent bucketβshard map decides placement. Buckets never move implicitly β you move them explicitly and rebalance carries only that bucket's keys (~1/N of the data per move).
See docs/REBALANCE.md for the full workflow.
ποΈ Artisan Commands
Shard Management
Virtual Bucket Management
Monitoring & Analysis
Health Monitoring
Performance Analysis
JSON Output Quick Reference
All commands that support output formatting only accept --format=table or --format=json.
Common payload conventions:
summary.status:ok,error, or command-specific lifecycle value (dry_run,completed, etc.)error: Present when command fails in JSON mode- Detail blocks by command:
install:summary.published_config,summary.ran_migrations,summary.skipped_migrationscreate:summary.shard,summary.driver,summary.database,summary.createdstatus:summary+shardsortablesdetail arrays depending on optionscleanup:summary+reporthealth:summary+issues(+fixwhen--fixis used)analyze:summary+results/analysisrebalance:summary(+movesin dry-run mode)
π Monitoring & Performance
Built-in Monitoring System
The package includes a comprehensive monitoring system that tracks shard health, performance, and distribution:
Advanced Health Monitoring
Caching & Performance Optimization
π Advanced Usage
Cross-Shard Query Builder
The package now includes a powerful cross-shard query builder that allows you to query across all shards seamlessly:
Convenient Cross-Shard Methods
Batch Operations Across Shards
Manual Shard Selection
π‘οΈ Configuration Validation
The package includes comprehensive configuration validation to prevent common setup issues:
Configuration Features
- Connection Validation: Ensures all shard connections are properly configured
- Strategy Validation: Validates sharding strategy classes and interfaces
- Redis Validation: Checks Redis connection configuration
- Auto-Provisioning Validation: Validates auto-scaling settings
- Helpful Recommendations: Provides optimization suggestions
π§ͺ Comprehensive Testing
The package includes extensive testing coverage:
Test Suites
Test Categories
Unit Tests:
ModuloStrategyTest- Tests modulo distribution algorithmConsistentHashingStrategyTest- Tests consistent hashing with shard changesRangeBasedStrategyTest- Tests range-based distribution
Integration Tests:
ShardManagerTest- Tests shard management functionalityShardLocatorTest- Tests Redis-based shard location- Cross-component interaction testing
Feature Tests:
CreateShardCommandTest- Tests shard creation command- End-to-end workflow testing
- Command validation and error handling
Testing Your Implementation
ποΈ Real-World Examples
E-commerce Application
Multi-Tenant SaaS Application
Social Media Platform
π Best Practices
1. Choosing the Right Shard Key
2. Monitoring and Maintenance
3. Handling Relationships
4. Performance Optimization
π€ Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Development Setup
π§ Troubleshooting
Common Issues
1. Configuration Validation Errors
2. Redis Connection Issues
3. Shard Distribution Problems
4. Performance Issues
Debug Mode
Performance Tuning
π Additional Resources
- Examples Directory - Complete working examples
- Test Suite - Comprehensive test coverage
- Configuration Reference - Full configuration options
- API Documentation - Detailed API reference
π Changelog
See CHANGELOG.md for the full release history (latest: v5.0.0 β virtual bucket sharding, cross-shard transactions, deep-module refactor, read replicas, write fencing, Pulse/Octane/tenancy integrations).
π License
This package is open-sourced software licensed under the MIT license.
π Acknowledgments
- Laravel Framework for the excellent foundation
- Redis for fast key-value storage
- The PHP community for continuous inspiration
Made for the Laravel community.
For questions, issues, or contributions, please visit our GitHub repository.
All versions of laravel-shard with dependencies
illuminate/support Version ^10.0|^11.0|^12.0|^13.0
illuminate/database Version ^10.0|^11.0|^12.0|^13.0