2 Haziran 2021 Çarşamba

Redis Data Eviction

Giriş
Açıklaması şöyle
Can store the details to disk when the physical memory is fully occupied. Redis has the mechanism to swap the values that are least recently used to disk and the latest values into the physical memory.
Supported Eviction Policies
Açıklaması şöyle. LFU kısaltması Least Frequently Used, LRU kısaltması Least Recently Used anlamına gelir.
No Eviction (Returns an error if the memory limit has been reached when trying to insert more data) Least Recently Used (LRU)
-All keys LRU (Evicts the least recently used keys out of all keys)
-Volatile LFU (Evicts the least frequently used keys out of all keys)
-All keys random (Randomly evicts keys out of all keys)
-Volatile random (Randomly evicts keys with an “expire” field set)
-Volatile TTL (Evicts the shortest time-to-live and least recently used keys out of all keys with an “expire” field set.)
-volatile LRU (Evicts the least recently used keys out of all keys with an “expire” field set)
-volatile LFU (Evicts the least frequently used keys out of all keys with an “expire” field set)
Açıklaması şöyle
Redis comprises several data eviction engines:

TTL - object eviction as soon as their lifetime expires;

LRU - long used data eviction;

RANDOM - random object eviction;

LFU - rarely used data eviction.
TTL İçin Expiration Event
Açıklaması şöyle. Yani Expiration Event gelmesi garanti edilen bir şey değil
In the field of e-commerce, payments, etc., there are often such scenarios, the user placed an order and then abandoned the payment, that order will be closed after a specified period of time operation, careful you must have found like a certain treasure, a certain east have such logic, and the time is very accurate, the error is within 1s; then how do they achieve it?
There are several general ways to implement this.

1. using the delayed cast feature of message queues such as rocketmq, rabbitmq, pulsar, etc.
2. using the DelayedQueue provided by redisson.

There are some solutions that are widely spread but have fatal flaws and should not be used to implement delayed tasks.

1. using redis’ expired listeners.
2. using rabbitmq’s dead letter queue.
3. using a non-persistent time wheel.
Redis'in resmi açıklaması şöyle
Basically expired events are generated when the Redis server deletes the key and not when the time to live theoretically reaches the value of zero
Redis'in gerçekleştirimi için  açıklama şöyle
The redis auto-expiration is implemented as a timed task that scans offline and removes partially expired keys; it inertly checks for expiration and removes expired keys when they are accessed. redis is never guaranteed to remove and send expiration notifications immediately at the set expiration time. In fact, it is not uncommon for expiration notifications to be several minutes later than the set expiration time.

In addition, keyspace notifications are sent using a fire and forget policy and are not guaranteed to be delivered like message queues. When a client subscribes to an event, it loses all events that were distributed to it during the disconnect.
Örnek
Şöyle yaparız
const { createClient } = require('redis');
const client = createClient({
    host: process.env.REDIS_ENDPOINT || 'localhost',
    port: process.env.REDIS_PORT || 6379
});
if (process.env.REDIS_PASSWORD) {
    client.auth(process.env.REDIS_PASSWORD);
}
client.config('set', 'notify-keyspace-events', 'Ex');
client.subscribe('__keyevent@0__:expired');
client.on('message', function (channel, key) {
    console.log(key);
});
Ancak burada dikkat edilmesi gereken bazı noktalar var. Açıklaması şöyle. Yani event tam vaktinde gelmek zorunda değil ve Redis cluster şeklinde çalışıyorsa, tüm düğümlere abone olmak gerekir.
- There are no guarantees that the Redis server will be able to generate the expired event at the time the key time to live reaches the value of zero
- expired events are generated when the Redis server deletes the key and not when the time to live theoretically reaches the value of zero.
- Pub/Sub communication in a cluster, events notifications are not broadcasted to all nodes.

Hiç yorum yok:

Yorum Gönder