51
REDIS - A Key value store Designveloper Website: https://www.designveloper.com/

Redis - A Key Value Store

Embed Size (px)

Citation preview

Page 1: Redis - A Key Value Store

REDIS - A Key value store

DesignveloperWebsite: https://www.designveloper.com/

Page 2: Redis - A Key Value Store

What is Key-Value Store?

Page 3: Redis - A Key Value Store

What is Key-Value Store?

▪A key-value store is like associative array where data are represented in the form of array ['key'] =value.

Page 4: Redis - A Key Value Store

What is Key-Value Store?

▪ If we know the key we can retrieve the value associated with it.▪ The value can be strings, hashes, lists, sets and sorted sets in Redis.

Page 5: Redis - A Key Value Store

What is Redis?

Page 6: Redis - A Key Value Store

What is Redis?

▪Redis is an in-memory database that persists on disk.▪ The data it stores is a key-value format structure but much different kind of values are supported such as Strings, Lists, Sets, Sorted Sets, and Hashes.

Page 7: Redis - A Key Value Store

What is Redis?

▪  Sometimes it is referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.▪ It is mostly used as caching server.

Page 8: Redis - A Key Value Store

When to use Redis?

Page 9: Redis - A Key Value Store

When to use Redis?

▪ It simply a caching system which speeds up the web application because RAM is much faster than secondary storage like Hard drive▪ It is useful in situations like “Showing latest items listings in your home page” or getting instant results

Page 10: Redis - A Key Value Store

When to use Redis?

▪Redis is a fantastic choice if you want a highly scalable data store shared by multiple processes, multiple applications, or multiple servers▪Real time analysis of what is happening in your web site which may be used for stats, Analytics, etc…

Page 11: Redis - A Key Value Store

What Are You Going To Learn?

Page 12: Redis - A Key Value Store

What Are You Going To Learn?

▪ Advantages of using Redis▪ Companies using Redis▪ Basic Terminology▪ Installing Redis▪ Redis Commands (CRUD Operations)

with Examples▪ Conclusion

Page 13: Redis - A Key Value Store

What are the Advantages of Redis?

Page 14: Redis - A Key Value Store

What are the Advantages of Redis?

▪ It supports advanced data types such as strings, hashes, lists, sets and sorted sets.▪Replication is another cool feature why Redis is used.▪Support for Persistence data make is a good choice.▪ It is usually fast.

Page 15: Redis - A Key Value Store

Companies Using Redis

Page 16: Redis - A Key Value Store

Companies Using Redis

Page 17: Redis - A Key Value Store

Basic Terminology Used in Redis

Page 18: Redis - A Key Value Store

Basic Terminology Used in Redis

▪String: String is a combination of characters. Strings are the most basic kind of Redis value. Redis Strings are binary safe which simply means that a Redis string can contain any kind of data, for example a JPEG image or a serialized Ruby object. In Redis, a string value can have a maximum of 512 Megabytes length.

Page 19: Redis - A Key Value Store

Basic Terminology Used in Redis

▪Lists: Lists are simply lists of strings which are sorted based on insertion order. We can add new elements at the start or at the end of the list.

Page 20: Redis - A Key Value Store

Basic Terminology Used in Redis

▪Sets: Sets are an unordered collection of Strings. It is possible to add, remove, and find the existence of members.

Page 21: Redis - A Key Value Store

Basic Terminology Used in Redis

▪Hashes: Hashes are maps between string fields and string values, so they are the perfect data type to represent objects for e.g. A User with a number of fields like first name, last name, age etc…

Page 22: Redis - A Key Value Store

Basic Terminology Used in Redis

▪Sorted Sets: Sorted Sets are non repeating collections of Strings and are similar to sets in redis. The difference is that every member of a Sorted Set is associated with score that is used in order to take the sorted set ordered, from the smallest to the greatest score. Since members are unique, the scores may be repeated.

Page 23: Redis - A Key Value Store

Installing Redis

Page 24: Redis - A Key Value Store

Installing Redis on Linux and Mac

▪ wget http://download.redis.io/releases/redis-2.8.3.tar.gz▪ tar xzf redis-2.8.3.tar.gz▪ cd redis-2.8.3▪ make▪ Run Redis with:▪ src/redis-server

Page 25: Redis - A Key Value Store

Installing Redis on Windows

▪ The Redis project does not directly support Windows. You need to download and build the code locally. ▪ https://github.com/rgl/redis/downloads▪ First run redis-server.exe and then redis-cli.exe which you can use to run the command line Redis command.

Page 26: Redis - A Key Value Store

Redis Commands (CRUD) with Examples

Page 27: Redis - A Key Value Store

create a value use SET command

▪ SET name “DSV”▪ SET name “Redis Tutorial” EX 20▪ The key and value will be deleted

automatically after 20 seconds.▪ SET name “Redis Tutorial” PX 1200▪ The key and value will be deleted

automatically after 1200 milliseconds.▪ EX (Expiry in second) or PX (Expiry in

milliseconds.)

