Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Redis NoSQL (Key-value) Database

2,345 views

Published on

Databases
Classical variant - store data in a relational database:

MySQL

PostgreSQL

H2

SQLite and many more...

Modern trend in a Web programming:

Store data in NoSQL databases

NoSQL Introduction

Non-relational database management system

Did not expose the standard SQL interface

Does not conform to ACID

Working with a huge quantity of data

Does not require a relational model

Distributed, fault-tolerant architecture

Installation

Linux:
1. $ wget http://download.redis.io/releases/redis-2.8.9.tar.gz
2. $ tar xzf redis-2.8.9.tar.gz
3. $ cd redis-2.8.9
4. $ make
5. $ sudo make install
6. $ cd utils
7. $ sudo ./install_server.sh

Windows:
1. Download the latest .exe package from
http://github.com/MSOpenTech/redis
2. Running the exe will install Redis as a service and install the Redis client.

Configuration

Redis is able to start without a configuration file using a built-in default configuration.

The proper way to configure Redis is by providing a Redis configuration file, usually called redis.conf.

Port: 6379
Timeout: 300
Maxclients 10000
Maxmemory <bytes>
Save 900 1

Running Redis Server and Client

Once the script completes, the redis-server will be running in the background.

You can start and stop redis with these commands.

sudo service redis_6379 start
sudo service redis_6379 stop

Access the redis database by typing the following command.

redis-cli

Redis Clients

Phpredis – Redis client written in C for PHP.

Redis-py – Mature Redis client for Python.

Redis-rb – Very stable & Muture Client for Rubys

Scala-redis – Mature Redis client for Scala

Node_redis – Recommended client for node.

Jedis – Java client library for Redis.

Eredis – A Redis erlang client library.

Example Code

Write a simple php script to test redis php client:

<?php>connect('127.0.0.1', 6379);
$redis->set('set_testkey', 1);
?>
Parameters:
Host: string. can be a host, or the path to a unix domain socket
Port: int, optional

Return value:
BOOL: TRUE on success, FALSE on error.

Redis Data Types

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

Strings

Lists

Sets

Hashes

Sorted Sets


Replication

Master-slave replication that allows slave Redis servers to be exact copies of master servers.

A master can have multiple slaves.

Slaves are able to accept connections from other slaves.

Replication can be used both for scalability and for data
redundancy.

Slave instance facility for scaling out read requests or
mitigate a disaster recovery scenario.

Resources

Published in: Engineering, Technology
  • Be the first to comment

