Redis everywhere - PHP London

Ricard Clau
Ricard ClauIndependent Consultant
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!
1 de 35

Recomendados

Document Locking with Redis in Symfony2 por
Document Locking with Redis in Symfony2Document Locking with Redis in Symfony2
Document Locking with Redis in Symfony2Tom Corrigan
3.8K vistas13 diapositivas
Databases in the hosted cloud por
Databases in the hosted cloudDatabases in the hosted cloud
Databases in the hosted cloudColin Charles
1.2K vistas48 diapositivas
Better encryption & security with MariaDB 10.1 & MySQL 5.7 por
Better encryption & security with MariaDB 10.1 & MySQL 5.7Better encryption & security with MariaDB 10.1 & MySQL 5.7
Better encryption & security with MariaDB 10.1 & MySQL 5.7Colin Charles
6.5K vistas51 diapositivas
Best practices for MySQL/MariaDB Server/Percona Server High Availability por
Best practices for MySQL/MariaDB Server/Percona Server High AvailabilityBest practices for MySQL/MariaDB Server/Percona Server High Availability
Best practices for MySQL/MariaDB Server/Percona Server High AvailabilityColin Charles
4.9K vistas145 diapositivas
The Complete MariaDB Server Tutorial - Percona Live 2015 por
The Complete MariaDB Server Tutorial - Percona Live 2015The Complete MariaDB Server Tutorial - Percona Live 2015
The Complete MariaDB Server Tutorial - Percona Live 2015Colin Charles
6.6K vistas198 diapositivas
Capacity planning for your data stores por
Capacity planning for your data storesCapacity planning for your data stores
Capacity planning for your data storesColin Charles
1K vistas34 diapositivas

Más contenido relacionado

La actualidad más candente

Redis Developers Day 2014 - Redis Labs Talks por
Redis Developers Day 2014 - Redis Labs TalksRedis Developers Day 2014 - Redis Labs Talks
Redis Developers Day 2014 - Redis Labs TalksRedis Labs
2.1K vistas16 diapositivas
MariaDB Server & MySQL Security Essentials 2016 por
MariaDB Server & MySQL Security Essentials 2016MariaDB Server & MySQL Security Essentials 2016
MariaDB Server & MySQL Security Essentials 2016Colin Charles
1.2K vistas50 diapositivas
Distributions from the view a package por
Distributions from the view a packageDistributions from the view a package
Distributions from the view a packageColin Charles
1.2K vistas25 diapositivas
Meet MariaDB 10.1 at the Bulgaria Web Summit por
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
530 vistas68 diapositivas
Meet MariaDB Server 10.1 London MySQL meetup December 2015 por
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
1.2K vistas72 diapositivas
Cool MariaDB Plugins por
Cool MariaDB Plugins Cool MariaDB Plugins
Cool MariaDB Plugins Colin Charles
1.5K vistas15 diapositivas

La actualidad más candente(20)

