0 Welcome to followGithub
Welcome to pay attention to all dry technical public number
1 the interview questions
What are Redis expiration policies? What are the memory flushing mechanisms? Write the LRU code implementation by hand?
2
1) What happened to the data written in Redis?
How often does redis in production lose some data? It’s in there. It’ll be gone in a minute.
Asking that question means redis is useless. Redis is a cache. You store it as a cache, right?
What is cache? Use memory as a cache. Is memory infinite? Memory is precious and finite, while disks are cheap and plentiful! A machine may have only a few tens of gigabytes of memory, but a few terabytes of hard disk space. Redis is primarily memory based for high performance, high concurrency read and write operations.
Since memory is limited, for example redis can only use 10 gigabytes, what if you write 20 gigabytes of data into it? Of course it kills 10 gigabytes, and then it keeps 10 gigabytes. What data does that kill? What data is retained? Of course, it is to eliminate the infrequently used data, keep the commonly used data.
So, this is one of the most basic concepts of caching, data will expire, either you set an expiration date or Redis will kill it.
Set Key Value Expiration time (1 hour)Copy the code
The key you set in, it’s gone an hour later, it’s no longer valid
2) Data clearly expired, why still occupy the memory?
Another is if you set an expiration date, do you know how Redis makes it expire for you? When will it be deleted? If you don’t know, someone asked earlier, why is redis still high in memory usage when so much data is supposed to be out of date? That’s because you don’t know how Redis deletes expired keys.
Redis has a total of 10GB of memory. Now you have written 5g data into it. The result is that you set the expiration time for all the data, and the data will expire after 1 hour. 5G data has expired. I checked in Redis, but I couldn’t find it. As a result, the expired data still occupied the memory of Redis.
If you don’t even know this question, you will be confused and unable to answer it. When you write code online, you will take it for granted that the data written into Redis will exist, which will lead to various loopholes and bugs in the system. Who will be responsible?
3,
3.1 Setting the Expiration Time
When we set a key, we can specify an expire time for the key, such as 1 hour. Ten minutes? This is useful because we can specify that the cache expires.
If you set up a batch of keys to last only an hour, how does Redis delete the batch of keys after the next hour?
The answer is: regular deletion + lazy deletion
Periodically delete
By default, Redis randomly selects some keys with expiration time every 100ms, checks whether they are expired, and deletes them if they are expired.
If you put 100,000 keys in redis, all of which are set to expire, and you check 100,000 keys every few hundred milliseconds, the Redis will basically die, and the CPU will be overloaded with expired keys.
Note that this is not a case of going through all the keys with expiration dates every 100ms, which would be a performance disaster. In fact redis randomly selects some keys every 100ms to check and delete.
However, the problem is that regular deletion may result in many expired keys not being deleted when the time comes. What can be done? So is
Lazy to delete
That is, when you get a key, Redis checks, is that key expired if it’s expired? If it’s out of date it’s deleted at this point and it doesn’t give you anything back.
It is not that the key is deleted when the time is up, but that Redis lazily checks it again when you query the key
By combining the above two methods, the expired key is guaranteed to be killed.
Redis will delete your expired key unless your system checks the key. Redis will delete the expired key.
But this is actually a problem. What happens if you periodically delete a lot of old keys and you don’t check them in time? A large number of expired keys are piled up in the redis memory block, so that the redis memory block is exhausted. How to do?
The answer is: go through the memory elimination mechanism.
3.2 Memory Elimination
If the memory of Redis is too much, the memory will be flushed out. There are some strategies as follows:
Redis has 10 keys, now it is full, redis needs to delete 5 keys
One key is queried 100 times in the last minute, one key is queried 50 times in the last 10 minutes, and one key is queried twice in the last hour
- Noeviction: New write operations will bug when memory is not large enough to accommodate new writes.
- Allkeys-lru: Removes the least recently used key (most frequently used) from the key space when memory is insufficient to accommodate new writes
- Allkeys-random: Remove a random key from the key space when memory is insufficient to hold new data
- Volatile -lru: Removing the least recently used key from the expired key space when memory is insufficient to accommodate new writes (not appropriate)
- Volatile -random: Randomly removes a key from the expired key space when memory is insufficient to accommodate new writes
- Volatile – TTL: When the memory is insufficient to accommodate new data, the key whose expiration time is earlier is removed from the key space
3.3 Write an LRU by hand
Indeed sometimes ask this, because some candidates if really through the five passes, the previous questions are answered very well, then in fact let him write LRU algorithm, you can check the coding skills
You can hand-write the original LRU algorithm in the field, which is too much code to be practical
public class LRUCache<K, V> extends LinkedHashMap<K, V> { private final int CACHE_SIZE; Public LRUCache(int cacheSize) {super((int) math.ceil (cacheSize / 0.75) + 1, 0.75f, true); CACHE_SIZE = cacheSize; CACHE_SIZE = cacheSize; } @Override protected boolean removeEldestEntry(Map.Entry eldest) { return size() > CACHE_SIZE; The oldest data in the map is automatically deleted when the number of data in the map exceeds the specified number of data in the cache}}Copy the code
At the very least, you should be able to write the above code, not build your own LRU from the ground up, but at least know how to implement a Java version of LRU using existing JDK data structures
reference
“Java engineer interview shock first season – Chinese Shi Shan teacher” welcome to pay attention to all dry technical public number :JavaEdge