SlideShare a Scribd company logo
1 of 35
Download to read offline
REDIS
EVERYWHERE
Ricard Clau (@ricardclau)
PHP London - 1st August 2013
HELLO WORLD
• Ricard Clau, born and grown up in Barcelona
• Software engineer at Hailo
• Symfony2 lover and PHP believer
• Open-source contributor, sometimes I give talks
• Twitter @ricardclau / Gmail ricard.clau@gmail.com
WHAT IS REDIS?
• REmote DIctionary Server
• Created in 2009 by Salvatore Sanfilipo (@antirez)
• Open source
• Advanced in-memory key-value data-structure server
• Stop thinking SQL, think back to structures!
TO ME REDIS IS LIKE...
AGENDA
• REDIS introduction
• Getting started with PHP / Symfony2: sessions, caching
• Cookbooks: queues, statistics, leaderboards, users online,
friends online, locking
• War Stories from a 7.5M DAU project
• Final thoughts
INTRODUCTIONTO REDIS
And some demystifications
DATATYPES
• Strings up to 512 Mb
• Lists of elements sorted by insertion order (max 2^32 - 1)
• Sets unordered collection of elements supporting unions,
intersections and differences (max 2^32 - 1)
• Hashes key-value pairs (max 2^32 - 1)
• Sorted sets automatically ordered by score (max 2^32 - 1)
ONLY IN-MEMORY?
• Your data needs to fit in your memory
• It has configurable persistance: RDB snapshots,AOF
persistence logs, combine both or no persistance at all
• Think like memcached on steroids with persistance
• http://redis.io/topics/persistence
• “Memory is the new disk, disk is the new tape”
INTERESTING FEATURES
• Master-slave replication
• Pipelining to improve performance
• Transactions (kind of with MULTI ... EXEC)
• Publisher / Subscriber
• Scripting with LUA (~stored procedures)
• Predictable performance (check commands complexity)
SHARDING DATA
• Redis cluster will be available in 3.0 (delayed every year...)
• Project Twemproxy can help sharding memcached / redis
• Client-side partitioning is enough for most cases
• Range partitioningVS hash partitioning
• Presharding can help scaling horizontally
HARDWARETIPS
• Redis is single-threaded, so a 2 core machine is enough
• If using EC2, maybe m1.large (7.5Gb) is the most suitable,
but if it is not enough you might consider any m2 memory
optimized type
• CPU is rarely the bottleneck with Redis
• Read carefully the doc to maximize performance!
GETTING STARTED WITH PHP
It is easier than you may think!
PHP CLIENTS
• http://redis.io/clients
• Predis (PHP) vs phpredis (PHP extension)
• Predis is very mature, actively maintained, feature complete,
extendable, composer friendly and supports all Redis versions
• Phpredis is faster, but not backwards compatible
• Network latency is the biggest performance killer
REDIS-CLI AND MONITOR
• Redis-cli to connect to a Redis instance
• With Monitor we can watch live activity!
SESSIONS, CACHING
• Super-easy to implement with commercial frameworks
• Commands used: GET <session-id>, SETEX <session-id>
<expires> <serialized-data>
• Much better performance than PDO and also persistent!
• Caching and temp-data storage work exactly equal
• Be careful with temp-data storage and memory usage!
SYMFONY2 INTEGRATION
• There must be a bundle for that...
• https://github.com/snc/SncRedisBundle
• Out of the box: Session management, Monolog handler,
Swiftmailer spooling, Doctrine caching
• Full integration with Symfony2 profiler
REDIS EVERYWHERE
SOME COOKBOOKS
And comparisons with other technologies
QUEUE PROCESSING
• Using Lists and LPUSH / RPOP instructions
• We can have different languages accessing data
• We used that to send OpenGraph requests to Facebook
(REALLY slow API) with Python daemons
• ~80k messages/minute processed with 1 m1.large EC2
• If intensive, consider using RabbitMQ,ActiveMQ, ZeroMQ...
REAL-TIME ANALYTICS
• We can use hashes to group many stats under one key
• Most of the times we have counters:
HINCRBY “stats" <key> <increment>
• HMGET “stats” <key1> <key2> ... to retrieve values and
HMSET “stats”“stat1” <value1> “stat2” <value2> to set them
• HGETALL to get the full hash
LEADERBOARDS
• Super-easy with Redis, heavy consuming with other systems
• Each board with a single key, using sorted sets
• ZINCRBY “rankXXX” <increment> <userId>
• Top N ZREVRANGE “rankXXX” 0 N [WITHSCORES]
• We stored several milions of members under 1 key
WHO IS ONLINE? (I)
• We can use SETS to achieve it!
• One set for every minute, SADD <minute> <userId>
Time +1m +2m +3m
2
2
7
2
3
1
4 1
1
1
3
5
SUNION
2
1
3
5
7
4
WHO IS ONLINE? (II)
• Online users == everyone active in the last N minutes
• SUNION <now> <now-1> <now-2> ...
• SUNIONSTORE “online” <now> <now-1> <now-2> ...
• Number of users: SCARD “online”
• Obtain online users with SMEMBERS “online”
FRIENDS ONLINE?
• If we store user´s friends in a SET with key friends-<user-id>
• Friends online: SINTERSTORE “friends-<userid>-online”
“online”“friends-<userid>”
• Imagine how you would do it without Redis!
7
1
3
5
2
4
15
9
3
10
7
1
ONLINE
FRIENDS-12
SINTER
FRIENDS-12-ONLINE
DISTRIBUTED LOCKING (I)
• Problem in heavy-write applications: 2 almost concurrent
requests trying to update same records
• SELECT FOR UPDATE? Easy to create deadlocks!
timeline
R1 R2
R1read
R2save
R2read
R1save
R1 updates
are lost!!!!!
DISTRIBUTED LOCKING (II)
• Locking can be solved with Redis: SETNX returns true only
if key does not exist.
• PseudoCode: try SETNX <lock> <time+expires>, if false
check if existing lock has expired and overwrite if so.
Otherwise, sleep some milliseconds and try to acquire
again. Remember to release lock at the end!
• 8 m1.xlarge instances to lock 7.5M Daily Active Users
• Alternatives:Apache Zookeeper, perhaps better for scaling
WAR STORIES
http://apps.facebook.com/dragoncity/
(~7.5M DAU Facebook Game)
PROGRESSIVELY
• DragonCity had ~2M DAU when we introduced Redis
• First usages: Queues PHP-Python and Temporary storage
• Introduced Locking with Redis -> End of deadlocks
• Used for temporary contests involving rankings
• Session tracking, cheat detection and many others
• About 7.5M DAU and similar amount of servers
REDIS IS SUPER-FAST
• Your network gets killed much before Redis is down
• Always increase your ulimit values in Redis servers
• We had Gigabit network connectivity between machines but
it was not enough to handle the data sent and retrieved
• Redis was just at 20% of CPU
• Do some heavy-load tests before launching!
SURPRISES WITH MEMORY
• Redis documentation:“Sets of binary-safe strings”so,
we changed values in sets from being just numbers to number
+ :<char> to add some info to values
• We expected ~20% memory increase but in was 500%
• https://github.com/antirez/redis/blob/unstable/src/t_set.c#L55
• If value can be represented as long, it is stored more efficiently
• http://alonso-vidales.blogspot.co.uk/2013/06/not-all-redis-
values-are-strings.html
FINALTHOUGHTS
When and when not to consider Redis
REDIS IS PERFECT FOR...
• Intensive read-write data applications
• Temporary stored data
• Data that fits in memory
• Problems that fit Redis built-in data types
• Predictability in performance needed (all commands
complexity documented)
BUT IS NOT SUITABLE WHEN..
• Big data sets, archive data
• Relational data (RDBMS are absolutely fine to scale)
• We don´t know how we will access data
• Reporting applications (no where clauses)
• ALWAYS choose the right tool for the job!
SPECIALTHANKS
• Ronny López (@ronnylt) & AlonsoVidales (@alonsovidales)
• All backend and systems engineers at @socialpoint
• Of course, to all of you for coming the 1st of August!
• Check our open-source project Redtrine, PR welcome:
https://github.com/redtrine/redtrine
QUESTIONS?
• Twitter: @ricardclau
• E-mail: ricard.clau@gmail.com
• Github: https://github.com/ricardclau
• Blog about PHP and Symfony2: http://www.ricardclau.com
• Hailo is hiring! If you are interested, talk to us or apply!

