Skip to content

Instantly share code, notes, and snippets.

@Yiftach
Last active December 18, 2015 02:39
Show Gist options
  • Save Yiftach/5712959 to your computer and use it in GitHub Desktop.
Save Yiftach/5712959 to your computer and use it in GitHub Desktop.
Redis Cloud MZ Heroku overview

Redis Cloud Multi-AZ is a fully-managed cloud service for hosting and running your Redis dataset with enhanced level highly-availability. Your dataset is constantly replicated to a different Availability Zone (data center) and an instant auto-failover mechanism guarantees data is served without interruption in case of a complete or partial outages of the data center hosting your Redis dataset.

Like the basic Redis Cloud add-on, Redis Cloud Multi-AZ provides auto-scalability and predictable and stable top performance. You can quickly and easily get your apps up and running with Redis Cloud Multi-AZ through its add-on for Heroku, just tell us how much memory you need and get started instantly with your first truly highly-available Redis database. You can then add as many Redis databases as you need (each running in a dedicated process, in a non-blocking manner) and increase or decrease the memory size of your plan without affecting your existing data.

Redis Cloud Multi-AZ offers a fast storage engine, eliminating the well known bottlenecks of writing to disk. In addition, you can easily import an existing dataset to any of your Redis Cloud Multi-AZ databases, from your S3 account or from any other Redis server. Daily backups are performed automatically and in addition, you can backup your dataset manually at any given time. The service guarantees high performance, as if you were running the strongest cloud instances.

Getting started

Start by installing the add-on:

A list of all plans available can be found [here](http://addons.heroku.com/rediscloud-mz).

:::term $ heroku addons:add rediscloud-mz:1000

Once Redis Cloud Multi-AZ has been added, you will notice a REDISCLOUD_MZ_URL config vars in your heroku config containing the username, password, hostname and port of your first Redis Cloud Multi-AZ database.

:::term
$ heroku config:get REDISCLOUD_MZ_URL
http://rediscloud-mz:password@hostname:port

Note: as database provisioning is carried out asynchronously, please wait until the creation and initialization of the database is complete. This process shouldn't take more than 60 seconds. You are ready to go when the "hostname" value above is different than your "localhost".

Next, setup your app to start using the Redis Cloud Multi-AZ add-on. In the following sections we have documented the interfaces with several languages and frameworks supported by Heroku.

Using Redis from Ruby

The redis-rb is a very stable and mature redis client and the easiest way to access Redis from Ruby.

Install redis-rb:

:::term
sudo gem install redis

Configuring Redis from Rails

For Rails 2.3.3 up to Rails 3.0, update the config/environment.rb to include the redis gem:

:::ruby
config.gem 'redis' 

For Rails 3.0 and above, update the Gemfile:

:::ruby
gem 'redis'  

And then install the gem via Bundler:

:::term
bundle install

Lastly, create a new redis.rb initializer in config/initializers/ and add the following code snippet:

:::ruby
uri = URI.parse(ENV["REDISCLOUD_MZ_URL"])
$redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)

Configuring Redis on Sinatra

Add this code snippet to your configure block:

:::ruby
configure do
    . . .
	require 'redis'
	uri = URI.parse(ENV["REDISCLOUD_MZ_URL"])
	REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
    . . .
end

Using Redis on Unicorn

No special setup is required when using Redis Cloud Multi-AZ with a Unicorn server. Users running Rails apps on Unicorn should follow the instructions in the Configuring Redis from Rails section and users running Sinatra apps on Unicorn should follow the instructions in the Configuring Redis on Sinatra section.

Testing (Ruby)

:::ruby
redis.set("foo", "bar")
# => "OK"
redis.get("foo")
# => "bar"

Using Redis from Java

Jedis is a blazingly small, sane and easy to use Redis java client. You can download the latest build from github or use it as a maven dependency:

:::java
<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
	<version>2.0.0</version>
	<type>jar</type>
	<scope>compile</scope>
</dependency>

Configure connection to your Redis Cloud Multi-AZ service using REDISCLOUD_MZ_URLconfig vars and the following code snippet:

:::java
try { 
		URI redisUri = new URI(System.getenv("REDISCLOUD_MZ_URL"));
		JedisPool pool = new JedisPool(new JedisPoolConfig(),
	    		redisUri.getHost(),
	    		redisUri.getPort(),
	    		Protocol.DEFAULT_TIMEOUT,
	    		redisUri.getUserInfo().split(":",2)[1]);
} catch (URISyntaxException e) {
   	    // URI couldn't be parsed.		   
} 

Testing (Java)

:::java
Jedis jedis = pool.getResource();
jedis.set("foo", "bar");
String value = jedis.get("foo");
// return the instance to the pool when you're done
pool.returnResource(jedis);

