Redis - A Key Value Store

Preview:

Citation preview

REDIS - A Key value store

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

What is 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.

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.

What is Redis?

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.

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.

When to use Redis?

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

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…

What Are You Going To Learn?

What Are You Going To Learn?

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

with Examples▪ Conclusion

What are the Advantages of Redis?

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.

Companies Using Redis

Companies Using Redis

Basic Terminology Used in Redis

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.

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.

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.

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…

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.

Installing Redis

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

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.

Redis Commands (CRUD) with Examples

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.)

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.

GET command

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

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

update a value

delete the value

▪ Del id

LIST

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”

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"]

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"

SETS

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.

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"

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

SORTED SETS

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.

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"

Sorted sets

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

HASH 

Hash

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

Hash

▪ HSET dsv:1 name “ken" ▪ HSET dsv:1 email “friendkenten@gmail.com" ▪ HSET dsv:1 password “handsome" ▪ HSET dsv:2 name “hao" ▪ HSET dsv:2 email “haonv@designveloper.com" ▪ HSET dsv:2 password “handsometoo"

Hash

▪ HMSET dsv:1 name “ken” email “friendkenten@gmail.com ” password “handsome”▪ HMSET dsv:2 name “hao” email

“haonv@designveloper.com” password “handsometoo”

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

PROJECT DEMO

THANK YOUTHE END

Recommended