Redis Developers Day 2014 - Redis Labs Talks por Redis Labs
Redis Developers Day 2014 - Redis Labs TalksRedis Developers Day 2014 - Redis Labs Talks
Redis Developers Day 2014 - Redis Labs Talks
Redis Labs2.1K vistas
MariaDB Server & MySQL Security Essentials 2016 por Colin Charles
MariaDB Server & MySQL Security Essentials 2016MariaDB Server & MySQL Security Essentials 2016
MariaDB Server & MySQL Security Essentials 2016
Colin Charles1.2K vistas
Distributions from the view a package por Colin Charles
Distributions from the view a packageDistributions from the view a package
Distributions from the view a package
Colin Charles1.2K vistas
Meet MariaDB 10.1 at the Bulgaria Web Summit por Colin Charles
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
Colin Charles530 vistas
Meet MariaDB Server 10.1 London MySQL meetup December 2015 por Colin Charles
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
Colin Charles1.2K vistas
Cool MariaDB Plugins por Colin Charles
Cool MariaDB Plugins Cool MariaDB Plugins
Cool MariaDB Plugins
Colin Charles1.5K vistas
My first moments with MongoDB por Colin Charles
My first moments with MongoDBMy first moments with MongoDB
My first moments with MongoDB
Colin Charles473 vistas
The Complete MariaDB Server tutorial por Colin Charles
The Complete MariaDB Server tutorialThe Complete MariaDB Server tutorial
The Complete MariaDB Server tutorial
Colin Charles2.7K vistas
MariaDB 10.1 what's new and what's coming in 10.2 - Tokyo MariaDB Meetup por Colin Charles
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
Colin Charles2.2K vistas
MariaDB - the "new" MySQL is 5 years old and everywhere (LinuxCon Europe 2015) por Colin 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)
Colin Charles803 vistas
MariaDB Server Compatibility with MySQL por Colin Charles
MariaDB Server Compatibility with MySQLMariaDB Server Compatibility with MySQL
MariaDB Server Compatibility with MySQL
Colin Charles2.4K vistas
Securing your MySQL / MariaDB Server data por Colin Charles
Securing your MySQL / MariaDB Server dataSecuring your MySQL / MariaDB Server data
Securing your MySQL / MariaDB Server data
Colin Charles942 vistas
Lessons from database failures por Colin Charles
Lessons from database failures Lessons from database failures
Lessons from database failures
Colin Charles383 vistas
M|18 Running MariaDB TX on Containers por MariaDB plc
M|18 Running MariaDB TX on ContainersM|18 Running MariaDB TX on Containers
M|18 Running MariaDB TX on Containers
MariaDB plc660 vistas
The MySQL Server ecosystem in 2016 por Colin Charles
The MySQL Server ecosystem in 2016The MySQL Server ecosystem in 2016
The MySQL Server ecosystem in 2016
Colin Charles2.5K vistas
MySQL in the Hosted Cloud - Percona Live 2015 por Colin Charles
MySQL in the Hosted Cloud - Percona Live 2015MySQL in the Hosted Cloud - Percona Live 2015
MySQL in the Hosted Cloud - Percona Live 2015
Colin Charles1K vistas
Lessons from database failures por Colin Charles
Lessons from database failuresLessons from database failures
Lessons from database failures
Colin Charles891 vistas
MariaDB: in-depth (hands on training in Seoul) por Colin 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)
Colin Charles9.2K vistas
Managing 50K+ Redis Databases Over 4 Public Clouds ... with a Tiny Devops Team por Redis Labs
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
Redis Labs28K vistas

Destacado

Redis Everywhere - Sunshine PHP por
Redis Everywhere - Sunshine PHPRedis Everywhere - Sunshine PHP
Redis Everywhere - Sunshine PHPRicard Clau
22.5K vistas45 diapositivas
Redis & ZeroMQ: How to scale your application por
Redis & ZeroMQ: How to scale your applicationRedis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your applicationrjsmelo
23.4K vistas33 diapositivas
Building a bakery of Windows servers with Packer - London WinOps por
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
2.4K vistas17 diapositivas
Modern software architectures - PHP UK Conference 2015 por
Modern software architectures - PHP UK Conference 2015Modern software architectures - PHP UK Conference 2015
Modern software architectures - PHP UK Conference 2015Ricard Clau
3.7K vistas52 diapositivas
Slide Seminar PHP Indonesia - NoSQL Redis por
Slide Seminar PHP Indonesia - NoSQL RedisSlide Seminar PHP Indonesia - NoSQL Redis
Slide Seminar PHP Indonesia - NoSQL Redisrifqi alfian
2.1K vistas18 diapositivas
Redis in Practice: Scenarios, Performance and Practice with PHP por
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
1.6K vistas24 diapositivas

Destacado(20)

