There are many fast data stores, and then there is Redis. Learn about this excellent NoSQL solution that is a powerful in-memory key-value store. Learn how to solve traditionally difficult problems with Redis, and how you can benefit from 100,000 reads/writes a second on commodity hardware. We’ll discuss how and when to use the different datatypes and commands to fit your needs. We’ll discuss the different PHP libraries with their pros and cons. We’ll then show some live examples on how to use it for a chatroom, and how Redis manages a billion data points for our dating matching system. Finally, we’ll discuss some of the upcoming features in the near future, such as clustering and scripting.
2. edis)
About Me (I <3
R
• Director of Development
for DeseretNews.com
• CIO of CEVO
• I Make (and Break)
Web Stuff
• Focus on Scalable,
Real-time Websites
& APIs
3. About Presentation
• We’ll ask for questions
several times during
presentation & the end.
• I will post links, slides,
resource, etc.
• Goal: Educate & Inspire
you on how, when, and
why to use Redis!
19. Real-Time Data
• High Volume Writes
• High Volume Reads
• Low Latency / Delay
• Grows Exponentially
compared to Rest of
your Data
20. Traditional Data Stores
• Examples: Databases (i.e.
MySQL, Postgre)
• Writing is Resource
Intensive
• Stored on Disk (aka
Slower)
• Slower as Volume Grows ds...)
or
the rw
• Challenge for Replication
(i no
21. olu me
ighV
ith H ites
s w Wr
wn s &
el tdo ead
M R
32. Step One:
Install & Run
• Linux & Mac: Compile
from Source
• No Dependencies
• make && make install
• Windows
• Download Binaries
• Compile? Best of Luck
33. Running Redis
• Run: redis-server
• Can use configuration
file
• Run as a “service”:
http://redis.io/topics/
quickstart
• redis-cli to try out
commands
35. Redis Keys
• Keys MUST be UNIQUE
• Keys are Binary Safe Strings
• Super Long Keys (i.e. 1024 bytes) are costly to
look up
• Cryptic Short Keys (i.e. u:1000:pwd) have
little performance improvement over a
descriptive key (i.e. user:1000:password)
42. Quick Variable Guide
• Hashes - Small in Size,Very
Efficient
• Lists - Awesome Queues,
Size Doesn’t Affect
Performance
• Sets - Great for
Intersecting with others
• Sorted Sets - Use to Keep
“Indexes”, Sorts using
Scores
44. Learning the
Commands
• Generic Commands for
All Keys
• Commands for Each
Data Type
• Each Command has
Big O Notation for
Performance
• Simple,Yet Powerful
48. Using Redis With PHP
• Predis
• PHP Library
• Easy to Use
• Very Fast to Update
New Features
• phpredis
• PHP Extension
ing
• Faster, but requires Be Us
compiling module e ’ll dis
W Pre
50. Simple Cache
• Data Types:
• Strings
• Commands:
• SETEX <key> <seconds to expire> <value>
• GET <key>
• EXPIREAT <key> <timestamp>
51. Connecting
<?php
// Include the Predis Autoloader
require 'predis/lib/Predis/Autoloader.php';
// Register the Autoloader
PredisAutoloader::register();
// Create a Client with defaults
$redis = new PredisClient();
// Create with Connection String
$redis = new PredisClient('tcp://10.0.0.1:6379');
/** Our Examples Will Assume This Is Already Done **/
52. Simple Cache
$key = 'cache.user:justin';
$data_str = $redis->get($key);
if($data_str)
{
! $data = unserialize($data_str);
}
else
{
! // Really Expensive Method of Getting This Data
! $data = MyDatabase::GetExpensiveData();
! $redis->setex($key, 60, serialize($data));
}
/* Do something with the $data */
55. Marking Users Online
/* Store Current User */
// Current User ID
$user_id = 1234;
// Get Current Time
$now = time();
$min = date("i",$now);
// Generate the Key
$key = "online:".$min;
// Adding user to online users
$redis->sadd($key, $user_id);
$redis->expire($key, 60 * 10); // Expire in 10 minutes
60. Few Things About Redis
• Single Threaded
• Can “Shard” For More Capacity /
Performance
• All Commands are Atomic
• Transactions for Multiple Atomic
Commands
• Pipelining for High Performance
61. Persistence
• Snapshots on a Configurable Schedule
• Will “fork” the process and write the
DataSet to Disk
• Append-Only File
• Will Let You Survive a “Reboot”
62. New Stuff / On The
Horizon
• Lua Scripting (Kinda Querying)
• Redis Clustering