This add-on is operated by Redis
Enterprise-Class Memcached for Developers
Memcached Cloud
Last updated March 13, 2024
Table of Contents
Memcached Cloud is a fully-managed service for running your Memcached in a reliable and fail-safe manner. Your dataset is constantly replicated, so if a node fails, an auto-switchover mechanism guarantees data is served without interruption. Memcached Cloud provides various data persistence options as well as remote backups for disaster recovery purposes. You can quickly and easily get your apps up and running with Memcached Cloud through its add-on for Heroku, just tell us how much memory you need and get started instantly with your first Memcached bucket.
A Memcached bucket is created in seconds and from that moment on, all operations are fully-automated. The service completely frees developers from dealing with nodes, clusters, server lists, scaling and failure recovery, while guaranteeing absolutely no data loss.
Getting started
Start by installing the add-on: A list of all plans available can be found here.
$ heroku addons:create memcachedcloud
Once Memcached Cloud has been added, you will notice a three new config vars in your heroku
environment containing the servers, username and password of your first Memcached Cloud bucket:
MEMCACHEDCLOUD_SERVERS
, MEMCACHEDCLOUD_USERNAME
, MEMCACHEDCLOUD_PASSWORD
.
Use the following heroku
command to view them:
$ heroku config
Next, setup your app to start using the Memcached Cloud add-on. In the following sections we have documented the interfaces with several languages and frameworks supported by Heroku.
Using Memcached from Ruby
Dalli is a high performance, pure Ruby client for accessing Memcached servers that uses binary protocol.
To use Dalli with Rails 3.x, update your gems with:
gem 'dalli'
And then install the gem via Bundler:
bundle install
Lastly, add the following line in your config/environments/production.rb
:
if ENV["MEMCACHEDCLOUD_SERVERS"]
config.cache_store = :mem_cache_store, ENV["MEMCACHEDCLOUD_SERVERS"].split(','), { :username => ENV["MEMCACHEDCLOUD_USERNAME"], :password => ENV["MEMCACHEDCLOUD_PASSWORD"] }
end
Configuring Memcached on Sinatra
Add this code snippet to your configure block:
configure do
. . .
require 'dalli'
if ENV["MEMCACHEDCLOUD_SERVERS"]
$cache = Dalli::Client.new(ENV["MEMCACHEDCLOUD_SERVERS"].split(','), :username => ENV["MEMCACHEDCLOUD_USERNAME"], :password => ENV["MEMCACHEDCLOUD_PASSWORD"])
end
. . .
end
Using Memcached on Unicorn
No special setup is required when using Memcached Cloud with a Unicorn server. Users running Rails apps on Unicorn should follow the instructions in the Configuring Memcached from Rails section and users running Sinatra apps on Unicorn should follow the instructions in the Configuring Memcached on Sinatra section.
Testing from Ruby
$cache.set("foo", "bar")
# => true
$cache.get("foo")
# => "bar"
A Sinatra sample application is available at GitHub.
Browse the source code or
Using Memcached from Java
spymemcached is a simple, asynchronous, single-threaded Memcached client written in Java. You can download the latest build from: https://code.google.com/p/spymemcached/downloads/list.
If you are using Maven
, start by adding the following repository:
<repositories>
<repository>
<id>spy</id>
<name>Spy Repository</name>
<layout>default</layout>
<url>http://files.couchbase.com/maven2/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
Then, specify the actual artifact as follows:
<dependency>
<groupId>spy</groupId>
<artifactId>spymemcached</artifactId>
<version>2.8.9</version>
<scope>provided</scope>
</dependency>
Configure the connection to your Memcached Cloud service by using the environment variables as shown in the following code snippet:
try {
AuthDescriptor ad = new AuthDescriptor(new String[] { "PLAIN" },
new PlainCallbackHandler(System.getenv("MEMCACHEDCLOUD_USERNAME"), System.getenv("MEMCACHEDCLOUD_PASSWORD")));
MemcachedClient mc = new MemcachedClient(
new ConnectionFactoryBuilder()
.setProtocol(ConnectionFactoryBuilder.Protocol.BINARY)
.setAuthDescriptor(ad).build(),
AddrUtil.getAddresses(System.getenv("MEMCACHEDCLOUD_SERVERS")));
} catch (IOException ex) {
// the Memcached client could not be initialized.
}
Testing from Java
mc.set("foo", 0, "bar");
Object value = mc.get("foo");
A Java sample application, running on Play!, is available at GitHub.
Browse the source code or
Using Memcached from Python
bmemcached is a pure, thread safe, python module to access memcached via binary protocol.
Use pip
to install it:
$ pip install python-binary-memcached
Configure the connection to your Memcached Cloud service using the MEMCACHEDCLOUD
config vars and the following code snippet:
import os
import urlparse
import bmemcached
import json
mc = bmemcached.Client(os.environ.get('MEMCACHEDCLOUD_SERVERS').split(','), os.environ.get('MEMCACHEDCLOUD_USERNAME'), os.environ.get('MEMCACHEDCLOUD_PASSWORD'))
Testing from Python
mc.set('foo', 'bar')
print client.get('foo')
Django
Memcached Cloud can be used as a Django cache backend with django-bmemcached.
To do so, install django-bmemcached:
$ pip install python-binary-memcached
$ pip install django-bmemcached
Next, configure your CACHES
in the settings.py
file:
import os
import urlparse
import json
CACHES = {
'default': {
'BACKEND': 'django_bmemcached.memcached.BMemcached',
'LOCATION': os.environ.get('MEMCACHEDCLOUD_SERVERS').split(','),
'OPTIONS': {
'username': os.environ.get('MEMCACHEDCLOUD_USERNAME'),
'password': os.environ.get('MEMCACHEDCLOUD_PASSWORD')
}
}
}
Testing from Django
from django.core.cache import cache
cache.set("foo", "bar")
print cache.get("foo")
A Django sample application is available at GitHub.
Browse the source code or
Using Memcached from PHP
PHP Memcached ensures efficient performance as well as advanced features on top of the core memcached functions.
Heroku’s PHP environment supports it as a third party extension - just add the following module to your composer.json
file:
"require": {
"ext-memcached": "*"
}
Next, ensure that your new requirements are “frozen” to composer.lock
by running:
$ composer update
For more information on enabling the extension and potential troubleshooting (e.g. when you don’t have the memcached
extension available on your local computer), refer to the using optional extensions extensions section of Heroku’s PHP reference documentation.
Configure a connection to your Memcached Cloud service using the MEMCACHEDCLOUD
config vars and the following code snippet:
$mc = new Memcached();
$mc->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$mc->addServers(array_map(function($server) { return explode(':', $server, 2); }, explode(',', $_ENV['MEMCACHEDCLOUD_SERVERS'])));
$mc->setSaslAuthData($_ENV['MEMCACHEDCLOUD_USERNAME'], $_ENV['MEMCACHEDCLOUD_PASSWORD']);
Testing from PHP
$mc->set('foo', 'bar');
echo $mc->get('foo');
A PHP sample application is available at GitHub.
Browse the source code or
Using Memcached from Node.js
MemJS is a pure Node.js client library for using memcached. It uses the binary protocol and support SASL authentication.
You can install it with:
$ npm install memjs
Configure a connection to your Memcached Cloud service using the following code snippet:
var memjs = require('memjs');
var client = memjs.Client.create(process.env.MEMCACHEDCLOUD_SERVERS, {
username: process.env.MEMCACHEDCLOUD_USERNAME,
password: process.env.MEMCACHEDCLOUD_PASSWORD
});
Testing from Node.js
client.set("foo", "bar");
client.get("foo", function (err, value, key) {
if (value != null) {
console.log(value.toString()); // Will print "bar"
}
});
A Node.js sample application is available at GitHub.
Browse the source code or
Memcached Cloud Dashboard
Our dashboard displays all of the performance and usage metrics for your Memcached Cloud service on a single screen, as shown in the following screenshot:
To access your Memcached Cloud dashboard run:
$ heroku addons:open memcachedcloud
You can then find your dashboard under the MY BUCKETS menu.
Alternatively, open the Memcached Cloud add-on from your application’s dashboard at heroku.com.
Bucket Backups
You can access the backups of your database from our dashboard via the MY BUCKETS > Manage menu. After you select a bucket from that page, use the the Backup Now button to create an on-demand backup or the My Backups link for a list of links to your bucket’s backup files.
Adding Memcached buckets to your plan
Memcached Cloud allows you to add multiple Memcached buckets to your plan, each running in a dedicated process, in a non-blocking manner (i.e. without interfering with your other buckets). You can create as many buckets as you need, limited by the memory size of your plan.
Your first Memcached bucket is created automatically upon launching the Memcached Cloud add-on and its servers and credentials are maintained in the MEMCACHEDCLOUD
config vars. To add more buckets, simply access your Memcached Cloud console and click the Add Bucket button in the MY BUCKETS > Manage page.
The Memcached Cloud console will provide you a new server and credentials for connecting to your new Memcached bucket.
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:
$ heroku addons:upgrade memcachedcloud:5000
Removing the add-on
Memcached Cloud can be removed via the following command:
This will destroy all associated data and cannot be reversed!
$ heroku addons:destroy memcachedcloud
Support
All Memcached Cloud support and runtime issues should be submitted via the Heroku Support channels. We recommend CC:ing support@redislabs.com for urgent issues. We are also available on twitter @redislabsinc.