Knowledge sharing about Redis, mainly focusing on:
Why to use Redis? Comparison of some in-memory storages and their scenarios
How to make Redis faster? Consider time complexity, communication latency and serialization
Practice of replication and sentinel in PHP
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
Redis in Practice: Scenarios, Performance and Practice with PHP
1. Redis in Practice
• Why Redis
• How to make Redis Faster
• Replication and Sentinel
Chen Huang
c.huang@*****.com
2. Content
• Why Redis
Comparison of In-Memory Storages
Scenarios
Sharding (Parallel Scaling)
• How to make Redis Faster
• Replication and Sentinel
3. Comparison of In-Memory Storages
Redis 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 ? ?
5. Scenarios
• List / Queue
Java Redis
LinkedList / ArrayDeque List
list.offerFirst(element) LPUSH list element
list.offerLast(element) RPUSH list element
list.pollFirst() LPOP list
list.pollLast() RPOP list
6. • Session
[Servlet API]
HttpSession session = req.getSession(); // got by session id
Object oldValue = session.getAttribute(key);
session.setAttribute(key, newValue);
[Implementation]
Scenarios
Java Redis
HashMap<String, HashMap> Hash
map.get(id).get(key); HGET id key
map.get(id).put(key, value); HSET id key value
7. Scenarios
• Session Timeout
[Servlet API]
session.set(key, new HttpSessionBindingListener() {
public void valueUnbound(HttpSessionBindingEvent event) {
// Trigger Timeout Event
}
});
[Implementation]
Java Redis
LinkedHashMap 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 Event
HDEL id, id, ...
ZREMRANGEBYSCORE zset 0 now
8. Sharding (Parallel Scaling)
• Redis is Single-Threaded
• Multiple Redis Instances in one Server
• Partition by Hash of Keys
9. Content
• Why Redis
• How to make Redis Faster
Time Complexity
Communication Latency
Serialization
• Replication and Sentinel
10. Time Complexity
See Redis Documentation for more Details
Structure Operation Complexity
Hash 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)
13. 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();
14. 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
22. 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