think less.. and more.. name age user 1 jim 21 user 2 amy 44 user:1:name = Jim user:1:age = 21 user:2:name = Amy user:2:age = 44 just keys and values.. a la SQLite, MySQL, etc.. Friday, 23 November 12
Real time logging LLOOGGs! Just keep pushing stu" onto a list.. LLOOGG was/is a service run by Salvatore that Redis was born out of. Friday, 23 November 12
Scoreboarding Sony did/does this Sorted sets are ideal! Add entries to a sorted set and query for results later. SuperDude1 20 pts xx_idiot_xx 19 pts mrkewl 11 pts freddieM 2 pts Friday, 23 November 12
Session storage redis-store library can help with this for your Ruby webapps SET session:[session_key] “{ stuff: 123, blah: 234 }” Friday, 23 November 12
Cache Think like old school memcached usage Some folks use Redis and memcached, keeping memcached for cache only and Redis for the more complex stu" Friday, 23 November 12
GroupOn, Stack Over#ow, Craigslist Tumblr, Microsoft There’s a lot more than this but just to make the point.. Redis is not some crazy far out technology. & 200+ Redis servers at Pinterest..! Friday, 23 November 12
Single threaded and event driven Think Node.js or a simple EventMachine loop (technically in Redis 2.4+ some threads are used for background IO tasks but not servicing requests) Friday, 23 November 12
Individual operations are atomic Nothing can interrupt or clash with a command you issue. Remember: Redis is single threaded processed just one at a time.. Friday, 23 November 12
Data can be “expired” Want a key/value to die in 60 seconds? You got it. INCR hits:33.12.32.22 EXPIRE hits:33.12.32.22 3600 EXPIREAT hits:33.12.32.22 1928374758 Or cache invalidation! Friday, 23 November 12
Everything is stored in memory It has persistence but.. your entire dataset is in memory too. If it can’t !t, stu"* will be pruned. * - special technical term Friday, 23 November 12
But it does have persistence! This makes Redis actually useful ;-) RDB “snapshots” and AOF logs Forks in order to do dumps and “copy on write” saves a lot of pain Friday, 23 November 12
Lots of Redis clients C, C#, C++, Clojure, Lisp, Ruby, Python, Go, Erlang Scala, Objective C, Lua, Node, PHP, Smalltalk, Tcl Some languages even have ORMs supporting Redis. Like Ohm in Ruby. DataMapper has an adapter too.And Python’s redis_wrap. Friday, 23 November 12
Scriptable (with Lua) A handy way to keep more complex operations atomic Server can also store scripts and recall them using SHA1 hash Friday, 23 November 12
EXEC if (redis.call("type",KEYS[1]) ~= "string" or redis.call("get",KEYS[1]) ~= ARGV[1]) then redis.call("set",KEYS[1],ARGV[2]) end , 1, “some key” Friday, 23 November 12
Can be used from PostgreSQL with a foreign data wrapper CREATE EXTENSION redis_fdw; CREATE SERVER redis_server FOREIGN DATA WRAPPER redis_fdw OPTIONS (address '127.0.0.1', port '6379'); CREATE FOREIGN TABLE redis_db0 (key text, value text) SERVER redis_server OPTIONS (database '0'); ... SELECT id, email, value as visits FROM users, redis_db0 WHERE ('user_' || cast(id as text)) = cast(redis_db0.key as text) AND cast(value as int) > 40; Friday, 23 November 12
f e d c b a RPUSH LPOP RPUSH my_q a RPUSH my_q b LPOP my_q == “a” LPOP my_q == “b” LPOP my_q == (nil) Or BLPOP to block (wait) until something can be popped Queues Not a native data type. Just a list! Friday, 23 November 12
contains:aba contains:ase abacus cabal baba teabag cabaret vase vaseline baseline unbased phase suitcase SINTER contains:aba contains:ase == database database This is only a simple example. SINTER can take any number of arguments! SUNION is another command that will join sets together. SDIFF will give you the difference. Friday, 23 November 12
contains:aba contains:ase abacus cabal baba teabag cabaret vase vaseline baseline unbased phase suitcase SINTERSTORE resultset contains:aba contains:ase database SUNIONSTORE does the same for set unions. database resultset Friday, 23 November 12
$text = “pet” $ids = SMEMBERS starts:$text MGET *user:$ids And to query.. Not actual syntax, implying.. MGET user:1 user:2 user:3, etc. Friday, 23 November 12
ZADD scoreboard 10 “Johnny” ZADD scoreboard 8 “Fred” ZADD scoreboard 20 “Amy” ZREVRANGE scoreboard 0 1 A very short example.. .. gets us the top 2 scorers! Friday, 23 November 12
HSET user:1 name “John” HGET user:1 name Yet another very short example.. Other commands include HGETALL, HKEYS, HVALUES, HINCRBY, etc.. name John age 20 gender male user:1 Friday, 23 November 12
WATCH somekey GET somekey MULTI SET someotherkey $x EXEC You can also ensure a transaction fails if a dependency is broken. Put result into $x This fails if somekey changed. (check and set) Friday, 23 November 12
Redis Cluster Distributed and fault tolerant Redis Essentially.. auto-sharding (to 4096 slots) Only a subset of Redis (no complex multi-key ops) Friday, 23 November 12