Clojure with a Touch of Redis

If you’ve ever done enterprise web application development, you’ve heard of Redis. Redis has a lot of uses, but the most common is as replacement for your standard session. Even just using it’s key-value store (think hashmap on steroids), Redis provides so much power that many enterprise sites integrate it into their larger projects.

Lucky for those of us using Clojure, Redis integrates nicely using Carmine, an open source Clojure library for interacting with Redis.

First you need to get Redis installed for your platform. Most platforms have an installer like brew or yum that will take care of the details. If you feel very confident in your abilities, download the current source for Redis and ‘make’ it. Building your own is by far the best solution if the installer for your platform is not up-to-date.

Redis comes with several tools. The ‘redis-server’ is the one that starts the server. The ‘redis-cli’ command line interface is wonderful for learning to interact directly with Redis, before writing too much code. You can’t really install Redis without it’s tools, but they don’t take much extra room, so don’t fret.

Redis is intended to be run from a secure environment. Never expose Redis to the Internet or put it in a DMZ. Bad things will happen … Bad security things will happen. Always keep Redis protected by a firewall and other standard security measures.

Even though I protect Redis I always configure it to require a password. Normally to start a Redis server–you’ll want one running Redis server per application–you issue the command line redis-server . However, to get a password going, you’ll want a custom configuration file with ‘requirepass‘ set. In that case, the command to start the Redis server will look something like redis-server <myconf>.conf .

Once the Redis server starts, you should see something like the following in your terminal or your log.

Maybe, yours will be all green and not so colorful.

Once you have your Redis server running and requiring a password, use the ‘redis-cli’ tool to connect to it and test your connection. Start the command line interface. If you are running the Redis server on localhost, and your command line interface is running on localhost, then connecting will be simple.

If you are looking at connecting to another host and specifying the port try something similar to this.

For a full list of options type,

Because we required a password, your first command after connecting needs to be an AUTH command. (Technically, AUTH and other Redis commands do not need to be uppercase, but convention is to use uppercase, just to make it easier to read Redis related code.)

Finally, issue the ping command and you should see a pong response.

Okay, enough redis-cli. Use control-d to exit and let’s get on with the Clojure end of things.

You should already have lein installed on your system. Create a new app to get Redis and Clojure working together.

Now, add Carmine to the list of dependencies in the project.clj file.

The whole project.clj will likely look simiar to the following.

I have modified a lot of the following code from code on the Carmine site. Make sure you visit the Carmine home page for lots of docs about using Carmine.

Issuing the command lein run  from the project root should download and install the Carmine library into your project. Usually it quietly happens in the background, so you don’t have to worry about it.

Note that you need to replace ‘mypassword’ with your actual password, and 127.0.0.1 with the address you configured your Redis server to bind to.

The result of this simple Clojure app will be the word PONG printed to your terminal.

For quick reference, see the full docs at the Carmine site.

Also, using one ‘wcar*’ to send multiple Redis commands results in better efficiency. When you do that, the results of each command are returned in a Clojure vector.

If you want to see a specific Clojure tutorial, please let me know on Twiter.

Leave a Reply

Your email address will not be published. Required fields are marked *