java - mySQL里有2000w数据,redis中只存20w的数据,如何保证redis中的数据都是热点数据?
ringa_lee
ringa_lee 2017-04-18 09:06:49
0
4
978

mySQL里有2000w数据,redis中只存20w的数据,如何保证redis中的数据都是热点数据?

ringa_lee
ringa_lee

ringa_lee

reply all(4)
洪涛

Based on @ybak's answer, I will introduce a relatively simple and practical solution.

Limit the memory occupied by Redis. Redis will load hot data into memory according to its own data elimination strategy.
So, calculate the approximate memory occupied by 20W data, and then set the Redis memory limit.

小葫芦

What data is the question?

Such as user data. The database has 20 million entries.
Active users:
redis sortSet puts users who have logged in within two days (for convenience, active users within one day), log in ZADD once, and overwrite their scores (login time) if the set already exists. Key: login:users, value: score timestamp, value userid. Set up a periodic task, such as deleting the data in the sort set before 3 o'clock on the previous day at 03:00:00 every day (to ensure that the set does not grow disorderly and retain active users in the past day).

When fetching, get the current timestamp (int 10 digits), then subtract 1 day to get the active users in the past 24 hours according to the score range.

洪涛

Looking at your question, you should just use Redis as a cache.
Provide a simple way to implement cache invalidation: LRU (recently less used elimination)
That is, every time the redis cache hits, the hit cache will be increased by a certain amount ttl (expiration time) (set according to specific circumstances, such as 10 minutes).
After a period of time, the ttl of hot data will be larger and will not automatically expire, while cold data will basically expire immediately after it exceeds the set ttl

黄舟

When the size of the redis memory data set increases to a certain size, the data elimination strategy will be implemented.

redis provides 6 data elimination strategies:

volatile-lru: Select the least recently used data from the data set with expiration time set to eliminate
volatile-ttl
volatile-random
allkeys-lru
allkeys-random
no-enviction

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template