Docs cipi.yml Web panel API MCP Guides Compare GitHub Telegram channel
  1. Home
  2. Guides
  3. Laravel cache, sessions and queues on Valkey (Redis)
Laravel

Laravel cache, sessions and queues on Valkey (Redis)

What Redis and Valkey are, when to move Laravel's cache, sessions and queues off the database, and how to do it on Cipi with one isolated Valkey instance per app: switching drivers, memory limits, eviction, moving off a shared instance, and debugging.

By Updated 3 min read

Laravel can keep cache, sessions and queues in several places. New apps on Cipi keep them in the database, which works fine at the start. As traffic grows, moving them to Valkey, an in-memory store, makes them much faster and lighter on the database.

Key ideas, in plain words#

TermMeaning
CacheSaved results (Cache::remember) so you don't compute them again
SessionWhat the app remembers about a logged-in visitor between requests
QueueJobs waiting for a worker to process them
Valkey / RedisAn in-memory key-value store: very fast, data mostly in RAM
EvictionWhat Valkey does when it's full: e.g. drop the least recently used keys
  • Laravel app shopCACHE_STORE, SESSION_DRIVER, QUEUE_CONNECTION
  • Valkey for shop127.0.0.1:64xx, own password, user shop
  • Valkey for bloganother port, another password
  • Ubuntu servernothing exposed to the internet

When to switch#

SignMove
Heavy Cache:: use, slow pages under loadCACHE_STORE=redis
Many logged-in users, the sessions table growsSESSION_DRIVER=redis
You want Horizon, or many jobs per minuteQUEUE_CONNECTION=redis with Horizon
Rate limiting, Cache::lock(), Reverb scalingAlready covered by CACHE_STORE=redis

You can switch one at a time.

Before you start#

  • A Laravel app on Cipi. phpredis is installed with every PHP version, and the .env already holds REDIS_HOST, REDIS_PORT and REDIS_PASSWORD for the app's own instance.
bash
sudo cipi valkey list
sudo cipi app env shop --get=REDIS_PORT

1. Switch the drivers#

bash
sudo cipi app env shop --set=CACHE_STORE=redis
sudo cipi app env shop --set=SESSION_DRIVER=redis
sudo cipi app artisan shop config:cache  # deploys cache the config: rebuild it
sudo cipi worker restart shop            # workers re-read the config

Queues on Valkey run under Horizon (plain workers read the database connection):

bash
sudo cipi app env shop --set=QUEUE_CONNECTION=redis
sudo cipi app artisan shop config:cache
sudo cipi worker horizon enable shop     # needs laravel/horizon in the app

Horizon guide.

What happensEffect
CacheStarts empty: the first requests rebuild it
SessionsLogged-in users are logged out once (old sessions were in the database)
Queue (with Horizon)Jobs already in the jobs table stay there: drain them once with queue:work database --stop-when-empty

Switch sessions at a quiet time, or tell users they'll need to log in again.

2. Check it works#

bash
sudo cipi app artisan shop tinker --execute="Cache::put('ping','pong',60); echo Cache::get('ping');"
pong
sudo cipi valkey list                    # shop: its own instance, running

3. Cap memory and pick an eviction policy#

Without a cap, an instance grows with your data. For an app that still uses the shared instance, set the cap while you isolate it (step 4):

bash
sudo cipi valkey isolate shop --maxmemory=256mb --policy=allkeys-lru

For an app that already has its own instance (every new app), add the two lines to its config and restart it:

bash
echo -e "maxmemory 256mb\nmaxmemory-policy allkeys-lru" | sudo tee -a /etc/cipi-valkey/shop.conf
sudo cipi valkey restart shop
PolicyUse for
allkeys-lruMostly cache: drops the least used keys when full
volatile-lruCache plus data without expiry (sessions with TTL, queue): drops only keys with an expiry
noevictionQueues only: refuses writes when full, so jobs are never dropped silently

If you keep queues and cache in the same instance, volatile-lru is the safe middle: cache keys have a TTL, queued jobs don't.

Valkey
$ cipi valkey list
$ cipi valkey isolate shop --maxmemory=256mb

4. Apps still on the shared instance#

Older servers started with one Valkey for everybody. Move each app to its own:

bash
sudo cipi pending                          # lists apps still on the shared instance
sudo cipi valkey isolate shop              # or --all
Step 1 of 5Maintenance on

The app returns 503 for a few seconds so nothing writes during the move.

If the copy fails, the app stays exactly where it was. Once nobody uses the shared instance, sudo cipi valkey flush-shared empties it and changes its password.

5. Look inside#

bash
sudo cipi app env shop --get=REDIS_PORT
valkey-cli -p 6401 -a "$(sudo cipi app env shop --get=REDIS_PASSWORD)" info memory
valkey-cli -p 6401 -a "…" --scan --pattern '*cache*' | head

Avoid KEYS * on a big instance: it blocks it. --scan doesn't.

Good habits#

HabitWhy
Always set a TTL on cache keysLets eviction work and memory stay flat
Don't treat cache as storageAnything in it may disappear
Separate queue and cache Redis databases (REDIS_CACHE_DB)cache:clear won't touch queued jobs
Restart workers after driver changesThey keep the old config in memory

Troubleshooting#

ProblemFix
Connection refusedInstance stopped: sudo cipi valkey restart shop
NOAUTH / WRONGPASS.env and instance disagree: compare REDIS_PASSWORD with requirepass in /etc/cipi-valkey/shop.conf
Everyone logged outExpected once after switching SESSION_DRIVER
Jobs stuck in the databaseDrain the old jobs table (see step 1)
OOM command not allowedMemory cap reached with noeviction: raise --maxmemory or change policy
Class "Redis" not foundVery rare: the app's PHP lacks phpredis; reinstall that PHP version

Questions

Is Valkey the same as Redis?

For Laravel, yes. Valkey is the open-source (BSD) continuation of Redis, maintained under the Linux Foundation. Same protocol, same phpredis driver, same REDIS_* variables, redis as the driver name.

Why not keep everything in the database?

You can, and new Cipi apps do. Valkey is faster for high traffic, lots of cache keys, Horizon, rate limiting and locks, and it takes that load off the database.

Do apps share one Valkey?

No. Each Laravel app gets its own instance, on its own port, with its own password, running as the app's user. One app can't read or flush another's data.

Will I lose sessions if Valkey restarts?

Each instance keeps data on disk in /var/lib/cipi-valkey/<app>, so a normal restart keeps it. Treat cache as disposable and don't store anything in it you can't rebuild.

How much memory does it use?

About 10 MB per idle instance, growing with your data. Set a cap and an eviction policy (step 3).

What does `cache:clear` clear?

Only the app's own instance (or prefix). With isolated instances it can't touch another app.

What if a step fails?

Read the troubleshooting table above first. Then check the matching docs page, open a GitHub issue, or ask in the community. Most commands print the fix when they refuse something.

Try it on your own server

Free and open source. One command on a fresh Ubuntu 24.04 or 26.04 VPS.

wget -O - https://cipi.sh/setup.sh | bash
Read the quickstart