Redis from entry to give up series (seven) expired, out of memory strategies

Redis from entry to give up series (seven) expired, out of memory strategies

This article example is based on: 5.0.4

Expiration Policy

Redis There are two key for setting the expiration time of the expiration policy

  • Inert delete
  • Timing randomly deleted

Inert delete

When the timing is inert to delete the key you want to get to do it again when I judge here as a demonstration of drawing a String type:

GitHub,Social Coding

int expireIfNeeded(redisDb *db, robj *key) {
    if (!keyIsExpired(db,key)) return 0;

    /* If we are running in the context of a slave, instead of
     * evicting the expired key from the database, we return ASAP:
     * the slave key expiration is controlled by the master that will
     * send us synthesized DEL operations for expired keys.
     *
     * Still we try to return the right information to the caller,
     * that is, 0 if we think the key should be still valid, 1 if
     * we think the key is expired at this time. */
    if (server.masterhost != NULL) return 1;

    /* Delete the key */
    server.stat_expiredkeys++;
    propagateExpire(db,key,server.lazyfree_lazy_expire);
    notifyKeyspaceEvent(NOTIFY_EXPIRED,
        "expired",key,db->id);
    return server.lazyfree_lazy_expire ? dbAsyncDelete(db,key) :
                                         dbSyncDelete(db,key);
}
复制代码

We found that when key has set an expiration time, and then key has expired, then redis will determine whether to open up lazyfree_lazy_expire, if open, then asynchronous delete Without the synchronization delete. Characteristics after (4.0, when the big key to delete when very useful)

Timing randomly deleted

Once deleted with inert meet the needs of a part, but in practical applications, there will have expired without being accessible to the key, which would occupy memory for no reason. So redis through how to solve it?

redis there will be a timed task, running 10 times per second.

  • Randomly selecting an expiration time set key 20 for detecting expired
  • Delete all expired keys
  • If more than 25% of key expires, please start again from step.

Since redis is single-threaded, if the timing is random deletion policy and let the words above, then when there is a large number of key expiration time, there will be redis case can not handle client requests? No, no, in fact, act as a timing redis randomly deleted when there is a limit, the upper limit of the scan time is set, the default up to 25ms (timelimit = 1000000 * ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC / server.hz ​​/ 100;), so when the client request period, the server is in the scanning expired, the client will wait for up to 25ms then their business processes.

So if redis set a timeout period is too short, then there may be a lot of time out the connection, in order to avoid this problem, it is recommended that it is time to set the expiration time, we want to add a random range, to avoid a large number of key overdue -

When there are shots from the, from the library is not open regular random deleted, all dependent master file opened aof add a del command, and then execute the statement to remove the expired key to achieve from the library

Out of memory strategies

We know redis are pure in-memory database, if redis use beyond the time limit of memory, there will swap behavior using disk operations compared to memory, the performance degradation is not a Ding slightest. This time we need to set maximum use of memory redis

maxmemory <bytes>
复制代码

By setting maxmemory, when using the memory of more than maxmemory, redis offers several alternative strategies to control the amount of memory used

  • noeviction (when memory is insufficient to hold the new data is written, the new write operation will be given)
  • allkeys-lru (if memory is insufficient to accommodate the new data is written in the key space, the key is removed the least recently used)
  • allkeys-random (if memory is insufficient to accommodate the new data is written in the key space, remove a random key)
  • volatile-lru (when insufficient memory to write new data received, the expiration time set key space, the key is removed the least recently used)
  • volatile-random (when insufficient memory to write new data received, the expiration time set key space, remove a random key)
  • volatile-ttl (when memory is insufficient to hold the new data is written in the key space is provided in the expiration time, the expiration time has an earlier priority key removed)

Written in the last

Dragon Boat Festival three days this past friends - Tell me what you would like to have a good rest, and then continue up moving bricks ~ hhhhh

Reproduced in: https: //juejin.im/post/5cfe63a46fb9a07f0c467776

Guess you like

Origin blog.csdn.net/weixin_34357267/article/details/93161886