![Stackhero for Valkey](https://s3.amazonaws.com/assets.heroku.com/addons.heroku.com/icons/9547/original.png)
This add-on is operated by Stackhero
Valkey on dedicated instances, up-to-date versions and super attractive prices.
Stackhero for Valkey
Last updated February 11, 2025
The Stackhero for Valkey add-on is currently in beta.
Table of Contents
- Provisioning the add-on
- Local setup
- Configure Valkey on Java
- Configure Valkey on Ruby
- Configure Valkey on Python
- Configure Valkey on PHP
- Configure Valkey on Go
- Configure Valkey on Node.js
- Dashboard and web UI (Redis Commander)
- Set eviction policy
- Upgrading your plan
- Removing the add-on
- Support
- Additional resources
Stackhero for Valkey provides a managed Valkey instance running on a fully dedicated instance.
Valkey is an open-source fork of the Redis project before they changed their license. Valkey is a drop-in replacement for Redis 7.2 and previous versions.
With the Stackhero for Valkey add-on, you’ll get:
- A private instance (dedicated VM) for high performances and security
- TLS encryption (aka SSL)
- An automatic backup every 24 hours
- A graphical web UI (Redis Commander)
- One click to update to new Valkey versions
All Redis clients can connect to Stackhero for Valkey, and there’s a Valkey/Redis client library for almost every language out there, including Ruby, Node.js, Java, Python, Clojure, and Erlang.
When you choose a Valkey or Redis client library, opt for one that supports TLS encryption (aka SSL) for the best security. We strongly discourage you to use Valkey without TLS support unless you know exactly what you are doing.
Provisioning the add-on
Stackhero for Valkey can be attached to a Heroku application via the CLI:
A list of all plans available can be found here.
$ heroku addons:create --wait ah-valkey-stackhero --app <your app name>
Creating ah-valkey-stackhero...
Your add-on is being provisioned on Stackhero. It will be available in around 2 minutes.
After you provision Stackhero for Valkey, the STACKHERO_VALKEY_URL_TLS
and STACKHERO_VALKEY_URL_CLEAR
config variables are available in your app’s configuration. They contain the URLs to your Valkey instance as its credentials.
STACKHERO_VALKEY_URL_TLS
is the URL to your Valkey instance with TLS encryption. We recommend you use this URL to connect to your Valkey instance.STACKHERO_VALKEY_URL_CLEAR
is the URL to your Valkey instance with no encryption (clear).
You can see the content of those variables via the heroku config:get
command:
$ heroku config:get STACKHERO_VALKEY_URL_TLS
rediss://user:password@domain:port
After you install Stackhero for Valkey, you must configure your application to fully integrate with the add-on.
Local setup
After you provision the add-on, it’s necessary to locally replicate its config variables so your development environment can operate against the service.
Use the Heroku Local command-line tool to configure, run, and manage process types specified in your app’s Procfile. Heroku Local reads configuration variables from a .env
file. To view all of your app’s config variables, type heroku config
. Use these commands for each value that you want to add to your .env
file:
$ heroku config:get STACKHERO_VALKEY_URL_TLS -s >> .env
$ heroku config:get STACKHERO_VALKEY_URL_CLEAR -s >> .env
Credentials and other sensitive configuration values should not be committed to source-control. In Git, exclude the .env
file with: echo .env >> .gitignore
.
For more information, see Running Apps Locally.
Configure Valkey on Java
You can use the environment variable STACKHERO_VALKEY_URL_TLS
to connect to Valkey.
Here’s an example of a connection using Jedis:
private static Jedis getConnection() throws URISyntaxException {
URI valkeyURI = new URI(System.getenv("STACKHERO_VALKEY_URL_TLS"));
Jedis jedis = new Jedis(valkeyURI);
return jedis;
}
For more information, see the official Jedis repository and the official wiki.
In a multithreaded environment, like a web server, use Jedis Pool:
public static JedisPool getPool() {
URI valkeyURI = new URI(System.getenv("STACKHERO_VALKEY_URL_TLS"));
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(10);
poolConfig.setMaxIdle(5);
poolConfig.setMinIdle(1);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true);
poolConfig.setTestWhileIdle(true);
JedisPool pool = new JedisPool(poolConfig, valkeyURI);
return pool;
}
For more information, see the official Jedis wiki.
Configure Valkey on Ruby
Install the Redis gem:
$ bundle add redis
With Rails, you have to create the initializer file config/initializers/redis.rb
like this:
$redis = Redis.new(url: ENV["STACKHERO_VALKEY_URL_TLS"])
To use Valkey as a cache system, edit the config/environments/production.rb
file and add this line:
config.cache_store = :redis_cache_store, { url: ENV['STACKHERO_VALKEY_URL_TLS'] }
By default caching is only enabled on the production environment.
To test caching on development, edit the file config/environments/development.rb
, add the configuration from above, and add config.action_controller.perform_caching = true
to enable caching.
A good way to test that caching works is to start a Rails console with bin/rails console
, then test writing with Rails.cache.write("foo", "bar")
.
Configure Valkey on Sidekiq
To use Stackhero for Valkey as your Sidekiq Valkey server, set the environment variable REDIS_PROVIDER
to STACKHERO_VALKEY_URL_TLS
:
$ heroku config:set REDIS_PROVIDER=STACKHERO_VALKEY_URL_TLS
Sidekiq will automatically use Stackero for Valkey then.
Configure Valkey on Resque
Edit the file config/resque.yml
and replace production: <%= ENV['REDIS_URL'] %>
with this value:
production: <%= ENV['STACKHERO_VALKEY_URL_TLS'] %>
Resque will then use Stackero for Valkey.
Configure Valkey on Python
Install the Redis package:
$ pip install redis
$ pip freeze > requirements.txt
import os
import redis
r = redis.from_url(os.environ.get("STACKHERO_VALKEY_URL_TLS"))
For more information, see the official Python redis package documentation.
How to avoid error “Connection closed by server” with Valkey and Python
The error redis.exceptions.ConnectionError: Connection closed by server
is potentially related to the fact that your Python app doesn’t exchange data with Valkey for a certain time and the connection closes automatically.
When your app tries to exchange again with Valkey, the connection doesn’t work anymore and you get the error Connection closed by server
.
To resolve this, you can pass the health_check_interval
setting to your Valkey connection like this:
r = redis.from_url(
'rediss://default:<password>@XXXXXX.stackhero-network.com:<port>',
health_check_interval=10,
socket_connect_timeout=5,
retry_on_timeout=True,
socket_keepalive=True
)
If you use the Valkey PubSub feature, the redis-py
library assumes that you call functions get_message()
or listen()
more frequently than the health_check_interval
value in seconds.
In this example, health_check_interval
is set to 10 seconds. This means your app has to call get_message()
or listen()
at least one time each 10 seconds. For more information, see the redis-py official documentation.
If this isn’t the case, you’ll get the error Connection closed by server
.
To avoid that, a trick is to call check_health()
regularly.
Here’s an example:
import redis
import threading
# Connection to Valkey
r = redis.from_url(
'rediss://default:<password>@XXXXXX.stackhero-network.com:<port>',
health_check_interval=10,
socket_connect_timeout=5,
retry_on_timeout=True,
socket_keepalive=True
)
# Create a PubSub instance
p = r.pubsub()
# Subscribe to the channel "test"
p.subscribe('test')
# Create a function that will call `check_health` every 5 seconds
def redis_auto_check(p):
t = threading.Timer(5, redis_auto_check, [ p ])
t.start()
p.check_health()
# Call the redis_auto_check function
redis_auto_check(p)
Configure Valkey on PHP
Retrieve the URL with getenv('STACKHERO_VALKEY_URL_TLS')
and pass it to your preferred Valkey client library.
Configure Valkey on Go
First install the go-redis package:
$ go get github.com/go-redis/redis/v8
Then, import it in your code:
import "github.com/gomodule/redigo/redis"
Finally, connect to the Valkey server using the STACKHERO_VALKEY_URL_TLS
variable:
opt, err := redis.ParseURL(os.Getenv("STACKHERO_VALKEY_URL_TLS"))
if err != nil {
panic(err)
}
rdb := redis.NewClient(opt)
For more information, see the official go-redis repository
Configure Valkey on Node.js
We recommend using ioredis.
Install the ioredis
package:
$ npm install ioredis
const Ioredis = require('ioredis');
(async () => {
const valkey = new Ioredis(process.env.STACKHERO_VALKEY_URL_TLS);
// Set key "stackhero-example-key" to "abcd"
await valkey.set('stackhero-example-key', 'abcd');
// Get key "stackhero-example-key"
const value = await valkey.get('stackhero-example-key');
console.log(`Key "stackhero-example-key" has value "${value}"`);
// Finally delete key "stackhero-example-key"
await valkey.del('stackhero-example-key');
})().catch(error => {
console.error('An error occurred!', error);
});
For more examples, see the official ioredis repository.
Dashboard and web UI (Redis Commander)
Stackhero dashboard allows you to see your instance usage, restart it, and apply updates. It also gives you the ability to access the web UI to consult your Valkey data directly in a graphical way.
You can access the dashboard via the CLI:
$ heroku addons:open ah-valkey-stackhero
Opening ah-valkey-stackhero for sharp-mountain-4005
You can also visit the Heroku Dashboard and select the application in question. Then, select Stackhero for Valkey
from the Add-ons
menu.
Set eviction policy
Yon can define how Valkey will react when you consume more memory than available on your plan.
To do so, connect to your Stackhero dashboard, select your Valkey service, then click Configure
and set the Eviction policy
setting.
Upgrading your plan
You cannot downgrade an existing add-on.
Application owners must carefully manage the migration timing to ensure proper application function during the migration process.
Use the heroku addons:upgrade
command to migrate to a new plan.
$ heroku addons:upgrade ah-valkey-stackhero:newplan
-----> Upgrading ah-valkey-stackhero:newplan to sharp-mountain-4005... done
Your plan has been updated to: ah-valkey-stackhero:newplan
Removing the add-on
You can remove Stackhero for Valkey via the CLI:
This will destroy all associated data and cannot be undone!
$ heroku addons:destroy ah-valkey-stackhero
-----> Removing ah-valkey-stackhero from sharp-mountain-4005... done
Support
You can submit support and runtime issues for Stackhero for Valkey via one of the Heroku Support channels. For urgent issues, CC support@stackhero.io.