3. What is Redis?
Redis means REmote DIrectory Server.
Redis is an advanced key-value NoSQL data store.
It is similar to memcached except the dataset is not volatile. Like memcached,
Redis can store string values, but it can also store lists, sets, and ordered sets.
All these data types can be manipulated with atomic operations that push,
pop, add and remove elements, perform server side union, intersection,
difference between sets, and more.
Redis also supports different kinds of sorting.
4. History
Early 2009 - Salvatore Sanfilippo, an Italian developer, started the Redis project
He was working on a real-time web analytics solution and found that MySQL could not
provide the necessary performance.
June 2009 - Redis was deployed in production for the LLOOGG real-time web analytics
website
March 2010 - VMWare hired Sanfilippo to work full-time on Redis (remains BSD licensed)
Subsequently, VMWare hired Pieter Noordhuis, a major Redis contributor, to assist on
the project.
7. Sample Commands
Command Line : redis-cli
SET users:GeorgeWashington "job: President, born:1732, dislikes: cherry trees"
GET users:GeorgeWashington
> SET population 6
OK
> INCRBY population 10
(integer) 16
> INCR population
(integer) 17
9. Redis with Grails
Plugin: https://grails.org/plugin/redis
The Redis plugin provides integration with a Redis datastore.
This plugin gives grails apps a pooled connection to a Redis instance and provides a number
of helper methods and caching/memoization methods that can greatly speed up your
application performance.
10. Redis with Grails
def redisService
The redisService bean wraps the pool connection. It has a number of
caching/memoization helper functions, template methods, and basic Redis
commands, it will be your primary interface to Redis.
redisService.foo = "bar"
assert "bar" == redisService.foo
redisService.sadd("months", "february")
assert true == redisService.sismember("months", "february")
12. Memoization
Memoization is a write-through caching technique. The plugin gives a number of methods that take a key
name, and a closure as parameters. These methods first check Redis to see if the key exists. If it does, it
returns the value of the key and does not execute the closure. If it does not exist in Redis, it executes the
closure and saves the result in Redis under the key. Subsequent calls will then be served the cached value
from Redis rather than recalculating.
This technique is very useful for caching values that are frequently requested but expensive to calculate.
14. Redis Taglib
The redis:memoize TagLib lets you leverage memoization within your GSP files. Wrap it
around any expensive to generate text and it will cache it and quickly serve it from Redis.
<redis:memoize key="mykey" expire="3600">
<!--
insert expensive to generate GSP content here
taglib body will be executed once, subsequent calls
will pull from redis till the key expires
-->
<div id='header'>
... expensive header stuff here that can be cached ...
</div>
</redis:memoize>
16. Redis Session Plugin
Stores HTTP sessions in a Redis data store.
This plugin lets you store HTTP session data in a redis store using Database
Session Plugin.
https://grails.org/plugin/redis-database-session
17. Redis Persistence
#Snapshotting (RDB) : Snapshots at predefined intervals, which may also
depend on the number of changes. Any changes between these intervals will be
lost at a power failure or crash.
# Append-Only files (AOF): Writing a kind of change log at every data change.
You can fine-tune how often this is physically written to the disk, but if you chose to
always write immediately (which will cost you some performance), then there will be
no data loss caused by the in-memory nature of Redis
18. Redis @ Twitter
Redis on Twitter supports over 30 billion timeline updates per day based on
5000 tweets per second or 400,000,000 tweets per day.
There is no doubt, Twitter’s infrastructure deals with extremely high scale
demands. So, next time you get a Tweet from Katy Perry, remember 39 million
inserts just occurred on Redis.
While there might be 5000 tweets per second on average and peaks up to
12,000, views are actually what keeps the datastore busy. There are over
300,000 queries per second on home timelines and 30,000 on search-based
timelines.
19. Redis @ Pinterest
Things which Redis stores for a Pinterest Profile.
A list of users who you follow
A list of boards (and their related users) who you follow
A list of your followers
A list of people who follow your boards
A list of boards you follow
A list of boards you unfollowed after following a user
The followers and unfollowers of each board
Redis stores the above lists for each of it’s 70 million users—it basically stores the entire
follower graph.