SlideShare uma empresa Scribd logo
1 de 55
Baixar para ler offline
Rediscover Speed With
Redis (and PHP)
Errazudin Ishak
Agenda
About Me
What on earth
For what reason
So how to do that
Can I use it with
Ok, now what
Summary
ABOUT ME
Day job
Staff Engineer @ Mimos Bhd Malaysia
Focuses on web application development,
  deployment, performance, security and
  stability.
I was here..
2009
foss.my , MyGOSSCON

2010
Entp. PHP Techtalk, BarcampKL, PHP Meetup, MOSC2010,
   PHP Northwest UK, MyGOSSCON

2011
INTAN Tech Update, Wordpress Conf. Asia, Joomla! Day, MOSC,
   OWASP Day

2012
OWASP Appsec Asia Pacific, Australia
WHAT ON EARTH
NoSQL Family
Key value stores : Redis, Voldemort,
 Cassandra
NoSQL Family
Column oriented : cass, hbase
NoSQL Family
Doc collection db : CouchDB, MongoDB
NoSQL Family
Graph DB : Neo4j, AllegroGraph
Redis
REmote Dictionary Server
Advanced Key-value store
Disk backed In-memory database (with virt
  mem)
High write throughput (30-150k ops/sec)
Data structures (list, hashes, sets, atomic
  ops)
Redis, unique?
Simple, lightweight
In memory (RAM).. Fast, fast, fast (very!)
Disk-backed, background writes
Master-slave config.
Multi language support
FOR WHAT REASON
Source : http://goo.gl/CM7wq




      “Memory is the new disk. Disk is the
           new tape.” - Jim Gray
Source : http://www.infoq.com/news/2008/06/ram-is-disk
Issue : Write heavy workload
Scaling reads : easy
Scaling write : headache
Advanced key-value store
Persistence
Replication
Transaction
Pipelining
Publish/Subscribe
Use cases
Realtime analytics
Caching server (memcached on steroid)
Queue (scheduler, take time to process)
Clicks (eg. Reddit, digg)
Who




  “We also use Redis extensively; it
powers our main feed, our activity feed,
       our sessions system…”
Who
Who
• flickr
Who
Who
Who
SO HOW TO DO THAT
Get, Set.. Go!
Installation
Download, extract and compile
http://redis.io/download

…and redis-server and redis-cli, ready to
 rumble
Get, Set.. Go!
$ wget http://redis.googlecode.com/files/redis-
  2.4.15.tar.gz
$ tar xzf redis-2.4.15.tar.gz
$ cd redis-2.4.15
$ make
Data Structure server

                 Strings
         SET, GET, SETRANGE, INCR, DECR


                   Lists
         LPUSH, RPUSH, RPOP, LRANGE…


                    Sets
        SADD, SINTER, SMEMBER, ZADD …



                 Hashes
             HSET, HMSET, HGETALL
Strings
Basic
512MB
As atomic counters
Append to Strings
Random access
Encode lots with little space
Lists
List of Strings

Max length of a list is 232 - 1 elements
 (4294967295, more than 4 billion of
 elements per list).
Sets
Collection of Strings (unordered)

Support a number of server side commands
  to compute sets

Max number of members in a set is 232 - 1
 (4294967295, more than 4 billion of
 members per set).
Hashes
Maps between string fields and string
 values
CAN I USE IT WITH …
PHP : Strings
$src/redis-cli
redis> set hello earth
OK
redis> get hello
“earth”

