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

# Redis (Object cache)

[Redis](https://redis.io/documentation) is a multi-model database that allows you to store data in memory
for high-performance data retrieval and key-value storage.
Upsun Cloud supports two different Redis configurations:

* [Persistent](#persistent-redis): to set up fast persistent storage for your application
* [Ephemeral](#ephemeral-redis): to set up a non-persistent cache for your application

## Active versions

See [Image statuses](https://developer.upsun.com/docs/core-concepts/upgrade-runtimes-services.md#image-statuses) for what Active means.

You can select the major and minor version.

Patch versions are applied periodically for bug fixes and other maintenance updates. By default, you get the latest available patches each time you deploy. If your project has a [maintenance window](https://developer.upsun.com/docs/core-concepts/build-deploy.md#maintenance-window) configured, Upsun Cloud applies patches only during that window. Upsun Cloud may still apply urgent or critical fixes outside of the maintenance window.

* 8.8
* 8.0
* 7.2

## Sunset versions

The following versions are still available in your projects, including code pushes, but new projects can't use them.
These versions are decommissioned 180 calendar days after entering Sunset — the exact decommission date is shown on your project's Overview page and on the relevant activity in the **Activity** panel. See [Image statuses](https://developer.upsun.com/docs/core-concepts/upgrade-runtimes-services.md#image-statuses) for what each status means.

* 7.0
* 6.2
* 6.0
* 5.0
* 4.0
* 3.2
* 3.0
* 2.8

## Decommissioned versions

The following versions have been decommissioned and are no longer available. See [Image statuses](https://developer.upsun.com/docs/core-concepts/upgrade-runtimes-services.md#image-statuses) for what each status means.
If your project uses a decommissioned version, you must update to an [active version](#active-versions).

*No decommissioned versions.*

Note that versions 3.0 and higher support up to 64 different databases per instance of the service,
while Redis 2.8 only supports a single database.

## Service types

Depending on your needs,
you can set up Redis as [persistent](#persistent-redis) or [ephemeral](#ephemeral-redis).

## Relationship reference

For each service [defined via a relationship](#usage-example) to your application,
Upsun Cloud automatically generates corresponding environment variables within your application container,
in the `$<RELATIONSHIP-NAME>_<SERVICE-PROPERTY>` format.

Here is example information available through the [service environment variables](https://developer.upsun.com/docs/development/variables#service-environment-variables) themselves,
or through the [`PLATFORM_RELATIONSHIPS` environment variable](https://developer.upsun.com/docs/development/variables/use-variables#use-provided-variables).

> [tab] Service environment variables
>
> You can obtain the complete list of available service environment variables in your app container by running `upsun ssh env`.
>
> Note that the information about the relationship can change when an app is redeployed or restarted or the relationship is changed. So your apps should only rely on the [service environment variables](https://developer.upsun.com/docs/development/variables#service-environment-variables) directly rather than hard coding any values.
>
> &#x20;       REDIS\_USERNAME=
> &#x20;       REDIS\_SCHEME=redis
> &#x20;       REDIS\_SERVICE=redis
> &#x20;       REDIS\_FRAGMENT=
> &#x20;       REDIS\_IP=123.456.78.90
> &#x20;       REDIS\_EPOCH=0
> &#x20;       REDIS\_HOSTNAME=azertyuiopqsdfghjklm.redis.service.\_.eu-1.platformsh.site
> &#x20;       REDIS\_PORT=6379
> &#x20;       REDIS\_CLUSTER=azertyuiopqsdf-main-afdwftq
> &#x20;       REDIS\_HOST=rediscache.internal
> &#x20;       REDIS\_REL=redis
> &#x20;       REDIS\_PATH=
> &#x20;       REDIS\_QUERY=\{}
> &#x20;       REDIS\_PASSWORD=
> &#x20;       REDIS\_TYPE=redis:\{\{version:redis:latest}}
> &#x20;       REDIS\_PUBLIC=false
> &#x20;       REDIS\_HOST\_MAPPED=false
> &#x20;    &#x20;

> [tab] `PLATFORM_RELATIONSHIPS` environment variable
>
> For some advanced use cases, you can use the [`PLATFORM_RELATIONSHIPS` environment variable](https://developer.upsun.com/docs/development/variables/use-variables#use-provided-variables).
> The structure of the `PLATFORM_RELATIONSHIPS` environment variable can be obtained by running `upsun relationships` in your terminal:
>
> &#x20;       \{
> &#x20;         "username": null,
> &#x20;         "scheme": "redis",
> &#x20;         "service": "redis",
> &#x20;         "fragment": null,
> &#x20;         "ip": "123.456.78.90",
> &#x20;         "hostname": "azertyuiopqsdfghjklm.redis.service.\_.eu-1.platformsh.site",
> &#x20;         "port": 6379,
> &#x20;         "cluster": "azertyuiopqsdf-main-7rqtwti",
> &#x20;         "host": "redis.internal",
> &#x20;         "rel": "redis",
> &#x20;         "path": null,
> &#x20;         "query": \[],
> &#x20;         "password": null,
> &#x20;         "type": "redis:\{\{version:redis:latest}}",
> &#x20;         "public": false,
> &#x20;         "host\_mapped": false
> &#x20;       }
> &#x20;    &#x20;
>
> Here is an example of how to gather [`PLATFORM_RELATIONSHIPS` environment variable](https://developer.upsun.com/docs/development/variables/use-variables#use-provided-variables) information in a [`.environment` file](https://developer.upsun.com/docs/development/variables/set-variables#when-to-use-env-files):
>
> ```bash .environment theme={null}
> # Decode the built-in credentials object variable.
> export RELATIONSHIPS_JSON="$(echo "$PLATFORM_RELATIONSHIPS" | base64 --decode)"
>
> # Set environment variables for individual credentials.
> export APP_REDIS_HOST="$(echo "$RELATIONSHIPS_JSON" | jq -r '.redis[0].host')"
> ```

The format of the relationship is identical whether your Redis service is [ephemeral](#ephemeral-redis) or [persistent](#persistent-redis).

## Persistent Redis

By default, Redis is an ephemeral service that stores data in memory.
This allows for fast data retrieval,
but also means data can be lost when a container is moved or shut down.

To solve this issue, configure your Redis service as persistent.
Persistent Redis stores data on a disk,
restoring it if the container restarts.

To switch from persistent to ephemeral Redis,
set up a new service with a different name.

> [!WARNING]
>
> Warning
> Upsun Cloud sets the maximum amount of memory (`maxmemory`) Redis can use for the data set,
> and it cannot be amended.
> It is defined by comparing the following values and keeping the lower of the two:
>
> * Disk size / 2
> * Container memory × 0.75
>
> For example:
>
> * 1024 MB disk, 1024 MB memory → `maxmemory` = 512 MB
>   *(disk/2 = 512, lower than 1024 × 0.75 = 768)*
> * 6144 MB disk, 1024 MB memory → `maxmemory` = 768 MB
>   *(memory × 0.75 = 768, lower than 6144/2 = 3072)*

### Usage example

#### 1. Configure the service

To define the service, use the `redis-persistent` endpoint:


&#x20;   services:
&#x20;     \# The name of the service container. Must be unique within a project.
&#x20;     \<SERVICE\_NAME>:
&#x20;       type: redis-persistent:\<VERSION>
&#x20;&#x20;

Note that changing the name of the service replaces it with a brand new service and all existing data is lost.
Back up your data before changing the service.

#### 2. Define the relationship

To define the relationship, use the `redis` endpoint :

> [tab] Using default endpoints
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         \<APP\_NAME>:
> &#x20;           \# Relationships enable access from this app to a given service.
> &#x20;           \# The example below shows simplified configuration leveraging a default service
> &#x20;           \# (identified from the relationship name) and a default endpoint.
> &#x20;           \# See the Application reference for all options for defining relationships and endpoints.
> &#x20;           relationships:
> &#x20;             \<SERVICE\_NAME>:
> &#x20;    &#x20;
>
> You can define `SERVICE_NAME` as you like, so long as it’s unique between all defined services and matches in both the application and services configuration.
>
> The example above leverages [default endpoint](https://developer.upsun.com/docs/configure-apps/image-properties/relationships.md) configuration for relationships. That is, it uses default endpoints behind the scenes, providing a [relationship](https://developer.upsun.com/docs/configure-apps/image-properties/relationships.md) (the network address a service is accessible from) that is identical to the name of that service.
>
> Depending on your needs, instead of default endpoint configuration, you can use [explicit endpoint configuration](https://developer.upsun.com/docs/configure-apps/image-properties/relationships.md).
>
> With the above definition, the application container now has [access to the service](#use-in-app) via the relationship `SERVICE_NAME` and its corresponding [service environment variables](https://developer.upsun.com/docs/development/variables#service-environment-variables).

> [tab] Using explicit endpoints
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         \<APP\_NAME>:
> &#x20;           \# Relationships enable access from this app to a given service.
> &#x20;           \# The example below shows configuration with an explicitly set service name and endpoint.
> &#x20;           \# See the Application reference for all options for defining relationships and endpoints.
> &#x20;           relationships:
> &#x20;             \<RELATIONSHIP\_NAME>:
> &#x20;               service: \<SERVICE\_NAME>
> &#x20;               endpoint: redis
> &#x20;    &#x20;
>
> You can define `SERVICE_NAME` and `<RELATIONSHIP_NAME>` as you like, so long as it's unique between all defined services and relationships
> and matches in both the application and services configuration.
>
> The example above leverages [explicit endpoint](https://developer.upsun.com/docs/configure-apps/image-properties/relationships.md) configuration for relationships.
>
> Depending on your needs, instead of explicit endpoint configuration,
> you can use [default endpoint configuration](https://developer.upsun.com/docs/configure-apps/image-properties/relationships.md).
>
> With the above definition, the application container now has [access to the service](#use-in-app) via the relationship `<RELATIONSHIP_NAME>` and its corresponding [service environment variables](https://developer.upsun.com/docs/development/variables#service-environment-variables).

For PHP, enable the [extension](https://developer.upsun.com/docs/languages/php/extensions.md) for the service:

> [tab] Using default endpoints
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         \<APP\_NAME>:
> &#x20;           \# PHP extensions.
> &#x20;           runtime:
> &#x20;             extensions:
> &#x20;               \- redis
> &#x20;           \# Relationships enable access from this app to a given service.
> &#x20;           \# The example below shows simplified configuration leveraging a default service
> &#x20;           \# (identified from the relationship name) and a default endpoint.
> &#x20;           \# See the Application reference for all options for defining relationships and endpoints.
> &#x20;           relationships:
> &#x20;             \<SERVICE\_NAME>:
> &#x20;    &#x20;

> [tab] Using explicit endpoints
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         \<APP\_NAME>:
> &#x20;           \# PHP extensions.
> &#x20;           runtime:
> &#x20;             extensions:
> &#x20;                 \- redis
> &#x20;           \# Relationships enable access from this app to a given service.
> &#x20;           \# The example below shows configuration with an explicitly set service name and endpoint.
> &#x20;           \# See the Application reference for all options for defining relationships and endpoints.
> &#x20;           relationships:
> &#x20;             \<RELATIONSHIP\_NAME>:
> &#x20;               service: \<SERVICE\_NAME>
> &#x20;               endpoint: redis
> &#x20;    &#x20;

### Configuration example

> [tab] Using default endpoints
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         \<APP\_NAME>:
> &#x20;           source:
> &#x20;             root: "myapp"
>
> &#x20;           \[...]
>
> &#x20;           \# PHP extensions.
> &#x20;           runtime:
> &#x20;             extensions:
> &#x20;               \- redis
>
> &#x20;           \# Relationships enable access from this app to a given service.
> &#x20;           \# The example below shows simplified configuration leveraging a default service
> &#x20;           \# (identified from the relationship name) and a default endpoint.
> &#x20;           \# See the Application reference for all options for defining relationships and endpoints.
> &#x20;           relationships:
> &#x20;             redis:
> &#x20;       services:
> &#x20;           \# The name of the service container. Must be unique within a project.
> &#x20;           redis:
> &#x20;             type: redis-persistent:\{\{version:redis:latest}}

> [tab] Using explicit endpoints
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         \<APP\_NAME>:
> &#x20;           source:
> &#x20;             root: "myapp"
>
> &#x20;           \[...]
>
> &#x20;           \# PHP extensions.
> &#x20;           runtime:
> &#x20;             extensions:
> &#x20;               \- redis
>
> &#x20;           \# Relationships enable access from this app to a given service.
> &#x20;           \# The example below shows configuration with an explicitly set service name and endpoint.
> &#x20;           \# See the Application reference for all options for defining relationships and endpoints.
> &#x20;           relationships:
> &#x20;             redis:
> &#x20;               service: redis
> &#x20;               endpoint: redis
> &#x20;       services:
> &#x20;         \# The name of the service container. Must be unique within a project.
> &#x20;         redis:
> &#x20;           type: redis-persistent:\{\{version:redis:latest}}

### Use in app

To use the configured service in your app, add a configuration file similar to the following to your project.

> [tab] Using default endpoints
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         \<APP\_NAME>:
> &#x20;           source:
> &#x20;             root: "myapp"
>
> &#x20;           \# PHP extensions.
> &#x20;           runtime:
> &#x20;             extensions:
> &#x20;               \- redis
>
> &#x20;           \[...]
>
> &#x20;           \# Relationships enable access from this app to a given service.
> &#x20;           \# The example below shows simplified configuration leveraging a default service
> &#x20;           \# (identified from the relationship name) and a default endpoint.
> &#x20;           \# See the Application reference for all options for defining relationships and endpoints.
> &#x20;           relationships:
> &#x20;             redis:
> &#x20;       services:
> &#x20;         \# The name of the service container. Must be unique within a project.
> &#x20;         redis:
> &#x20;           type: redis-persistent:\{\{version:redis:latest}}

> [tab] Using explicit endpoints
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         \<APP\_NAME>:
> &#x20;           source:
> &#x20;             root: "myapp"
>
> &#x20;           \# PHP extensions.
> &#x20;           runtime:
> &#x20;             extensions:
> &#x20;               \- redis
>
> &#x20;           \[...]
>
> &#x20;           \# Relationships enable access from this app to a given service.
> &#x20;           \# The example below shows configuration with an explicitly set service name and endpoint.
> &#x20;           \# See the Application reference for all options for defining relationships and endpoints.
> &#x20;           relationships:
> &#x20;             redis:
> &#x20;               service: redis
> &#x20;               endpoint: redis
> &#x20;       services:
> &#x20;         \# The name of the service container. Must be unique within a project.
> &#x20;         redis:
> &#x20;           type: redis-persistent:\{\{version:redis:latest}}

This configuration defines a single application (`myapp`), whose source code exists in the `<PROJECT_ROOT>/myapp` directory.\

`myapp` has access to the `redis` service, via a relationship whose name is [identical to the service name](#2-define-the-relationship)
(as per [default endpoint](https://developer.upsun.com/docs/configure-apps/image-properties/relationships.md) configuration for relationships).

From this, `myapp` can retrieve access credentials to the service through the [relationship environment variables](#relationship-reference).

```bash myapp/.environment theme={null}
# Set environment variables for individual credentials.
# For more information, please visit /docs/development/variables#service-environment-variables.
export CACHE_HOST="${REDIS_HOST}"
export CACHE_PORT="${REDIS_PORT}"
export CACHE_PASSWORD="${REDIS_PASSWORD}"
export CACHE_SCHEME="${REDIS_SCHEME}"

# Surface a Redis connection string for use in app.
export CACHE_URL="${CACHE_SCHEME}://${CACHE_PASSWORD}@${CACHE_HOST}:${CACHE_PORT}"
```

The above file — `.environment` in the `myapp` directory — is automatically sourced by Upsun Cloud into the runtime environment, so that the variable `CACHE_URL` can be used within the application to connect to the service.

Note that `CACHE_URL`, and all Upsun Cloud [service environment variables](https://developer.upsun.com/docs/development/variables#service-environment-variables) like `REDIS_HOST`,
are environment-dependent.
Unlike the build produced for a given commit,
they can’t be reused across environments and only allow your app to connect to a single service instance on a single environment.

A file very similar to this is generated automatically for your when using the `upsun ify` command to [migrate a codebase to Upsun Cloud](https://developer.upsun.com/docs/get-started.md).

## Ephemeral Redis

By default, Redis is an ephemeral service that serves as a non-persistent cache.
Ephemeral Redis stores data only in memory and requires no disk space.
When the service reaches its memory limit, it triggers a cache cleanup.
To customize those cache cleanups, set up an [eviction policy](#eviction-policy).

Make sure your app doesn't rely on ephemeral Redis for persistent storage as it can cause issues.
For example, if a container is moved during region maintenance,
the `deploy` and `post_deploy` hooks don't run and an app that treats the cache as permanent shows errors.

To prevent data from getting lost when a container is moved or shut down,
you can use the [persistent Redis](#persistent-redis) configuration.
Persistent Redis provides a cache with persistent storage.

### Usage example

#### 1. Configure the service

To define the service, use the `redis` endpoint:


&#x20;   services:
&#x20;     \# The name of the service container. Must be unique within a project.
&#x20;     \<SERVICE\_NAME>:
&#x20;       type: redis:\<VERSION>
&#x20;&#x20;

Note that changing the name of the service replaces it with a brand new service and all existing data is lost.
Back up your data before changing the service.

#### 2. Define the relationship

To define the relationship, use the `redis` endpoint :

> [tab] Using default endpoints
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         \<APP\_NAME>:
> &#x20;           \# Relationships enable access from this app to a given service.
> &#x20;           \# The example below shows simplified configuration leveraging a default service
> &#x20;           \# (identified from the relationship name) and a default endpoint.
> &#x20;           \# See the Application reference for all options for defining relationships and endpoints.
> &#x20;           relationships:
> &#x20;             \<SERVICE\_NAME>:
> &#x20;    &#x20;
>
> You can define `SERVICE_NAME` as you like, so long as it’s unique between all defined services and matches in both the application and services configuration.
>
> The example above leverages [default endpoint](https://developer.upsun.com/docs/configure-apps/image-properties/relationships.md) configuration for relationships. That is, it uses default endpoints behind the scenes, providing a [relationship](https://developer.upsun.com/docs/configure-apps/image-properties/relationships.md) (the network address a service is accessible from) that is identical to the name of that service.
>
> Depending on your needs, instead of default endpoint configuration, you can use [explicit endpoint configuration](https://developer.upsun.com/docs/configure-apps/image-properties/relationships.md).
>
> With the above definition, the application container now has [access to the service](#use-in-app) via the relationship `SERVICE_NAME` and its corresponding [service environment variables](https://developer.upsun.com/docs/development/variables#service-environment-variables).

> [tab] Using explicit endpoints
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         \<APP\_NAME>:
> &#x20;           \# Relationships enable access from this app to a given service.
> &#x20;           \# The example below shows configuration with an explicitly set service name and endpoint.
> &#x20;           \# See the Application reference for all options for defining relationships and endpoints.
> &#x20;           relationships:
> &#x20;             \<RELATIONSHIP\_NAME>:
> &#x20;               service: \<SERVICE\_NAME>
> &#x20;               endpoint: redis
> &#x20;    &#x20;
>
> You can define `SERVICE_NAME` and `<RELATIONSHIP_NAME>` as you like, so long as it's unique between all defined services and relationships
> and matches in both the application and services configuration.
>
> The example above leverages [explicit endpoint](https://developer.upsun.com/docs/configure-apps/image-properties/relationships.md) configuration for relationships.
>
> Depending on your needs, instead of explicit endpoint configuration,
> you can use [default endpoint configuration](https://developer.upsun.com/docs/configure-apps/image-properties/relationships.md).
>
> With the above definition, the application container now has [access to the service](#use-in-app) via the relationship `<RELATIONSHIP_NAME>` and its corresponding [service environment variables](https://developer.upsun.com/docs/development/variables#service-environment-variables).

For PHP, enable the [extension](https://developer.upsun.com/docs/languages/php/extensions.md) for the service:

> [tab] Using default endpoints
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         \<APP\_NAME>:
> &#x20;           source:
> &#x20;             root: "myapp"
>
> &#x20;           \[...]
>
> &#x20;           \# PHP extensions.
> &#x20;           runtime:
> &#x20;             extensions:
> &#x20;               \- redis
>
> &#x20;            \# Relationships enable access from this app to a given service.
> &#x20;           \# The example below shows simplified configuration leveraging a default service
> &#x20;           \# (identified from the relationship name) and a default endpoint.
> &#x20;           \# See the Application reference for all options for defining relationships and endpoints.
> &#x20;           relationships:
> &#x20;             \<SERVICE\_NAME>:
> &#x20;       services:
> &#x20;         \# The name of the service container. Must be unique within a project.
> &#x20;         \<SERVICE\_NAME>:
> &#x20;           type: redis:\<VERSION>
> &#x20;    &#x20;

> [tab] Using explicit endpoints
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         \<APP\_NAME>:
> &#x20;           source:
> &#x20;             root: "myapp"
>
> &#x20;           \[...]
>
> &#x20;           \# PHP extensions.
> &#x20;           runtime:
> &#x20;             extensions:
> &#x20;               \- redis
>
> &#x20;           \# Relationships enable access from this app to a given service.
> &#x20;           \# The example below shows configuration with an explicitly set service name and endpoint.
> &#x20;           \# See the Application reference for all options for defining relationships and endpoints.
> &#x20;           relationships:
> &#x20;             \<RELATIONSHIP\_NAME>:
> &#x20;               service: \<SERVICE\_NAME>
> &#x20;               endpoint: redis
> &#x20;       services:
> &#x20;         \# The name of the service container. Must be unique within a project.
> &#x20;         \<SERVICE\_NAME>:
> &#x20;           type: redis:\<VERSION>
> &#x20;    &#x20;

### Configuration example

> [tab] Using default endpoints
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         myapp:
> &#x20;           source:
> &#x20;             root: "myapp"
>
> &#x20;           \[...]
>
> &#x20;           \# PHP extensions.
> &#x20;           runtime:
> &#x20;             extensions:
> &#x20;               \- redis
>
> &#x20;            \# Relationships enable access from this app to a given service.
> &#x20;           \# The example below shows simplified configuration leveraging a default service
> &#x20;           \# (identified from the relationship name) and a default endpoint.
> &#x20;           \# See the Application reference for all options for defining relationships and endpoints.
> &#x20;           relationships:
> &#x20;             redis:
> &#x20;       services:
> &#x20;         \# The name of the service container. Must be unique within a project.
> &#x20;         redis:
> &#x20;           type: redis:\{\{version:redis:latest}}

> [tab] Using explicit endpoints
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         myapp:
> &#x20;           source:
> &#x20;             root: "myapp"
>
> &#x20;           \[...]
>
> &#x20;           \# PHP extensions.
> &#x20;           runtime:
> &#x20;             extensions:
> &#x20;               \- redis
>
> &#x20;           \# Relationships enable access from this app to a given service.
> &#x20;           \# The example below shows configuration with an explicitly set service name and endpoint.
> &#x20;           \# See the Application reference for all options for defining relationships and endpoints.
> &#x20;           relationships:
> &#x20;             redis:
> &#x20;               service: redis
> &#x20;               endpoint: redis
> &#x20;       services:
> &#x20;         \# The name of the service container. Must be unique within a project.
> &#x20;         redis:
> &#x20;           type: redis:\{\{version:redis:latest}}

### Use in app

To use the configured service in your app, add a configuration file similar to the following to your project.

> [tab] Using default endpoints
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         myapp:
> &#x20;           source:
> &#x20;             root: "myapp"
>
> &#x20;           \[...]
>
> &#x20;           \# PHP extensions.
> &#x20;           runtime:
> &#x20;             extensions:
> &#x20;               \- redis
> &#x20;           \# Relationships enable access from this app to a given service.
> &#x20;           \# The example below shows simplified configuration leveraging a default service
> &#x20;           \# (identified from the relationship name) and a default endpoint.
> &#x20;           \# See the Application reference for all options for defining relationships and endpoints.
> &#x20;           relationships:
> &#x20;             redis:
> &#x20;       services:
> &#x20;         \# The name of the service container. Must be unique within a project.
> &#x20;         redis:
> &#x20;           type: redis:\{\{version:redis:latest}}

> [tab] Using explicit endpoints
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         myapp:
> &#x20;           source:
> &#x20;             root: "myapp"
>
> &#x20;           \[...]
>
> &#x20;           \# PHP extensions.
> &#x20;           runtime:
> &#x20;             extensions:
> &#x20;               \- redis
> &#x20;           \# Relationships enable access from this app to a given service.
> &#x20;           \# The example below shows configuration with an explicitly set service name and endpoint.
> &#x20;           \# See the Application reference for all options for defining relationships and endpoints.
> &#x20;           relationships:
> &#x20;             redis:
> &#x20;               service: redis
> &#x20;               endpoint: redis
> &#x20;       services:
> &#x20;         \# The name of the service container. Must be unique within a project.
> &#x20;         redis:
> &#x20;           type: redis:\{\{version:redis:latest}}

This configuration defines a single application (`myapp`), whose source code exists in the `<PROJECT_ROOT>/myapp` directory.\

`myapp` has access to the `redis` service, via a relationship whose name is [identical to the service name](#2-define-the-relationship)
(as per [default endpoint](https://developer.upsun.com/docs/configure-apps/image-properties/relationships.md) configuration for relationships).

From this, `myapp` can retrieve access credentials to the service through the [relationship environment variables](#relationship-reference).

```bash myapp/.environment theme={null}
# Set environment variables for individual credentials.
# For more information, please visit /docs/development/variables#service-environment-variables.
export CACHE_HOST="${REDIS_HOST}"
export CACHE_PORT="${REDIS_PORT}"
export CACHE_PASSWORD="${REDIS_PASSWORD}"
export CACHE_SCHEME="${REDIS_SCHEME}"

# Surface a Redis connection string for use in app.
export CACHE_URL="${CACHE_SCHEME}://${CACHE_PASSWORD}@${CACHE_HOST}:${CACHE_PORT}"
```

The above file — `.environment` in the `myapp` directory — is automatically sourced by Upsun Cloud into the runtime environment, so that the variable `CACHE_URL` can be used within the application to connect to the service.

Note that `CACHE_URL`, and all Upsun Cloud [service environment variables](https://developer.upsun.com/docs/development/variables#service-environment-variables) like `REDIS_HOST`,
are environment-dependent.
Unlike the build produced for a given commit,
they can’t be reused across environments and only allow your app to connect to a single service instance on a single environment.

A file very similar to this is generated automatically for your when using the `upsun ify` command to [migrate a codebase to Upsun Cloud](https://developer.upsun.com/docs/get-started.md).

## Multiple databases

Redis 3.0 and above support up to 64 databases.
But you can't set up different access rights to each database.
When you set up a relationship connection,
access to all of the databases is automatically granted.

The way to access a particular database depends on the [client library](https://redis.io/clients) you're using:

> [tab] PHP
>
> Use the Redis [`select` command](https://redis.io/commands/select):
>
> ```php theme={null}
> <?php
> $redis = new Redis();
> $redis->connect(getenv('REDIS_HOST'), getenv('REDIS_PORT'));
>
> $redis->select(0);       // switch to DB 0
> $redis->set('x', '42');  // write 42 to x
> $redis->move('x', 1);    // move to DB 1
> $redis->select(1);       // switch to DB 1
> $redis->get('x');        // returns 42
> ```

> [tab] Python
>
> To manage [thread safety](https://github.com/redis/redis-py/blob/master/docs/advanced_features.rst#user-content-a-note-about-threading),
> the Python library suggests using separate client instances for each database:
>
> ```python theme={null}
> import os
> from redis import Redis
>
> database0 = Redis(host=os.getenv('REDIS_HOST'), port=os.getenv('REDIS_PORT'), db=0)
> database1 = Redis(host=os.getenv('REDIS_HOST'), port=os.getenv('REDIS_PORT'), db=1)
> ```

> [tab] Node.js
>
> Use the Redis [`select` command](https://redis.io/commands/select):
>
> ```javascript theme={null}
> const redis = require('redis');
>
> const client = redis.createClient(process.env.REDIS_PORT, process.env.REDIS_HOST);
>
> await client.SELECT(0);                  // switch to DB 0
> await client.set('x', '42');             // write 42 to x
> await client.MOVE('x', 1);               // move to DB 1
> await client.SELECT(1);                  // switch to DB 1
> const value = await client.get('x');     // returns 42
> ```

## Eviction policy

When Redis reaches its memory limit,
it triggers a cache cleanup.
To customize those cache cleanups, set up an eviction policy such as the following:


&#x20;   services:
&#x20;     \# The name of the service container. Must be unique within a project.
&#x20;     redis:
&#x20;       type: "redis:\{\{version:redis:latest}}"
&#x20;       configuration:
&#x20;         maxmemory\_policy: allkeys-lfu

The following table presents the possible values:

| Value | Policy description |
| - | - |
| `allkeys-lru` | Removes the oldest cache items first. This is the default policy when `maxmemory_policy` isn't set. |
| `noeviction` | New items aren’t saved when the memory limit is reached. |
| `allkeys-lfu` | Removes least frequently used cache items first. |
| `volatile-lru` | Removes least recently used cache items with the `expire` field set to `true`. |
| `volatile-lfu` | Removes least frequently used cache items with the `expire` field set to `true`. |
| `allkeys-random` | Randomly removes cache items to make room for new data. |
| `volatile-random` | Randomly removes cache items with the `expire` field set to `true`. |
| `volatile-ttl` | Removes cache items with the `expire` field set to `true` and the shortest remaining `time-to -live` value. |

For more information on the different policies,
see the official [Redis documentation](https://redis.io/docs/latest/operate/rs/databases/memory-performance/eviction-policy/).

## Access your Redis service

After you've [configured your Redis service](#usage-example),
you can access it using either the Upsun Cloud CLI
or through the [Redis CLI](https://redis.io/docs/latest/operate/rs/references/cli-utilities/).

### Upsun Cloud CLI

Unlike the Redis CLI, connecting via the Upsun Cloud CLI does not require additional authentication steps if you are already authenticated in your terminal.

Access your Redis service by running the command:

```bash theme={null}
upsun redis
```

### Redis CLI

Retrieve the hostname and port you can connect to
through the `PLATFORM_RELATIONSHIPS` [environment variable](https://developer.upsun.com/docs/development/variables/use-variables.md#use-provided-variables).
To do so, run the `upsun relationships` command.

After you've retrieved the hostname and port, [open an SSH session](https://developer.upsun.com/docs/development/ssh).
To access your Redis service, run the following command:


&#x20; redis-cli -h \<HOSTNAME> -p \<PORT>
&#x20;&#x20;

If you have a Grid project, note that the `CONFIG GET` and `CONFIG SET` admin commands are restricted.
To get the current configuration, run the following command:


&#x20; redis-cli -h \<HOSTNAME> -p \<PORT> info
&#x20;&#x20;

## Use Redis as a handler for PHP sessions

A PHP session allows you to store different data for each user through a unique session ID.
By default, PHP handles sessions using files.
But you can use Redis as a session handler,
which means Redis stores and retrieves the data saved into sessions.

To set up Redis as your session handler, add a configuration similar to the following:

> [tab] Using default endpoints
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         myapp:
> &#x20;           source:
> &#x20;             root: "myapp"
>
> &#x20;           type: "php:\{\{version:php:latest}}"
>
> &#x20;           \# PHP extensions.
> &#x20;           runtime:
> &#x20;             extensions:
> &#x20;               \- redis
>
> &#x20;           relationships:
> &#x20;             redissession:
>
> &#x20;           variables:
> &#x20;             php:
> &#x20;               session.save\_handler: redis
> &#x20;               session.save\_path: "tcp\://\$\{REDISSESSION\_HOST}:\$\{SESSIONSTORAGE\_PORT}"
>
> &#x20;           web:
> &#x20;             locations:
> &#x20;               '/':
> &#x20;                 root: 'web'
> &#x20;                 passthru: '/index.php'
>
> &#x20;       services:
> &#x20;         \# The name of the service container. Must be unique within a project.
> &#x20;         redissession:
> &#x20;           type: "redis-persistent:\{\{version:redis:latest}}"

> [tab] Using explicit endpoints
>
> &#x20;       applications:
> &#x20;         \# The name of the app container. Must be unique within a project.
> &#x20;         myapp:
> &#x20;           source:
> &#x20;             root: "myapp"
>
> &#x20;           type: "php:\{\{version:php:latest}}"
>
> &#x20;           \# PHP extensions.
> &#x20;           runtime:
> &#x20;             extensions:
> &#x20;               \- redis
>
> &#x20;           \# Relationships enable access from this app to a given service.
> &#x20;           \# The example below shows configuration with an explicitly set service name and endpoint.
> &#x20;           \# See the Application reference for all options for defining relationships and endpoints.
> &#x20;           relationships:
> &#x20;             redissession:
> &#x20;               service: redissession
> &#x20;               endpoint: redis
>
> &#x20;           variables:
> &#x20;             php:
> &#x20;               session.save\_handler: redis
> &#x20;               session.save\_path: "tcp\://\$\{REDISSSESSION\_HOSTNAME}:\$\{REDISSSESSION\_PORT}>"
>
> &#x20;           web:
> &#x20;             locations:
> &#x20;               '/':
> &#x20;                 root: 'web'
> &#x20;                 passthru: '/index.php'
>
> &#x20;       services:
> &#x20;         \# The name of the service container. Must be unique within a project.
> &#x20;         redissession:
> &#x20;           type: "redis-persistent:\{\{version:redis:latest}}"

## Exporting Data Persistent

This section applies only to **persistent** Redis services configured with `type: redis-persistent`.
Ephemeral Redis (`type: redis`) stores data only in memory; its data does not need to be exported.

The recommended approach is to use the `BGSAVE` command to write a point-in-time RDB snapshot to disk,
then download it.

1. Open an SSH tunnel:

```bash Terminal theme={null}
upsun tunnel:single --relationship <RELATIONSHIP_NAME>
```

2. Trigger a background save and wait for it to complete:

```bash Terminal theme={null}
redis-cli -h 127.0.0.1 -p 30000 BGSAVE
# Poll until "Background saving terminated" appears:
redis-cli -h 127.0.0.1 -p 30000 LASTSAVE
```

3. Download the RDB dump file from the container:

```bash Terminal theme={null}
# Connect via SSH and locate the dump file
upsun ssh -- find / -name "dump.rdb" 2>/dev/null

# Copy it locally
upsun scp remote:/path/to/dump.rdb ./redis-dump.rdb
```

The `.rdb` file can be imported into any Redis or Valkey instance by placing it in the data directory
and restarting the service.


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