(example taken from Jedis docs).

Using Redis from Python

redis-py is the most common client to access Redis from Python.

Use pip to install it:

:::term
sudo pip install redis

Configure connection to your Redis-Cloud service using REDISCLOUD_MZ_URLconfig vars and the following code snippet:

:::python
import os
import urlparse
import redis
url = urlparse.urlparse(os.environ.get('REDISCLOUD_MZ_URL'))
r = redis.Redis(host=url.hostname, port=url.port, password=url.password)

Testing (Python):

:::python
>>> r.set('foo', 'bar')
True
>>> r.get('foo')
'bar'

###Django-redis-cache

Redis can be used as the back-end cache for Django.

To do so, install django-redis-cache:

:::term
pip install django-redis-cache

Next, configure your CACHES in the settings.py file:

:::python
import os
import urlparse
redis_url = urlparse.urlparse(os.environ.get('REDISCLOUD_MZ_URL'))
CACHES = {
	'default': {
	'BACKEND': 'redis_cache.RedisCache',
	'LOCATION': '%s:%s' % (redis_url.hostname, redis_url.port),
	'OPTIONS': {
        'PASSWORD': redis_url.password,
		'DB': 0,
    }
  }
}

Testing (Django)

:::python
from django.core.cache import cache
cache.set("foo", "bar")
print cache.get("foo")

Using Redis from PHP

Predis is a flexible and feature-complete PHP client library for Redis.

Instructions for installing the Predis library can be found here.

Loading the library to your project should be straightforward:

:::javascript
<?php
// prepend a base path if Predis is not present in your "include_path".
require 'Predis/Autoloader.php';
Predis\Autoloader::register();

Configure connection to your Redis Cloud Multi-AZ service using REDISCLOUD_MZ_URLconfig vars and the following code snippet:

:::javascript
$redis = new Predis\Client(array(
	'host' => parse_url($_ENV['REDISCLOUD_MZ_URL'], PHP_URL_HOST), 
	'port' => parse_url($_ENV['REDISCLOUD_MZ_URL'], PHP_URL_PORT),
	'password' => parse_url($_ENV['REDISCLOUD_MZ_URL'], PHP_URL_PASS), 
));

Testing (PHP)

:::javascript
$redis->set('foo', 'bar');
$value = $redis->get('foo');

Using Redis from Node.js

node_redis is a complete Redis client for node.js.

You can install it with:

:::term
npm install redis

Configure connection to your Redis-Cloud service using REDISCLOUD_URLconfig vars and the following code snippet:

:::javascript
var redis = require('redis');
var url = require('url');
var redisURL = url.parse(process.env.REDISCLOUD_MZ_URL);
var client = redis.createClient(redisURL.port, redisURL.hostname, {no_ready_check: true});
client.auth(redisURL.auth.split(":")[1]);

###Testing (Node.js)

:::javascript
client.set('foo', 'bar');
client.get('foo', function (err, reply) {
    console.log(reply.toString()); // Will print `bar`
});

Dashboard

Our dashboard presents all performance and usage metrics of your Redis Cloud Multi-AZ service on a single screen, as shown below:

Dashboard

To access your Redis Cloud Multi-AZ dashboard run:

:::term
heroku addons:open rediscloud-mz

You can then find your dashboard under the MY REDIS DATABASES menu.

Alternatively, open the Redis Cloud Multi-AZ add-on from your application's dashboard at heroku.com.

Adding Redis databases to your plan

Redis Cloud Multi-AZ allows you to add multiple Redis databases to your plan, each running in a dedicated process, in a non-blocking manner (i.e. without interfering with your other databases). You can create as many databases as you need, limited by the memory size of your plan.

Your first Redis database is created automatically upon launching the Redis Cloud Multi-AZ add-on and its URL and credentials are maintained in REDISCLOUD_MZ_URLconfig vars. To add more databases, simply access your Redis Cloud Multi-AZ console and click the New DB button in the MY DATABASES > Manage page.

The Redis Cloud Multi-AZ console will provide you a new URL for connecting to your new Redis database.

Migrating between plans

Plans migration is easy and instant. It requires no code change and has no effect on your existing datasets. You can use the 'heroku addons:upgrade' command to migrate to a new plan:

An example of how to upgrade to a 5GB plan

:::term
$ heroku addons:upgrade rediscloud-mz:5000

Removing the add-on

Redis Cloud Multi-AZ can be removed via the following command:

:::term
$ heroku addons:remove rediscloud-mz
This will destroy all associated data and cannot be reversed!

Support

All Redis Cloud Multi-AZ support and runtime issues should be submitted via the Heroku Support channels. Any non-support related issues or product feedback is welcome via email at support@garantiadata.com.

Additional resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment