SlideShare a Scribd company logo
1 of 7
Download to read offline
Redis
© EInnovator.org
Redis is a NoSql in-memory data-store that supports
several canonical data-structures as persistent data
model. In its simplest use, Redis works as a key–value
store – allowing values to be set and retrieve by key.
However, values in Redis are typed. In addition to binary
String values (blobs), values can also represent data-
structure types, including: Lists, Hashes (dictionaries),
Sets, Sorted Sets, and HyperLogLog.
A distinguishing feature of Redis is that it loads the full
state of the data-store into memory, thus allowing for very
fast access. Redis uses two different techniques to
persist state – snapshoot files periodically written with the
full state kept in memory, and append-only files (AOF)
with the history of operations. Several techniques can be
used to mitigate the memory footprint of Redis when the
dataset grow beyond the physical memory limits – such
as expiring not so much used key-values, and partitioning
data across multiple Redis nodes connected as a cluster.
Redis also supports for replication of data for increased
fault-tolerance, using (semi-synchronous) master-slave
replication. High-available and automatic failover is
achieved by setting up a small cluster of dedicated
monitoring servers – known as sentinels.
Redis is most popular for its recognized high-
performance, and has a wide range if use cases – from
simple caching, to session state persistence, and as a
main database for applications where in-memory data
management is desirable.
Redis is very straightforward to install. In a typical Linux
installation or MacOS, it builds from the source code out-
of-the-box without requiring any configuration.
Downloading the source, extracting the tar ball, and
running make build tool should be enough. For
WindowsOS, there is a distribution managed by a
Microsoft team, that currently has an MSI installer that
setup Redis as a windows service.
To start Redis manually, both in Linux and WindowsOS,
use command redis-server. By default, Redis listen for
client connection on TCP/IP port 6379. Depending on the
OS, kernel version, and current kernel configuration,
Redis might suggest on startup some OS-level setting for
increased performance.
» Example: Installation Redis (Linux)
wget http://download.redis.io/releases/redis-3.2.0.tar.gz
tar -xzf redis-3.2.0.tar.gz
cd redis-3.2.0
make
make install
» Example: Installation Redis (Linux)
$ redis-server
[12536] 27 May 16:47:10.100 # Warning: no config file specified,
using the default config. In order to specify a config file use
redis-server /path/to/redis.conf
...
[12536] 27 May 16:47:10.124 # Server started, Redis version 3.0.501
[12536] 27 May 16:47:10.383 * DB loaded from disk: 0.258 seconds
[12536] 27 May 16:47:10.383 * The server is now ready to accept
connections on port 6379
Redis can be configured by starting the redis-server with
the path to a configuration file as argument. The
configuration file is a text file with a simple syntax – one
setting per line containing a property name followed by
one or more arguments.
» Example: Starting Redis with Configuration File
redis-server /path/redis.com
» Example: Redis Configuration File
port 5000
A more comprehensive review of the Redis configuration
and deployment options is covered in a companion
EInnovator refcard Redis Administration (#10).
Redis comes with a convenient command-line tool (CLI),
that can be used to execute arbitrary Redis commands.
The CLI support all the commands defined by Redis wire-
level protocol (also text-based).
© EInnovator.org
++QuickGuides»EInnovator.org
9
Software Engineering School
Content
» Redis Install & Configuration
» Redis Common Operations
» Redis Data-Types:
» String, List, Hash, Set, ZSet
» HyperLogLog
» Redis Transactions
» LUA Scripting with Redis
Jorge Simão, Ph.D.
Redis
Data-Types & Operations
Redis Overview
Redis Installation
Configuring Redis
Redis CLI and Tools
© EInnovator.org
Redis
» Example: Starting Redis CLI Interactively
$ redis-cli
» Example: Execute Commands on Redis CLI
> SET mykey somevalue
> GET mykey
> KEYS *
» Example: Getting Help from Redis CLI
redis-cli --help
» Example: Execute Single Command w/ CLI
redis-cli keys "*"
Redis top-level data-model is a collection of key–value pairs,
with the key being a binary string and values one of several
predefined data-structure types (String, List, Hash, Set,
SortedSet, HyperLogLog, Geo). Redis support a wide-range
of operations to manipulate the key–value pairs. Most
operations are specific to each type of value, but some
operations are common to all types. Some operations are
additionally used to administer the Redis server, a sentinel, or
a cluster of servers.
Operations that operate on key–values are used either to
set/modify the value or to get the value, in part or in whole.
When a set/modify operation is applied and the key does not
exist yet, the key–value is created and the type is inferred from
the operation. If the key already exists and the current value is
of a type incompatible with the attempted operation an error
occurs.
The simplest value type is a String, where the values also
binary strings – like the keys. Command SET sets a String
value for a key; command GET gets the value. Commands that
are common to all types of value, include: EXISTS, to check if
a key exist (whatever the type); TYPE, to get the type of value
for a key; DEL, to delete an entry. Once a key is deleted it can
be set later with any other type.
» Example: Basic Operations on Keys
> SET user:1:login "2016-10-1T8:20"
OK
> EXISTS user:1:login
(integer) 1
> TYPE user:1:login
string
> DEL user:1:login
(integer) 1
Redis key-value entries can be automatically removed after a
certain amount of time. This is specially useful when Redis is
being used as a cache. Command (P)EXPIRE sets an
expiration interval in (milli-)seconds on a key, and
(P)EXPIREAT defines a the date-time when the expiration will
happen (Unix epoch time).
» Example: Set Expire Time on Key
> SET bid:101 "120,EUR"
> EXPIRE bid:101 10
> GET bid:101
"120,EUR"
> TTL bid:101
"7"
> GET bid:101
(nil)
It is also possible to a Redis server with multiple logical
independent databases within the same server, each identified
by an integer index. Command SELECT changes the current
database (default is 0). Command MOVE moves a key–value
to another database. The number of available databases is
defined in the property databases of the REDIS configuration
file.
Table below summarizes the Redis operations that are
common to all data-types.
Common Ops Description
KEYS Get value for key matching pattern
SCAN Scan keys matching pattern
EXISTS Check if key exists
TYPE Get type of key value
RENAME Rename key
RENAMENX Rename key (if target does not exists)
DEL Delete Key
RANDOMKEY Get random key
MOVE Move key across databases
SORT Sort values
EXPIRE, PEXPIRE Expire key in (mili-)seconds
EXPIREAT.
PEXPIREAT
Expire key at time (mili-)seconds (Unix epoch)
TTL, PTTL Get time before expiration in (milli-)seconds
PERSIST Cancel expiration time
MOVE Move key–value to another database
A common use for String values is to store serialized objects.
But it can also be used in several other ways, beyond full state
Software Engineering School
2
Generic Redis Operations
Operations on Strings
© EInnovator.org
Redis
write/read with operations SET/GET (or MSET/MGET to
handle multiple keys at the same time). Method APPEND
concatenates a String value to the tail of an existing String
value (e.g. to append the text entry of a growing log). An
alternative approach to String appending, is to use the List
data-type (see below). Method STRLEN returns the current
length of the String. When values are numbers represented
as text, operations INCR(BY)(FLOAT) can be used to
atomically increment (or decrement) a value in an atomic way.
String values can also be used as bit arrays. Method
SETBIT/GETBIT sets/get the value of a bit with specified
index. Other operation exists also for bit inspection and
manipulation. Bit-wise logical operations are supported with
command BITOP. Since Redis 3.2, a String can also be used
as arrays of integers with operation BITFIELD.
» Example: Appending Log Entries to a Redis String
> APPEND log "[INFO] 2016-1-10: GET /n"
> APPEND log "[INFO] 2016-1-10: GET /refcard/redisn"
$ redis-cli GET log > locahost.log
Table below summarizes the Redis operations for Strings.
String Operation Description
GET Get value of entry by key
SET Set key-value entry
SETNX Set key-value (if key does not exit)
SETEX Set key-value (if key already exits)
GETSET Set value and return previous value
MGET Get values of multiple keys
MSET Set multiple key-value entries
MSETNX Set multiple key-value entries (if keys do not
exit)
SETEX,PSETEX Set value and expiration of key atomically
INCR, INCRBY Increment value (numeric)
INCRBYFLOAT Increment value (numeric)
APPEND Append string to tail
DECR, DECRBY Decrement value (numeric)
STRLEN Get string length
GETRANGE Get sub-string by start-end index
SETRANGE Set sub-string by start-end index
GETBIT Get bit
SETBIT Set bit
BITPOS Get position of first bit set to 1 or 0
BITCOUNT Count number of bit with value 1
BITOP Apply logical operation between two bit string
BITFIELD Get integer value at specified position in string
Lists are ordered collections of elements. They are useful as a
generic collection where the getting elements by order of
insertion is enough or where ordering might not be crucial (e.g.
when modelling entity associations). Redis support operations
to inspect & manipulate from both sides of a List – left and
right. Method LPUSH append an element to the left side the
list, while method RPUSH appends an element to the right side
of the List. The element values are always binary Strings –
i.e. Redis does not support nested data-structures. Method
LPOP/RPOP retrieves and removes the left/right most element
of the List (sometimes called head/tail, by convention).
Operation LRANGE retrieves a sub-set of a List by specifying
first and last index. Indexes start at 0. Negative indexes specify
positions counting from end/right of the List. Internally, a List
is represented as double-linked list plus two pointer for the
head and tail. So random access operations in a List (e.g.
Indexing a single element or pagination) are not very efficient
(O(N) complexity).
» Example: Pushing/Retreive Elements into/from List
> LPUSH user:101:msgs "Hi!"
> LPUSH user:101:msgs "What up!?" "Going out?!"
> LRANGE album:100:msgs 0 -1
Lists can also be used as work queues by using blocking
operations BLPOP and BRPOP. If the List is empty these
operations block the client, till some other client makes a
(R/L)PUSH on the List. Table below summarizes the Redis
operations for Lists.
List Operation Description (Default)
LPUSH Left push element in List
LPUSHX Left push element in List
LPOP Left pop element from List
LLEN Length of List
LSET Set/modify element in List index
LINDEX Get index of element
LRANGE Get sub-list in index range
LREM Remove element from List
LTRIM Truncate List to size
LINSERT Insert element in List
Software Engineering School
3
Operations on Lists
© EInnovator.org
Redis
RPUSH Right push element in List
RPUSHX Right push element in List
RPOP Right pop element from List
BRPOP Pop element and block if empty
RPOPLPUSH Pop from list and push in another list
BRPOPLPUSH Pop from list and push in another list
BLPOP Pop element and block if empty
An Hash typed value is a key-value dictionary that supports
binary strings as entry value (i.e. no nesting is allowed, like in a
List). Hashes are useful to represent objects as a collection of
properties without requiring binary serialization or marshalling
to be done to encode the object (as would be the case for a
String value), or as a generic mapping data-structure.
Internally, a Redis Hash is implemented as a closed
HashTable which doubles the capacity when the fraction of
used entries exceeds a threshold. Redis also maintains two
tables (current and previous) to prevent copy delays on
insertion.
Keys inside Hashes are sometimes called fields. Fields can be
identified by name and get/set/modified individually or multiple
at the same time. Operation HMSET is used to set the value of
several Hash fields. Operation HGET get the value of a field,
and HGETALL the value of all fields.
» Example: Storing Entity Properties as Hash Fields
> HMSET user:1000 username adam birthyear 2001 sex male
» Example: Retrieve Entity Properties from Hash
> HKEYS user:1000
1) "username"
2) "birthyear"
3) "sex"
> HGET user:1000 username
"adam"
> HGETALL user:1000
1) "username"
2) "adam"
3) "birthyear"
4) "2001"
5) "sex"
6) "male"
Table below summarizes the Redis operations for Hashes.
Hash Operation Description
HKEYS Get keys of Hash entries
HVALS Get values of Hash entries
HSET Set key-value as Hash entry
HSETNX Set value in Hash entry (if not exits)
HMSET Set multiple key-value as Hash entries
HEXISTS Check if Hash entry exist for key
HLEN Get size of Hash (number of entries)
HDEL Delete entry from Hash with key
HGET Get value from Hash entry with key
HMGET Get values of multiples Hash entries
HGETALL Get all key-value in Hash
HINCRBY Increment value of hash entry (numbers)
HINCRBYFLOAT Increment value of hash entry (numbers)
HSTRLEN Length (in bytes) of hash entry
HSCAN Scan hash entries
A Set is an unordered collection of elements without repetition.
Internally, a Set is implemented similarly to an Hash with an
additional element count integer field for efficiency. Operation
SADD adds one or more elements (binary String values) to
the Set. Operation SISMEMBER checks for membership, and
operation SMEMBERS returns all elements. Redis also
supports algebraic operations between two sets, such as:
union (SUNION), interception (SINTER), and difference
(SDIFF). The result Set is returned, or stored as another Set if
using operations S{UNION|INTER|DIFF}STORE.
» Example: Adding/Retrieve Element to/from Set
> SADD users adam01 eva99 joe11 jack5 eva99
4
> SMEMBERS
1) "jack5"
2) "joe111"
3) "eva99"
4) "james007"
5) "adam01"
> SISMEMBER users eva99
(integer) 1
» Example: Set Algebraic Operations
> SADD user:eva99:friends adrian2 jack5 james007
> SADD user:adam01:friends joe111 james007
> SUNION user:eva99:friends user:adam01:friends
1) "james007"
2) "joe111"
3) "adrian2"
Software Engineering School
4
Operations on Hashes
Operations on Sets
-
© EInnovator.org
Redis
4) "jack5"
Table below summarizes the Redis operations for Set.
Set Operation Description
SADD Add element to Set (if non existing)
SPOP Remove element from Set
SMOVE Move element from one Set to another
SREM Remove elements from Set (?)
SISMEMBER Check if element is member of Set
SMEMBERS Get elements in Set
SRANDMEMBER Get a random member from Set
SCARD Get cardinality of Set (number of elements)
SSCAN Scan elements in Set
SUNION Return union of two Sets
SUNIONSTORE Make union and store in named Set
SINTER Return interception of two Sets
SINTERSTORE Make interception and store in named Set
SDIFF Return difference of two Sets
SDIFFSTORE Make difference and store in named Set
A Sorted Set, or ZSet, is an ordered collection without
repetitions where the ordering is determined by a double
numeric value (the Z score) specified when the element is
added. ZSets as useful for a variety of purposes, such as
creating ordered indexes. Internally, a ZSet is represented as a
ordered skip list which guarantees O(log(N)) complexity for
most operations.
Operation ZRANGE retrieves the elements of the ZSet with a
rank order within a range, ordered by score. Operation
ZRANGEBYSCORE retrieves the elements of the ZSet with a
score within a range, ordered by score. Option WITHSCORES
retrieves the scores in addition to the values. For the special
case where all elements of the ZSet have the same score,
operation ZRANGEBYLEX retrieves the elements by
lexicographical (alphabetical) order.
» Example: Adding & Retrieving Elements on/from ZSet
> ZADD users:reputation 1346 adam1
> ZADD users:reputation 3452 eva33
...
ZRANGE users:reputation 0 -1
1)"eva33"
2)"adam1"
...
» Example: Retreiving Element Within Score Range
> ZRANGEBYSCORE users:reputation 2000 +INF WITHSCORES
1) "eva33"
2) "43452"
Table below summarizes the Redis operations for ZSet.
ZSet Operation Description
ZADD Add element (with score) to ZSet (if non
existing)
ZREM Remove element from ZSet
ZCARD Get cardinality (total number of elements)
ZCOUNT Get count of element with score in range
ZLEXCOUNT Get count of elements in lexicographical range
ZSCORE Get score of element
ZRANK Get rank (order) of element
ZREVRANK Get rank of element in reverse ordering
ZINCRBY Increments score of element by a value
ZSCAN Scan elements
ZRANGE Get elements with rank in a range
ZRANGEBYSCORE Get elements ordered lexicographically
ZRANGEBYLEX Get elements with score in a range
ZREVRANGE Get elements with rank in a range reversed
ZREVRANGEBYSCORE Get elements lexicographically reversed
ZREVRANGEBYLEX Get elements with score in a range reversed
ZREMRANGEBYRANK Remove elements with rank in a range
ZREMRANGEBYSCORE Remove elements ordered lexicographically
ZREMRANGEBYLEX Remove elements with score in a range
ZUNIONSTORE Compute&store the union of two ZSet
ZINTERSTORE Compute&store the interception of two ZSet
An HyperLogLog is a (probabilistic) data-structure, invented
by Philippe Flajolet, for counting the approximated number of
unique elements in the set without storing the actual elements.
Redis supports HyperLogLog due to its high-performance
constant-time approach for element counting. Command
PFADD add an element to a named HyperLogLog. And
command PFCOUNT returns the counting estimate.
» Example: Adding & Counting w/ HyperLogLog
> PFADD ips 192.168.255.251
Software Engineering School
5
Operations on Sorted Sets
Operations on HyperLogLogs
© EInnovator.org
Redis
> PFADD ips 192.168.255.221
...
> PFCOUNT hll
(integer) 3218
Table below summarizes the Redis operations for
HyperLogLog.
HyperLogLog Operation Description
PFADD Add value to HyperLogLog
PFCOUNT (Estimate) Cardinality
PFMERGE Merge two HyperLogLog
In addition to data-structure storage&retrieval, Redis can also
be used as a simple messaging-middleware. Operation
SUBSCRIBE is used by a client that wants to subscribe to one
(or more) named channels. Operation PSUBSCRIBE is similar,
but a channel named pattern is specified. The client (thread)
making this operation should not send additional commands.
The sent messages are delivered as a stream through the
connection channel. To send messages to named channel
clients should issue command PUBLISH.
» Example: Subscribing to Channels
> SUBSCRIBE channel:1 channel:2
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel:1"
3) (integer) 1
1) "subscribe"
2) "channel:2"
3) (integer) 2
» Example: Subscribing Channels Matching Pattern
> PSUBSCRIBE channel:*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "channel:*"
3) (integer) 1
» Example: Publishing Message to Channel
> PUBLISH channel1 "Hello Redis!"
1) "message"
2) "channel1"
3) "Hello Redis!"
The message publishing/dissemination mechanism is also
used internally by Redis to notify clients of relevant events
(e.g. a key just expired, deleted, or modified). Clients can get
this notifications by subscribing to the relevant channel name
(e.g. deleting key xyz makes Redis send a notification
message to channels __keyspace@0__:xyz del, and
__keyevent@0__:del xyz).
Table below summarizes the Redis PubSub commands.
PubSub Command Description
SUBSCRIBE Subscribe to named channel(s)
PSUBSCRIBE Subscribe to matching channels
PUBLISH Publish/Send message to channel
UNSUBSCRIBE Unsubscribe to named channel(s)
PUNSUBSCRIBE Unsubscribe to matching channels
Redis supports a simple form of transactional semantics
where operations are buffered before execution until the
transaction completes. Command MULTI sets a client
connection in transactional mode. Command EXEC is used
mark the completion of the transaction with success, and
trigger the execution of buffered commands. All commands in a
completed transaction are guaranteed to be applied in an
isolated (no race conditions) and (quasi-)atomic way.
Command DISCARD is used to cancel all commands buffered
so far (i.e. they are nor executed). Redis does not support
transaction rollback in the traditional sense – if an operation
execution fails during the EXEC, the previous operation might
already have made some changes in some keys.
Redis also support a simple form of optimistic concurrency
control using command WATCH. Any (concurrent) modification
done in the specified key while the transaction is being
executed is detected and forces the EXEC to fail before the
first operation is applied.
» Example: Running Multiple Commans in Transaction
> MULTI
OK
> INCR foo
QUEUED
> INCR bar
QUEUED
> EXEC
1) (integer) 1
2) (integer) 1
Table below summarizes the Redis commands for transaction
management and concurrency control.
Tx Operation Description
MULTI Mark transaction start
Software Engineering School
6
Redis Transactions
Redis PubSub (Messaging)
Redis
EXEC Mark transaction completion (commit)
DISCARD Mark transaction abort (discard)
WATCH Check for concurrent updates on key
Complex operations involving multiple/repetitive access to one
or more keys can be optimized by running them as Lua
Scripts inside the Redis server. Lua function redis.call() is
used to interface with Redis and invoke any desired
command. Command EVAL is used to evaluate a Lua script.
To avoid having to transfer a (possible long) script to the server
often, Redis maintains a script cache. A SHA digest is used to
unequivocally identify a script. Command SCRIPT LOAD is
used to load a script in the cache. Command EVALSHA
executes the script identified by a SHA digest.
» Example: Evaluating a Lua Script
> EVAL "return {KEYS[1],KEYS[2],ARGV[1]}" 2 key1 key2 abc
1) "key1"
2) "key2"
3) "abc"
» Example: Loading a Lua Script & Get SHA
SCRIPT LOAD "return redis.call('get','product:ids')"
> EVALSHA 6b1bf486c81ceb7edf3c093f4c48582e38c0e791 0
Table below summarizes the Redis commands for scripting.
Script Operation Description
EVAL Execute Script and return result
EVALSHA Execute cached Script by SHA
SCRIPT EXIST Check if Script with SHA exists
SCRIPT LOAD Load Scritpt from file
SCRIPT FLUSH Remove Script from cache
SCRIPT KILL Terminate script
• Redis Project home page: http://redis.io/
• Redis sample configuration file:
https://raw.githubusercontent.com/antirez/redis/3.2/redis.conf
About the Author
Jorge Simão is a software engineer and IT Trainer, with
two decades long experience on education delivery both in
academia and industry. Expert in a wide range of
computing topics, he his an author, trainer, and director
(Education & Consulting) at EInnovator. He holds a B.Sc.,
M.Sc., and Ph.D. in Computer Science and Engineering.
Redis Training
Redis Administration course teaches how to install,
manage, and configure Redis. Covers topics such as Redis
master-slave replication, high-availability, and Redis cluster.
Book for a training event in a date&location of your choice:
www.einnovator.org/course/redis-administration
Redis Development course teaches how build applications
that use Redis, with extensive coverage of Spring Data
Redis (Java track) & C# drivers (.Net track).
Book for a training event in a date&location of your choice:
www.einnovator.org/course/redis-development
++ QuickGuides » EInnovator.org
» Spring Container, Spring MVC, Spring WebFlow
» RabbitMQ, Spring XD
» Cloud Foundry, Spring Cloud
» and much more...
++ Courses » EInnovator.org
» Core Spring, Spring Web, Enterprise Spring
» RabbitMQ, CloudFoundry
» BigData and Hadoop, Spring XD, Spark
» and much more...
EInnovator – Software Engineering School
EInnovator.org offers the best Software Engineering resources and education, in partnership with the most
innovative companies in the IT-world. Focusing on both foundational and cutting-edge technologies and
topics, at EInnovator software engineers and data-scientists can get all the skills needed to become top-of-
the-line on state-of-the-art IT professionals.
Training – Bookings & Inquiries
training@einnovator.org
Consultancy – Partnerships & Inquiries
consulting@einnovator.org
General Info
info@einnovator.org
Software Engineering School
Copyright © 2014 EInnovator.org. All rights reserved.
7
Redis Lua Scripting
Resources
Contacts

More Related Content

What's hot (20)

StackExchange.redis
StackExchange.redisStackExchange.redis
StackExchange.redis
 
Unqlite
UnqliteUnqlite
Unqlite
 
Redis Use Patterns (DevconTLV June 2014)
Redis Use Patterns (DevconTLV June 2014)Redis Use Patterns (DevconTLV June 2014)
Redis Use Patterns (DevconTLV June 2014)
 
Hadoop
HadoopHadoop
Hadoop
 
Kicking ass with redis
Kicking ass with redisKicking ass with redis
Kicking ass with redis
 
Introduction to-linux
Introduction to-linuxIntroduction to-linux
Introduction to-linux
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
 
Registry
RegistryRegistry
Registry
 
Redis and it's data types
Redis and it's data typesRedis and it's data types
Redis and it's data types
 
Dsfdssdfsdf
DsfdssdfsdfDsfdssdfsdf
Dsfdssdfsdf
 
Redis学习笔记
Redis学习笔记Redis学习笔记
Redis学习笔记
 
Redis 101
Redis 101Redis 101
Redis 101
 
Perl for System Automation - 01 Advanced File Processing
Perl for System Automation - 01 Advanced File ProcessingPerl for System Automation - 01 Advanced File Processing
Perl for System Automation - 01 Advanced File Processing
 
Introduction to redis
Introduction to redisIntroduction to redis
Introduction to redis
 
Paris Redis Meetup Introduction
Paris Redis Meetup IntroductionParis Redis Meetup Introduction
Paris Redis Meetup Introduction
 
Perl Programming - 03 Programming File
Perl Programming - 03 Programming FilePerl Programming - 03 Programming File
Perl Programming - 03 Programming File
 
Scaling php applications with redis
Scaling php applications with redisScaling php applications with redis
Scaling php applications with redis
 
2015 bioinformatics bio_python
2015 bioinformatics bio_python2015 bioinformatics bio_python
2015 bioinformatics bio_python
 
Elastic 101 tutorial - Percona Europe 2018
Elastic 101 tutorial - Percona Europe 2018 Elastic 101 tutorial - Percona Europe 2018
Elastic 101 tutorial - Percona Europe 2018
 
Linux administration training
Linux administration trainingLinux administration training
Linux administration training
 

Similar to quickguide-einnovator-9-redis

Redis Installation Configuration And Implementation
Redis Installation Configuration And ImplementationRedis Installation Configuration And Implementation
Redis Installation Configuration And ImplementationAbhijeet Shekhar
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to RedisItamar Haber
 
Redispresentation apac2012
Redispresentation apac2012Redispresentation apac2012
Redispresentation apac2012Ankur Gupta
 
Extend Redis with Modules
Extend Redis with ModulesExtend Redis with Modules
Extend Redis with ModulesItamar Haber
 
Tamir Dresher - DotNet 7 What's new.pptx
Tamir Dresher - DotNet 7 What's new.pptxTamir Dresher - DotNet 7 What's new.pptx
Tamir Dresher - DotNet 7 What's new.pptxTamir Dresher
 
Fun with Ruby and Redis
Fun with Ruby and RedisFun with Ruby and Redis
Fun with Ruby and Redisjavier ramirez
 
Redis学习笔记
Redis学习笔记Redis学习笔记
Redis学习笔记锐 张
 
What's New for Developers in SQL Server 2008?
What's New for Developers in SQL Server 2008?What's New for Developers in SQL Server 2008?
What's New for Developers in SQL Server 2008?ukdpe
 
SQL Server 2008 Overview
SQL Server 2008 OverviewSQL Server 2008 Overview
SQL Server 2008 OverviewEric Nelson
 
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)Serban Tanasa
 
Spring data ii
Spring data iiSpring data ii
Spring data ii명철 강
 
Developing a Redis Module - Hackathon Kickoff
 Developing a Redis Module - Hackathon Kickoff Developing a Redis Module - Hackathon Kickoff
Developing a Redis Module - Hackathon KickoffItamar Haber
 
Ekon bestof rtl_delphi
Ekon bestof rtl_delphiEkon bestof rtl_delphi
Ekon bestof rtl_delphiMax Kleiner
 
Postgres Vienna DB Meetup 2014
Postgres Vienna DB Meetup 2014Postgres Vienna DB Meetup 2014
Postgres Vienna DB Meetup 2014Michael Renner
 
Tuga IT 2017 - Redis
Tuga IT 2017 - RedisTuga IT 2017 - Redis
Tuga IT 2017 - RedisNuno Caneco
 

Similar to quickguide-einnovator-9-redis (20)

Redis Installation Configuration And Implementation
Redis Installation Configuration And ImplementationRedis Installation Configuration And Implementation
Redis Installation Configuration And Implementation
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
 
Redispresentation apac2012
Redispresentation apac2012Redispresentation apac2012
Redispresentation apac2012
 
Extend Redis with Modules
Extend Redis with ModulesExtend Redis with Modules
Extend Redis with Modules
 
Tamir Dresher - DotNet 7 What's new.pptx
Tamir Dresher - DotNet 7 What's new.pptxTamir Dresher - DotNet 7 What's new.pptx
Tamir Dresher - DotNet 7 What's new.pptx
 
Redis basics
Redis basicsRedis basics
Redis basics
 
Fun with Ruby and Redis
Fun with Ruby and RedisFun with Ruby and Redis
Fun with Ruby and Redis
 
Redis学习笔记
Redis学习笔记Redis学习笔记
Redis学习笔记
 
What's New for Developers in SQL Server 2008?
What's New for Developers in SQL Server 2008?What's New for Developers in SQL Server 2008?
What's New for Developers in SQL Server 2008?
 
SQL Server 2008 Overview
SQL Server 2008 OverviewSQL Server 2008 Overview
SQL Server 2008 Overview
 
User biglm
User biglmUser biglm
User biglm
 
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
 
Redis
RedisRedis
Redis
 
Spring data ii
Spring data iiSpring data ii
Spring data ii
 
Developing a Redis Module - Hackathon Kickoff
 Developing a Redis Module - Hackathon Kickoff Developing a Redis Module - Hackathon Kickoff
Developing a Redis Module - Hackathon Kickoff
 
Ekon bestof rtl_delphi
Ekon bestof rtl_delphiEkon bestof rtl_delphi
Ekon bestof rtl_delphi
 
Postgres Vienna DB Meetup 2014
Postgres Vienna DB Meetup 2014Postgres Vienna DB Meetup 2014
Postgres Vienna DB Meetup 2014
 
Redis database
Redis databaseRedis database
Redis database
 
Windows PowerShell
Windows PowerShellWindows PowerShell
Windows PowerShell
 
Tuga IT 2017 - Redis
Tuga IT 2017 - RedisTuga IT 2017 - Redis
Tuga IT 2017 - Redis
 

More from jorgesimao71

quickguide-einnovator-8-spring-cloud
quickguide-einnovator-8-spring-cloudquickguide-einnovator-8-spring-cloud
quickguide-einnovator-8-spring-cloudjorgesimao71
 
quickguide-einnovator-5-springxd
quickguide-einnovator-5-springxdquickguide-einnovator-5-springxd
quickguide-einnovator-5-springxdjorgesimao71
 
quickguide-einnovator-2-spring4-dependency-injection-annotations
quickguide-einnovator-2-spring4-dependency-injection-annotationsquickguide-einnovator-2-spring4-dependency-injection-annotations
quickguide-einnovator-2-spring4-dependency-injection-annotationsjorgesimao71
 
quickguide-einnovator-7-spring-mvc
quickguide-einnovator-7-spring-mvcquickguide-einnovator-7-spring-mvc
quickguide-einnovator-7-spring-mvcjorgesimao71
 
quickguide-einnovator-3-rabbitmq
quickguide-einnovator-3-rabbitmqquickguide-einnovator-3-rabbitmq
quickguide-einnovator-3-rabbitmqjorgesimao71
 
quickguide-einnovator-10-redis-admin
quickguide-einnovator-10-redis-adminquickguide-einnovator-10-redis-admin
quickguide-einnovator-10-redis-adminjorgesimao71
 
quickguide-einnovator-11-spring-integration
quickguide-einnovator-11-spring-integrationquickguide-einnovator-11-spring-integration
quickguide-einnovator-11-spring-integrationjorgesimao71
 
quickguide-einnovator-4-cloudfoundry
quickguide-einnovator-4-cloudfoundryquickguide-einnovator-4-cloudfoundry
quickguide-einnovator-4-cloudfoundryjorgesimao71
 

More from jorgesimao71 (8)

quickguide-einnovator-8-spring-cloud
quickguide-einnovator-8-spring-cloudquickguide-einnovator-8-spring-cloud
quickguide-einnovator-8-spring-cloud
 
quickguide-einnovator-5-springxd
quickguide-einnovator-5-springxdquickguide-einnovator-5-springxd
quickguide-einnovator-5-springxd
 
quickguide-einnovator-2-spring4-dependency-injection-annotations
quickguide-einnovator-2-spring4-dependency-injection-annotationsquickguide-einnovator-2-spring4-dependency-injection-annotations
quickguide-einnovator-2-spring4-dependency-injection-annotations
 
quickguide-einnovator-7-spring-mvc
quickguide-einnovator-7-spring-mvcquickguide-einnovator-7-spring-mvc
quickguide-einnovator-7-spring-mvc
 
quickguide-einnovator-3-rabbitmq
quickguide-einnovator-3-rabbitmqquickguide-einnovator-3-rabbitmq
quickguide-einnovator-3-rabbitmq
 
quickguide-einnovator-10-redis-admin
quickguide-einnovator-10-redis-adminquickguide-einnovator-10-redis-admin
quickguide-einnovator-10-redis-admin
 
quickguide-einnovator-11-spring-integration
quickguide-einnovator-11-spring-integrationquickguide-einnovator-11-spring-integration
quickguide-einnovator-11-spring-integration
 
quickguide-einnovator-4-cloudfoundry
quickguide-einnovator-4-cloudfoundryquickguide-einnovator-4-cloudfoundry
quickguide-einnovator-4-cloudfoundry
 

