
This add-on is operated by Stackhero
Redis® on dedicated instances, up-to-date versions and super attractive prices.
Stackhero for Redis®
Last updated July 14, 2022
Table of Contents
Stackhero for Redis provides a managed Redis instance running on a fully dedicated instance.
The Stackhero for Redis add-on provides the following features:
- A private instance (dedicated VM)
- A dedicated public IP (v4)
- TLS encryption (aka SSL)
- An automatic backup every 24 hours
- A graphical web UI (Redis Commander)
- One click to update to new Redis versions
Provisioning the Add-on
Stackhero for Redis can be attached to a Heroku application via the CLI:
A list of all plans available can be found here.
$ heroku addons:create ah-redis-stackhero --app <your app name>
-----> Adding ah-redis-stackhero to sharp-mountain-4005... done
After provisioning Stackhero for Redis, the STACKHERO_REDIS_URL_TLS
and STACKHERO_REDIS_URL_CLEAR
config variables are available in your app’s configuration. They contain the URLs to your Redis instance.
STACKHERO_REDIS_URL_TLS
- the URL to your Redis instance with TLS encryption. We recommend you chose a library that supports TLS encryption.STACKHERO_REDIS_URL_CLEAR
- the URL to your Redis instance with no encryption (clear).
Review config var values with the heroku config:get
CLI command:
$ heroku config:get STACKHERO_REDIS_URL_TLS
rediss://user:password@domain:port
Local Setup
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 an .env
file. Use heroku config
to view your app’s config variables. Use the following command for each value that you want to add to your .env
file:
heroku config:get <STACKHERO_REDIS_VARIABLE> -s >> .env
For example:
$ heroku config:get STACKHERO_REDIS_URL_TLS -s >> .env
$ heroku config:get STACKHERO_REDIS_URL_CLEAR -s >> .env
Don’t commit credentials and other sensitive configuration values to source-control. In Git, exclude the .env
file with: echo .env >> .gitignore
.
For more information, see Heroku Local.
Connecting to Redis
There are Redis clients for most popular programming language. We recommend you chose a library that supports TLS encryption.
Connecting with Java
You can use the environment variable STACKHERO_REDIS_URL_TLS
to connect to Redis.
Here’s an example of a connection using Jedis:
private static Jedis getConnection() throws URISyntaxException {
URI redisURI = new URI(System.getenv("STACKHERO_REDIS_URL_TLS"));
Jedis jedis = new Jedis(redisURI);
return jedis;
}
For more information about Jedis, see the official Jedis repository and the official wiki.
In a multithreaded environment, like a webserver, use Jedis Pool:
public static JedisPool getPool() {
URI redisURI = new URI(System.getenv("STACKHERO_REDIS_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, redisURI);
return pool;
}
For more information about using Jedis in a multi-threaded environment, see the Jedis wiki.
Connecting with Ruby
Install the Redis gem:
$ gem 'redis'
$ bundle install
For more information about the Ruby Redis gem, see the Ruby Redis repository.
With Rails, you have to create the initializer file config/initializers/redis.rb
:
$redis = Redis.new(url: ENV["STACKHERO_REDIS_URL_TLS"])
Connecting with Python
Install the Redis package:
$ pip install redis
$ pip freeze > requirements.txt
import os
import redis
r = redis.from_url(os.environ.get("STACKHERO_REDIS_URL_TLS"))
For more information about the Python Redis package, see the Python redis package documentation.
Connecting with PHP
Retrieve the URL with getenv('STACKHERO_REDIS_URL_TLS')
and pass it to your preferred Redis client library.
Connecting with 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 Redis server using the STACKHERO_REDIS_URL_TLS
variable:
opt, err := redis.ParseURL(os.Getenv("STACKHERO_REDIS_URL_TLS"))
if err != nil {
panic(err)
}
rdb := redis.NewClient(opt)
For more information about using the Go Redis package, see the go-redis repository.
Connecting with Node.js
We recommend using ioredis.
Install the ioredis
package:
$ npm install ioredis
const Ioredis = require('ioredis');
(async () => {
const redis = new Ioredis(process.env.STACKHERO_REDIS_URL_TLS);
// Set key "stackhero-example-key" to "abcd"
await redis.set('stackhero-example-key', 'abcd');
// Get key "stackhero-example-key"
const value = await redis.get('stackhero-example-key');
console.log(`Key "stackhero-example-key" has value "${value}"`);
// Finally delete key "stackhero-example-key"
await redis.del('stackhero-example-key');
})().catch(error => {
console.error('An error occurred!', error);
});
For more examples, see the ioredis repository.
Dashboard and Web UI (Redis Commander)
You can see your instance usage, restart your instance, and apply updates from the Stackhero dashboard. You can also access the Redis console web UI.
Access the dashboard via the CLI:
$ heroku addons:open ah-redis-stackhero
Opening ah-redis-stackhero for sharp-mountain-4005
You can also visit the Heroku Dashboard, select your application, and then select Stackhero for Redis from the Add-ons menu.
Set Eviction Policy
The eviction policy specifies how Redis reacts when you consume more memory than available on your plan. To specify an eviction policy, connect to your Stackhero dashboard, select your Redis service, select Configure
, and then specify the Eviction policy
.
Upgrading Your Plan
You can’t downgrade an existing add-on.
Use the heroku addons:upgrade
command to migrate to a new plan.
$ heroku addons:upgrade ah-redis-stackhero:newplan
-----> Upgrading ah-redis-stackhero:newplan to sharp-mountain-4005... done
Your plan has been updated to: ah-redis-stackhero:newplan
Manage migration timing to ensure your application is available during the migration process.
Removing the Add-on
You can remove Stackhero for Redis via the CLI:
This destroys all associated data and can’t be undone!
$ heroku addons:destroy ah-redis-stackhero
-----> Removing ah-redis-stackhero from sharp-mountain-4005... done
Support
Stackhero for Redis support and runtime issues should be submitted via one of the Heroku Support channels. We recommend adding support@stackhero.io in copy for urgent issues.
Additional Resources
- Redis documentation by Stackhero
- Redis managed cloud