Using Redis in Laravel [Series]
Written by Christophe Limpalair on 12/13/2015
In this introduction to the Redis Series, I briefly describe what Redis is and explain how to implement it in Laravel with Predis. Predis is a PHP library for Redis that is very easy to install.
(Make sure to view in 720p)
However, it is not the most powerful library out there for large scale PHP applications because it is written in PHP. PHPRedis, an alternative, is written in C and can perform better under heavy loads and when dealing with large data.
While Predis should work well for most people, the second episode walks through the installation and implementation of PhpRedis.
After that, we will walk through practical use cases so that you can get a better idea of what Redis has to offer and whether it can be helpful for your app.
Here's the first practical use case -- storing page views and using Redis Sorted Sets to create "Top Viewed Pages" or "Top Liked Posts" sections on your website.
The second practical use case shows how to cache database queries. This is really useful if you have static content (like blog posts), that you know won't change for a certain amount of time. With Redis and Laravel Caching system, you can cache data for a certain amount of time, and then re-fetch from the database. Doing it this way prevents your database from being overloaded with work during high traffic loads.
Just think of the difference between querying your database once every 30 minutes (even if you get 5,000 views) vs. querying 5,000 times!
In this third practical use case, we take a look at using Sorted Sets and Sets in Redis to tag anything from articles and products to movies. Using these data types let's us use methods like sdiff, sunion, and zInterStore. These methods allow us to see which tags intersect in different posts, for example. Powerful stuff!
In this use case, we create a Twitter news feed using only Redis and Laravel. This means we rely entirely on Redis functions which shows you how powerful it is. I had a lot of fun with this one because there were so many options I could go with. I decided to keep it simple, but you should go all out :).
In this episode, we look at queuing with Redis using Laravel's Commands. These commands make our job ridiculously easy so this is a much shorter episode.
While Redis may not be the best queuing system because that's not what it was specifically built for, it certainly has it's uses especially if you want to keep a simpler stack and still have a quick queuing system.
By the way, I upgraded my gear so video should be crystal clear in high def now :)
Episode 8 - Data Types
Instead of having data types scattered all over the various episodes, I have them all explained with examples and practical use cases in one video:
I thought it would be a lot of help to also write a blog post about these data types. That way you can have a reference for your personal projects. Run into a logic roadblock with Redis in your application? Take a look at this post.
Episode 9 - Redis Configuration
Do you know what happens when Redis runs out of memory to use while under heavy load? The way you have it configured right now could very well take down your server. What about backing up your data? Do you know exactly when and where your information gets stored? If your server shuts down unexpectedly, how much data will you lose?
I address all these questions and more in this episode. If you use Redis in production, this is a must watch.
One more episode and I will conclude the series so I can move on to an AWS series.
Episode 10 - Pub/Sub with Redis and Laravel
This episode shows you how to start with Pub/Sub with Redis in Laravel. This is an interesting feature that can benefit you in a number of different ways. For example, you could clear cache and recalculate your home page to display new user-submitted content after one of those users submits. That way, you don't have to worry about setting long cache times and serving stale content, but you still get the benefits of caching.
This video concludes the series on Laravel and Redis. Hope you enjoyed it, and that it helped you take your application(s) further.
Resources to learn more:
- PhpRedis Documentation (Most, if not all, commands are identical in Predis)
- Interactive "Try Redis" demo
- Redis main website