> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fly.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Laravel and Redis

[Redis](https://redis.io/) 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](https://hub.docker.com/r/flyio/redis/tags) for the build:

   ```bash theme={null}
   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

   <br />

   <br />

2. Afterwards, add a [volume](/volumes) that will persist your Redis data

   ```bash theme={null}
   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

   <br />

   <br />

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

   ```toml theme={null}
   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

   <br />

   <br />

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

   ```bash theme={null}
   fly secrets set REDIS_PASSWORD=<redacted>
   ```

5. Finally, deploy your Redis Fly App!

   ```bash theme={null}
   fly deploy
   ```

### *Redis Official Docker Image*

If you would like to use [Redis' official docker image](https://hub.docker.com/_/redis) 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](#fly-io-redis-docker-image), with a revision to the value passed for `--image`

   ```bash theme={null}
   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:

   ```toml theme={null}
   [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](/networking/private-networking#fly-io-internal-addresses).

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

   ```bash theme={null}
   cd <laravel-app-folder>
   ```

3. Then revise your Laravel Fly App's `fly.toml` `[env]` configuration with the retrieved Fly .internal Address:

   ```toml theme={null}
   [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:

   ```bash theme={null}
   fly secrets set REDIS_PASSWORD=<redacted>
   ```

5. Finally, deploy!

   ```bash theme={null}
   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

   ```bash theme={null}
   cd off-redis
   ```

2. Then run the `fly proxy` for port 6379 or whichever REDIS\_PORT you have configured:

   ```bash theme={null}
   fly proxy 6379
   ```

3. Next, revise your local Laravel application's .env file to update your Redis connection:

   ```env theme={null}
   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](https://upstash.com/)! To set up, you can follow our-in-depth guide [here](/upstash/redis)

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:

```output theme={null}
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:

   ```toml theme={null}
   [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...`

   <br />

   <br />
2. Then deploy your `fly.toml` changes:

   ```bash theme={null}
   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.
