![Page 1: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/1.jpg)
Redis in Practice• Why Redis• How to make Redis Faster• Replication and Sentinel
Chen Huangc.huang@*****.com
![Page 2: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/2.jpg)
Content
• Why Redis Comparison of In-Memory Storages Scenarios Sharding (Parallel Scaling)
• How to make Redis Faster
• Replication and Sentinel
![Page 3: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/3.jpg)
Comparison of In-Memory StoragesRedis Memcached MemSQL
Data Structure Key-Value, List, Hash, Set, Z-Set Key-Value Relational
Concurrency Poor Good Good
Sharding Not Generally Available Yes Yes
Transaction Optimistic Lock CAS No
Scripting Lua No No
Replication Yes Repcached Yes
Persistance Yes MemcacheDB Yes
High Availability Redis Sentinel ? ?
![Page 4: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/4.jpg)
Scenarios
• CacheJava:
Map cache = new LinkedHashMap() { protected boolean removeEldestEntry(Map.Entry eldest)
{ return isTimeout(eldest); }}
Redis:SETEX key timeout value
![Page 5: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/5.jpg)
Scenarios
• List / Queue
Java RedisLinkedList / ArrayDeque List
list.offerFirst(element) LPUSH list element
list.offerLast(element) RPUSH list element
list.pollFirst() LPOP list
list.pollLast() RPOP list
![Page 6: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/6.jpg)
• Session[Servlet API]HttpSession session = req.getSession(); // got by session idObject oldValue = session.getAttribute(key);session.setAttribute(key, newValue);
[Implementation]
Scenarios
Java RedisHashMap<String, HashMap> Hash
map.get(id).get(key); HGET id key
map.get(id).put(key, value); HSET id key value
![Page 7: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/7.jpg)
Scenarios• Session Timeout
[Servlet API]session.set(key, new HttpSessionBindingListener() { public void valueUnbound(HttpSessionBindingEvent event) { // Trigger Timeout Event }});
[Implementation]
Java RedisLinkedHashMap Hash + Sorted Set (ZSet)
Iterator i = map.values().iterator();while (i.hasNext() && isTimeout(i.next())) { // Trigger Timeout Event i.remove();}
ZRANGEBYSCORE zset 0 now// Trigger Timeout EventHDEL id, id, ...ZREMRANGEBYSCORE zset 0 now
![Page 8: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/8.jpg)
Sharding (Parallel Scaling)
• Redis is Single-Threaded• Multiple Redis Instances in one Server• Partition by Hash of Keys
![Page 9: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/9.jpg)
Content
• Why Redis
• How to make Redis Faster Time Complexity Communication Latency Serialization
• Replication and Sentinel
![Page 10: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/10.jpg)
Time Complexity
See Redis Documentation for more Details
Structure Operation ComplexityHash HGET id key
Key-Value GET id
Key-Value KEYS prefix*
List LPUSH/LPOP
List LINDEX/LSET
Sorted Set ZADD
Sorted Set ZRANGEBYSCORE
O(1)
O(1)
O(n)
O(1)
O(n)
O(log(n))
O(log(n)+m)
![Page 11: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/11.jpg)
Communication Latency
• Massive (Multi-Row) Insertion[SQL]
× INSERT INTO cache (key, value) VALUES ("key1", "value1");INSERT INTO cache (key, value) VALUES ("key2", "value2");...
√ INSERT INTO cache (key, value) VALUES ("key1", "value1"),("key2", "value2"), ("key3", "value3"), ...
[Redis]× HSET cache key1 value1
HSET cache key2 value2...
√ HMSET cache key1 value1 key2 value2 ...
![Page 12: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/12.jpg)
Communication Latency
• Pipeline (Batch)
![Page 13: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/13.jpg)
Communication Latency
• Pipeline (Batch)[SQL in Java]
try (Statement stmt = conn.createStatement()) {stmt.addBatch("INSERT INTO cache1 ...");stmt.addBatch("INSERT INTO cache2 ...");stmt.executeBatch();
}
[Redis in Java]Pipeline pl = jedis.pipelined();pl.hset("cache1", "key1", "value1");pl.hset("cache2", "key2", "value2");pl.sync();
![Page 14: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/14.jpg)
Communication Latency
• Scripting[Java]int balance = Integer.parseInt(jedis.get("balance"));if (balance > 100) {
jedis.set("balance", Integer.toString(balance - 100));}
[Lua]local i = tonumber(redis.call('get', 'balance'))if i > 100 then
redis.call('set', 'balance', tostring(i - 100))end
![Page 15: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/15.jpg)
Communication Latency
• SQL Efficiency
Multi-Row Insertion > Batch > Stored Procedure
• Redis Efficiency
Massive Insertion > Pipeline > Scripting
![Page 16: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/16.jpg)
Serialization
![Page 17: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/17.jpg)
Serialization
![Page 18: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/18.jpg)
Serialization
• Kryo vs. Protobuf
![Page 19: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/19.jpg)
Content
• Why Redis
• How to make Redis Faster
• Replication and Sentinel Replication Sentinel Practice with PHP
![Page 20: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/20.jpg)
Replication
Master
Slave-1
Slave-2
Slave-3
Web Server
Write
Sync to
Sync to
Sync toWeb Server
Read
Read
Read
![Page 21: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/21.jpg)
Sentinel
A
B
C
D
X
B
C
D
A
Sentinels Sentinels
Sync from
Sync from
![Page 22: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/22.jpg)
Practice with PHP
Shard 1 Shard 2
Sentinels
Shard 3 Shard 4
Web Server
Config Center
Write
Read
Scheduled Config Sync
Web Server
Web Server
redis.config.php
redis.config.php
redis.config.php
redis.config.php
Scheduled Config Sync
![Page 23: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/23.jpg)
Practice with PHP
![Page 24: Redis in Practice: Scenarios, Performance and Practice with PHP](https://reader034.vdocument.in/reader034/viewer/2022042618/58a1ce441a28ab9d338b6d03/html5/thumbnails/24.jpg)
References
• Redishttp://redis.io/
• phpredishttps://github.com/phpredis/phpredis