Redis Everywhere - Sunshine PHP por Ricard Clau
Redis Everywhere - Sunshine PHPRedis Everywhere - Sunshine PHP
Redis Everywhere - Sunshine PHP
Ricard Clau22.5K vistas
Redis & ZeroMQ: How to scale your application por rjsmelo
Redis & ZeroMQ: How to scale your applicationRedis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your application
rjsmelo23.4K vistas
Building a bakery of Windows servers with Packer - London WinOps por Ricard Clau
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
Ricard Clau2.4K vistas
Modern software architectures - PHP UK Conference 2015 por Ricard Clau
Modern software architectures - PHP UK Conference 2015Modern software architectures - PHP UK Conference 2015
Modern software architectures - PHP UK Conference 2015
Ricard Clau3.7K vistas
Slide Seminar PHP Indonesia - NoSQL Redis por rifqi alfian
Slide Seminar PHP Indonesia - NoSQL RedisSlide Seminar PHP Indonesia - NoSQL Redis
Slide Seminar PHP Indonesia - NoSQL Redis
rifqi alfian2.1K vistas
Redis in Practice: Scenarios, Performance and Practice with PHP por Chen Huang
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
Chen Huang1.6K vistas
[2B5]nBase-ARC Redis Cluster por NAVER D2
[2B5]nBase-ARC Redis Cluster[2B5]nBase-ARC Redis Cluster
[2B5]nBase-ARC Redis Cluster
NAVER D24.5K vistas
Beyond relational database - Building high performance websites using Redis a... por Dinh Pham
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 Pham2.7K vistas
MOSC2012 - Building High-Performance Web-Application with PHP & MongoDB por r1dotmy
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
r1dotmy2.1K vistas
美团点评沙龙 飞行中换引擎--美团配送业务系统的架构演进之路 por 美团点评技术团队
美团点评沙龙 飞行中换引擎--美团配送业务系统的架构演进之路美团点评沙龙 飞行中换引擎--美团配送业务系统的架构演进之路
美团点评沙龙 飞行中换引擎--美团配送业务系统的架构演进之路
Redis 常见使用模式分析 por vincent253
Redis 常见使用模式分析Redis 常见使用模式分析
Redis 常见使用模式分析
vincent2534.1K vistas
Redis/Lessons learned por Tit Petric
Redis/Lessons learnedRedis/Lessons learned
Redis/Lessons learned
Tit Petric7.4K vistas
Highly scalable caching service on cloud - Redis por Krishna-Kumar
Highly scalable caching service on cloud - RedisHighly scalable caching service on cloud - Redis
Highly scalable caching service on cloud - Redis
Krishna-Kumar 3.6K vistas
Scaling php applications with redis por jimbojsb
Scaling php applications with redisScaling php applications with redis
Scaling php applications with redis
jimbojsb17K vistas
深入了解Redis por iammutex
深入了解Redis深入了解Redis
深入了解Redis
iammutex9.1K vistas
Using Redis at Facebook por Redis Labs
Using Redis at FacebookUsing Redis at Facebook
Using Redis at Facebook
Redis Labs2.7K vistas
Scaling PHP to 40 Million Uniques por Jonathan Klein
Scaling PHP to 40 Million UniquesScaling PHP to 40 Million Uniques
Scaling PHP to 40 Million Uniques
Jonathan Klein7.3K vistas
Redis cluster por iammutex
Redis clusterRedis cluster
Redis cluster
iammutex7K vistas

Similar a Redis everywhere - PHP London

Speed up your Symfony2 application and build awesome features with Redis por
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
27.7K vistas52 diapositivas
REDIS327 por
REDIS327REDIS327
REDIS327Rajan Bhatt
351 vistas37 diapositivas
Redis meetup por
Redis meetupRedis meetup
Redis meetupNikhil Dole
131 vistas23 diapositivas
Big Data Developers Moscow Meetup 1 - sql on hadoop por
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
931 vistas41 diapositivas
Add Redis to Postgres to Make Your Microservices Go Boom! por
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
6.1K vistas40 diapositivas
New York REDIS Meetup Welcome Session por
New York REDIS Meetup Welcome SessionNew York REDIS Meetup Welcome Session
New York REDIS Meetup Welcome SessionAleksandr Yampolskiy
2K vistas23 diapositivas

Similar a Redis everywhere - PHP London(20)

Speed up your Symfony2 application and build awesome features with Redis por Ricard Clau
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
Ricard Clau27.7K vistas
Big Data Developers Moscow Meetup 1 - sql on hadoop por bddmoscow
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
bddmoscow931 vistas
Add Redis to Postgres to Make Your Microservices Go Boom! por Dave Nielsen
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 Nielsen6.1K vistas
Redis - The Universal NoSQL Tool por Eberhard Wolff
Redis - The Universal NoSQL ToolRedis - The Universal NoSQL Tool
Redis - The Universal NoSQL Tool
Eberhard Wolff2.4K vistas
What's new with enterprise Redis - Leena Joshi, Redis Labs por 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
Redis Labs1.7K vistas
IBM Internet-of-Things architecture and capabilities por IBM_Info_Management
IBM Internet-of-Things architecture and capabilitiesIBM Internet-of-Things architecture and capabilities
IBM Internet-of-Things architecture and capabilities
IBM_Info_Management3.5K vistas
Amazon RDS for MySQL – Diagnostics, Security, and Data Migration (DAT302) | A... por Amazon Web Services
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 por Ricard Clau
Big Data! Great! Now What? #SymfonyCon 2014Big Data! Great! Now What? #SymfonyCon 2014
Big Data! Great! Now What? #SymfonyCon 2014
Ricard Clau23.4K vistas
Citrix Synergy 2014 - Syn233 Building and operating a Dev Ops cloud: best pra... por Citrix
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...
Citrix1.1K vistas
Hpc lunch and learn por John D Almon
Hpc lunch and learnHpc lunch and learn
Hpc lunch and learn
John D Almon709 vistas
NoSQL for great good [hanoi.rb talk] por Huy Do
NoSQL for great good [hanoi.rb talk]NoSQL for great good [hanoi.rb talk]
NoSQL for great good [hanoi.rb talk]
Huy Do1.3K vistas
Citrix Synergy 2014: Going the CloudPlatform Way por Iliyas Shirol
Citrix Synergy 2014: Going the CloudPlatform WayCitrix Synergy 2014: Going the CloudPlatform Way
Citrix Synergy 2014: Going the CloudPlatform Way
Iliyas Shirol442 vistas
10 Ways to Scale Your Website Silicon Valley Code Camp 2019 por Dave Nielsen
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
Dave Nielsen225 vistas
Moving Beyond Cache by Yiftach Shoolman Redis Labs - Redis Day Seattle 2020 por Redis Labs
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
Redis Labs210 vistas

