What is Redis?

Redis is awesome NoSQL database.

From one side, it is simple key-value store, similar to memcached (comparison).

...but from other side...

Redis supports not only strings, but also sophisticated data types such lists, sets, sorted sets, hashes and more.

Using Redis is very simple to do structures such stacks, decks, queues, pipelines and many more.

Redis operations are incredible fast, atomic and non-blocking. Unlike memcached, data may be shared between different programming languages.

Redis runs in memory, it is incredible fast (110,000 SETs/second, 81,000 GETs/second), but at the same time it is persistent, e.g. data is not lost if server is stopped or crashed. This means that Redis can be used as data store.

Using Redis you can speed up your web application or website by thousand times.

Redis supports many programming languages.


Redis data types

Redis is not a plain key-value store, it is a persistent database server, supporting different kind of values. All the following data types are supported as key / values:


Redis clients

To connect the Redis server you need to have client library.

From UNIX/Linux command line you can use program called redis-cli:

redis-cli -h hostname -p port -a password

If the server runs on default port 6379, you can omit -p argument.

If the server runs on localhost (127.0.0.1), you can omit -h argument.


Basic commands

There are two basic commands are SET and GET.

In this example we define key "abc" and set its value to 56:

redis 127.0.0.1> set abc 56
OK

In this example we get the value of key "abc":

redis 127.0.0.1> get abc
"56"

PHP examples

MySQL cache in PHP

$r = new Redis();
$r->connect("127.0.0.1", "6379");

$id = (int) $_REQUEST["id"];
$sql = "select * from users where id = $id";

// Create a hash key
$key = "sql_cache:" . md5($sql);

// Check if data is in cache.
if ($data = @unseralize( $r->get($key) ) === false){
	$data = exec_db_query($sql);
	
	// Put data into cache for 1 hour
	$r->set($key, serialize($data) );
	$r->expires($key, 3600);
}

display_data($data);

We moved all other programming examples here:

Code Library


PHP session handler

Another example is scaling PHP/MySQL servers using phpredis extension.

If you have large web application running on two or more servers, you need to use PHP with sticky-sessions.

Sticky-sessions are performance killers, because traffic and visitors can not be "balanced" between the servers.

Example show modified php.ini that install redis as session handler. Using this setting all servers to store session data on single place, probably best place on MySQL database server.

Notice all this is achieved, without any complicated and slow stuff such MySQL sessions or msession extension.

Setting sessions data into the memory also guarantee much better performance than disk based "file" sessions.

extension=redis.so

session.save_handler = redis
session.save_path = "tcp://localhost:6379"

We moved all other articles here:

Articles Library


Where to find more information?

More information can be found on Redis site:
http://redis.io/.

Redis documentation can be found here:
http://redis.io/documentation.

Full command list can be found here:
http://redis.io/commands.

Redis Cookbook:
http://rediscookbook.org/.

Redis tutorial:
http://simonwillison.net/static/2010/redis-tutorial/.


Some videos about Redis

Redis - The Hacker's Database
from GoogleTechTalks

The Beauty of Simplicity: Mastering Database Design Using Redis
by Ryan Briones from ChicagoRuby


Slides

Why Is NoSQL So Darn Popular?
by John Nunemaker

Redis -- Memory as the New Disk
by Tim Lossen


Comments

blog comments powered by Disqus
© Jul.2011 - 2017, E-Nick.org
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Cookie Policy.