The final session of my day at DDD Southwest was an introductory talk by Chris Hay about Redis, “an open-source advanced key-value store”. It introduced Redis, looked at some of the basic features and commands and their possible applications. There’s full documentation of all Redis commands here.
So What Is Redis then?
Released on April 10 2009 Salvatore Sanfilippo (@antirez)
Open source, sponsored by VMWare
It is an open source, in-memory key value data store, which does more than just keys and values. (so NoSQL with knobs on.) It also does sets, lists & counters. It was written in C by Salvatore Sanfilippo (@antirez) and is very very fast. Comparisons by use as a cache server would include AppFabric& memcached. It is used by stackoverflow, reddit, & others.
Redis runs on Linux. There is a WIndows build-ish, but use Linux.
Redis is a server – you use a client or a client API library connect to it. A command-line client is installed wit the server, but there are many thrid party redis clients – listed here. For .NET developers, there are two good options.
Booksleeve uses a lot of clever async stuff with Task<T> but the demos all used ServiceStack.Redis because it is easier to work with and simpler to read.
Working with KEYS
There are three redis commands to work with key value pairs – SET, GET & EXPIRE
redis-cli SET MyKey "Hello World"
redis-cli GET MyKey
redis-cli EXPIRE MyKey 10 <- after 10 seconds
using (var cli = new RedisClient(IPaddress))
ViewData[MyKey] = cli.Get<string>("MyKey");
// cli.Set<string>("MyKey", "Hello World");
Redis Keys are useful for Caching and Sessions. There is a Microsoft session provider written for use against redis. (SQL Server provider really sucks)
Working with COUNTERS
Commands: increment, decrement, increment by, decrement by
Default increment is 1
redis-cli incr aCounter
redis-cli decr aCounter
redis-cli incrby aCounter 2
redis-cli decrby aCounter 2
Redis runs on a single thread so it is completely thread-safe. incr, decr ain't going to produce clashes and same results for same things. Counters useful for web page hit counters, perf counters, analytics, unique ids, sequences etc.
Working with SETS
Commands: sadd, smembers, srem
SADD MySet C (adds C to MySet)
SMEMBERS MySet (gets the members of MySet)
SREM MySet C (removes C from MySet)
Remember that sets are unordered and all members are unique. The power of sets comes in the set arithmetic
SUNION MySet1 MySet2
SDIFF MySet1 MySet2
SINTER MySet1 MySet2
You can find the rest of the commands here.
Q\A : Redis deals with strings typically, but it is generally pretty good with other types as it stores things at byte level. JSON Serialization works well but prefer to avoid it if possible.
Sets are useful for tagging (a la Stack Overflow).
Useful for indexes, object graph relationships.
Demo of ChukGraph
(or How I almost got myself shot in the head by gangsters...)
(Queue - first in first out)
LPUSH mylist A
(Stack - last in last out)
RPUSH mylist A
Typically, you send command to Redis without waiting for a response. A reply is returned in response to each request. However, it is possible to pipeline/queue requests and wait for a response which is a cumulative result of all the requests.
Redis sort of supports transactions
Atomic : yes
Consistent : need to add a WATCH command for that
Isolated : Yup - be aware of threads though (single threaded only)
Durability : Kind of, but the more durable you want it (persisted to disk for instance), the slower redis is. It takes about 10ms to do this. (Which is longer than you want - really)
Redis also supports LUA Scripting
PUBLISH channel message
SUBSCRIBE, UNSUBSCRIBE channel
You can set up a group of Redis servers in a master-slave configuration
- Slaves are read-only
- Why do this? Redis is single-threaded, so push intensive queries to the slaves to prevent blocking and have them publish the changes back to master
At some point in the future, there will be Redis clustering.