Más de Ricard Clau

devopsbcn23.pdf por
devopsbcn23.pdfdevopsbcn23.pdf
devopsbcn23.pdfRicard Clau
187 vistas13 diapositivas
devopsbcn22.pdf por
devopsbcn22.pdfdevopsbcn22.pdf
devopsbcn22.pdfRicard Clau
117 vistas14 diapositivas
NoEresTanEspecial-PulpoCon22.pdf por
NoEresTanEspecial-PulpoCon22.pdfNoEresTanEspecial-PulpoCon22.pdf
NoEresTanEspecial-PulpoCon22.pdfRicard Clau
1.3K vistas36 diapositivas
DevOps & Infraestructura como código: Promesas Rotas por
DevOps & Infraestructura como código: Promesas RotasDevOps & Infraestructura como código: Promesas Rotas
DevOps & Infraestructura como código: Promesas RotasRicard Clau
132 vistas27 diapositivas
DevOps Barcelona Conference 2018 - Intro por
DevOps Barcelona Conference 2018 - IntroDevOps Barcelona Conference 2018 - Intro
DevOps Barcelona Conference 2018 - IntroRicard Clau
811 vistas12 diapositivas
Hashicorp at holaluz por
Hashicorp at holaluzHashicorp at holaluz
Hashicorp at holaluzRicard Clau
535 vistas37 diapositivas

Más de Ricard Clau(11)

devopsbcn23.pdf por Ricard Clau
devopsbcn23.pdfdevopsbcn23.pdf
devopsbcn23.pdf
Ricard Clau187 vistas
devopsbcn22.pdf por Ricard Clau
devopsbcn22.pdfdevopsbcn22.pdf
devopsbcn22.pdf
Ricard Clau117 vistas
NoEresTanEspecial-PulpoCon22.pdf por Ricard Clau
NoEresTanEspecial-PulpoCon22.pdfNoEresTanEspecial-PulpoCon22.pdf
NoEresTanEspecial-PulpoCon22.pdf
Ricard Clau1.3K vistas
DevOps & Infraestructura como código: Promesas Rotas por Ricard Clau
DevOps & Infraestructura como código: Promesas RotasDevOps & Infraestructura como código: Promesas Rotas
DevOps & Infraestructura como código: Promesas Rotas
Ricard Clau132 vistas
DevOps Barcelona Conference 2018 - Intro por Ricard Clau
DevOps Barcelona Conference 2018 - IntroDevOps Barcelona Conference 2018 - Intro
DevOps Barcelona Conference 2018 - Intro
Ricard Clau811 vistas
Hashicorp at holaluz por Ricard Clau
Hashicorp at holaluzHashicorp at holaluz
Hashicorp at holaluz
Ricard Clau535 vistas
What we talk about when we talk about DevOps por Ricard Clau
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
Ricard Clau928 vistas
Scaling with Symfony - PHP UK por Ricard Clau
Scaling with Symfony - PHP UKScaling with Symfony - PHP UK
Scaling with Symfony - PHP UK
Ricard Clau13K vistas
Escalabilidad y alto rendimiento con Symfony2 por Ricard Clau
Escalabilidad y alto rendimiento con Symfony2Escalabilidad y alto rendimiento con Symfony2
Escalabilidad y alto rendimiento con Symfony2
Ricard Clau5.7K vistas
Betabeers Barcelona - Buenas prácticas por Ricard Clau
Betabeers Barcelona - Buenas prácticasBetabeers Barcelona - Buenas prácticas
Betabeers Barcelona - Buenas prácticas
Ricard Clau1.4K vistas
Desymfony - Servicios por Ricard Clau
Desymfony  - ServiciosDesymfony  - Servicios
Desymfony - Servicios
Ricard Clau3K vistas

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!