Redis NoSQL (Key-value) Database

  1. 1. Presenter: Mubashar Iqbal Software Engineer
  2. 2. Classical variant - store data in a relational database: • MySQL • PostgreSQL • H2 • SQLite and many more... Modern trend in a Web programming: • Store data in NoSQL databases Databases
  3. 3. • Non-relational database management system • Did not expose the standard SQL interface • Does not conform to ACID • Working with a huge quantity of data • Does not require a relational model • Distributed, fault-tolerant architecture NoSQL Introduction
  4. 4. Categories of NoSQL Databases  Key-Value Memcached, Redis, Riak  Column Family Cassandra, HBase  Document MongoDB, CouchDB  Graph Neo4j, InfoGrid
  5. 5. Redis  Redis means Remote Dictionary Server  Open Source  Key-Value Data store  Data structure server  In-Memory Dataset  Written in ANSI C and Sponsored by Pivotal. http://redis.io/topics/introduction
  6. 6. Why and when you should use Redis?  Caching  Master-slave replication, automatic failover  High-IO workloads  Distributed Locks  Data that expires  Cookie storage  Analytics  Ad Targeting  Search engines  Small Data  Bigger Data  Forums
  7. 7. Who is using Redis? http://redis.io/topics/whos-using-redis
  8. 8. Storing lots of Pinterest lists in Redis Next time you log in to Pinterest, remember that Redis is running in the background and storing several types of lists for you as a user:  A list of users who you follow  A list of boards (and their related users) who you follow  A list of your followers  A list of people who follow your boards  A list of boards you follow  A list of boards you unfollowed after following a user  The followers and unfollowers of each board
  9. 9. Installation Linux: 1. $ wget http://download.redis.io/releases/redis-2.8.9.tar.gz 2. $ tar xzf redis-2.8.9.tar.gz 3. $ cd redis-2.8.9 4. $ make 5. $ sudo make install 6. $ cd utils 7. $ sudo ./install_server.sh Windows: 1. Download the latest .exe package from http://github.com/MSOpenTech/redis 2. Running the exe will install Redis as a service and install the Redis client.
  10. 10. Configuration Redis is able to start without a configuration file using a built-in default configuration. The proper way to configure Redis is by providing a Redis configuration file, usually called redis.conf.  Port: 6379  Timeout: 300  Maxclients 10000  Maxmemory <bytes>  Save 900 1
  11. 11. Running Redis Server and Client Once the script completes, the redis-server will be running in the background. You can start and stop redis with these commands. sudo service redis_6379 start sudo service redis_6379 stop Access the redis database by typing the following command. redis-cli
  12. 12. Redis Clients  Phpredis – Redis client written in C for PHP.  Redis-py – Mature Redis client for Python.  Redis-rb – Very stable & Muture Client for Rubys  Scala-redis – Mature Redis client for Scala  Node_redis – Recommended client for node.  Jedis – Java client library for Redis.  Eredis – A Redis erlang client library.
  13. 13. Example Code Write a simple php script to test redis php client: <?php $redis=new Redis() or die("Can't load redis client."); $redis->connect('127.0.0.1', 6379); $redis->set('set_testkey', 1); ?> Parameters: Host: string. can be a host, or the path to a unix domain socket Port: int, optional Return value: BOOL: TRUE on success, FALSE on error.
  14. 14. Redis Data Types Redis is often referred to as a data structure server since keys can contain:  Strings  Lists  Sets  Hashes  Sorted Sets http://redis.io/topics/data-types
  15. 15. Strings  Most basic kind of Redis value  Binary safe - can contain any kind of data  Max 512 Megabytes in length  Can be used as atomic counters using commands in the INCR family APPEND – Append a value to a key GET – Get the value of a key SET – Set the string value of a key MGET – Get the values of all the given keys MSET – Set multiple keys to multiple values
  16. 16. 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 LSET – Set the value of an element in a list by its index LLEN – Get the length of a list LREM – Remove elements from a list LINSERT – Insert an element before or after another element in a list LRANGE – Get a range of elements from a list LPOP – Remove and get the first element in a list RPOP – Remove and get the last element in a list
  17. 17. Sets  Redis Sets are an unordered collection of Strings.  You can track unique things using Redis Sets. SADD – Add one or more members to a set SISMEMBER – Determine if a given value is a member of a set SMEMBERS – Get all the members in a set SPOP – Remove and return a random member from a set SRANDMEMBER – Get one or multiple random members from a set SREM – Remove one or more members from a set
  18. 18. Hashes  Map between string fields and string values  Every hash can store more than 4 billion field-value pairs. HSET – Set the string value of a hash field HGET – Get the value of a hash field HMGET – Get the values of all the given hash fields HMSET – Set multiple hash fields to multiple values HLEN – Get the number of fields in a hash HKEYS – Get all the fields in a hash HGETALL – Get all the fields and values in a hash
  19. 19. 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 ZADD – Add one or more members to a sorted set, or update its score if it already exists ZREM – Remove one or more members from a sorted set ZRANGE – Return a range of members in a sorted set, by index ZINCRBY – Increment the score of a member in a sorted set
  20. 20. Replication Master-slave replication that allows slave Redis servers to be exact copies of master servers.  A master can have multiple slaves.  Slaves are able to accept connections from other slaves.  Replication can be used both for scalability and for data redundancy.  Slave instance facility for scaling out read requests or mitigate a disaster recovery scenario.
  21. 21. Setup the Master Server To configure replication is very simple just add the following line to the slave configuration file: slaveof 192.168.1.1 6379 Replace 192.168.1.1 6379 with your master IP address and port. Replication only needs to be defined on Slave systems, the Master server does not require any special configuration.
  22. 22. Performance Popularity of Redis:  Speed  Rich semantics  Stability  Durability / Persistence - Redis snapshots - Append-only file (AOF)
  23. 23. Comparison
  24. 24. Resources 1. http://www.redis.io 2. http://bencane.com/2013/11/12/installing-redis- and-setting-up-master-slave-replication/ 3. http://oldblog.antirez.com/post/redis-memcached- benchmark.html 4. http://oldblog.antirez.com/post/update-on- memcached-redis-benchmark.html 5. http://blog.gopivotal.com/pivotal/case-studies- 2/using-redis-at-pinterest-for-billions-of-relationships

×