Introduction to Redis - In Memory Key Value Datastore
Join the DZone community and get the full member experience.Join For Free
i have been thinking about taking a deep dive into nosql databases for a long time but wasn't sure which one should i start with. there are a lot of nosql databases in the market, each solving a set of problems. i wanted to learn a nosql database that does not have a steep learning curve and is generic enough to solve more than one problem. so, i started looking into different nosql databases when i found my starting point: redis .
redis is an in-memory key value datastore written in ansi c programming language by salvatore sanfilippo . redis not only supports string datatype but it also supports list, set, sorted sets, hashes datatypes, and provides a rich set of operations to work with these types. if you have worked with memcached (an in-memory object caching system) you will find that it is very similar, but redis is memcached++. redis not only supports rich datatypes, it also supports data replication and can save data on disk. the key advantages of redis are :
- exceptionally fast : redis is very fast and can perform about 110000 sets per second, about 81000 gets per second . you can use the redis-benchmark utility for doing the same on your machine.
- supports rich data types : redis natively supports most of the datatypes that most developers already know like list, set, sorted set, hashes. this makes it very easy to solve a variety of problems because we know which problem can be handled better by which data type.
- operations are atomic : all the redis operations are atomic, which ensures that if two clients concurrently access redis server will get the updated value.
- multiutility tool : redis is a multi utility tool and can be used in a number of usecases like caching, messaging-queues (redis natively supports publish/ subscribe ), any short lived data in your application like web application sessions, web page hit counts, etc. there are a lot of people using redis and they can be found at http://redis.io/topics/whos-using-redis
getting started with redis
it is very easy to get started with redis. on unix boxes:
- you can either clone the git repository or download and unpack the tar .
- open a new terminal and go to the directory in which you cloned or unpacked redis and fire make command.
- run ./src/redis-server which will start the redis server with default configuration.
- open a new terminal and type ./src/redis-cli which will start the redis client you will use to work with redis server.
for windows user
- download redis from dmajkic git repository .
- extract the zip and add <path to redis>\redis-2.1.8\64bit to your path.
- open a new command shell and type redis-server
- open a new command shell and type redis-cli
i am a windows user so i am writing this article based on the latest redis distribution from dmajkic git repository . once you have both the server and client running you can run some commands as shown in image below
redis> pingpongredis> inforedis_version:2.1.8redis_git_sha1:00000000redis_git_dirty:0arch_bits:64....
if you are able to successfully run all the command then that means your redis installation is complete. if you don't want to download and install redis on your machine you can try redis from the browser http://try.redis-db.com/
let's take a look at its data types
the datatypes supported by redis are strings, lists, sets, sorted sets, hashes. i will now briefly talk about different redis types and play with them using redis-cli .
string is the basic data type supported by redis. you can set the string value of a key, and you can get the string value of a key using set and get commands.
redis> set firstname shekharokredis> set lastname gulatiokredis> get firstname"shekhar"redis> get lastname"gulati"
there are a lot of other operations like append, getrange, mset, strlength, etc. available for strings. you can find the whole list here . if the values for your keys are integers, then you can use decr, decrby, incrby, or incr, which allow you to increment and decrement values. these can be very useful in scenarios where you want to maintain a count of something like number of hits for a web page.
redis> incr hitcounter(integer) 1redis> incr hitcounter(integer) 2redis> incr hitcounter(integer) 3redis> decr hitcounter(integer) 2
list is an ordered collection which lets you add any number of elements in it. the element can be unique or non-unique. apart from adding and getting elements from a list, redis supports lots of operations for lists like pop, push, range, etc. you can find the full list of commands here . lets take an example that we want to maintain a list of words (uniqueness does not matter) and we want to get last 3 words we added in our system.
redis> lpush words austerity(integer) 1redis> lpush words socialism(integer) 2redis> lpush words moritorium(integer) 3redis> lpush words socialism(integer) 4redis> lpush words socialism(integer) 5redis> lrange words 0 21) "socialism"2) "socialism"3) "moritorium"
one thing to note is that in redis lists are zero based.
sets are unordered collections of binary strings. to add an element to a set you use the sadd command and to get all the members of a set you use the smembers command. you can find the full list of set commands here . lets take an example that we want to maintain a collection of all unique words added in our system.
redis> sadd uniquewords austerity(integer) 1redis> sadd uniquewords pragmatic(integer) 1redis> sadd uniquewords moritorium(integer) 1redis> sadd uniquewords socialism(integer) 1redis> sadd uniquewords socialism(integer) 0
as you can see, because we were using set we can't add the same word (socialism) twice.
sorted sets are sets that can be sorted based on a score. so each time you add a value to the set you provide a score which is used for sorting. for example, we want to sort our words on the basis of their length.
redis> zadd wordswithlength 9 austerity(integer) 1redis> zadd wordswithlength 7 furtive(integer) 1redis> zadd wordswithlength 5 bigot(integer) 1redis> zrange wordswithlength 0 -11) "bigot"2) "furtive"3) "austerity"
there are lot of other useful commands for sorted sets which can be found here .
hashes allow you store key-value pair against a hash. this can be useful in scenarios where you want to save an object with several properties.
redis> hset user:1 name shekhar(integer) 1redis> hset user:1 lastname gulati(integer) 1redis> hget user:1redis> hget user:1 name"shekhar"redis> hgetall user:11) "name"2) "shekhar"3) "lastname"4) "gulati"
loose ends -- what's not been covered
apart from providing all these datatypes, redis also supports transactions and publish/subscribe. i will talk about these features in a future post.
we will take a deeper look into these datatypes in my future posts on redis, where we will build up a dictionary application using redis as a datastore. i will be using spring data redis support for interacting with redis. spring support for redis was also one of the reasons that i thought of using redis.
Opinions expressed by DZone contributors are their own.