Redis and Clojure

Check out my previous post about Redis.

In this post I build a very simple example of using Redis with Clojure. I will be using a client library for Redis written in Clojure called redis-clojure. You could also use the java library, to see complete list of supported languages go to this link.

So here we go..

  1. Create a simple clojure project (I personally use Leinigen), to create a new project execute ‘lein new‘ this will create an entire project structure.
  2. Edit the project.clj file under the newly created project directory and add a new dependency for redis-clojure, the file should look close to this after you are done.
    (defproject "1.0.0-SNAPSHOT"
      :description "simple example of using redis"
      :dependencies [[org.clojure/clojure "1.1.0"]
                     [org.clojure/clojure-contrib "1.1.0"]
                     [redis-clojure "1.0.3-SNAPSHOT"]]
      :dev-dependencies [[swank-clojure "1.2.1"]])
  3. run ‘lein deps‘ so that all the dependencies are downloaded.
  4. Edit the file core.clj under the directory try-redis/src/com/dev/try_redis, and add the following.
      (:require redis))
    (defn test-redis []
         (redis/with-server {:host "" :port 6379 :db 0}
             (redis/set "foo" "bar")
             (println (redis/get "foo")))))

    On lines 7 and 8 we are setting key value pair and retriving the value.

  5. Start the redis server ‘./redis-server redis.conf
  6. Now we are ready to execute the script, there are 2 ways to do this.
    1. The easiest way is just going to your porject root directory and run ‘lein repl‘ (see the below oouput) which opens a read evaluate loop and once you have that run ‘(load-file “src/com/dev/try_redis/core.clj”)‘ to load the file and then you can run ‘(‘ to run the example.
    2. I personally use emacs/slime, but for this option you need to have emacs and slime-clojure installed (See my emacs page). Run ‘lein swank‘ in the project directory and then in your emacs connect to it using ‘M-x slime-connect‘, this will open up a repl, do a C-c C-k to compile the file and in the repl you can execute using ‘(’

If everything has gone will you should see this output.

Clojure 1.1.0
user=> (load-file "src/com/dev/try_redis/core.clj")
user=> (         


Recently I have heard a lot about Redis, so I decided to try it out. But first a little intro about Redis. “Redis is a database. To be specific, Redis is a database implementing a dictionary, where every key is associated with a value.” .

Think about Redis as memcache on steroids, Redis extends the basic key/value store paradigm by letting you have values of a certain type and defines operations that are unique to each type.

For example Redis lets you associate a key with a list and then lets you do list specific operations.

lpush mylist 1 --adds 1 to mylist
lpush mylist 2
llen mylist --returns the length of mylist

By the way if you just want to try out Redis without having to download and install, then check out this link.

Supported types are Lists, Sets, Sorted Sets and Strings. Below are some interesting operations (to see the entire list go to this link).

  • Adding elements to either the head or tail of a list.
  • Pop the fist element atomically (very lispy)
  • Union two sets.
  • Sorted Sets are sorted by score that you provide. It also uses the score when inserting new elements.

Redis also persists data by writing to the desk asynchronously this way you can have your entire application just use Redis without the need to have a separate database.

Here is a simple example using Redis

  • Download Redis “wget”.
  • Extract Redis and run ‘make’ in the directory.
  • Run the Redis server ‘./redis-server redis.conf ‘
  • Now you can start playing with the server using redis-cli, try the following

    ./redis-cli set name devender
    ./redis-cli get name