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

# Redis for Caching, Sessions and Queues on Servbase

> Create a Redis database on Servbase, connect your app to it, and use it for caching, sessions and queues with Node.js, Python, PHP and Laravel examples.

Redis gives your apps a fast key-value store for caching, sessions, rate limits and queues.

## How Redis works on Servbase

* A **Redis database** is a key prefix. A database named `user0000_cache` owns every key that starts with `user0000_cache:`.
* A **Redis user** signs in with a username and password and can use only the keys of its own databases.
* Redis is reachable **only from your apps** at `127.0.0.1:6390`. It is not exposed to the internet.
* Redis runs separately from the Servbase platform, and every customer is isolated from every other customer.

> [!WARNING]
>
> Redis memory is shared on each server. When it is full, the least recently used keys are removed. Use Redis for data you can rebuild (caches, sessions, queues), and keep the only copy of important data in MySQL or PostgreSQL.

## Create a Redis database

**1. Create a Redis user**

Open **Databases**, select **Create Database User**, choose **Redis**, then enter a username and password.

**2. Create the database**

Select **Create Database**, choose the Redis user, and enter a name such as `cache`. The final name, for example `user0000_cache`, is the key prefix.

**3. Add the variables to your app**

Open your app, go to **Settings**, then **Variables**, and add:

| Variable | Value |
| - | - |
| `REDIS_HOST` | `127.0.0.1` |
| `REDIS_PORT` | `6390` |
| `REDIS_USERNAME` | your Redis username |
| `REDIS_PASSWORD` | your Redis password (mark it as secret) |
| `REDIS_PREFIX` | your database name followed by a colon, for example `user0000_cache:` |

## Use it in your code

Every key must start with your prefix. The examples set the prefix once so the rest of your code does not need to.

```javascript Node.js (ioredis) theme={null}
import Redis from "ioredis";

const redis = new Redis({
  host: process.env.REDIS_HOST,
  port: Number(process.env.REDIS_PORT),
  username: process.env.REDIS_USERNAME,
  password: process.env.REDIS_PASSWORD,
  keyPrefix: process.env.REDIS_PREFIX, // every key is stored as "<prefix>name"
  enableReadyCheck: false,
});

// Cache a value for 5 minutes
await redis.set("homepage", JSON.stringify(data), "EX", 300);
const cached = await redis.get("homepage");
```

```python Python (redis-py) theme={null}
import os
import redis

r = redis.Redis(
    host=os.environ["REDIS_HOST"],
    port=int(os.environ["REDIS_PORT"]),
    username=os.environ["REDIS_USERNAME"],
    password=os.environ["REDIS_PASSWORD"],
    decode_responses=True,
)
prefix = os.environ["REDIS_PREFIX"]

r.set(f"{prefix}homepage", "cached html", ex=300)
print(r.get(f"{prefix}homepage"))
```

```php PHP (phpredis) theme={null}
<?php
$redis = new Redis();
$redis->connect(getenv('REDIS_HOST'), (int) getenv('REDIS_PORT'));
$redis->auth([getenv('REDIS_USERNAME'), getenv('REDIS_PASSWORD')]);
$redis->setOption(Redis::OPT_PREFIX, getenv('REDIS_PREFIX'));

$redis->setex('homepage', 300, $html);
echo $redis->get('homepage');
```

### Laravel

Set these in your app's variables, then use the `redis` cache, session or queue driver:

```bash theme={null}
REDIS_CLIENT=phpredis
REDIS_HOST=127.0.0.1
REDIS_PORT=6390
REDIS_USERNAME=user0000_redis
REDIS_PASSWORD=your-password
REDIS_PREFIX=user0000_cache:
CACHE_STORE=redis
SESSION_DRIVER=redis
```

## Example: cache an API response (Express)

```javascript theme={null}
app.get("/products", async (req, res) => {
  const hit = await redis.get("products");
  if (hit) return res.json(JSON.parse(hit));

  const products = await db.query("SELECT * FROM products");
  await redis.set("products", JSON.stringify(products), "EX", 60);
  res.json(products);
});
```

## Commands that are not available

To keep customers isolated, commands that could see or change data outside your prefix, or affect the whole server, are disabled. These include `KEYS`, `SCAN`, `RANDOMKEY`, `DBSIZE`, `INFO`, `SELECT`, `FLUSHDB`, `FLUSHALL`, `CONFIG`, `MONITOR`, `SORT`, `PUBSUB` and `FUNCTION`.

* To remove a group of keys, keep their names in a set, or give them an expiry time.
* Laravel's `php artisan cache:clear` uses `FLUSHDB` and is not available. Use cache tags or expiry times instead.
* Pub/Sub channel names must also start with your prefix.
* Libraries that call `INFO` while connecting may need that check turned off (for example `enableReadyCheck: false` in ioredis).

## Change the password or delete

* **Change password:** in **Database Users**, select the key icon. Apps using the old password are disconnected, so update `REDIS_PASSWORD` in your app variables.
* **Delete a database:** removes every key with that prefix.
* **Delete the user:** removes the user and all keys of its databases.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.