1. Go to this page and download the library: Download kefyusuf/laravel-shard library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Laravel\RedisShard\Metrics\ShardHealthReport;
use Laravel\RedisShard\Metrics\ShardDiagnosticReport;
$light = app(ShardHealthReport::class)->toArray();
$full = app(ShardDiagnosticReport::class)->toArray();
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Laravel\RedisShard\Traits\Shardable;
class User extends Model
{
use Shardable;
protected $fillable = ['name', 'email', 'password'];
public function getShardKeyName(): string
{
return 'email'; // defaults to primary key when omitted
}
}
// Create a user - routed using the shard key value
$user = User::create([
'name' => 'John Doe',
'email' => '[email protected]',
'password' => bcrypt('password'),
]);
// Single-shard lookup by shard key
$user = User::where('email', '[email protected]')->first();
// Single-shard bulk lookup by shard key
$users = User::whereIn('email', ['[email protected]', '[email protected]'])->get();
// Resolve the shard connection directly
$shardConnection = ShardManager::getShardConnection('users', '[email protected]');
use App\Jobs\SyncUserProfile;
use function Laravel\RedisShard\Queue\dispatchSharded;
dispatchSharded(new SyncUserProfile(), $user);
// or
SyncUserProfile::dispatchSharded($user);
Route::get('/users/{email}', 'UserController@show')
->middleware('shard:users,email');
// Group routes with shard middleware
Route::middleware(['shard:users,email'])->group(function () {
Route::get('/users/{email}/profile', 'UserController@profile');
Route::get('/users/{email}/orders', 'UserController@orders');
Route::put('/users/{email}', 'UserController@update');
});
use Laravel\RedisShard\Cache\ShardCache;
$cache = app(ShardCache::class);
// Cache shard connections for faster lookups
$cache->putShardConnection('users', '[email protected]', 'shard1');
$connection = $cache->getShardConnection('users', '[email protected]');
// Cache metadata for performance
$cache->putShardMetadata('shard1', $metadata);
// Get cache statistics
$stats = $cache->getStats();
// Use the CrossShardQueryable trait in your models
use Laravel\RedisShard\Traits\Shardable;
use Laravel\RedisShard\Traits\CrossShardQueryable;
class User extends Model
{
use Shardable, CrossShardQueryable;
}
// Simple cross-shard queries
$activeUsers = User::crossShard()->where('status', 'active')->get();
$recentUsers = User::crossShard()->orderBy('created_at', 'desc')->limit(10)->get();
// Cross-shard aggregations
$totalUsers = User::crossShard()->count();
$averageSalary = User::crossShard()->avg('salary');
$maxAge = User::crossShard()->max('age');
// Complex queries with multiple conditions
$results = User::crossShard()
->where('status', 'active')
->whereLike('name', '%john%')
->orderBy('created_at', 'desc')
->limit(50)
->get();
// Search across all shards
$users = User::searchAcrossShards('email', '[email protected]');
// Find first match across shards
$user = User::findAcrossShards('email', '[email protected]');
// Get aggregated statistics
$stats = User::aggregateAcrossShards('salary');
// Returns: ['count' => 1000, 'sum' => 50000, 'avg' => 50, 'min' => 20, 'max' => 100]
// Search with LIKE pattern
$users = User::searchLikeAcrossShards('name', '%john%');
// Get recent records across shards
$recentUsers = User::recentAcrossShards(20, 'created_at');
// Paginate across shards
$paginated = User::paginateAcrossShards(1, 15);
// Returns: ['data' => Collection, 'total' => 1000, 'per_page' => 15, ...]
// Batch update across all shards
$updatedCount = User::batchUpdateAcrossShards(
['status' => 'inactive'], // conditions
['status' => 'archived'] // updates
);
// Batch delete across shards
$deletedCount = User::batchDeleteAcrossShards(['status' => 'spam']);
// Get shard distribution
$distribution = User::getShardDistribution();
// Returns detailed distribution with counts and percentages per shard
// Execute custom operations on all shards
$results = User::executeOnAllShards(function ($model, $shard) {
return $model->where('created_at', '>', now()->subDays(7))->count();
});
// Create user on specific shard
$user = new User(['name' => 'John', 'email' => '[email protected]']);
$user->setConnection('shard2');
$user->save();
// Query specific shard directly
$users = User::on('shard1')->where('status', 'active')->get();
// Get shard information for a model
$user = User::find(1);
$shardInfo = $user->getShardInfo();
// Returns: ['shard_connection' => 'shard1', 'shard_key' => 'email', 'shard_key_value' => '[email protected]']
use Laravel\RedisShard\Validation\ConfigValidator;
// Automatic validation on service provider boot
// Manual validation
try {
ConfigValidator::validate(config('redis_sharding'));
} catch (\Laravel\RedisShard\Exceptions\ConfigurationException $e) {
echo $e->getMessage();
}
// Get configuration recommendations
$recommendations = ConfigValidator::getRecommendations(config('redis_sharding'));
foreach ($recommendations as $recommendation) {
echo $recommendation['type'] . ': ' . $recommendation['message'];
}
// Test shard distribution
$distribution = [];
for ($i = 1; $i <= 1000; $i++) {
$shard = ShardManager::getShardConnection('users', $i);
$distribution[$shard] = ($distribution[$shard] ?? 0) + 1;
}
// Test cross-shard queries
$totalUsers = User::crossShard()->count();
$shardCounts = User::getShardDistribution();
// Test health monitoring
$monitor = app(\Laravel\RedisShard\Monitoring\ShardMonitor::class);
$health = $monitor->collectMetrics()['health'];
// Product model with sharding by category
class Product extends Model
{
use Shardable;
protected ?string $shardKey = 'category_id';
}
// Order model sharded by user
class Order extends Model
{
use Shardable;
protected ?string $shardKey = 'user_email';
public function user()
{
// Cross-shard relationship
return User::findAcrossShards('email', $this->user_email);
}
}
// Get sales analytics across all shards
$analytics = Order::crossShard()
->where('created_at', '>=', now()->subDays(30))
->selectRaw('DATE(created_at) as date, SUM(total) as daily_total')
->groupBy('date')
->get();
// Posts sharded by user
class Post extends Model
{
use Shardable, CrossShardQueryable;
protected ?string $shardKey = 'user_id';
}
// Get trending posts across all shards
$trendingPosts = Post::crossShard()
->where('created_at', '>=', now()->subHours(24))
->where('likes_count', '>', 100)
->orderBy('likes_count', 'desc')
->limit(50)
->get();
// User feed with cross-shard data
$userFeed = Post::crossShard()
->whereIn('user_id', $followingUserIds)
->orderBy('created_at', 'desc')
->limit(20)
->get();
// ✅ Good: High cardinality, evenly distributed
protected ?string $shardKey = 'user_email';
protected ?string $shardKey = 'user_id';
// ❌ Avoid: Low cardinality, uneven distribution
protected ?string $shardKey = 'status'; // Only few values
protected ?string $shardKey = 'country'; // Uneven distribution
// Regular health checks
Schedule::command('shard:health --fix')->hourly();
// Monitor distribution balance
Schedule::command('shard:analyze --format=json')->daily();
// Collect metrics for monitoring systems
$metrics = app(\Laravel\RedisShard\Monitoring\ShardMonitor::class)->collectMetrics();
// Keep related data on the same shard
class User extends Model
{
use Shardable;
protected ?string $shardKey = 'email';
public function profile()
{
// Profile uses same shard key
return $this->hasOne(UserProfile::class, 'user_email', 'email');
}
}
// For cross-shard relationships, use explicit queries
public function orders()
{
return Order::searchAcrossShards('user_email', $this->email);
}
// Use caching for frequently accessed data
$user = Cache::remember("user:{$email}", 3600, function () use ($email) {
return User::findAcrossShards('email', $email);
});
// Batch operations for efficiency
$results = User::batchUpdateAcrossShards(
['status' => 'inactive'],
['last_activity' => now()]
);