More Related Content

What's hot

Redis Developers Day 2014 - Redis Labs Talks
Redis Developers Day 2014 - Redis Labs TalksRedis Developers Day 2014 - Redis Labs Talks
Redis Developers Day 2014 - Redis Labs TalksRedis Labs
 
MariaDB Server & MySQL Security Essentials 2016
MariaDB Server & MySQL Security Essentials 2016MariaDB Server & MySQL Security Essentials 2016
MariaDB Server & MySQL Security Essentials 2016Colin Charles
 
Distributions from the view a package
Distributions from the view a packageDistributions from the view a package
Distributions from the view a packageColin Charles
 
Meet MariaDB 10.1 at the Bulgaria Web Summit
Meet MariaDB 10.1 at the Bulgaria Web SummitMeet MariaDB 10.1 at the Bulgaria Web Summit
Meet MariaDB 10.1 at the Bulgaria Web SummitColin Charles
 
Meet MariaDB Server 10.1 London MySQL meetup December 2015
Meet MariaDB Server 10.1 London MySQL meetup December 2015Meet MariaDB Server 10.1 London MySQL meetup December 2015
Meet MariaDB Server 10.1 London MySQL meetup December 2015Colin Charles
 
Cool MariaDB Plugins
Cool MariaDB Plugins Cool MariaDB Plugins
Cool MariaDB Plugins Colin Charles
 
My first moments with MongoDB
My first moments with MongoDBMy first moments with MongoDB
My first moments with MongoDBColin Charles
 
The Complete MariaDB Server tutorial
The Complete MariaDB Server tutorialThe Complete MariaDB Server tutorial
The Complete MariaDB Server tutorialColin Charles
 
MariaDB 10.1 what's new and what's coming in 10.2 - Tokyo MariaDB Meetup
MariaDB 10.1   what's new and what's coming in 10.2 - Tokyo MariaDB MeetupMariaDB 10.1   what's new and what's coming in 10.2 - Tokyo MariaDB Meetup
MariaDB 10.1 what's new and what's coming in 10.2 - Tokyo MariaDB MeetupColin Charles
 
MariaDB - the "new" MySQL is 5 years old and everywhere (LinuxCon Europe 2015)
MariaDB - the "new" MySQL is 5 years old and everywhere (LinuxCon Europe 2015)MariaDB - the "new" MySQL is 5 years old and everywhere (LinuxCon Europe 2015)
MariaDB - the "new" MySQL is 5 years old and everywhere (LinuxCon Europe 2015)Colin Charles
 
MariaDB Server Compatibility with MySQL
MariaDB Server Compatibility with MySQLMariaDB Server Compatibility with MySQL
MariaDB Server Compatibility with MySQLColin Charles
 
