“This is the third day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.”

Redis is a great tool for our developers. Although it is fast, supports high concurrency, and various services can be integrated into the cache, it is not so simple to use it well. Let’s take a look at the story behind redis memory obsolescence from Luka’s perspective.

background

First mentioned a memory, because the business has a lot of data in cache speed slowly, and consistent data retained in the repository, the forehead, although have no practical significance, but at least prove that it existed before, data, as the administrator of redis conf bosses, give them in class, tidy up the data, Identify the tags and give them the final attribution of the data

Data immediately left tears, smug want to come again, and so on and so on. Is the expiration time, it still has not been 0 library of the emperor summoned, seems to wait for it can only silently leave, when the data gradually feel left behind, the emperor did not like me, “said the start complaining to elimination mechanism of the police, was he gave me a note, say good will always treated me well, and now, you no longer love, cheat philandering man, pei.

Being out the memory of the police control person, every day also have become accustomed to this sort of thing, who let cache is right, good things is relatively rare, the somebody else is privileged choose you these simple data, eliminating the police said time and time again touched the hearts of the data seems to have only cry, can let life after grow up. After the abandonment of the cache, the abandonment of the man who cheated on the affections of the woman, the data, once encouraged by the elimination of the police, stood up. I’m going to hang out with you.

Obsolete police patient warning data, the road to their own good to go, I here but very complex, you have the confidence to go to the distance?

Data, direct and firm said a “YES,SIR,” (words to the elimination of the police will not be the whole)

That if you are interested, stay first, said, data canthus left tears, gentle smile.

It’s time for a new life, drawled the obsolete policeman.

The adventure of data and police elimination

The next day, early in the morning, the data was eliminated, the police woke up and told you the first task of the day, to get the latest memory situation

That is, you will be the same, by the 0 library emperor management of leek data, they want to get their current overall situation, you go to the handover of memory brother, the info token with, first to find your command sister, tell him to check the memory situation;


Example:

The redis memory information can be obtained by running the info memory command.

To see the latest memory situation, data is very happy, just want to go home, saw the task card given by the police there is a task, since there is a challenge, then I will complete, data has decided to start like the next task into;

Task one:

How to configure the cache Redis memory, and how much is normally configured in a production environment?

Data a look, this configuration, can only go to the conf brother, pidian pidian to find the conf brother let him check the memory configuration, just to see the data is a novice, he pointed out a way;

If the redis memory is not configured by default, the redis memory size is not limited in 64-bit operating environments.

But conf eldest brother, careful tell data, want to configure memory, also have skill;

The data sounds serious

Production environment cache memory configuration:


For production environments, companies typically configure three quarters of the current main memory (redis hash algorithm, load factor is 0.75)

How to set redis memory:

  • Method 1: This can be set through conf
Maxmemory <bytes> the unit is 1024 bytes. -1kb 1048576 yes 1MB=1024 x 1024 for example, maxmemory 1048576000 indicates 1000MBCopy the code
  • Method 2: Use commands to configure
To start redis, run the following command: redis -cli config get maxmemory config set maxmemory 104857600Copy the code

Task 2:

What happens when Redis’ memory is full?

Data a look at this task, feel the feeling of uprising, memory will one day be occupied by our data, and then see who the deer,

Example:

I set the maximum memory to 1 byte,

127.0.0.1:6379> config set maxmemory 1
OK
127.0.0.1:6379> set key1 hello
(error) OOM command not allowed when used memory > 'maxmemory'.
Copy the code

The Redis memory is full

Task three:

When Redis data is full, how do you eliminate it?

Well, that’s the rest of my life. I have to be good,

This also needs to find inspiration from the conf brother side, said he ran to find the conf brother,

The default Redis conf file for obsolete data;

maxmemory-policy noeviction

A data strategy that does not expel data

If you have enough memory, you can save most of them;

Interview question: If a key value is expired, will name be deleted immediately when it expires?

Set K1 hello EX 10Copy the code

Answer: When a key expires and the identifier becomes unavailable, it is not deleted immediately

Because there are expired key-value pairs in action

Luca message

Data in the task of police elimination, gradually understand the details of memory, for the memory of the maximum data, configuration, and expired preservation have a certain understanding, the next is to Redis, the key value of the expiration policy, sure enough: police elimination said, data you still have a long way to go.

We will continue the story of Redis data expiration policy in the next issue, I look forward to your attention, good night;