quickguide-einnovator-9-redis

  • 1. Redis © EInnovator.org Redis is a NoSql in-memory data-store that supports several canonical data-structures as persistent data model. In its simplest use, Redis works as a key–value store – allowing values to be set and retrieve by key. However, values in Redis are typed. In addition to binary String values (blobs), values can also represent data- structure types, including: Lists, Hashes (dictionaries), Sets, Sorted Sets, and HyperLogLog. A distinguishing feature of Redis is that it loads the full state of the data-store into memory, thus allowing for very fast access. Redis uses two different techniques to persist state – snapshoot files periodically written with the full state kept in memory, and append-only files (AOF) with the history of operations. Several techniques can be used to mitigate the memory footprint of Redis when the dataset grow beyond the physical memory limits – such as expiring not so much used key-values, and partitioning data across multiple Redis nodes connected as a cluster. Redis also supports for replication of data for increased fault-tolerance, using (semi-synchronous) master-slave replication. High-available and automatic failover is achieved by setting up a small cluster of dedicated monitoring servers – known as sentinels. Redis is most popular for its recognized high- performance, and has a wide range if use cases – from simple caching, to session state persistence, and as a main database for applications where in-memory data management is desirable. Redis is very straightforward to install. In a typical Linux installation or MacOS, it builds from the source code out- of-the-box without requiring any configuration. Downloading the source, extracting the tar ball, and running make build tool should be enough. For WindowsOS, there is a distribution managed by a Microsoft team, that currently has an MSI installer that setup Redis as a windows service. To start Redis manually, both in Linux and WindowsOS, use command redis-server. By default, Redis listen for client connection on TCP/IP port 6379. Depending on the OS, kernel version, and current kernel configuration, Redis might suggest on startup some OS-level setting for increased performance. » Example: Installation Redis (Linux) wget http://download.redis.io/releases/redis-3.2.0.tar.gz tar -xzf redis-3.2.0.tar.gz cd redis-3.2.0 make make install » Example: Installation Redis (Linux) $ redis-server [12536] 27 May 16:47:10.100 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf ... [12536] 27 May 16:47:10.124 # Server started, Redis version 3.0.501 [12536] 27 May 16:47:10.383 * DB loaded from disk: 0.258 seconds [12536] 27 May 16:47:10.383 * The server is now ready to accept connections on port 6379 Redis can be configured by starting the redis-server with the path to a configuration file as argument. The configuration file is a text file with a simple syntax – one setting per line containing a property name followed by one or more arguments. » Example: Starting Redis with Configuration File redis-server /path/redis.com » Example: Redis Configuration File port 5000 A more comprehensive review of the Redis configuration and deployment options is covered in a companion EInnovator refcard Redis Administration (#10). Redis comes with a convenient command-line tool (CLI), that can be used to execute arbitrary Redis commands. The CLI support all the commands defined by Redis wire- level protocol (also text-based). © EInnovator.org ++QuickGuides»EInnovator.org 9 Software Engineering School Content » Redis Install & Configuration » Redis Common Operations » Redis Data-Types: » String, List, Hash, Set, ZSet » HyperLogLog » Redis Transactions » LUA Scripting with Redis Jorge Simão, Ph.D. Redis Data-Types & Operations Redis Overview Redis Installation Configuring Redis Redis CLI and Tools
  • 2. © EInnovator.org Redis » Example: Starting Redis CLI Interactively $ redis-cli » Example: Execute Commands on Redis CLI > SET mykey somevalue > GET mykey > KEYS * » Example: Getting Help from Redis CLI redis-cli --help » Example: Execute Single Command w/ CLI redis-cli keys "*" Redis top-level data-model is a collection of key–value pairs, with the key being a binary string and values one of several predefined data-structure types (String, List, Hash, Set, SortedSet, HyperLogLog, Geo). Redis support a wide-range of operations to manipulate the key–value pairs. Most operations are specific to each type of value, but some operations are common to all types. Some operations are additionally used to administer the Redis server, a sentinel, or a cluster of servers. Operations that operate on key–values are used either to set/modify the value or to get the value, in part or in whole. When a set/modify operation is applied and the key does not exist yet, the key–value is created and the type is inferred from the operation. If the key already exists and the current value is of a type incompatible with the attempted operation an error occurs. The simplest value type is a String, where the values also binary strings – like the keys. Command SET sets a String value for a key; command GET gets the value. Commands that are common to all types of value, include: EXISTS, to check if a key exist (whatever the type); TYPE, to get the type of value for a key; DEL, to delete an entry. Once a key is deleted it can be set later with any other type. » Example: Basic Operations on Keys > SET user:1:login "2016-10-1T8:20" OK > EXISTS user:1:login (integer) 1 > TYPE user:1:login string > DEL user:1:login (integer) 1 Redis key-value entries can be automatically removed after a certain amount of time. This is specially useful when Redis is being used as a cache. Command (P)EXPIRE sets an expiration interval in (milli-)seconds on a key, and (P)EXPIREAT defines a the date-time when the expiration will happen (Unix epoch time). » Example: Set Expire Time on Key > SET bid:101 "120,EUR" > EXPIRE bid:101 10 > GET bid:101 "120,EUR" > TTL bid:101 "7" > GET bid:101 (nil) It is also possible to a Redis server with multiple logical independent databases within the same server, each identified by an integer index. Command SELECT changes the current database (default is 0). Command MOVE moves a key–value to another database. The number of available databases is defined in the property databases of the REDIS configuration file. Table below summarizes the Redis operations that are common to all data-types. Common Ops Description KEYS Get value for key matching pattern SCAN Scan keys matching pattern EXISTS Check if key exists TYPE Get type of key value RENAME Rename key RENAMENX Rename key (if target does not exists) DEL Delete Key RANDOMKEY Get random key MOVE Move key across databases SORT Sort values EXPIRE, PEXPIRE Expire key in (mili-)seconds EXPIREAT. PEXPIREAT Expire key at time (mili-)seconds (Unix epoch) TTL, PTTL Get time before expiration in (milli-)seconds PERSIST Cancel expiration time MOVE Move key–value to another database A common use for String values is to store serialized objects. But it can also be used in several other ways, beyond full state Software Engineering School 2 Generic Redis Operations Operations on Strings
  • 3. © EInnovator.org Redis write/read with operations SET/GET (or MSET/MGET to handle multiple keys at the same time). Method APPEND concatenates a String value to the tail of an existing String value (e.g. to append the text entry of a growing log). An alternative approach to String appending, is to use the List data-type (see below). Method STRLEN returns the current length of the String. When values are numbers represented as text, operations INCR(BY)(FLOAT) can be used to atomically increment (or decrement) a value in an atomic way. String values can also be used as bit arrays. Method SETBIT/GETBIT sets/get the value of a bit with specified index. Other operation exists also for bit inspection and manipulation. Bit-wise logical operations are supported with command BITOP. Since Redis 3.2, a String can also be used as arrays of integers with operation BITFIELD. » Example: Appending Log Entries to a Redis String > APPEND log "[INFO] 2016-1-10: GET /n" > APPEND log "[INFO] 2016-1-10: GET /refcard/redisn" $ redis-cli GET log > locahost.log Table below summarizes the Redis operations for Strings. String Operation Description GET Get value of entry by key SET Set key-value entry SETNX Set key-value (if key does not exit) SETEX Set key-value (if key already exits) GETSET Set value and return previous value MGET Get values of multiple keys MSET Set multiple key-value entries MSETNX Set multiple key-value entries (if keys do not exit) SETEX,PSETEX Set value and expiration of key atomically INCR, INCRBY Increment value (numeric) INCRBYFLOAT Increment value (numeric) APPEND Append string to tail DECR, DECRBY Decrement value (numeric) STRLEN Get string length GETRANGE Get sub-string by start-end index SETRANGE Set sub-string by start-end index GETBIT Get bit SETBIT Set bit BITPOS Get position of first bit set to 1 or 0 BITCOUNT Count number of bit with value 1 BITOP Apply logical operation between two bit string BITFIELD Get integer value at specified position in string Lists are ordered collections of elements. They are useful as a generic collection where the getting elements by order of insertion is enough or where ordering might not be crucial (e.g. when modelling entity associations). Redis support operations to inspect & manipulate from both sides of a List – left and right. Method LPUSH append an element to the left side the list, while method RPUSH appends an element to the right side of the List. The element values are always binary Strings – i.e. Redis does not support nested data-structures. Method LPOP/RPOP retrieves and removes the left/right most element of the List (sometimes called head/tail, by convention). Operation LRANGE retrieves a sub-set of a List by specifying first and last index. Indexes start at 0. Negative indexes specify positions counting from end/right of the List. Internally, a List is represented as double-linked list plus two pointer for the head and tail. So random access operations in a List (e.g. Indexing a single element or pagination) are not very efficient (O(N) complexity). » Example: Pushing/Retreive Elements into/from List > LPUSH user:101:msgs "Hi!" > LPUSH user:101:msgs "What up!?" "Going out?!" > LRANGE album:100:msgs 0 -1 Lists can also be used as work queues by using blocking operations BLPOP and BRPOP. If the List is empty these operations block the client, till some other client makes a (R/L)PUSH on the List. Table below summarizes the Redis operations for Lists. List Operation Description (Default) LPUSH Left push element in List LPUSHX Left push element in List LPOP Left pop element from List LLEN Length of List LSET Set/modify element in List index LINDEX Get index of element LRANGE Get sub-list in index range LREM Remove element from List LTRIM Truncate List to size LINSERT Insert element in List Software Engineering School 3 Operations on Lists
  • 4. © EInnovator.org Redis RPUSH Right push element in List RPUSHX Right push element in List RPOP Right pop element from List BRPOP Pop element and block if empty RPOPLPUSH Pop from list and push in another list BRPOPLPUSH Pop from list and push in another list BLPOP Pop element and block if empty An Hash typed value is a key-value dictionary that supports binary strings as entry value (i.e. no nesting is allowed, like in a List). Hashes are useful to represent objects as a collection of properties without requiring binary serialization or marshalling to be done to encode the object (as would be the case for a String value), or as a generic mapping data-structure. Internally, a Redis Hash is implemented as a closed HashTable which doubles the capacity when the fraction of used entries exceeds a threshold. Redis also maintains two tables (current and previous) to prevent copy delays on insertion. Keys inside Hashes are sometimes called fields. Fields can be identified by name and get/set/modified individually or multiple at the same time. Operation HMSET is used to set the value of several Hash fields. Operation HGET get the value of a field, and HGETALL the value of all fields. » Example: Storing Entity Properties as Hash Fields > HMSET user:1000 username adam birthyear 2001 sex male » Example: Retrieve Entity Properties from Hash > HKEYS user:1000 1) "username" 2) "birthyear" 3) "sex" > HGET user:1000 username "adam" > HGETALL user:1000 1) "username" 2) "adam" 3) "birthyear" 4) "2001" 5) "sex" 6) "male" Table below summarizes the Redis operations for Hashes. Hash Operation Description HKEYS Get keys of Hash entries HVALS Get values of Hash entries HSET Set key-value as Hash entry HSETNX Set value in Hash entry (if not exits) HMSET Set multiple key-value as Hash entries HEXISTS Check if Hash entry exist for key HLEN Get size of Hash (number of entries) HDEL Delete entry from Hash with key HGET Get value from Hash entry with key HMGET Get values of multiples Hash entries HGETALL Get all key-value in Hash HINCRBY Increment value of hash entry (numbers) HINCRBYFLOAT Increment value of hash entry (numbers) HSTRLEN Length (in bytes) of hash entry HSCAN Scan hash entries A Set is an unordered collection of elements without repetition. Internally, a Set is implemented similarly to an Hash with an additional element count integer field for efficiency. Operation SADD adds one or more elements (binary String values) to the Set. Operation SISMEMBER checks for membership, and operation SMEMBERS returns all elements. Redis also supports algebraic operations between two sets, such as: union (SUNION), interception (SINTER), and difference (SDIFF). The result Set is returned, or stored as another Set if using operations S{UNION|INTER|DIFF}STORE. » Example: Adding/Retrieve Element to/from Set > SADD users adam01 eva99 joe11 jack5 eva99 4 > SMEMBERS 1) "jack5" 2) "joe111" 3) "eva99" 4) "james007" 5) "adam01" > SISMEMBER users eva99 (integer) 1 » Example: Set Algebraic Operations > SADD user:eva99:friends adrian2 jack5 james007 > SADD user:adam01:friends joe111 james007 > SUNION user:eva99:friends user:adam01:friends 1) "james007" 2) "joe111" 3) "adrian2" Software Engineering School 4 Operations on Hashes Operations on Sets
  • 5. - © EInnovator.org Redis 4) "jack5" Table below summarizes the Redis operations for Set. Set Operation Description SADD Add element to Set (if non existing) SPOP Remove element from Set SMOVE Move element from one Set to another SREM Remove elements from Set (?) SISMEMBER Check if element is member of Set SMEMBERS Get elements in Set SRANDMEMBER Get a random member from Set SCARD Get cardinality of Set (number of elements) SSCAN Scan elements in Set SUNION Return union of two Sets SUNIONSTORE Make union and store in named Set SINTER Return interception of two Sets SINTERSTORE Make interception and store in named Set SDIFF Return difference of two Sets SDIFFSTORE Make difference and store in named Set A Sorted Set, or ZSet, is an ordered collection without repetitions where the ordering is determined by a double numeric value (the Z score) specified when the element is added. ZSets as useful for a variety of purposes, such as creating ordered indexes. Internally, a ZSet is represented as a ordered skip list which guarantees O(log(N)) complexity for most operations. Operation ZRANGE retrieves the elements of the ZSet with a rank order within a range, ordered by score. Operation ZRANGEBYSCORE retrieves the elements of the ZSet with a score within a range, ordered by score. Option WITHSCORES retrieves the scores in addition to the values. For the special case where all elements of the ZSet have the same score, operation ZRANGEBYLEX retrieves the elements by lexicographical (alphabetical) order. » Example: Adding & Retrieving Elements on/from ZSet > ZADD users:reputation 1346 adam1 > ZADD users:reputation 3452 eva33 ... ZRANGE users:reputation 0 -1 1)"eva33" 2)"adam1" ... » Example: Retreiving Element Within Score Range > ZRANGEBYSCORE users:reputation 2000 +INF WITHSCORES 1) "eva33" 2) "43452" Table below summarizes the Redis operations for ZSet. ZSet Operation Description ZADD Add element (with score) to ZSet (if non existing) ZREM Remove element from ZSet ZCARD Get cardinality (total number of elements) ZCOUNT Get count of element with score in range ZLEXCOUNT Get count of elements in lexicographical range ZSCORE Get score of element ZRANK Get rank (order) of element ZREVRANK Get rank of element in reverse ordering ZINCRBY Increments score of element by a value ZSCAN Scan elements ZRANGE Get elements with rank in a range ZRANGEBYSCORE Get elements ordered lexicographically ZRANGEBYLEX Get elements with score in a range ZREVRANGE Get elements with rank in a range reversed ZREVRANGEBYSCORE Get elements lexicographically reversed ZREVRANGEBYLEX Get elements with score in a range reversed ZREMRANGEBYRANK Remove elements with rank in a range ZREMRANGEBYSCORE Remove elements ordered lexicographically ZREMRANGEBYLEX Remove elements with score in a range ZUNIONSTORE Compute&store the union of two ZSet ZINTERSTORE Compute&store the interception of two ZSet An HyperLogLog is a (probabilistic) data-structure, invented by Philippe Flajolet, for counting the approximated number of unique elements in the set without storing the actual elements. Redis supports HyperLogLog due to its high-performance constant-time approach for element counting. Command PFADD add an element to a named HyperLogLog. And command PFCOUNT returns the counting estimate. » Example: Adding & Counting w/ HyperLogLog > PFADD ips 192.168.255.251 Software Engineering School 5 Operations on Sorted Sets Operations on HyperLogLogs
  • 6. © EInnovator.org Redis > PFADD ips 192.168.255.221 ... > PFCOUNT hll (integer) 3218 Table below summarizes the Redis operations for HyperLogLog. HyperLogLog Operation Description PFADD Add value to HyperLogLog PFCOUNT (Estimate) Cardinality PFMERGE Merge two HyperLogLog In addition to data-structure storage&retrieval, Redis can also be used as a simple messaging-middleware. Operation SUBSCRIBE is used by a client that wants to subscribe to one (or more) named channels. Operation PSUBSCRIBE is similar, but a channel named pattern is specified. The client (thread) making this operation should not send additional commands. The sent messages are delivered as a stream through the connection channel. To send messages to named channel clients should issue command PUBLISH. » Example: Subscribing to Channels > SUBSCRIBE channel:1 channel:2 Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "channel:1" 3) (integer) 1 1) "subscribe" 2) "channel:2" 3) (integer) 2 » Example: Subscribing Channels Matching Pattern > PSUBSCRIBE channel:* Reading messages... (press Ctrl-C to quit) 1) "psubscribe" 2) "channel:*" 3) (integer) 1 » Example: Publishing Message to Channel > PUBLISH channel1 "Hello Redis!" 1) "message" 2) "channel1" 3) "Hello Redis!" The message publishing/dissemination mechanism is also used internally by Redis to notify clients of relevant events (e.g. a key just expired, deleted, or modified). Clients can get this notifications by subscribing to the relevant channel name (e.g. deleting key xyz makes Redis send a notification message to channels __keyspace@0__:xyz del, and __keyevent@0__:del xyz). Table below summarizes the Redis PubSub commands. PubSub Command Description SUBSCRIBE Subscribe to named channel(s) PSUBSCRIBE Subscribe to matching channels PUBLISH Publish/Send message to channel UNSUBSCRIBE Unsubscribe to named channel(s) PUNSUBSCRIBE Unsubscribe to matching channels Redis supports a simple form of transactional semantics where operations are buffered before execution until the transaction completes. Command MULTI sets a client connection in transactional mode. Command EXEC is used mark the completion of the transaction with success, and trigger the execution of buffered commands. All commands in a completed transaction are guaranteed to be applied in an isolated (no race conditions) and (quasi-)atomic way. Command DISCARD is used to cancel all commands buffered so far (i.e. they are nor executed). Redis does not support transaction rollback in the traditional sense – if an operation execution fails during the EXEC, the previous operation might already have made some changes in some keys. Redis also support a simple form of optimistic concurrency control using command WATCH. Any (concurrent) modification done in the specified key while the transaction is being executed is detected and forces the EXEC to fail before the first operation is applied. » Example: Running Multiple Commans in Transaction > MULTI OK > INCR foo QUEUED > INCR bar QUEUED > EXEC 1) (integer) 1 2) (integer) 1 Table below summarizes the Redis commands for transaction management and concurrency control. Tx Operation Description MULTI Mark transaction start Software Engineering School 6 Redis Transactions Redis PubSub (Messaging)
  • 7. Redis EXEC Mark transaction completion (commit) DISCARD Mark transaction abort (discard) WATCH Check for concurrent updates on key Complex operations involving multiple/repetitive access to one or more keys can be optimized by running them as Lua Scripts inside the Redis server. Lua function redis.call() is used to interface with Redis and invoke any desired command. Command EVAL is used to evaluate a Lua script. To avoid having to transfer a (possible long) script to the server often, Redis maintains a script cache. A SHA digest is used to unequivocally identify a script. Command SCRIPT LOAD is used to load a script in the cache. Command EVALSHA executes the script identified by a SHA digest. » Example: Evaluating a Lua Script > EVAL "return {KEYS[1],KEYS[2],ARGV[1]}" 2 key1 key2 abc 1) "key1" 2) "key2" 3) "abc" » Example: Loading a Lua Script & Get SHA SCRIPT LOAD "return redis.call('get','product:ids')" > EVALSHA 6b1bf486c81ceb7edf3c093f4c48582e38c0e791 0 Table below summarizes the Redis commands for scripting. Script Operation Description EVAL Execute Script and return result EVALSHA Execute cached Script by SHA SCRIPT EXIST Check if Script with SHA exists SCRIPT LOAD Load Scritpt from file SCRIPT FLUSH Remove Script from cache SCRIPT KILL Terminate script • Redis Project home page: http://redis.io/ • Redis sample configuration file: https://raw.githubusercontent.com/antirez/redis/3.2/redis.conf About the Author Jorge Simão is a software engineer and IT Trainer, with two decades long experience on education delivery both in academia and industry. Expert in a wide range of computing topics, he his an author, trainer, and director (Education & Consulting) at EInnovator. He holds a B.Sc., M.Sc., and Ph.D. in Computer Science and Engineering. Redis Training Redis Administration course teaches how to install, manage, and configure Redis. Covers topics such as Redis master-slave replication, high-availability, and Redis cluster. Book for a training event in a date&location of your choice: www.einnovator.org/course/redis-administration Redis Development course teaches how build applications that use Redis, with extensive coverage of Spring Data Redis (Java track) & C# drivers (.Net track). Book for a training event in a date&location of your choice: www.einnovator.org/course/redis-development ++ QuickGuides » EInnovator.org » Spring Container, Spring MVC, Spring WebFlow » RabbitMQ, Spring XD » Cloud Foundry, Spring Cloud » and much more... ++ Courses » EInnovator.org » Core Spring, Spring Web, Enterprise Spring » RabbitMQ, CloudFoundry » BigData and Hadoop, Spring XD, Spark » and much more... EInnovator – Software Engineering School EInnovator.org offers the best Software Engineering resources and education, in partnership with the most innovative companies in the IT-world. Focusing on both foundational and cutting-edge technologies and topics, at EInnovator software engineers and data-scientists can get all the skills needed to become top-of- the-line on state-of-the-art IT professionals. Training – Bookings & Inquiries training@einnovator.org Consultancy – Partnerships & Inquiries consulting@einnovator.org General Info info@einnovator.org Software Engineering School Copyright © 2014 EInnovator.org. All rights reserved. 7 Redis Lua Scripting Resources Contacts