Securing your MySQL / MariaDB Server data
Securing your MySQL / MariaDB Server dataSecuring your MySQL / MariaDB Server data
Securing your MySQL / MariaDB Server dataColin Charles
 
Lessons from database failures
Lessons from database failures Lessons from database failures
Lessons from database failures Colin Charles
 
M|18 Running MariaDB TX on Containers
M|18 Running MariaDB TX on ContainersM|18 Running MariaDB TX on Containers
M|18 Running MariaDB TX on ContainersMariaDB plc
 
The MySQL Server ecosystem in 2016
The MySQL Server ecosystem in 2016The MySQL Server ecosystem in 2016
The MySQL Server ecosystem in 2016Colin Charles
 
MySQL in the Hosted Cloud - Percona Live 2015
MySQL in the Hosted Cloud - Percona Live 2015MySQL in the Hosted Cloud - Percona Live 2015
MySQL in the Hosted Cloud - Percona Live 2015Colin Charles
 
Lessons from database failures
Lessons from database failuresLessons from database failures
Lessons from database failuresColin Charles
 
MariaDB: in-depth (hands on training in Seoul)
MariaDB: in-depth (hands on training in Seoul)MariaDB: in-depth (hands on training in Seoul)
MariaDB: in-depth (hands on training in Seoul)Colin Charles
 
Managing 50K+ Redis Databases Over 4 Public Clouds ... with a Tiny Devops Team
Managing 50K+ Redis Databases Over 4 Public Clouds ... with a Tiny Devops TeamManaging 50K+ Redis Databases Over 4 Public Clouds ... with a Tiny Devops Team
Managing 50K+ Redis Databases Over 4 Public Clouds ... with a Tiny Devops TeamRedis Labs
 

What's hot (20)

Redis Developers Day 2014 - Redis Labs Talks
Redis Developers Day 2014 - Redis Labs TalksRedis Developers Day 2014 - Redis Labs Talks
Redis Developers Day 2014 - Redis Labs Talks
 
MariaDB Server & MySQL Security Essentials 2016
MariaDB Server & MySQL Security Essentials 2016MariaDB Server & MySQL Security Essentials 2016
MariaDB Server & MySQL Security Essentials 2016
 
Distributions from the view a package
Distributions from the view a packageDistributions from the view a package
Distributions from the view a package
 
Meet MariaDB 10.1 at the Bulgaria Web Summit
Meet MariaDB 10.1 at the Bulgaria Web SummitMeet MariaDB 10.1 at the Bulgaria Web Summit
Meet MariaDB 10.1 at the Bulgaria Web Summit
 
Meet MariaDB Server 10.1 London MySQL meetup December 2015
Meet MariaDB Server 10.1 London MySQL meetup December 2015Meet MariaDB Server 10.1 London MySQL meetup December 2015
Meet MariaDB Server 10.1 London MySQL meetup December 2015
 
Cool MariaDB Plugins
Cool MariaDB Plugins Cool MariaDB Plugins
Cool MariaDB Plugins
 
My first moments with MongoDB
My first moments with MongoDBMy first moments with MongoDB
My first moments with MongoDB
 
The Complete MariaDB Server tutorial
The Complete MariaDB Server tutorialThe Complete MariaDB Server tutorial
The Complete MariaDB Server tutorial
 
MariaDB 10.1 what's new and what's coming in 10.2 - Tokyo MariaDB Meetup
MariaDB 10.1   what's new and what's coming in 10.2 - Tokyo MariaDB MeetupMariaDB 10.1   what's new and what's coming in 10.2 - Tokyo MariaDB Meetup
MariaDB 10.1 what's new and what's coming in 10.2 - Tokyo MariaDB Meetup
 
MariaDB - the "new" MySQL is 5 years old and everywhere (LinuxCon Europe 2015)
MariaDB - the "new" MySQL is 5 years old and everywhere (LinuxCon Europe 2015)MariaDB - the "new" MySQL is 5 years old and everywhere (LinuxCon Europe 2015)
MariaDB - the "new" MySQL is 5 years old and everywhere (LinuxCon Europe 2015)
 
MariaDB Server Compatibility with MySQL
MariaDB Server Compatibility with MySQLMariaDB Server Compatibility with MySQL
MariaDB Server Compatibility with MySQL
 
Securing your MySQL / MariaDB Server data
Securing your MySQL / MariaDB Server dataSecuring your MySQL / MariaDB Server data
Securing your MySQL / MariaDB Server data
 
MySQL highav Availability
MySQL highav AvailabilityMySQL highav Availability
MySQL highav Availability
 
Lessons from database failures
Lessons from database failures Lessons from database failures
Lessons from database failures
 
M|18 Running MariaDB TX on Containers
M|18 Running MariaDB TX on ContainersM|18 Running MariaDB TX on Containers
M|18 Running MariaDB TX on Containers
 
The MySQL Server ecosystem in 2016
The MySQL Server ecosystem in 2016The MySQL Server ecosystem in 2016
The MySQL Server ecosystem in 2016
 
MySQL in the Hosted Cloud - Percona Live 2015
MySQL in the Hosted Cloud - Percona Live 2015MySQL in the Hosted Cloud - Percona Live 2015
MySQL in the Hosted Cloud - Percona Live 2015
 
Lessons from database failures
Lessons from database failuresLessons from database failures
Lessons from database failures
 
MariaDB: in-depth (hands on training in Seoul)
MariaDB: in-depth (hands on training in Seoul)MariaDB: in-depth (hands on training in Seoul)
MariaDB: in-depth (hands on training in Seoul)
 
