+ All Categories
Home > Documents > Redis: NoSQL data storage

Redis: NoSQL data storage

Date post: 12-Jan-2016
Category:
Upload: aelwen
View: 42 times
Download: 1 times
Share this document with a friend
Description:
Redis: NoSQL data storage. Databases. Classical variant - store data in a relationa l database MySQL PostgreSQL H2 SQLite HSQLDB and many more ... Modern trend in a Web programming : store data in NoSQL databases. NoSQL introduction. - PowerPoint PPT Presentation
40
Redis: NoSQL data storage
Transcript
Page 1: Redis: NoSQL data storage

Redis: NoSQL data storage

Page 2: Redis: NoSQL data storage

Databases

• Classical variant - store data in a relational database• MySQL

• PostgreSQL

• H2

• SQLite

• HSQLDB

• and many more...

• Modern trend in a Web programming:

store data in NoSQL databases

Page 3: Redis: NoSQL data storage

NoSQL introduction

• NoSQL is a non-relational database management system, different from traditional RDBMS in some significant ways

• Carlo Strozzi used the term NoSQL in 1998 to name his lightweight, open-source relational database that did not expose the standard SQL interface

Page 4: Redis: NoSQL data storage

NoSQL introduction

• In 2009, Eric Evans reused the term to refer databases which are non-relational, distributed, and does not conform to ACID

• The NoSQL term should be used as in the Not-Only-SQL and not as No to SQL or Never SQL

Page 5: Redis: NoSQL data storage

ACID

• Atomicity• "all or nothing": if one part of the transaction

fails, the entire transaction fails, and the database state is left unchanged

• Consistency• Ensures that any transaction will bring the

database from one valid state to another. Any data written to the database must be valid according to all defined rules (constraints, cascades, triggers etc).

Page 6: Redis: NoSQL data storage

ACID

• Isolation• Ensures that the concurrent execution of

transactions results in a system state that could have been obtained if transactions are executed serially

• Durability• Means that once a transaction has been

committed, it will remain so, even in the event of power loss, crashes, or errors

Page 7: Redis: NoSQL data storage

NoSQL introduction

• Two trends:• The exponential growth of the volume of data

generated by users and systems• The increasing interdependency and complexity of

data, accelerated by the Internet, Web 2.0, social networks

• NoSQL databases are useful when working with a huge quantity of data and the data's nature does not require a relational model for the data structure

Page 8: Redis: NoSQL data storage

NoSQL characteristics

• Does not use SQL as its query language• NoSQL databases are not primarily built on tables, and

generally do not use SQL for data manipulation

• May not give full ACID guarantees• Usually only eventual consistency is guaranteed or

transactions limited to single data items

• Distributed, fault-tolerant architecture• Data are partitioned and held on large number of servers,

and is replicated among these machines: horizontal scaling

Page 9: Redis: NoSQL data storage

Eventual consistency

• Given a sufficiently long period of time over which no changes are sent, all updates can be expected to propagate eventually through the system and all the replicas will be consistent

• Conflict resolution:• Read repair: The correction is done when a read finds

an inconsistency. This slows down the read operation.• Write repair: The correction takes place during a

write operation, if an inconsistency has been found, slowing down the write operation.

• Asynchronous repair: The correction is not part of a read or write operation.

Page 10: Redis: NoSQL data storage

Eventual consistency

• Given a sufficiently long period of time over which no changes are sent, all updates can be expected to propagate eventually through the system and all the replicas will be consistent

• Conflict resolution:• Read repair: The correction is done when a read finds

an inconsistency. This slows down the read operation.• Write repair: The correction takes place during a

write operation, if an inconsistency has been found, slowing down the write operation.

• Asynchronous repair: The correction is not part of a read or write operation.

Page 11: Redis: NoSQL data storage

BASE vs ACID

• BASE is an alternative to ACID• Basically Available• Soft state• Eventual consistency

• Weak consistency• Availability first• Approximate answers• Faster

Page 12: Redis: NoSQL data storage

Scalability

• Scalability is the ability of a system to handle a growing amount of work in a capable manner or its ability to be enlarged to accommodate that growths

• Scale horizontally (scale out)• Add more nodes to a system, such as adding a new

computer to a distributed software application

• Scale vertically (scale up)• Add resources to a single node in a system, typically CPUs or

memory

Page 13: Redis: NoSQL data storage

CAP theorem

• States that it is impossible for a distributed computer system to simultaneously provide all three of the following guarantees:

• Consistency • All nodes see the same data at the same time

• Availability• A guarantee that every request receives a response about

whether it was successful or failed

• Partition tolerance • The system continues to operate despite arbitrary

message loss or failure of part of the system

Page 14: Redis: NoSQL data storage

Brewer’s Conjecture

• In 2000, a conjecture was proposed in the keynote speech by Eric Brewer at the ACM Symposium on the Principles of Distributed Computing

