Enabling Redis For Your App

Board Recently, OpenShift announced support for community-contributed cartridges. This way we again push the envelope of openness of the PaaS systems one level further and prove that PaaS systems do not have to be lock-in nightmares for your company.

Redis is one of the most requested features of OpenShift users. Even though Redis is still not "officially" supported, you can easily use deploy it with the community contributed cartridge.

What does that mean? At Red Hat, we strongly believe that we should provide an exceptional service and thus we can not push new features into the production "like crazy" because all the pieces need to be well-tested and got to go through a rigorous review.

On the other hand, we strive to be as open and extensible as possible by giving OpenShift users the means (cartridges) to extend OpenShift to get what they need when they want it by collaborating as a community. Even though Red Hat does not yet have an officially supported Redis cartridge for you, we are here to provide you with a stable and super-secure environment where you can actually run Redis via the community-contributed cartridge. And running Redis this way is not a difficult task at all.

What is Redis?

Redis is an open source, BSD licensed, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.

Redis is a NoSQL in-memory data-store with support for complex use-cases and querying. One of the benefits of Redis is it's crazy speed, thus people do use it as a caching component (e.g. replacement for Mamcache), however Redis (compared to Memcache) provides features that make it well-capable database application compared to "dumb" key value store Memcache is.

Redis Use Cases

Caching is one of the simplest things to use Redis for. Most frameworks provide drivers for Redis and allow to be configured to use Redis as a caching store.

Redis as cache for Ruby frameworks

Redis as cache for Django

Radis as cache for Servlets

Analytics may be done using Redis. Redis's data-structures allow you to use the database for building real-time analytics systems.

Minuteman - Fast analytics using Redis

Crashalytics - user-story

Redis may be used as a Message queue. Whenever you need to pass some events among a distributed system you may consider Redis as an option. One specific-use case that Redis is very much used for is out-of-process task execution. When your web application needs to do some intensive task, it pushes information about it to Redis and some worker component gets the "job" from there.

Sidekiq

General purpose data store is also possible.

A case study: Design and implementation of a simple Twitter clone using only the Redis key-value store as database and PHP

Redis as the primary data store? WTF?!

And many more, like 11 Common Web Use Cases Solved In Redis.

Getting started

Go to the Download page on the Redis website and download the distribution for your system. You may also use your favorite package manager to install Redis

yum install redis
apt-get install redis
brew install redis

Once you have Redis installed, start the server. It might have been done by your package manager, or you can do it manually

redis-server

when the server is started we can connected to it and start using it.

redis-cli

Now, you can operate Redis from the command line tool.

Key-value store

Let's see how to use Redis as a simple key-value store. First, let's set the value for key "openshift" to "awesome"

SET "openshift" "awesome"

and, now, let's read the value for the same key

GET "openshift"

and finally delete it.

DEL "openshift"

WoooHooo .... simple, isn't it?

Message Passing

Let's start two clients. One will wait till a message is pushed into Redis and will print it.

BRPOP "openshift" 0

After this command, you should see your client to block. Now, let's start 2nd client, push a message,

RPUSH "openshift" "awesome"

and check the status in the 1st window.

Simple, yet effective.

If you like what you have seen, I can only recommend you to check the documentation and read more about the possibilities Redis offers. You will be also probably interested in a list of all the client libraries for Redis for your favorite language.

Redis on OpenShift

As I mentioned already, Redis is not yet officially supported on OpenShift so there are a few more steps to deploy it, however it is not complex at all! First, let's remember how you would add a supported cartridge (e.g. postgresql)

rhc cartridge add postgresql-9.2 -a (you app name)

and now let's deploy Redis!

rhc cartridge add "http://cartreflect-claytondev.rhcloud.com/reflect?github=smarterclayton/openshift-redis-cart" -a (your app name)

When the command finishes, you shall have Redis deployed in your gear.

** The only difference is that you can not use the "name" for the cartridge, but need to know the URL where to fetch the cartridge from.**

Testing Redis on OpenShift

To see Redis is actually deployed and working, we shall ssh into the gear.

rhc ssh (your app name)

Check the information Redis published for us.

env | grep REDIS

Finally, connect to Redis

telnet $OPENSHIFT_REDIS_DB_HOST $OPENSHIFT_REDIS_DB_PORT

and authenticate, you have the password available in the environment variables we printed previously. Once connected, just write

AUTH (you passowrd)

Once you hit enter you should see "+OK" as a response. Now, you can use Redis! Let's see what my test looked like

AUTH ZTNiMGM0NDI5OGZjMWMxNDlhZmJmNGM4OTk2ZmI5
+OK
SET "openshift" "awesome"
+OK
GET "openshift"
$9
"awesome"
DEL "openshift"
:1
QUIT
+OK

Oh, I might have mention that Redis uses text-based protocol, so it's extremely-super-simple to play with it over such a simple tool as telnet is.

Redis Cluster

Redis does support cluster-based deployments. For more information on this topic, check the Redis clustering on the Redis cartridge homepage

Conclusion

You are now fully equipped to use Redis on OpenShift even though the support is not yet enterprise-grade SLA supported. It's fully functional and you can use you it for building your applications. Happy Redising!

See it for Yourself

What's Next?

Hi Marek,

Thank you for putting together all these pieces of the puzzle. Would you expand this post (or may be come up with the new one) diving deeper into "Redis and scaled application" topic? I wonder how can I use Redis with my scaled Rails app. I would prefer to use master-slave configuration with Sentinel managing failover between them. Last time I checked, rails-store gem (that you mentioned) was unable to properly work in Sentinel configuration.

Thanks again, Boris

There are some instructions on how to configure Redis Sentinel on the cartridge's Readme.md here: https://github.com/smarterclayton/openshift-redis-cart
Can you take a look to see if it helps?

This cartridge breaks console and does not allow to uninstall itself:

$ rhc cartridge remove redis -a app1
Unable to complete the requested operation due to: undefined method `categories' for nil:NilClass.
Reference ID: 01903ec2cea4a5a9603ee4ffb2cdc00b

Thank you for your feedback! Can you help us out by reporting the problem on the repo itself? https://github.com/smarterclayton/openshift-redis-cart/issues

This redis install looks very useful, but I'm using PHP - and believe I also need to install phpredis, is there instructions how to do that somewhere?

I tried adding 'redis' to the deplist.txt, and was told i should try 'pecl/redis'. So I put that in deplist.txt.

After my git push, I got this warning early on:

remote: WARNING: php_bin /opt/rh/php54/root/usr/bin/php appears to have a suffix 54/root/usr/bin/php, but config variable php_suffix does not match

And then this error at the end, once it had built.

remote: Installing '/opt/rh/php54/root/usr/lib64/php/modules/redis.so' remote: ERROR: failed to write /opt/rh/php54/root/usr/lib64/php/modules/redis.so (copy(/opt/rh/php54/root/usr/lib64/php/modules/redis.so): failed to open stream: Permission denied)

So, pretty close... anyone help with the last step?!

The URL for gear http://cartreflect-claytondev.rhcloud.com/reflect?github=smarterclayton/openshift-redis-cart is not working (Service Temporarily Unavailable). I have also opened the issue on GitHub.

@nduong

I found the redis password is invariant string

$ rhc cartridge status redis -a cantas Using smarterclayton-redis-2.6 (Redis) for 'redis'

RESULT:

Redis is running slave Connect to: cantas-onepiecejs.rhcloud.com: password: ZTNiMGM0NDI5OGZjMWMxNDlhZmJmNGM4OTk2ZmI5 Redis Sentinel is not running

the password is same with above chapter's password string. what happen on it?