Managing 50K+ Redis Databases Over 4 Public Clouds ... with a Tiny Devops Team
Managing 50K+ Redis Databases Over 4 Public Clouds ... with a Tiny Devops TeamManaging 50K+ Redis Databases Over 4 Public Clouds ... with a Tiny Devops Team
Managing 50K+ Redis Databases Over 4 Public Clouds ... with a Tiny Devops Team
 

Viewers also liked

Redis Everywhere - Sunshine PHP
Redis Everywhere - Sunshine PHPRedis Everywhere - Sunshine PHP
Redis Everywhere - Sunshine PHPRicard Clau
 
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
 
Building a bakery of Windows servers with Packer - London WinOps
Building a bakery of Windows servers with Packer - London WinOpsBuilding a bakery of Windows servers with Packer - London WinOps
Building a bakery of Windows servers with Packer - London WinOpsRicard Clau
 
Modern software architectures - PHP UK Conference 2015
Modern software architectures - PHP UK Conference 2015Modern software architectures - PHP UK Conference 2015
Modern software architectures - PHP UK Conference 2015Ricard Clau
 
Slide Seminar PHP Indonesia - NoSQL Redis
Slide Seminar PHP Indonesia - NoSQL RedisSlide Seminar PHP Indonesia - NoSQL Redis
Slide Seminar PHP Indonesia - NoSQL Redisrifqi alfian
 
Redis in Practice: Scenarios, Performance and Practice with PHP
Redis in Practice: Scenarios, Performance and Practice with PHPRedis in Practice: Scenarios, Performance and Practice with PHP
Redis in Practice: Scenarios, Performance and Practice with PHPChen Huang
 
[2B5]nBase-ARC Redis Cluster
[2B5]nBase-ARC Redis Cluster[2B5]nBase-ARC Redis Cluster
[2B5]nBase-ARC Redis ClusterNAVER D2
 
Beyond relational database - Building high performance websites using Redis a...
Beyond relational database - Building high performance websites using Redis a...Beyond relational database - Building high performance websites using Redis a...
Beyond relational database - Building high performance websites using Redis a...Dinh Pham
 
MOSC2012 - Building High-Performance Web-Application with PHP & MongoDB
MOSC2012 - Building High-Performance Web-Application with PHP & MongoDBMOSC2012 - Building High-Performance Web-Application with PHP & MongoDB
MOSC2012 - Building High-Performance Web-Application with PHP & MongoDBr1dotmy
 
美团点评沙龙 飞行中换引擎--美团配送业务系统的架构演进之路
美团点评沙龙 飞行中换引擎--美团配送业务系统的架构演进之路美团点评沙龙 飞行中换引擎--美团配送业务系统的架构演进之路
美团点评沙龙 飞行中换引擎--美团配送业务系统的架构演进之路美团点评技术团队
 
Redis 常见使用模式分析
Redis 常见使用模式分析Redis 常见使用模式分析
Redis 常见使用模式分析vincent253
 
美团点评技术沙龙010-Redis Cluster运维实践
美团点评技术沙龙010-Redis Cluster运维实践美团点评技术沙龙010-Redis Cluster运维实践
美团点评技术沙龙010-Redis Cluster运维实践美团点评技术团队
 
Redis/Lessons learned
Redis/Lessons learnedRedis/Lessons learned
Redis/Lessons learnedTit Petric
 
Highly scalable caching service on cloud - Redis
Highly scalable caching service on cloud - RedisHighly scalable caching service on cloud - Redis
Highly scalable caching service on cloud - RedisKrishna-Kumar
 
Scaling php applications with redis
Scaling php applications with redisScaling php applications with redis
Scaling php applications with redisjimbojsb
 
深入了解Redis
深入了解Redis深入了解Redis
深入了解Redisiammutex
 
Using Redis at Facebook
Using Redis at FacebookUsing Redis at Facebook
Using Redis at FacebookRedis Labs
 
Scaling PHP to 40 Million Uniques
Scaling PHP to 40 Million UniquesScaling PHP to 40 Million Uniques
Scaling PHP to 40 Million UniquesJonathan Klein
 
Redis cluster
Redis clusterRedis cluster
Redis clusteriammutex
 

Viewers also liked (20)

Redis Everywhere - Sunshine PHP
Redis Everywhere - Sunshine PHPRedis Everywhere - Sunshine PHP
Redis Everywhere - Sunshine PHP
 
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
 
Building a bakery of Windows servers with Packer - London WinOps
Building a bakery of Windows servers with Packer - London WinOpsBuilding a bakery of Windows servers with Packer - London WinOps
Building a bakery of Windows servers with Packer - London WinOps
 
Modern software architectures - PHP UK Conference 2015
Modern software architectures - PHP UK Conference 2015Modern software architectures - PHP UK Conference 2015
Modern software architectures - PHP UK Conference 2015
 
Slide Seminar PHP Indonesia - NoSQL Redis
Slide Seminar PHP Indonesia - NoSQL RedisSlide Seminar PHP Indonesia - NoSQL Redis
Slide Seminar PHP Indonesia - NoSQL Redis
 
Redis in Practice: Scenarios, Performance and Practice with PHP
Redis in Practice: Scenarios, Performance and Practice with PHPRedis in Practice: Scenarios, Performance and Practice with PHP
Redis in Practice: Scenarios, Performance and Practice with PHP
 
[2B5]nBase-ARC Redis Cluster
[2B5]nBase-ARC Redis Cluster[2B5]nBase-ARC Redis Cluster
[2B5]nBase-ARC Redis Cluster
 
