Welcome to the Redisverse! ๐
Redis, short for REmote DIctionary Server, is more than just a name - itโs a powerhouse of speed and versatility in the world of data storage! Letโs dive into its magical realm! ๐ฎโจ
Setting Up Redis Camp ๐๏ธ
Building Redis Clusters ๐ฟ๐
If youโre not a lone ranger, but rather a team of data wranglers, itโs time to create some interconnected Redis clusters! Think of them as an Avengers squad for your data needs. ๐ค๐ก๏ธ
Choosing Your Side: PreDIS or PhpRedIS? ๐๐
Two popular libraries come to the rescue when interacting with our hero, Redis. We have PreDIS (the prepubescent sidekick) and PhpRedIS (the grizzled veteran). Both are great, but you might as well pick the one that suits your projectโs needs, Batman! ๐ฆ
Training with Redis Kung Fu ๐ฅ๐คนโโ๏ธ
Transactions: The Data Jedi Mind Trick ๐ซ๐
Imagine being able to perform multiple operations at once, as if they were a single, unified action! Thatโs exactly what transactions do. They allow you to execute complex commands with the precision and finesse of a data-manipulating ninja.
Pipelining Commands: Command Queue Chaos ๐ช๐ฅ
If you thought transactions were cool, just wait until you hear about pipelining! Itโs like having an assembly line for your Redis commands. Send multiple commands in a single shot and watch them execute with superhuman speed!
Join the Redis League of Communication ๐ฃ๏ธ๐ค
Pub/Sub: The Chat Room of Data Exchange ๐ฌ๐
Need to keep track of real-time data changes? Pub/Sub is your go-to! Itโs like having a secret agent network for your application. Publishers send messages (or events), and subscribers listen in, making it the perfect solution for chat rooms, notifications, or any other situation where real-time updates are crucial!
So there you have it, folks! Welcome to the exciting world of Redis โ where data is stored with superhuman speed and versatility. Remember to use your newfound powers wisely, and happy coding! ๐๐ป๐
Alright, buckle up, data wranglers! Weโre about to embark on a whirlwind tour of Redis, the swankiest open-source key-value store this side of Silicon Valley. Yes, itโs fancy - think of it as the black tie event of the database world. Itโs not just a data storage system; itโs a data structure server, and keys can house everything from basic strings to intricate hashes, lists, sets, and sorted sets.
Now, before you can start tossing around Redis like a boss with Laravel, there are a few preliminary steps. First off, we highly recommend installing the PhpRedis PHP extension via PECL. Itโs a bit more complex than your average user-land PHP package, but if your application is heavy on the Redis use, it can deliver performance like an Olympic sprinter on a sugar rush.
Lucky for you Laravel Sail shipmates, this extension is already anchored in your Docker containerโs treasure chest. If youโre feeling seasick and unable to install PhpRedis, fear not! You can always jump ship and install the predis/predis package via Composer. Itโs a Redis client written entirely in PHP, so no need for any extra extensions - just drop this line in your terminal:
composer require predis/predis
Now that weโve got the nautical jargon out of the way, letโs set sail to the configuration section! (Cue the pirate music)
Alright, buckle up, Redis rebels! Itโs time to get your settings straight in the wild world of Laravel. You can customize your appโs Redis configuration like a boss in the config/database.php file, which is where all the cool kids hang out.
Here, youโll find a redis array thatโs chock-full of Redis servers working tirelessly for your application:
'redis' => [
...
] // It's like a box of chocolates, but instead of candies, it's Redis servers!
Now, letโs dive into the juicy details. The redis array contains an assortment of options, each one representing a Redis server in your configuration file. But hereโs the twist - each server needs a name, host, and port, unless youโre feeling fancy and want to use a single URL for your Redis connection:
'redis' => [
...
'default' => [ // Our main squeeze, the default server
'url' => 'tcp://127.0.0.1:6379?database=0', // If you're using a URL, it's like speed-dating for Redis servers!
],
...
] // And here we have our cache server, just hanging around waiting for love... or data.
Remember, Redis is like the life of the party - always ready to serve up some data when you need it most! So, keep your settings tight and your Redis even tighter. Happy configuring! ๐๐ฅณ๐ช๐ผ๐ค๐ค๐ฅ๐
Alrighty, buckle up, Laravel astronauts! Itโs time to dive into the mystical world of Redis configurations, where data dances and connections sing a beautiful symphony. By default, our trusty Redis clients are like old-school radio DJs, spinning tunes over good olโ tcp. But hey, who needs a catchy beat when you can have an encrypted dance party with TLS/SSL?
To throw on those shiny encryption shades, simply sprinkle some configuration magic into your Redis server setup array. Hereโs how:
'default' => [
'scheme' => 'a fancy disco ball', // Okay, not exactly, but it feels cooler!
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
],
And just like that, youโve transformed your data connection into a secure nightclub, where only the right passwords can enter. Enjoy the party! ๐ค๐
Cluster-palooza! ๐ค
If your app is jamming with a posse of Redis servers, itโs time to round โem up and wrangle โem into a herd - err, cluster! To do that, you need to saddle up and ride on over to the clusters corral within your Redis configuration. You see, this cowpoke ainโt no stranger in the wild west of your applicationโs config/database.php configuration file:
'redis' => [
'client' => env('REDIS_CLIENT', 'phpredis'), // Ain't no party like a Redis client party, right?
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'), // The default cluster is like the head cowboy, in charge.
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'), // A unique prefix to separate your data from all the other wranglers out there.
],
'clusters' => [ // Here's where you lasso and rope in each server.
'default' => [
[
'url' => env('REDIS_URL'), // The URL of your Redis server, like the address to its ranch.
'host' => env('REDIS_HOST', '127.0.0.1'), // Its home on the range, in case you need to visit.
'username' => env('REDIS_USERNAME'), // The login credentials for when you drop by.
'password' => env('REDIS_PASSWORD'), // And the secret handshake.
'port' => env('REDIS_PORT', '6379'), // The port of call, where all the action happens.
'database' => env('REDIS_DB', '0'), // The database it keeps its herd in.
],
],
],
// ...
],
By default, Laravel saddles up with native Redis clustering because the options.cluster value is set to redis. Itโs a sturdy horse that knows how to handle a fall-over with grace.
Now, if youโre feeling rowdy and want to swap out the native Redis for client-side sharding (Predis), all you gotta do is high-ho Silver and git โer done by removing the options.cluster value:
'redis' => [
'client' => env('REDIS_CLIENT', 'phpredis'), // Stick with your trusty steed, Predis.
'clusters' => [ // Here's where you lasso and rope in each server.
// ...
],
// ...
],
Now that you know how to ride the Redis cluster roundup, you can wrangle your data with ease! ๐ ๐
Predis (aka Redisโ BFF)
If your app is the life of the party and you want it to hit the dance floor with Redis, make sure the REDIS_CLIENT env var is set to โpredisโ in your makeup room:
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'), // because, who doesn't love a good pre-party?
// ...
],
Now, Predis isnโt just your average wingman. It comes armed with a suite of connection swag to help your app and Redis servers communicate better than two lovebirds on a double date! To get these party tricks up and running, drop the connection swag into your Redis server configuration in your appโs config/database.php configuration file:
'default' => [
'url' => env('REDIS_URL'), // For those spontaneous meet-cutes online
'host' => env('REDIS_HOST', '127.0.0.1'), // Keeping it local, ya know?
'username' => env('REDIS_USERNAME'), // Because everyone needs a little validation
'password' => env('REDIS_PASSWORD'), // Security is key, after all!
'port' => env('REDIS_PORT', '6379'), // The DJ spins on this channel
'database' => env('REDIS_DB', '0'), // Pick your dance floor wisely
'read_write_timeout' => 60, // Party's over when the music stops!
],
Now get out there and make some moves, party people! ๐๐บ๏ธ๐
PhpRedis: The Chatty Redis Chummery! ๐๐
Ahoy there, Laravel sailor! When it comes to chatting with our trusty Redis pal, our ship, I mean, framework, defaults to using the PhpRedis extension as its interpreter ๐ค. Fancy a drink? Letโs mix up this potion:
'redis' => [
'client' => env('REDIS_CLIENT', 'phpredis'), // You know, like the environment variable we set earlier, matey!
// ...
],
Besides the default sea shanties, PhpRedis offers some exciting tavern yarns for you to spin: name, persistent, persistent_id, prefix, read_timeout, retry_interval, max_retries, backoff_algorithm, backoff_base, backoff_cap, timeout, and context ๐๏ธ. Wanna hear a tale? Add some of these options to your Redisโ seaworthiness in the config/database.php file:
'default' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'), // Just in case you were lost at sea...
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
// Oh, and let's not forget the tavern stories:
'read_timeout' => 60, // Because who wants a chatty Redis, right?
'context' => [
// For more engaging tales, adjust these settings as needed!
],
],
Now, letโs talk shop about retry and backoff configuration! With PhpRedis, you can set up some sea-worthy strategies to handle those pesky Redis errors. Here are the sailorโs secrets: read_timeout, retry_interval, max_retries, backoff_algorithm, backoff_base, backoff_cap, and timeout. You can use these when crafting your own backoff policy in the config/database.php file:
'default' => [
// ...
'options' => [
'strict' => false,
'backoff_policy' => [
'type' => 'exponential',
'base' => 2,
'max_retry_delay' => 60,
],
],
],
Ahoy there, Laravel pirates! Letโs hoist the sails of connection configuration and set course for Redis reconnection land. Yarrrr!
Reconnect and Retry Patchwork
When Old Redbeard (your Redis server) goes walkabout, weโve got a few handy tools to get him back: retry_interval, max_retries, backoff_algorithm, backoff_base, and backoff_cap. These seafaring options tell our PhpRedis mate how to reel in the runaway Redbeard.
Hereโs a smorgasbord of backoff algorithms we can use: default, decorrelated_jitter, equal_jitter, exponential, uniform, and constant. Sounds like an exotic cocktail menu, doesnโt it?
'default' => [
// Piratey stuff here!
'backoff_algorithm' => 'decorrelated_jitter', // Choose your poison!
],
Now, Predis 3.4.0 and beyond comes equipped with its very own built-in reconnect and retry gear via the Retry class. Letโs tweak it with these strategies: NoBackoff, EqualBackoff, or ExponentialBackoff.
use Predis\Retry;
use Predis\Retry\Strategy\ExponentialBackoff;
'default' => [
// Piratey stuff here!
'retry' => new Retry(
new ExponentialBackoff(
// Backoff base and cap, matey!
env('REDIS_BACKOFF_BASE', 100),
env('REDIS_BACKOFF_CAP', 1000),
true, // Enable jitter if ye be feeling lucky
),
env('REDIS_MAX_RETRIES', 3)
)
],
Avast! Now youโve got the lowdown on how to navigate those choppy Redis waters like a pro. Happy sailing, matey!
Ahoy there, Laravel sailors! Letโs dive into the world of Unix sockets, shall we? โCause when youโre working on the same ship (server) as your Redis instance, cutting out TCP overhead is like having a secret shortcut to Treasure Island.
To make Redis dance to our tune, letโs set sail and hoist the REDIS_HOST environment variable with the path of our trusty Redis socket anchored at /run/redis/redis.sock. And since Redis is a simple soul who doesnโt need a fancy port number, weโll tell it to hang out at REDIS_PORT=0.
Anchors aweigh! With these settings, your Laravel app will now communicate with Redis like long-lost pirate buddies sharing grog on the high seas. Arr matey, smooth sailing ahead! ๐ดโโ ๏ธ๐
Alright, letโs dive into the wild world of PhpRedis! This isnโt just your average Redis extension; itโs like a Swiss Army knife for data storage, with a side of pickles (or compression, if you prefer).
You see, these bad boys can be fine-tuned with a variety of serializers and compression algorithms. And the best part? You can configure them like a boss via the options array in your Redis configuration. Itโs like adding sprinkles on your data sundae!
'redis' => [
'client' => env('REDIS_CLIENT', 'phpredis'), // Don't forget to give it a cool name, like 'RedisMcFly'
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
'serializer' => Redis::SERIALIZER_MSGPACK, // Choose your poison: MSGPack it is!
'compression' => Redis::COMPRESSION_LZ4, // Because who doesn't love a good LZ4 laugh?
],
// ...
],
Now, these serializers are as varied as flavors of ice cream. You got the default Redis::SERIALIZER_NONE, the ever-versatile Redis::SERIALIZER_PHP, the sweet Redis::SERIALIZER_JSON, the exotic Redis::SERIALIZER_IGBINARY, and the trendy Redis::SERIALIZER_MSGPACK.
When it comes to compression, weโve got the basics (Redis::COMPRESSION_NONE, aka uncompressed, like your grandmaโs apple pie), along with the advanced Redis::COMPRESSION_LZF, Redis::COMPRESSION_ZSTD, and Redis::COMPRESSION_LZ4. Just remember: More compression equals less data bloat!
So, go forth and configure away! Your data will thank you for it. Or maybe theyโll just stay silent and let you think theyโre happy. Data can be mysterious like that.
Chatting with the Redis Maven
Whoa, itโs time to get cozy with our Redis pal! To communicate with this silicon sage, simply summon various methods on the Redis bartender. The Redis bartender serves up magic cocktails (commands), and just like in a real bar, you can order anything from the Redis menu!
In this example, weโll serve up a round of โGETโ shots by calling the get method on our trusty Redis bartender:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Redis;
use Illuminate\View\View;
class UserController extends Controller
{
/**
* Serve up the profile for a specific user.
*/
public function show(string $id): View
{
return view('user.profile', [
'user' => Redis::get('user:profile:'.$id) // Shaking, stirring, and serving!
]);
}
}
Now, just like in a real bar, you can order any of Redisโ drinks (commands). Laravel employs some secret magic to pass the orders directly to the Redis server. If your drink requires extras, donโt forget to include them when calling the bartender:
use Illuminate\Support\Facades\Redis;
Redis::set('name', 'Taylor'); // A round of "SET" on the house!
$values = Redis::lrange('names', 5, 10); // Five Long Island Iced Teas coming right up!
Fancy a bit of mixology? You can pass drinks directly to the server using the command method, which takes the drinkโs name as its first argument and an array of ingredients as its second:
$values = Redis::command('lrange', ['name', 5, 10]); // Let's hear it for the "LRANGE" cocktail!
Now that weโve got your taste buds tingling, letโs talk about using multiple Redis connections. Imagine you have a busy bar with different rooms serving up various drinks - this is much like having multiple Redis servers for different tasks! With Laravel, setting up these mixology stations (connections) is as simple as configuring your app/config/redis.php file:
return [
// ...
'connections' => [
'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'port' => env('REDIS_PORT', 6379),
'database' => 0, // Use database 0 for this connection
],
'queue' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'port' => env('REDIS_PORT', 6380),
'database' => 1, // Use database 1 for this connection
],
]
];
After setting up your connections, you can choose which bartender to interact with by using the connection name when calling a method on the Redis facade:
use Illuminate\Support\Facades\Redis;
// Serve drinks from the "cache" bar
$cachedName = Redis::set('name', 'Taylor', 'cache');
// Toss a few cocktails over to the "queue" bar
Redis::lrange('names', 5, 10, 'queue');
And thatโs it! Now youโre ready to dive deep into the world of Redis mixology with Laravel! Cheers! ๐น๐
Ahoy there! Youโre about to embark on a thrilling journey of juggling multiple Redis servers like a circus performer, all within the comfort of your Laravel app!
First things first: head over to your applicationโs config/database.php file and prepare for some server-swapping shenanigans. This is where you define your Redis connections/servers like lining up your juggling balls before the big show.
Need to grab a connection to one of those servers? No problemo! Just summon the mighty Redis facade and its trusty connection method:
$redis = Redis::connection('connection-name'); // Swap in your unique circus ball, er, connection name!
But if youโre feeling lazy (and letโs face it, who isnโt?), you can always stick with the default Redis connection. To do so, simply leave the connection method all on its lonesome:
$redis = Redis::connection(); // The trusty old juggling ball - the classic that never disappoints!
Now that youโve got your connection sorted, itโs time to put on a show with transactions! Imagine if your circus acts required every trick to be in perfect synchronization. Transactions are just like that for your Redis connections. Hereโs how to wrap multiple commands into a single transaction:
$redis = Redis::connection();
$redis->multi()
->set('key', 'value')
->incr('counter')
->exec();
Just remember, these transactions are like delicate juggling acts - all commands must succeed or they all fail. But fear not! If one command fails during the transaction, you can catch the exception and try again without affecting the other commands in the pipe.
Happy juggling! May your Redis connections always remain steady as a trapeze artist mid-flight.
Redis Transactions: The Magical MULTI-EXEC Spell! ๐ฉ๐ฎ
Get ready to embark on an exciting journey with the mystical Redis spellbookโs enchantment called transaction! This powerful charm serves as a friendly sorcererโs apprentice, wrapping your common Redis commands in a protective cloak of MULTI and EXEC. To cast this spell, you merely need to pass a magical incantation (closure) as its sole argument. Your spell will summon a wise Redis connection instance, upon which you can unleash all your commands with wild abandon!
use Redis;
use Illuminate\Support\Facades;
Facades\Redis::transaction(function (Redis $redis) {
$redis->wave_the_wand('user_visits', 1); // Increment user visits by one.
$redis->raise_the_staff('total_visits', 1); // Increment total visits by one.
});
Caution
Be warned, young sorcerer: When you cast the Redis transaction spell, do not attempt to pluck values from the connection while under its enchantment. Remember, your incantation is woven as a single, unified spell that wonโt be cast until your entire magical arsenal has been released! ๐โจ
And there you have it! Now go forth and conquer the world, one Redis transaction at a time! ๐๐๐
Alrighty, letโs dive into the world of Lua scripts in Laravelโs Redis playground! The eval method is like the Jedi mind trick of database operations - it lets you execute a whole bunch of Redis commands in one magical swoopโฆ atomically, even!
But wait, thereโs more! Unlike other methods, this one can interact with and inspect Redis key values during its operation. So itโs like having a magical talking mirror that not only performs tasks for you but also gives you a sneak peek behind the scenes! The language of choice for this magic mirror is Lua, which, if youโre curious, is a programming language thatโs been around since 1993 and has been described as having more personalities than a cat with seven lives (check it out at Lua.org!).
Now, the eval method might seem a bit scary at first, but donโt worry - just like meeting a new pet octopus, it gets easier with time and practice! The method expects several arguments: first, the Lua script (as a string), second, the number of keys it interacts with, third, the names of those keys, and lastly, any additional arguments you need within your script.
For instance, letโs write a simple script that increments a counter, checks if its new value is greater than five, increments a second counter if so, and then returns the value of the first counter:
$value = Redis::eval(<<<'LUA'
local counter = redis.call("incr", KEYS[1])
if counter > 5 then
redis.call("incr", KEYS[2])
end
return counter
LUA, 2, 'first-counter', 'second-counter');
But remember, with great power comes great responsibility! Before you start crafting your own Lua scripts, make sure to consult the Redis documentation for more information on Redis scripting - itโs like learning to ride a unicycle with a safety net!
Now that weโve broken the ice (and possibly some Redis keys), letโs get ready to write some magical Lua scripts in Laravelโs Redis playground!
Command Carnival Ride! ๐
Ever found yourself in a pickle, juggling a gaggle of Redis commands like a circus performer? Well, fear not! Laravelโs here to save the day with its very own command funhouse - itโs called Pipelining Commands! ๐ช
Instead of making a series of trips to your Redis server (thatโd be like asking an elephant for a glass of water every time youโre thirsty), you can use the pipeline method. This delightful little trickster accepts just one invitation: a party where it dances with a Redis instance! ๐
Once the Redis instance has RSVPed, you can unleash all your commands upon this unsuspecting dance partner. When the music stops, theyโll all be shoved into a bag and sent to the Redis server at once - saving countless trips to the server and keeping your network happy as a clam! ๐
The best part? Your commands will remain in the order they were issued, just like when you line up for a rollercoaster and donโt want anyone jumping the queue!
use Redis;
use Illuminate\Support\Facades;
Facades\Redis::pipeline(function (Redis $pipe) {
// Time to party! ๐ฅณ
for ($i = 0; $i < 1000; $i++) {
// Set 'key:$i' with some sweet dance moves - err, data! ๐บ๏ธ
$pipe->set("key:$i", $i);
}
});
Get ready to reduce server trips, increase efficiency, and make your network smile - all while feeling like the ringmaster of a command-filled circus! ๐ชโจ๐
Pub/Sub: A Symphony of Code in the Dataverse! ๐บ
In Laravelโs grand digital orchestra, the Pub/Sub section is where code instruments start communicating through the magical medium of Redis! This system enables your applications and processes to dance together like Fred Astaire and Ginger Rogers.
First, letโs create a glamorous channel listener by calling the sultry subscribe method in an Artisan command. Weโll place this sultry call within an Artisan command since the subscribe method begins a long-running process - think of it as a never-ending ballroom dance!
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
class RedisSubscribe extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'redis:subscribe';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Subscribe to a Redis channel and cha-cha-cha through the messages!';
/**
* Execute the console command.
*/
public function handle(): void
{
Redis::subscribe(['test-channel'], function (string $message) {
echo "Sweet whispers from the test-channel: {$message}";
});
}
}
Now, we can sashay over to our route and publish messages to the channel using the publish method. Think of it as a dance move that makes everyone on the Redis dance floor take notice!
use Illuminate\Support\Facades\Redis;
Route::get('/publish', function () {
// ...
Redis::publish('test-channel', json_encode([
'name' => 'Adam Wathan'
]));
});
By the way, have you heard about Wildcard Subscriptions? Itโs like being able to dance with multiple partners at once! ๐บ๐ Simply subscribe to a channel pattern like test-*, and your application will listen to messages from all channels that match. (You can find more details here). So grab your partner, or partners, and start dancing! ๐ถ๐
Alrighty, letโs dive into the exhilarating world of Wildcard Subscriptions! This feature, much like a hungry cat on the prowl for its next meal, allows you to snag all messages from any channel โ yes, all of them, and I mean all!
To set it up, simply call psubscribe and pass it a wildcard-shaped argument. Itโll be just as second nature as pouring your morning coffee (but donโt spill it on your keyboard).
Hereโs an example that should put a spring in your step:
// Listen to every little message, from every channel, like a nosy neighbor peeking through the window.
Redis::psubscribe(['*'], function (string $message, string $channel) {
// Print out each message like a late-night talk show host revealing secret gossip.
echo $message;
});
// If you're only interested in conversations about users, you can narrow it down by channel.
Redis::psubscribe(['users.*'], function (string $message, string $channel) {
// Print out user-related messages as if you're reading from the latest tabloid magazine.
echo $message;
});
So there you have it! Wildcard Subscriptions โ the Laravel way to listen in on all the juicy gossip happening across your channels, without even leaving home!