redis: nosql data storage · redis regardless of the type, a value is accessed by a key. it is...

42
REDIS: NOSQL DATA STORAGE Presented By- Shalini Somani (13MCEC22) 1

Upload: others

Post on 17-Mar-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

REDIS: NOSQL DATA

STORAGE

Presented By-

Shalini Somani

(13MCEC22)

1

CATEGORIES OF NOSQL STORAGES

Key-Value

memcached

Redis

Column Family

Cassandra

HBase

Document

MongoDB

Tabular

BigTable

Graph, XML, Object, Multivalued,…

2

REDIS

Redis is an open source, advanced key-value data store.

Often referred to as a data structure engine

- Strings

- Hashes

- Sets

- Lists

- Ordered Sets

3

REDIS ARCHITECTURE

4

REDIS

Regardless of the type, a value is accessed by a key.

It is possible to persist dataset either by

dumping the dataset to disk every once in a while.

or by appending each command to a log.

5

WHO IS USING REDIS?

6

INSTALLATION

Linux: http://redis.io/download

Windows

1. Clone from Git repo: https://github.com/MSOpenTech/redis

2. Unzip file from /redis/bin/release (e.g. redisbin64.zip) to /redis

3. Important files: /redis/redis-server.exe

/redis/redis-cli.exe 7

CONFIGURATION

Configuration file: /redis/redis.conf

It is possible to change a port (if you wish):

For development environment it is useful to change data persisting policy

port 6379

save 900 1

save 300 10

save 60 10000

save 10 1

save after 10 sec if at least 1 key changed

8

RUNNING REDIS SERVER

Run /redis/bin/redis-server.exe and specify configuration file to use

redis>redis-server redis.conf

9

RUNNING REDIS CLIENT

Run /redis/bin/redis-cli.exe

Now you can play with Redis a little bit

10

USEFUL COMMANDS

Print all keys:

Remove all keys from all databases

Synchronously save the dataset to disk

KEYS *

FLUSHALL

SAVE 11

REDIS KEYS

Keys are binary safe - it is possible to use any binary sequence as a key

The empty string is also a valid key

Too long keys are not a good idea

Too short keys are often also not a good idea ("u:1000:pwd" versus "user:1000:password")

Nice idea is to use some kind of schema, like: "object-type:id:field"

12

BASIC COMMANDS

DBSIZE - returns the number of keys in the database.

% DBSIZE

:0

This is a new database with no keys, so Redis says :0.

NOTE:- at a time Redis supports 16 Databases (0-15).

(this is the default no of databases which can me manually

changed in redis.cofig file)

We can navigate between various databases using-

SELECT [database_no] 13

BASIC COMMANDS

GET - fetches the value at the provided key.

% GET hello

$ -1

The key hello doesn’t exist, so Redis says “$-

1”,which is like nil or null.

Let’s set a value for that key:

14

BASIC COMMANDS

SET

- it is used to set the value of a key.

syntax-

SET [key] [value]

example-

% SET hello nirma

+OK

Here Nirma is value for key hello. 15

BASIC COMMANDS

Here, we SET the key hello to the 5-byte

value world, and Redis says +OK. Now if

we GET again:

% GET hello

nirma

EXIST- to check if a key exist.

% EXISTS mykey

:0

% EXISTS hello

:1

16

BASIC COMMANDS

SETNX – if we want to set value of key if it doesn’t

exist.

% SETNX elective

% I have opted for MD.

:1

% GET elective

I have opted for MD.

The second SETNX fails because the key ”elective”, having been created by

the first SETNX, now exists.

% SETNX elective

% Artificial

Intelligence

:0

% GET elective

I have opted for MD.

17

INCREMENT & DECREMENT

Redis lets us increment and decrement our values. First,

let’s create a counter and set it to 1:

% SET counter 1

% 1

+OK

We can also increment and decrement by values other than one:

To increment:

% INCR

counter :2

To decrement:

% DECR

counter :1

% INCRBY counter 5

:6

% DECRBY counter 6

:0 18

WHY INCREMENT AND DECREMENT COMMANDS

EXIST?

Example- we’re using Redis to track the

pageviews for a web site.

Every URL maps to a key whose value is a

counter.

When a user visits a URL, we increment the

counter.

Imagine two visitors, v1 and v2, visit the same

URL,about, at the same time.

The Redis code for this might be-

19

WHY INCREMENT AND DECREMENT

COMMANDS EXIST?

redis.get("/about") == 5 # 5 visits so far

v1 = redis.get("/about") # visitor 1 grabs the count

v2 = redis.get("/about") # visitor 2 grabs the count

redis.set("/about", v1 + 1) # visitor 1 sets the count + 1

redis.set("/about", v2 + 1) # visitor 2 sets the count + 1

redis.get("/about") == 6 # what?? should be 7

Solution-:

The increment and decrement commands prevent this

problem because they’re atomic: reading the value and

changing it occur in one motion and can’t be interrupted.

20

REDIS DATA TYPES

Redis is often referred to as a data structure server since keys can contain:

Strings

Lists

Sets

Hashes

Sorted Sets

21

REDIS STRINGS

Most basic kind of Redis value.

Binary safe - can contain any kind of data, for

instance a JPEG image or a serialized Ruby

object.

Max 512 Megabytes in length.

Can be used as atomic counters using commands

in the INCR family.

Can be appended with the APPEND command.

22

REDIS STRINGS: EXAMPLE

23

REDIS LISTS

Lists of strings, sorted by insertion order.

Lists let you associate an array of values to a single key.

Max length: (2^32 - 1) elements.

Model a timeline in a social network, using LPUSH to add new elements, and using LRANGE in order to retrieve recent items.

Use LPUSH together with LTRIM to create a list that never exceeds a given number of elements

24

REDIS LISTS: EXAMPLE

25

SETS

Sets are a lot like lists, except they provide set-semantics

(no duplicate values in a given set).

Various SET operations-:

1. SADD- Add one or more members to a SET.

2. SCARD- Get the number of memners in a SET.

3. SDIFF- Subtract multiple SETS.

4. SISMEMBER- Determine if a given value is member of

SET.

5. SINTER- Intersect multiple SETS.

6. SMEMBERS- Display all members of a SET...etc.

26

SET EXAMPLE…

27

REDIS SORTED SETS

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.

A sorted set is similar to a set, except each value is

associated (and sorted by) a score field.

In other words, when you add a value to a sorted set, you

also specify the score as a number. This determines the

order of the value within the set.

Probably the most advanced Redis data type

28

ORDERED SET EXAMPLE

29

REDIS HASHES

Map between string fields and string values.

Perfect data type to represent objects.

Rather than manipulating a key directly (like with a String) you manipulate the fields of a key.

30

HASH EXAMPLE

31

REDIS OPERATIONS

It is possible to run atomic operations on data types:

appending to a string

incrementing the value in a hash

pushing to a list

computing set intersection, union and difference

getting the member with highest ranking in a sorted set

32

USING REDIS IN JAVA

JRedis - Java Client for Redis

Jedis - a blazingly small and sane Redis Java client

Spring Data Redis

33

SPRING DATA PROJECT

Provides integration and support for many types of databases

Big Data: Apache Hadoop

Key-Value: Redis

Document: MongoDB

Graph: Neo4j

Column: HBase

34

REDIS COMMANDS….

35

36

37

38

39

40

41

RESOURCES

Redis

http://www.redis.io

42