Beyond relational database - Building high performance websites using Redis a...
Beyond relational database - Building high performance websites using Redis a...Beyond relational database - Building high performance websites using Redis a...
Beyond relational database - Building high performance websites using Redis a...
 
MOSC2012 - Building High-Performance Web-Application with PHP & MongoDB
MOSC2012 - Building High-Performance Web-Application with PHP & MongoDBMOSC2012 - Building High-Performance Web-Application with PHP & MongoDB
MOSC2012 - Building High-Performance Web-Application with PHP & MongoDB
 
美团点评沙龙 飞行中换引擎--美团配送业务系统的架构演进之路
美团点评沙龙 飞行中换引擎--美团配送业务系统的架构演进之路美团点评沙龙 飞行中换引擎--美团配送业务系统的架构演进之路
美团点评沙龙 飞行中换引擎--美团配送业务系统的架构演进之路
 
Redis 常见使用模式分析
Redis 常见使用模式分析Redis 常见使用模式分析
Redis 常见使用模式分析
 
美团点评技术沙龙010-Redis Cluster运维实践
美团点评技术沙龙010-Redis Cluster运维实践美团点评技术沙龙010-Redis Cluster运维实践
美团点评技术沙龙010-Redis Cluster运维实践
 
Redis 101
Redis 101Redis 101
Redis 101
 
Redis/Lessons learned
Redis/Lessons learnedRedis/Lessons learned
Redis/Lessons learned
 
Highly scalable caching service on cloud - Redis
Highly scalable caching service on cloud - RedisHighly scalable caching service on cloud - Redis
Highly scalable caching service on cloud - Redis
 
Scaling php applications with redis
Scaling php applications with redisScaling php applications with redis
Scaling php applications with redis
 
深入了解Redis
深入了解Redis深入了解Redis
深入了解Redis
 
Using Redis at Facebook
Using Redis at FacebookUsing Redis at Facebook
Using Redis at Facebook
 
Scaling PHP to 40 Million Uniques
Scaling PHP to 40 Million UniquesScaling PHP to 40 Million Uniques
Scaling PHP to 40 Million Uniques
 
Redis cluster
Redis clusterRedis cluster
Redis cluster
 

Similar to Redis everywhere - PHP London

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
 
Big Data Developers Moscow Meetup 1 - sql on hadoop
Big Data Developers Moscow Meetup 1  - sql on hadoopBig Data Developers Moscow Meetup 1  - sql on hadoop
Big Data Developers Moscow Meetup 1 - sql on hadoopbddmoscow
 
Add Redis to Postgres to Make Your Microservices Go Boom!
Add Redis to Postgres to Make Your Microservices Go Boom!Add Redis to Postgres to Make Your Microservices Go Boom!
Add Redis to Postgres to Make Your Microservices Go Boom!Dave Nielsen
 
New York REDIS Meetup Welcome Session
New York REDIS Meetup Welcome SessionNew York REDIS Meetup Welcome Session
New York REDIS Meetup Welcome SessionAleksandr Yampolskiy
 
Redis - The Universal NoSQL Tool
Redis - The Universal NoSQL ToolRedis - The Universal NoSQL Tool
Redis - The Universal NoSQL ToolEberhard Wolff
 
What's new with enterprise Redis - Leena Joshi, Redis Labs
What's new with enterprise Redis - Leena Joshi, Redis LabsWhat's new with enterprise Redis - Leena Joshi, Redis Labs
What's new with enterprise Redis - Leena Joshi, Redis LabsRedis Labs
 
IBM Internet-of-Things architecture and capabilities
IBM Internet-of-Things architecture and capabilitiesIBM Internet-of-Things architecture and capabilities
IBM Internet-of-Things architecture and capabilitiesIBM_Info_Management
 
Amazon RDS for MySQL – Diagnostics, Security, and Data Migration (DAT302) | A...
Amazon RDS for MySQL – Diagnostics, Security, and Data Migration (DAT302) | A...Amazon RDS for MySQL – Diagnostics, Security, and Data Migration (DAT302) | A...
Amazon RDS for MySQL – Diagnostics, Security, and Data Migration (DAT302) | A...Amazon Web Services
 
Big Data! Great! Now What? #SymfonyCon 2014
Big Data! Great! Now What? #SymfonyCon 2014Big Data! Great! Now What? #SymfonyCon 2014
Big Data! Great! Now What? #SymfonyCon 2014Ricard Clau
 
Citrix Synergy 2014 - Syn233 Building and operating a Dev Ops cloud: best pra...
Citrix Synergy 2014 - Syn233 Building and operating a Dev Ops cloud: best pra...Citrix Synergy 2014 - Syn233 Building and operating a Dev Ops cloud: best pra...
Citrix Synergy 2014 - Syn233 Building and operating a Dev Ops cloud: best pra...Citrix
 
Hpc lunch and learn
Hpc lunch and learnHpc lunch and learn
Hpc lunch and learnJohn D Almon
 
NoSQL for great good [hanoi.rb talk]
NoSQL for great good [hanoi.rb talk]NoSQL for great good [hanoi.rb talk]
NoSQL for great good [hanoi.rb talk]Huy Do
 
Citrix Synergy 2014: Going the CloudPlatform Way
Citrix Synergy 2014: Going the CloudPlatform WayCitrix Synergy 2014: Going the CloudPlatform Way
Citrix Synergy 2014: Going the CloudPlatform WayIliyas Shirol
 
10 Ways to Scale Your Website Silicon Valley Code Camp 2019
10 Ways to Scale Your Website Silicon Valley Code Camp 201910 Ways to Scale Your Website Silicon Valley Code Camp 2019
10 Ways to Scale Your Website Silicon Valley Code Camp 2019Dave Nielsen
 
