# 🗄️ Redis

## Information

The Redis add-on caches player data in Redis and keeps it in sync across a network of servers, so keys, cooldowns and win limits stay consistent as players move between them.

<!-- TODO - THUMBNAIL -->

👉 [You can purchase this add-on on Phoenix Plugins](https://www.phoenixplugins.com/)

## Installation&#x20;

To install this add-on, you can either:
- Drag and drop the jar file into the PhoenixCrates/addons folder, then restart your server.
- Download the plugin with the add-on selected during the downloading.

## Redis

Redis sits in front of your database, it does not replace it.

On the first start it generates a `redis.yml` file in the plugin's folder, where you set your connection details.

> **Warning:**
You still need MySQL configured. Redis is only the cache and locking layer, all data is still written to the database.

## Player Locking

While a server is handling a player, it holds a lock on that player in Redis so no other server can write their data at the same time. The lock is refreshed for as long as the player is online, and released when they leave or when the server shuts down.

If a lock cannot be obtained in time, the change is written straight to the database instead, so no rewards or keys are lost.

## Cross-Server Messaging

Servers connected to the same Redis with the same key prefix send each other messages, so an action taken on one server reaches the rest of the network. Resetting every [Crate Claim](crate-claim/README.md#admin-commands), for example, also clears the claims cached on the other servers. Messaging is enabled by default.

If the messaging connection drops, the server keeps retrying on its own until it is restored.

## Settings

| Setting | Description |
| ------- | ----------- |
| Host / Port / Password / Database | The connection details of your Redis server. |
| Connection Pool | The maximum, idle, and minimum number of connections kept open. Default is `8`, `8`, and `0`. |
| Key Prefix | The prefix used for every key the add-on writes and for its messaging channel. Networks sharing one Redis server stay apart, with separate player data, locks and messages, as long as each uses its own prefix. Default is `phoenixcrates`. |
| Data TTL | How long, in seconds, cached player data is kept. Default is `3600`. |
| Lock TTL | How long, in seconds, a player lock stays valid without a refresh. Default is `15`. |
| Heartbeat Interval | How often, in seconds, held locks are refreshed. Default is `5`. |
| Lock Poll Interval | How often, in milliseconds, a server retries while waiting for a lock. Default is `200`. |
| Max Lock Wait | How long, in milliseconds, to keep waiting for a lock before giving up and writing straight to the database. Default is `15000`. |
| Messaging | Turns cross-server messaging on or off. Enabled by default. |
| Messaging Channel | The channel servers talk over, placed after the key prefix. Default is `messages`. |
| Reconnect Delay | How long, in seconds, to wait before retrying a lost messaging connection. Default is `5`, and the minimum is `1`. |
| Debug | Logs every cache and lock operation to the console. Disabled by default. |

```yaml
lock:
  heartbeat-interval-seconds: 5
  ttl-seconds: 15
  poll-interval-ms: 200
  max-wait-ms: 15000
```

> **Info:**
The two lock timing options are not written into `redis.yml` when the file is first created. Add them yourself if you need to change them from the defaults above.

> **Info:**
If the connection to Redis fails on startup, the add-on logs a warning and the plugin keeps running on the database alone, without cross-server messaging.

Source: https://www.phoenixplugins.com/docs/phoenix-crates/add-ons/redis