• Slides: http://www.cs.berkeley.edu/~brewer/cs262b-2004/PODC-keynote.pdf

Page 15: Redis: NoSQL data storage

Formal proof

• In 2002, Seth Gilbert and Nancy Lynch of MIT, formally proved Brewer to be correct

• http://lpd.epfl.ch/sgilbert/pubs/BrewersConjecture-SigAct.pdf

• “We have shown that it is impossible to reliably provide atomic, consistent data when there are partitions in the network.”

• “It is feasible, however, to achieve any two of the three properties: consistency, availability, and partition tolerance.”

• “In particular, most real-world systems today are forced to settle with returning “most of the data, most of the time.””

Page 16: Redis: NoSQL data storage
Page 17: Redis: NoSQL data storage

Categories of NoSQL storages• Key-Value

• memcached• Redis

• Column Family• Cassandra

• Document• MongoDB

• Tabular• BigTable, HBase

• Graph, XML, Object, Multivalued,…

Page 18: Redis: NoSQL data storage

Redis

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

• Often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets

• Redis works with an in-memory dataset

• 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

Page 19: Redis: NoSQL data storage

Who is using Redis?

Page 20: Redis: NoSQL data storage

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

Page 21: Redis: NoSQL data storage

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 1save 300 10save 60 10000

save 10 1

save after 10 sec if at least 1 key changed

Page 22: Redis: NoSQL data storage

Running Redis Server

• Run /redis/bin/redis-server.exe and specify configuration file to useredis>redis-server redis.conf

Page 23: Redis: NoSQL data storage

Running Redis Client

• Run /redis/bin/redis-cli.exe

• Now you can play with Redis a little bit

Page 24: Redis: NoSQL data storage

Useful Commands

• Print all keys:

• Remove all keys from all databases

• Synchronously save the dataset to disk

KEYS *

FLUSHALL

SAVE

Page 25: Redis: NoSQL data storage

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"

Page 26: Redis: NoSQL data storage

Redis data types

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

•Strings

•Lists

•Sets

•Hashes

•Sorted Sets

Page 27: Redis: NoSQL data storage

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

Page 28: Redis: NoSQL data storage

Redis Strings: Example

Page 29: Redis: NoSQL data storage

Redis Lists

• Lists of strings, sorted by insertion order

• Add elements to a Redis List pushing new elements on the head (on the left) or on the tail (on the right) of the list

• 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

Page 30: Redis: NoSQL data storage

Redis Lists: Example

Page 31: Redis: NoSQL data storage

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

• You can do a lot of tasks with great performance that are really hard to model in other kind of databases

• Probably the most advanced Redis data type

Page 32: Redis: NoSQL data storage

Redis Hashes

• Map between string fields and string values

• Perfect data type to represent objects

HMSET user:1000 username antirez password P1pp0 age 34HGETALL user:1000HSET user:1000 password 12345HGETALL user:1000

Page 33: Redis: NoSQL data storage

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

Page 34: Redis: NoSQL data storage

Using Redis in Java

• JRedis - Java Client for Redis

• Jedis - a blazingly small and sane Redis Java client

• Spring Data Redis

Page 35: Redis: NoSQL data storage

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

Page 36: Redis: NoSQL data storage

Spring Data Redis

• Provides easy configuration and access to Redis from Spring applications

• Offers both low-level and high-level abstractions for interacting with the store

• freeing the user from infrastructural concerns

• Maven dependency: <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.1.0.RELEASE</version></dependency>

Page 37: Redis: NoSQL data storage

Spring Redis Configuration

Configure Jedis Factory and RedisTemplate:

<bean id="jedisConnectionFactory" class="org.springframework.data.redis

.connection.jedis.JedisConnectionFactory" p:host-name="localhost" p:port="6379" p:password=""/>

<bean id="redisTemplate" class="org.springframework.data

.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory"> <property name="keySerializer" ref="stringKeySerializer"/></bean>

<bean id="stringKeySerializer" class="org.springframework.data

.redis.serializer.StringRedisSerializer"/>

Page 38: Redis: NoSQL data storage

Java Code

Page 39: Redis: NoSQL data storage

FeedRedisDAOImplpublic class FeedRedisDAOImpl implements FeedRedisDAO {

@Autowired private RedisTemplate<String, Feed> template;

public void addFeed(Long userId, Feed feed){

RedisList<Feed> feeds = feeds(userId);

feeds.add(feed);

}

public List<Feed> getFeeds(Long userId){

RedisList<Feed> feeds = feeds(userId);

return convertToArrayList(feeds);

}

private RedisList<Feed> feeds(Long userId) {

DefaultRedisList<Feed> feeds =

new DefaultRedisList<Feed>(

RedisKeys.feeds(userId.toString()), template);

feeds.setMaxSize(50);

return feeds;

}

}

Page 40: Redis: NoSQL data storage

Resources

• Redishttp://www.redis.io


Recommended