Moving Beyond Cache by Yiftach Shoolman Redis Labs - Redis Day Seattle 2020
Moving Beyond Cache by Yiftach Shoolman Redis Labs - Redis Day Seattle 2020Moving Beyond Cache by Yiftach Shoolman Redis Labs - Redis Day Seattle 2020
Moving Beyond Cache by Yiftach Shoolman Redis Labs - Redis Day Seattle 2020Redis Labs
 

Similar to Redis everywhere - PHP London (20)

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
 
REDIS327
REDIS327REDIS327
REDIS327
 
Redis meetup
Redis meetupRedis meetup
Redis meetup
 
Big Data Developers Moscow Meetup 1 - sql on hadoop
Big Data Developers Moscow Meetup 1  - sql on hadoopBig Data Developers Moscow Meetup 1  - sql on hadoop
Big Data Developers Moscow Meetup 1 - sql on hadoop
 
Add Redis to Postgres to Make Your Microservices Go Boom!
Add Redis to Postgres to Make Your Microservices Go Boom!Add Redis to Postgres to Make Your Microservices Go Boom!
Add Redis to Postgres to Make Your Microservices Go Boom!
 
New York REDIS Meetup Welcome Session
New York REDIS Meetup Welcome SessionNew York REDIS Meetup Welcome Session
New York REDIS Meetup Welcome Session
 
Redis Labcamp
Redis LabcampRedis Labcamp
Redis Labcamp
 
Redis - The Universal NoSQL Tool
Redis - The Universal NoSQL ToolRedis - The Universal NoSQL Tool
Redis - The Universal NoSQL Tool
 
What's new with enterprise Redis - Leena Joshi, Redis Labs
What's new with enterprise Redis - Leena Joshi, Redis LabsWhat's new with enterprise Redis - Leena Joshi, Redis Labs
What's new with enterprise Redis - Leena Joshi, Redis Labs
 
IBM Internet-of-Things architecture and capabilities
IBM Internet-of-Things architecture and capabilitiesIBM Internet-of-Things architecture and capabilities
IBM Internet-of-Things architecture and capabilities
 
Amazon RDS for MySQL – Diagnostics, Security, and Data Migration (DAT302) | A...
Amazon RDS for MySQL – Diagnostics, Security, and Data Migration (DAT302) | A...Amazon RDS for MySQL – Diagnostics, Security, and Data Migration (DAT302) | A...
Amazon RDS for MySQL – Diagnostics, Security, and Data Migration (DAT302) | A...
 
Big Data! Great! Now What? #SymfonyCon 2014
Big Data! Great! Now What? #SymfonyCon 2014Big Data! Great! Now What? #SymfonyCon 2014
Big Data! Great! Now What? #SymfonyCon 2014
 
Citrix Synergy 2014 - Syn233 Building and operating a Dev Ops cloud: best pra...
Citrix Synergy 2014 - Syn233 Building and operating a Dev Ops cloud: best pra...Citrix Synergy 2014 - Syn233 Building and operating a Dev Ops cloud: best pra...
Citrix Synergy 2014 - Syn233 Building and operating a Dev Ops cloud: best pra...
 
Hpc lunch and learn
Hpc lunch and learnHpc lunch and learn
Hpc lunch and learn
 
Mini-Training: Redis
Mini-Training: RedisMini-Training: Redis
Mini-Training: Redis
 
NoSQL for great good [hanoi.rb talk]
NoSQL for great good [hanoi.rb talk]NoSQL for great good [hanoi.rb talk]
NoSQL for great good [hanoi.rb talk]
 
Citrix Synergy 2014: Going the CloudPlatform Way
Citrix Synergy 2014: Going the CloudPlatform WayCitrix Synergy 2014: Going the CloudPlatform Way
Citrix Synergy 2014: Going the CloudPlatform Way
 
10 Ways to Scale Your Website Silicon Valley Code Camp 2019
10 Ways to Scale Your Website Silicon Valley Code Camp 201910 Ways to Scale Your Website Silicon Valley Code Camp 2019
10 Ways to Scale Your Website Silicon Valley Code Camp 2019
 
Hadoop and friends
Hadoop and friendsHadoop and friends
Hadoop and friends
 
Moving Beyond Cache by Yiftach Shoolman Redis Labs - Redis Day Seattle 2020
Moving Beyond Cache by Yiftach Shoolman Redis Labs - Redis Day Seattle 2020Moving Beyond Cache by Yiftach Shoolman Redis Labs - Redis Day Seattle 2020
Moving Beyond Cache by Yiftach Shoolman Redis Labs - Redis Day Seattle 2020
 

More from Ricard Clau

NoEresTanEspecial-PulpoCon22.pdf
NoEresTanEspecial-PulpoCon22.pdfNoEresTanEspecial-PulpoCon22.pdf
NoEresTanEspecial-PulpoCon22.pdfRicard Clau
 
DevOps & Infraestructura como código: Promesas Rotas
DevOps & Infraestructura como código: Promesas RotasDevOps & Infraestructura como código: Promesas Rotas
DevOps & Infraestructura como código: Promesas RotasRicard Clau
 
DevOps Barcelona Conference 2018 - Intro
DevOps Barcelona Conference 2018 - IntroDevOps Barcelona Conference 2018 - Intro
DevOps Barcelona Conference 2018 - IntroRicard Clau
 
Hashicorp at holaluz
Hashicorp at holaluzHashicorp at holaluz
Hashicorp at holaluzRicard Clau
 
