Sling Academy
Home/PHP/How to Set Up and Configure Redis in Laravel

How to Set Up and Configure Redis in Laravel

Last updated: January 18, 2024

Introduction

Laravel, a robust MVC framework for PHP, provides an elegant way of using Redis, an advanced key-value store. Redis is known for its flexibility, performance, and wide language support. In this tutorial, we will dive into setting up and configuring Redis as a cache and session driver for a Laravel application.

Prerequisites

  • A Laravel application ready for configuration
  • Composer installed on your system
  • PHP installed on your server
  • Redis Server installed
  • Basic understanding of Laravel and Redis

Step-by-Step Instruction

Step 1: Install Laravel Redis Package

composer require predis/predis

This command will install the necessary package to communicate with Redis through PHP.

Step 2: Configure .env

Next, you’ll need to provide the connection details for Redis in your application’s `.env` file:

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

These settings define the host, password, and port for the Redis server.

Step 3: Configure Redis in config/database.php

Edit the `config/database.php` file and find the Redis configurations. You might see something like this:

'redis' => [

    'client' => env('REDIS_CLIENT', 'predis'),

    'options' => [
        'cluster' => env('REDIS_CLUSTER', 'redis'),
        'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
    ],

    'default' => [
        'url' => env('REDIS_URL'),
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', '6379'),
        'database' => env('REDIS_DB', '0'),
    ],

    'cache' => [
        'url' => env('REDIS_URL'),
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', '6379'),
        'database' => env('REDIS_CACHE_DB', '1'),
    ],

],

Make sure the configurations match the details specified in your .env file.

Step 4: Set Cache and Session Drivers

Adjust the `.env` file to use Redis for caching and sessions:

CACHE_DRIVER=redis
SESSION_DRIVER=redis

This alteration tells Laravel to use Redis as the driver for both cache and session data. Don’t forget to clear the cache after making changes:

php artisan config:cache

Step 5: Testing Redis Connection

To ensure that everything is set up properly, you can use Tinker to test the connection:

php artisan tinker
> cache()->store('redis')->put('Laravel', 'Awesome', 10);
> cache()->store('redis')->get('Laravel');

If you see ‘Awesome’ returned, then your application is successfully connected to Redis.

Step 6: Using Redis for Queues

If you want to use Redis for queue management, update the `.env` file once more:

QUEUE_CONNECTION=redis

Then, you can dispatch jobs to the Redis queue like any other queue in Laravel:

dispatch(new ProcessPodcast($podcast));

Don’t forget to run the queue worker:

php artisan queue:work --queue=default

Conclusion

Throughout this tutorial, you’ve learned how to set up and configure Redis in your Laravel application. With Redis acting as your session, caching, and queue management driver, you can expect improved application performance and efficient data handling.

Next Article: How to Implement Rate Limiting in Laravel

Previous Article: Specifying the HTTP method for Laravel routes

Series: Laravel & Eloquent Tutorials

PHP

You May Also Like

  • Pandas DataFrame.value_counts() method: Explained with examples
  • Constructor Property Promotion in PHP: Tutorial & Examples
  • Understanding mixed types in PHP (5 examples)
  • Union Types in PHP: A practical guide (5 examples)
  • PHP: How to implement type checking in a function (PHP 8+)
  • Symfony + Doctrine: Implementing cursor-based pagination
  • Laravel + Eloquent: How to Group Data by Multiple Columns
  • PHP: How to convert CSV data to HTML tables
  • Using ‘never’ return type in PHP (PHP 8.1+)
  • Nullable (Optional) Types in PHP: A practical guide (5 examples)
  • Explore Attributes (Annotations) in Modern PHP (5 examples)
  • An introduction to WeakMap in PHP (6 examples)
  • Type Declarations for Class Properties in PHP (5 examples)
  • Static Return Type in PHP: Explained with examples
  • PHP: Using DocBlock comments to annotate variables
  • PHP: How to ping a server/website and get the response time
  • PHP: 3 Ways to Get City/Country from IP Address
  • PHP: How to find the mode(s) of an array (4 examples)
  • PHP: Calculate standard deviation & variance of an array