- Persistent: to set up fast persistent storage for your application
- Ephemeral: to set up a non-persistent cache for your application
Supported versions
You can select the major and minor version. Patch versions are applied periodically for bug fixes and the like. When you deploy your app, you always get the latest available patches.Service types
Depending on your needs, you can set up Valkey as persistent or ephemeral.Relationship reference
For each service defined via a relationship to your application, Upsun 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 themselves,
or through the PLATFORM_RELATIONSHIPS environment variable.
- Service environment variables
- `PLATFORM_RELATIONSHIPS` environment variable
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 directly rather than hard coding any values.Database access
It should be noted that when you set up a relationship connection, access to all of the databases is automatically granted.Persistent Valkey
By default, Valkey 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 Valkey service as persistent. Persistent Valkey stores data on a disk, restoring it if the container restarts. To switch from persistent to ephemeral Valkey, set up a new service with a different name.Usage example
1. Configure the service
To define the service, use thevalkey-persistent endpoint:
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 thevalkey endpoint :
- Using default endpoints
- Using explicit endpoints
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 configuration for relationships. That is, it uses default endpoints behind the scenes, providing a relationship (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.With the above definition, the application container now has access to the service via the relationship SERVICE_NAME and its corresponding service environment variables.Configuration example
Ephemeral Valkey
By default, Valkey is an ephemeral service that serves as a non-persistent cache. Ephemeral Valkey 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. Make sure your app doesn’t rely on ephemeral Vedis for persistent storage as it can cause issues. For example, if a container is moved during region maintenance,thedeploy 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 Valkey configuration.
Persistent Valkey provides a cache with persistent storage.
Usage example
1. Configure the service
To define the service, use thevalkey endpoint:
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 thevalkey endpoint :
- Using default endpoints
- Using explicit endpoints
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 configuration for relationships. That is, it uses default endpoints behind the scenes, providing a relationship (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.With the above definition, the application container now has access to the service via the relationship SERVICE_NAME and its corresponding service environment variables.Configuration example
Use in app
To use the configured service in your app, add a configuration file similar to the following to your project. This configuration defines a single application (myapp), whose source code exists in the <PROJECT_ROOT>/myapp directory.myapp has access to the valkey service, via a relationship whose name is identical to the service name
(as per default endpoint configuration for relationships).
From this, myapp can retrieve access credentials to the service through the relationship environment variables.
myapp/.environment
.environment in the myapp directory — is automatically sourced by Upsun 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 service environment variables like VALKEY_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.
Eviction policy
When Valkey reaches its memory limit, it triggers a cache cleanup. To customize those cache cleanups, set up an eviction policy such as the following: 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. |
Access your Valkey service
After you’ve configured your Valkey service, you can access it using either the Upsun CLI or through the Valkey CLI.Upsun CLI
Unlike the Valkey CLI, connecting via the Upsun CLI does not require additional authentication steps if you are already authenticated in your terminal. Access your Valkey service by running the command:Valkey CLI
Retrieve the hostname and port you can connect to through thePLATFORM_RELATIONSHIPS environment variable.
To do so, run the upsun relationships command.
After you’ve retrieved the hostname and port, open an SSH session.
To access your Valkey service, run the following command:
To get the current configuration, run the following command:
Use Valkey 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 Valkey as a session handler, which means Valkey stores and retrieves the data saved into sessions. To set up Valkey as your session handler, add a configuration similar to the following:Migrate from Redis to Valkey
It is possible for a user to switch fromredis-persistent to valkey-persistent without losing data. To make this switch, change the type of the service from redis-persistent to valkey-persistent (also note the version change), while keeping the same service name. For example, replace this:
with the following:
Exporting Data Persistent
This section applies only to persistent Valkey services configured withtype: valkey-persistent.
Ephemeral Valkey (type: valkey) stores data only in memory and does not need to be exported.
The procedure is identical to Redis (persistent), using the same BGSAVE / RDB approach:
- Open an SSH tunnel:
Terminal
- Trigger a background save:
Terminal
- Download the
dump.rdbfile from the container as described in the Redis section.