What we talk about when we talk about DevOps
What we talk about when we talk about DevOpsWhat we talk about when we talk about DevOps
What we talk about when we talk about DevOpsRicard Clau
 
Scaling with Symfony - PHP UK
Scaling with Symfony - PHP UKScaling with Symfony - PHP UK
Scaling with Symfony - PHP UKRicard Clau
 
Escalabilidad y alto rendimiento con Symfony2
Escalabilidad y alto rendimiento con Symfony2Escalabilidad y alto rendimiento con Symfony2
Escalabilidad y alto rendimiento con Symfony2Ricard Clau
 
Betabeers Barcelona - Buenas prácticas
Betabeers Barcelona - Buenas prácticasBetabeers Barcelona - Buenas prácticas
Betabeers Barcelona - Buenas prácticasRicard Clau
 
Desymfony - Servicios
Desymfony  - ServiciosDesymfony  - Servicios
Desymfony - ServiciosRicard Clau
 

More from Ricard Clau (11)

devopsbcn23.pdf
devopsbcn23.pdfdevopsbcn23.pdf
devopsbcn23.pdf
 
devopsbcn22.pdf
devopsbcn22.pdfdevopsbcn22.pdf
devopsbcn22.pdf
 
NoEresTanEspecial-PulpoCon22.pdf
NoEresTanEspecial-PulpoCon22.pdfNoEresTanEspecial-PulpoCon22.pdf
NoEresTanEspecial-PulpoCon22.pdf
 
DevOps & Infraestructura como código: Promesas Rotas
DevOps & Infraestructura como código: Promesas RotasDevOps & Infraestructura como código: Promesas Rotas
DevOps & Infraestructura como código: Promesas Rotas
 
DevOps Barcelona Conference 2018 - Intro
DevOps Barcelona Conference 2018 - IntroDevOps Barcelona Conference 2018 - Intro
DevOps Barcelona Conference 2018 - Intro
 
Hashicorp at holaluz
Hashicorp at holaluzHashicorp at holaluz
Hashicorp at holaluz
 
What we talk about when we talk about DevOps
What we talk about when we talk about DevOpsWhat we talk about when we talk about DevOps
What we talk about when we talk about DevOps
 
Scaling with Symfony - PHP UK
Scaling with Symfony - PHP UKScaling with Symfony - PHP UK
Scaling with Symfony - PHP UK
 
Escalabilidad y alto rendimiento con Symfony2
Escalabilidad y alto rendimiento con Symfony2Escalabilidad y alto rendimiento con Symfony2
Escalabilidad y alto rendimiento con Symfony2
 
Betabeers Barcelona - Buenas prácticas
Betabeers Barcelona - Buenas prácticasBetabeers Barcelona - Buenas prácticas
Betabeers Barcelona - Buenas prácticas
 
Desymfony - Servicios
Desymfony  - ServiciosDesymfony  - Servicios
Desymfony - Servicios
 

