Laravel and Redis

Redis is a NoSQL database popularly used for cache storage, as a message broker, and even as a primary database. You can run your own Redis Fly App, or get one running and managed through Upstash Redis.

Laravel and Redis Fly App


You can set up your own Redis Fly App either through Fly.io’s Redis Docker Image, or through Redis’ Official Docker Image.

Fly.io Redis Docker Image


  1. First create a new directory and initialize it with fly launch, using Fly.io’s Redis Image for the build:

    mkdir fly-redis
    cd fly-redis
    
    fly launch --image flyio/redis:6.2.6 --name fly-redis --region ams --no-deploy
    
    • Pull the flyio/redis:6.2.6 image from the Docker repository through --image argument.
    • You can specify your Redis Fly App name through the --name argument
    • You can specify your region code through the --region attribute
    • You can opt not to immediately deploy by adding --no-deploy argument

  2. Afterwards, add a volume that will persist your Redis data

    fly volumes create redis_server --size 1
    
    • You can specify any volume name to replace redis_server above
    • You can specify your preferred volume size in GB through the --size argument

  3. Then, attach your volume to your Redis Fly App by revising its fly.toml to include [[mounts]]:

    app = "fly-redis"
    # Your primary region may differ
    primary_region = "ams"
    
    [build]
      image = "flyio/redis:6.2.6"
    
    [[mounts]]
      destination = "/data"
      source = "redis_server"
    
    • Under [[mounts]] attach the volume created in step #2, make sure that the name specified for the created volume is exactly the same string specified for its source attribute


  4. Next, set up the Redis Fly App password through fly secrets

    fly secrets set REDIS_PASSWORD=<redacted>
    
  5. Finally, deploy your Redis Fly App!

    fly deploy
    

Redis Official Docker Image


If you would like to use Redis’ official docker image to set up your Redis Fly App, the steps to get up and running are almost identical to the previous guide. There is just one minor revision on the image pulled and an additional step to set the Redis Fly App’s password.

  1. Follow the steps above, with a revision to the value passed for --image

    mkdir off-redis
    cd off-redis
    
    fly launch --image redis --name off-redis --region ams --no-deploy
    
    • Pull the redis image from the Docker repository through the --image argument.
  2. Before deploying, make sure to revise your fly.toml file to specify your Redis Fly App’s password.

    Add the [processes] section for the default app process and add the following commands:

    [processes]
    app = "sh -c exec redis-server --requirepass \"$REDIS_PASSWORD\""
    
    • Use the fly secret REDIS_PASSWORD you’ve configured earlier
    • If this passphrase is not set and your Laravel configuration sends a REDIS_PASSWORD during its connection to your Redis Fly App, watch out! You’ll get the infamous “ERR Client sent AUTH, but no password is set” error

Connect from a Laravel Fly App


Now that you have your Redis Fly App running, the next step is to connect it with your Laravel Fly App! Follow the steps below:

  1. First, retrieve your Redis Fly App’s Fly .internal Address.

  2. Next, make sure you move to your Laravel Fly App’s directory for better navigation:

    cd <laravel-app-folder>
    
  3. Then revise your Laravel Fly App’s fly.toml [env] configuration with the retrieved Fly .internal Address:

    [env]
      ...
      REDIS_HOST= "<redis_app_name>.internal"
      CACHE_DRIVER= "redis"
    
  4. Make sure you set your Laravel Fly App’s REDIS_PASSWORD through the fly secrets command:

    fly secrets set REDIS_PASSWORD=<redacted>
    
  5. Finally, deploy!

    fly deploy
    

That’s it! Your Laravel and Redis Fly Apps should now be connected.

Connect from a local environment


The simplest way to connect your local Laravel application to a Redis Fly App is to use fly proxy:

  1. Open your Redis Fly App’s local directory

    cd off-redis
    
  2. Then run the fly proxy for port 6379 or whichever REDIS_PORT you have configured:

    fly proxy 6379
    
  3. Next, revise your local Laravel application’s .env file to update your Redis connection:

    CACHE_DRIVER=redis
    REDIS_HOST=127.0.0.1
    REDIS_PASSWORD=<redacted>
    REDIS_PORT=6379
    

    Make sure the REDIS_PORT in your .env configuration matches the port used in fly proxy

    That’s it! Your local Laravel application should now be connected with your Redis Fly App.

Laravel with Upstash Managed Redis Fly App


Want a fully-managed Redis Fly App? Try Upstash Redis! To set up, you can follow our-in-depth guide here

Once you’ve configured the necessary details through the flyctl prompts, and the app deployment completes, you should get a summary of the Redis cluster you’ve just deployed, like so:

Your Upstash Redis database blue-brook-3843 is ready.
Apps in the personal org can connect to at redis://default:<redacted>@fly-blue-brook-3843.upstash.io
If you have redis-cli installed, use fly redis connect to connect to your database.

Connect From a Laravel Fly App


To test whether your Laravel Fly App can successfully connect with your Upstash Redis database, you can use your Upstash Redis Fly App as your Cache driver, then perform a quick cache:clear command.

  1. Revise the [env] configuration in your Laravel Fly App’s fly.toml file:

    [env]
      ...
      CACHE_DRIVER="redis"
      REDIS_URL="redis://default:<redacted>@fly-blue-brook-3843.upstash.io"
      REDIS_CACHE_DB=0
    
    • Set the Laravel cache driver to redis
    • Using REDIS_URL should override REDIS_HOST, add here the REDIS_URL received during fly redis create
    • Upstash only supports “database 0”, so update your REDIS_CACHE_DB config to 0. Otherwise, you will receive an error : SELECT failed: ERR Only 0th database is supported! Selected DB: 1...

  2. Then deploy your fly.toml changes:

    fly deploy
    

Testing Connection

In all examples above, Redis was used as our Laravel application’s Cache Driver.

To test if this connection is working or not, a simple php artisan cache:clear should let you know.