What’s New in Redis 4.0?

Lazyfree Feature

Many of you must have encountered the problem of deleting large keys. In addition to strings, Redis also supports list, set, hash, sorted set, and other complex data structures. These data structures have enriched Redis usage. However, improper usage may cause problems of over-sized single keys.> SCARD funs
(integer) 6320505> DEL funs
(integer) 1
(3.11s)> slowlog get
1) 1) (integer) 4
2) (integer) 1528169923
3) (integer) 3104812
4) 1) "DEL"
2) "funs"
5) ""
6) ""> UNLINK funs
(integer) 1
(3.11s)> slowlog get
(empty list or set)
  1. UNLINK: asynchronously deletes the key.
  2. FLUSHDB ASYNC: asynchronously clears the current DB.
  3. FLUSHALL ASYNC: asynchronously clears all DBs.
  1. lazyfree-lazy-expire: asynchronously deletes expired keys.
  2. lazyfree-lazy-eviction: asynchronously evicts keys.
  3. lazyfree-lazy-server-del: uses the asynchronous deletion function in the case of implicit deletion. For example, rename b, if b exists, delete b.
  4. slave-lazy-flush: in the case of full data synchronization, the slave asynchronously clears all DBs.

Lua Script Supports Random Operations

Redis is built with a Lua environment to allow users to expand features. However, for the sake of consistency, the scripts must be pure functions. In other words, when a Lua script is given the same parameters, the same results will be returned for repeated executions.

local now = redis.call('time')[1]redis.call('set','now',now)return redis.call('get','now')
$redis-cli --eval escript
(error) ERR Error running script (call to f_cfba5ec6a699dad183456f19d1099d8dabfdb80c):
@user_script:3: @user_script: 3: Write commands not allowed after non deterministic commands.
Call redis.replicate_commands() at the start of your script in order to switch to single commands replication mode.
redis.replicate_commands()local now = redis.call('time')[1]redis.call('set','now',now)return redis.call('get','now')
$redis-cli --eval escript
$redis-cli --eval escript

LFU-based Hotspot Key Discovery Mechanism

LFU is a memory eviction policy newly introduced in Redis 4.0. LFU provides a more precise memory eviction algorithm. Its core function is to record the key access frequency within a period of time. It brings an additional benefit — hotspot key discovery.> config get maxmemory-policy
1) "maxmemory-policy"
2) "noeviction"> object freq counter:000000006889
(error) ERR An LFU maxmemory policy is not selected, access frequency not tracked. Please note that when switching between policies at runtime LRU and LFU data will take some time to adjust.> config set maxmemory-policy allkeys-lfu
OK> object freq counter:000000006889
(integer) 3
$./redis-cli --hotkeys# Scanning the entire keyspace to find hot keys as well as
# average sizes per key type. You can use -i 0.1 to sleep 0.1 sec
# per 100 SCAN commands (not usually needed).
[00.00%] Hot key 'counter:000000000002' found so far with counter 87
[00.00%] Hot key 'key:000000000001' found so far with counter 254
[00.00%] Hot key 'mylist' found so far with counter 107
[00.00%] Hot key 'key:000000000000' found so far with counter 254
[45.45%] Hot key 'counter:000000000001' found so far with counter 87
[45.45%] Hot key 'key:000000000002' found so far with counter 254
[45.45%] Hot key 'myset' found so far with counter 64
[45.45%] Hot key 'counter:000000000000' found so far with counter 93
-------- summary -------Sampled 22 keys in the keyspace!
hot key found with counter: 254 keyname: key:000000000001
hot key found with counter: 254 keyname: key:000000000000
hot key found with counter: 254 keyname: key:000000000002
hot key found with counter: 107 keyname: mylist
hot key found with counter: 93 keyname: counter:000000000000
hot key found with counter: 87 keyname: counter:000000000002
hot key found with counter: 87 keyname: counter:000000000001
hot key found with counter: 64 keyname: myset

MEMORY Analysis Command

Memory analysis helps optimize Redis usage. The completely new MEMORY command can help users perform this operation. The MEMORY command has 5 sub-commands. These 5 sub-commands can be viewed from MEMORY HELP.> memory help
1) "MEMORY DOCTOR - Outputs memory problems report"
2) "MEMORY USAGE <key> [SAMPLES <count>] - Estimate memory usage of key"
3) "MEMORY STATS - Show memory usage details"
4) "MEMORY PURGE - Ask the allocator to release memory"
5) "MEMORY MALLOC-STATS - Show allocator internal stats"


We looked at some of the key features of Redis 4.0, focusing on features like Lazytree, Lua, and Memory. To learn more about Redis on Alibaba Cloud, visit the ApsaraDB for Redis product page.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com