Page 28: Redis - A Key Value Store

SETNX

▪ SETNX name “Redis Tutorial for Beginners”▪ The above command will set the value “Redis Tutorial for Beginners” at key “name” if it doesn’t exist, otherwise return 0 on failure.▪We can use the command “Set SETNX” to set value if not exist.

Page 29: Redis - A Key Value Store

GET command

▪ GET name▪ The above command will fetch value of the key “name”

Page 30: Redis - A Key Value Store

INCR command & DECR command

▪ INCR to atomically increment a number stored at a given key▪ SET id 1▪ Incr id => 2▪ Incr id => 3▪ Decr id => 2▪ Decr id => 1

Page 31: Redis - A Key Value Store

update a value

Page 32: Redis - A Key Value Store

delete the value

▪ Del id

Page 33: Redis - A Key Value Store

LIST

Page 34: Redis - A Key Value Store

LIST

▪ A list is a series of ordered values. Some of the important commands for interacting with lists are RPUSH, LPUSH, LLEN, LRANGE, LPOP, and RPOP. You can immediately begin working with a key as a list

▪ RPUSH puts the new value at the end of the list.▪ RPUSH friends "Alice" ▪ RPUSH friends "Bob”▪ LPUSH puts the new value at the start of the list.▪ LPUSH friends "Sam”

Page 35: Redis - A Key Value Store

LIST

▪ LRANGE gives a subset of the list. It takes the index of the first element you want to retrieve as its first parameter and the index of the last element you want to retrieve as its second parameter. A value of -1 for the second parameter means to retrieve elements until the end of the list.

▪ LRANGE friends 0 -1 => ["Sam","Alice","Bob"] ▪ LRANGE friends 0 1 => ["Sam","Alice"] ▪ LRANGE friends 1 2 => ["Alice","Bob"]

Page 36: Redis - A Key Value Store

LIST

▪ LLEN returns the current length of the list.

LLEN friends => 3 ▪ LPOP removes the first element from the list and returns it.

LPOP friends => "Sam" ▪ RPOP removes the last element from the list and returns it.

RPOP friends => "Bob"

Page 37: Redis - A Key Value Store

SETS

Page 38: Redis - A Key Value Store

SETS

▪ The next data structure that we'll look at is a set. A set is similar to a list, except it does not have a specific order and each element may only appear once. Some of the important commands in working with sets are

 SADD, SREM, SISMEMBER, SMEMBERS and SUNION.

Page 39: Redis - A Key Value Store

SETS

▪ SADD adds the given value to the set.SADD demo "flight" SADD demo "x-ray vision“SADD demo "reflexes"

▪ SREM removes the given value from the set.SREM demo "reflexes"

Page 40: Redis - A Key Value Store

SETS

▪ SMEMBERS demo - The above command will return all the value inside a set▪ SRANDMEMBER demo▪ SISMEMBER check member is exist▪ SISMEMBER demo ‘flight’ => 1

Page 41: Redis - A Key Value Store

SORTED SETS

Page 42: Redis - A Key Value Store

Sorted sets

▪ A sorted set is similar to a regular set, but now each value has an associated score. This score is used to sort the elements in the set.

Page 43: Redis - A Key Value Store

Sorted sets

▪ ZADD dsv 1991 “Hao Nguyen" ▪ ZADD dsv 1990 “Hau Nguyen" ▪ ZADD dsv 1987 “Hung Vo" ▪ ZADD dsv 1989 “Tu Anh" ▪ ZADD dsv 1985 “Phuong Nguyen“▪ ZADD dsv 1974 “Dan Vu"

Page 44: Redis - A Key Value Store

Sorted sets

▪ ZRANGE dsv 0 -1▪ ZRANGEBYSCORE dsv 1991 1987▪ ZREM dsv “two”▪ ZREMRANGEBYRANK dsv 0 1

Page 45: Redis - A Key Value Store

HASH 

Page 46: Redis - A Key Value Store

Hash

▪ Hashes are maps between string fields and string values, so they are the perfect data type to represent objects

Page 47: Redis - A Key Value Store

Hash

▪ HSET dsv:1 name “ken" ▪ HSET dsv:1 email “[email protected]" ▪ HSET dsv:1 password “handsome" ▪ HSET dsv:2 name “hao" ▪ HSET dsv:2 email “[email protected]" ▪ HSET dsv:2 password “handsometoo"

Page 48: Redis - A Key Value Store

Hash

▪ HMSET dsv:1 name “ken” email “[email protected] ” password “handsome”▪ HMSET dsv:2 name “hao” email

[email protected]” password “handsometoo”

Page 49: Redis - A Key Value Store

Hash

▪ To get a single value use HGETHGET dsv:1 name

To get all value data use HGETALLHGETALL dsv:1

To get all key data use HKEYSHKEYS dsv:1

Page 50: Redis - A Key Value Store

PROJECT DEMO

Page 51: Redis - A Key Value Store

THANK YOUTHE END