<?php
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
$redis->set(„hello',„earth');
$stored = $redis->get(„hello');
echo $stored;
PHP : Strings
$src/redis-cli
redis>set mosc"{"a":"1","b":"2"}“
OK
redis>get mosc
"{"a":"1","b":"2"}“

<?php
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
$data = array('a'=>'1','b'=>'2');
$json = json_encode($data);
$redis->set(„mosc',$json);
$stored = $redis->get(„mosc');
echo $stored;
PHP : Lists
redis>LPUSH mylist “earth"(integer)1
redis>LPUSH mylist "hello"(integer)2
//[„earth','hello']
redis>LRANGE mylist 0-1
1)"hello“
2)“earth"
PHP : Lists
<?php
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
$redis->delete('key1');
$redis->rpush('key1','A');//returns 1
$redis->rpush('key1','B');//returns 2
$redis->rpush('key1','C');//returns 3
$redis->rpush('key1','A');//returns 4
/*key1nowpointstothefollowinglist:['A','B','C','A']*/
PHP : Sets
<?php
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
$redis->delete('key2');
$redis->sadd('key2','A');//returns 1
$redis->sadd('key2','B');//returns 2
$redis->sadd('key2','C');//returns 3
$redis->sadd('key2','A');//returns false
/*key2nowpointstothefollowinglist:['A','B','C']*/
PHP : Hashes
redis>hmset firsthash a "1“ b “2“ c "3“ d "4“
OK
redis>hget firsthash c
"3“
redis>hset firsthash e "5“
(integer) 1
redis>hget firsthash e
"5“
redis>hget firsthash d
"4"
PHP : Hashes
<?php
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
$redis->delete('user:1');
$redis->hmset('user:1',
array('name'=>„Zack','salary'=>3000));
//Give Zack a $200 Raise:
$redis->hincrby('user:1','salary',200);
PHP : Hashes
<?php
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
$redis->hmset('user:2',
array('name'=>„Ali','salary'=>5000));
$redis->hmset('user:3',
array('name'=>„Jonah','salary'=>6000));
$uid=3;
$user=$redis->hgetall('user:'.$uid)
//{name:„Jonah',salary:6000}
echo $user['salary'];//6000
PHP : Pub/Sub
redis>subscribe chn801




redis>subscribe chn611
PHP : Pub/Sub
redis>publish chn801 “Kelantan 6-0 Perak”




redis>subscribe chn801
Reading messages…
1) “subscribe”
2) “chn801”
3) (integer) 1
1) “message”
2) “chn801”
3) “Kelantan 6-0 Perak”
PHP Clients
Predis
https://github.com/nrk/predis

Phpredis
https://github.com/nicolasff/phpredis
OK, NOW WHAT
Source : http://goo.gl/sPZQ6




“Redis is more than a key-value store,
   it’s a lifestyle” – Mathias Meyer
Redis Cluster?
Target : Redis 2.6
Unstable branch – Basis/fundamental parts
Release when rock solid and useful
End of 2012?
SUMMARY
“Different technologies excel at
 different things” – Weixi Yen
Resources
Redis commands
http://redis.io/commands

Redis Manifesto
http://antirez.com/post/redis-
  manifesto.html
Resources
Redis Cookbook
Resources
http://simonwillison.net/static/2010/redis-
  tutorial/
Diving deeper?
Peter Cooper’s
http://www.scribd.com/doc/33531219/Redi
  s-Presentation

Pre-order
Redis: The Definitive Guide
(Data modeling, caching, and
messaging)
Diving deeper?
Scaling Redis
http://petrohi.me/post/6323289515/scalin
  g-redis

Instagram Engineering blog
http://instagram-engineering.tumblr.com/
Rediscover Speed with Redis(and PHP)

Mais conteúdo relacionado

Destaque

Speed up your Symfony2 application and build awesome features with Redis
Speed up your Symfony2 application and build awesome features with RedisSpeed up your Symfony2 application and build awesome features with Redis
Speed up your Symfony2 application and build awesome features with RedisRicard Clau
 
Europycon2011: Implementing distributed application using ZeroMQ
Europycon2011: Implementing distributed application using ZeroMQEuropycon2011: Implementing distributed application using ZeroMQ
Europycon2011: Implementing distributed application using ZeroMQfcrippa
 
ZeroMQ Is The Answer
ZeroMQ Is The AnswerZeroMQ Is The Answer
ZeroMQ Is The AnswerIan Barber
 
PHP with Service BUS (RabbitMQ/Redis/MongoDB) - IMasters PHP Experience 2016
PHP with Service BUS (RabbitMQ/Redis/MongoDB) - IMasters PHP Experience 2016PHP with Service BUS (RabbitMQ/Redis/MongoDB) - IMasters PHP Experience 2016
PHP with Service BUS (RabbitMQ/Redis/MongoDB) - IMasters PHP Experience 2016Alexandre Brandão Lustosa
 
Redis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your applicationRedis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your applicationrjsmelo
 
Overview of ZeroMQ
Overview of ZeroMQOverview of ZeroMQ
Overview of ZeroMQpieterh
 

Destaque (6)

Speed up your Symfony2 application and build awesome features with Redis
Speed up your Symfony2 application and build awesome features with RedisSpeed up your Symfony2 application and build awesome features with Redis
Speed up your Symfony2 application and build awesome features with Redis
 
Europycon2011: Implementing distributed application using ZeroMQ
Europycon2011: Implementing distributed application using ZeroMQEuropycon2011: Implementing distributed application using ZeroMQ
Europycon2011: Implementing distributed application using ZeroMQ
 
ZeroMQ Is The Answer
ZeroMQ Is The AnswerZeroMQ Is The Answer
ZeroMQ Is The Answer
 
PHP with Service BUS (RabbitMQ/Redis/MongoDB) - IMasters PHP Experience 2016
PHP with Service BUS (RabbitMQ/Redis/MongoDB) - IMasters PHP Experience 2016PHP with Service BUS (RabbitMQ/Redis/MongoDB) - IMasters PHP Experience 2016
PHP with Service BUS (RabbitMQ/Redis/MongoDB) - IMasters PHP Experience 2016
 
Redis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your applicationRedis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your application
 
Overview of ZeroMQ
Overview of ZeroMQOverview of ZeroMQ
Overview of ZeroMQ
 

Último

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 

Último (20)

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 

Rediscover Speed with Redis(and PHP)