redis nosql (key-value) database
TRANSCRIPT
Presenter: Mubashar IqbalSoftware Engineer
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
• 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
Categories of NoSQL Databases
Key-ValueMemcached, Redis, Riak
Column FamilyCassandra, HBase
DocumentMongoDB, CouchDB
Graph
Neo4j, InfoGrid
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
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
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
InstallationLinux: 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/redis2. 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 startsudo 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 $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 socketPort: 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
http://redis.io/topics/data-types
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
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
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
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
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
ReplicationMaster-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.
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.
Performance
Popularity of Redis:
Speed
Rich semantics
Stability
Durability / Persistence
- Redis snapshots - Append-only file (AOF)
Comparison
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