Redis everywhere - PHP London

  • 2. HELLO WORLD • Ricard Clau, born and grown up in Barcelona • Software engineer at Hailo • Symfony2 lover and PHP believer • Open-source contributor, sometimes I give talks • Twitter @ricardclau / Gmail ricard.clau@gmail.com
  • 3. WHAT IS REDIS? • REmote DIctionary Server • Created in 2009 by Salvatore Sanfilipo (@antirez) • Open source • Advanced in-memory key-value data-structure server • Stop thinking SQL, think back to structures!
  • 4. TO ME REDIS IS LIKE...
  • 5. AGENDA • REDIS introduction • Getting started with PHP / Symfony2: sessions, caching • Cookbooks: queues, statistics, leaderboards, users online, friends online, locking • War Stories from a 7.5M DAU project • Final thoughts
  • 6. INTRODUCTIONTO REDIS And some demystifications
  • 7. DATATYPES • Strings up to 512 Mb • Lists of elements sorted by insertion order (max 2^32 - 1) • Sets unordered collection of elements supporting unions, intersections and differences (max 2^32 - 1) • Hashes key-value pairs (max 2^32 - 1) • Sorted sets automatically ordered by score (max 2^32 - 1)
  • 8. ONLY IN-MEMORY? • Your data needs to fit in your memory • It has configurable persistance: RDB snapshots,AOF persistence logs, combine both or no persistance at all • Think like memcached on steroids with persistance • http://redis.io/topics/persistence • “Memory is the new disk, disk is the new tape”
  • 9. INTERESTING FEATURES • Master-slave replication • Pipelining to improve performance • Transactions (kind of with MULTI ... EXEC) • Publisher / Subscriber • Scripting with LUA (~stored procedures) • Predictable performance (check commands complexity)
  • 10. SHARDING DATA • Redis cluster will be available in 3.0 (delayed every year...) • Project Twemproxy can help sharding memcached / redis • Client-side partitioning is enough for most cases • Range partitioningVS hash partitioning • Presharding can help scaling horizontally
  • 11. HARDWARETIPS • Redis is single-threaded, so a 2 core machine is enough • If using EC2, maybe m1.large (7.5Gb) is the most suitable, but if it is not enough you might consider any m2 memory optimized type • CPU is rarely the bottleneck with Redis • Read carefully the doc to maximize performance!
  • 12. GETTING STARTED WITH PHP It is easier than you may think!
  • 13. PHP CLIENTS • http://redis.io/clients • Predis (PHP) vs phpredis (PHP extension) • Predis is very mature, actively maintained, feature complete, extendable, composer friendly and supports all Redis versions • Phpredis is faster, but not backwards compatible • Network latency is the biggest performance killer
  • 14. REDIS-CLI AND MONITOR • Redis-cli to connect to a Redis instance • With Monitor we can watch live activity!
  • 15. SESSIONS, CACHING • Super-easy to implement with commercial frameworks • Commands used: GET <session-id>, SETEX <session-id> <expires> <serialized-data> • Much better performance than PDO and also persistent! • Caching and temp-data storage work exactly equal • Be careful with temp-data storage and memory usage!
  • 16. SYMFONY2 INTEGRATION • There must be a bundle for that... • https://github.com/snc/SncRedisBundle • Out of the box: Session management, Monolog handler, Swiftmailer spooling, Doctrine caching • Full integration with Symfony2 profiler
  • 18. SOME COOKBOOKS And comparisons with other technologies
  • 19. QUEUE PROCESSING • Using Lists and LPUSH / RPOP instructions • We can have different languages accessing data • We used that to send OpenGraph requests to Facebook (REALLY slow API) with Python daemons • ~80k messages/minute processed with 1 m1.large EC2 • If intensive, consider using RabbitMQ,ActiveMQ, ZeroMQ...
  • 20. REAL-TIME ANALYTICS • We can use hashes to group many stats under one key • Most of the times we have counters: HINCRBY “stats" <key> <increment> • HMGET “stats” <key1> <key2> ... to retrieve values and HMSET “stats”“stat1” <value1> “stat2” <value2> to set them • HGETALL to get the full hash
  • 21. LEADERBOARDS • Super-easy with Redis, heavy consuming with other systems • Each board with a single key, using sorted sets • ZINCRBY “rankXXX” <increment> <userId> • Top N ZREVRANGE “rankXXX” 0 N [WITHSCORES] • We stored several milions of members under 1 key
  • 22. WHO IS ONLINE? (I) • We can use SETS to achieve it! • One set for every minute, SADD <minute> <userId> Time +1m +2m +3m 2 2 7 2 3 1 4 1 1 1 3 5 SUNION 2 1 3 5 7 4
  • 23. WHO IS ONLINE? (II) • Online users == everyone active in the last N minutes • SUNION <now> <now-1> <now-2> ... • SUNIONSTORE “online” <now> <now-1> <now-2> ... • Number of users: SCARD “online” • Obtain online users with SMEMBERS “online”
  • 24. FRIENDS ONLINE? • If we store user´s friends in a SET with key friends-<user-id> • Friends online: SINTERSTORE “friends-<userid>-online” “online”“friends-<userid>” • Imagine how you would do it without Redis! 7 1 3 5 2 4 15 9 3 10 7 1 ONLINE FRIENDS-12 SINTER FRIENDS-12-ONLINE
  • 25. DISTRIBUTED LOCKING (I) • Problem in heavy-write applications: 2 almost concurrent requests trying to update same records • SELECT FOR UPDATE? Easy to create deadlocks! timeline R1 R2 R1read R2save R2read R1save R1 updates are lost!!!!!
  • 26. DISTRIBUTED LOCKING (II) • Locking can be solved with Redis: SETNX returns true only if key does not exist. • PseudoCode: try SETNX <lock> <time+expires>, if false check if existing lock has expired and overwrite if so. Otherwise, sleep some milliseconds and try to acquire again. Remember to release lock at the end! • 8 m1.xlarge instances to lock 7.5M Daily Active Users • Alternatives:Apache Zookeeper, perhaps better for scaling
  • 28. PROGRESSIVELY • DragonCity had ~2M DAU when we introduced Redis • First usages: Queues PHP-Python and Temporary storage • Introduced Locking with Redis -> End of deadlocks • Used for temporary contests involving rankings • Session tracking, cheat detection and many others • About 7.5M DAU and similar amount of servers
  • 29. REDIS IS SUPER-FAST • Your network gets killed much before Redis is down • Always increase your ulimit values in Redis servers • We had Gigabit network connectivity between machines but it was not enough to handle the data sent and retrieved • Redis was just at 20% of CPU • Do some heavy-load tests before launching!
  • 30. SURPRISES WITH MEMORY • Redis documentation:“Sets of binary-safe strings”so, we changed values in sets from being just numbers to number + :<char> to add some info to values • We expected ~20% memory increase but in was 500% • https://github.com/antirez/redis/blob/unstable/src/t_set.c#L55 • If value can be represented as long, it is stored more efficiently • http://alonso-vidales.blogspot.co.uk/2013/06/not-all-redis- values-are-strings.html
  • 31. FINALTHOUGHTS When and when not to consider Redis
  • 32. REDIS IS PERFECT FOR... • Intensive read-write data applications • Temporary stored data • Data that fits in memory • Problems that fit Redis built-in data types • Predictability in performance needed (all commands complexity documented)
  • 33. BUT IS NOT SUITABLE WHEN.. • Big data sets, archive data • Relational data (RDBMS are absolutely fine to scale) • We don´t know how we will access data • Reporting applications (no where clauses) • ALWAYS choose the right tool for the job!
  • 34. SPECIALTHANKS • Ronny López (@ronnylt) & AlonsoVidales (@alonsovidales) • All backend and systems engineers at @socialpoint • Of course, to all of you for coming the 1st of August! • Check our open-source project Redtrine, PR welcome: https://github.com/redtrine/redtrine
  • 35. QUESTIONS? • Twitter: @ricardclau • E-mail: ricard.clau@gmail.com • Github: https://github.com/ricardclau • Blog about PHP and Symfony2: http://www.ricardclau.com • Hailo is hiring! If you are interested, talk to us or apply!