Cache middleware interview (redis, memcached, mongodb)

Foreword

Github:https://github.com/yihonglei/nosql-middle

A project on how to use the cache, why use the cache, which undesirable problems may occur?

1, the project how to use caching?

How to use the project, requires a combination of scenarios you elaborate practical projects, the actual project if the local cache is useful, you can pay more attention to how to use,

Zuosa used, mainly to solve the problem, there is no problem.

2. Why use a cache?

Why use caching, which use caching middleware, this is what we should think about before use, rather than the Herd. General use cache to solve two problems,

High performance and concurrency.

high performance

For example, a page of data becomes less app, but nothing idle always someone to play brush page, check every library may be 500ms, the user brush back two to give up,

It is too slow. This time, according to business cases, these data to the cache, 2ms to check out, a 250-fold performance increase.

High concurrency

mysql relational database to a single QPS 2000 is difficult to bear, in time to catch the peak of the system, if the moment came ten thousand flow, which you might database

All kinds of overtime. For these flows, you can put the cache, step by step process from the cache, the cache memory is gone, it is natural memory support high concurrency.

3. What are the side caching problem exist?

Cache and database inconsistencies:

Data inconsistency was found with a cache usually, more classic solution is to read the time, first read cache, cache not, school database,

After the data is then taken into the cache, and returns a response. Updated when you update the database, and then delete the cache.

Avalanche cache, the cache penetration, caching breakdown problem:

Cache avalanche, such as A system peak time QPS up to 5000, if the cache hung up, all access requests are hit MySql, MySql could not carry hung up.

If you are using Redis, prevention programs are the following:

Beforehand: redis ensure high availability, the master from Sentinel +, redis Cluster, avoiding total collapse.
Things in: ehcache local cache + hystrix limiting & downgrade, MySQL avoid being killed.
Afterwards: redis persistent, once restarted, the data is automatically loaded from disk, rapid recovery of cached data.

Cache penetration, if not a hacker attack, a large number of requests to call the system, the cache is not the key, the request will hit the database,

In the case can not be found from the database, key to set a default value and an appropriate timeout, avoid using the same key has access,

It has access to the database.

Breakdown cache, the cache system setting key breakdown is generally put a certain expiration date, if all caught up in all the key at the same time has expired, all requests will hit the database.

General prevention programs need to be based on business conditions:

If the data hardly changes, it can be set to persistent data;

If the data is not very frequent changes, such as using Redis do distributed lock, waiting to get the lock, get executed, releases the lock after it's happened, so that other thread acquires the lock.

If you change more frequently, at an appropriate timing or timing script task to refresh the cache, the cache expires avoid breakdown situation.

Two Redis data types and usage scenarios?

Redis key using the key-value store of the way.

1、String

String can be a string, integer or floating.

Commonly used commands: https://blog.csdn.net/yhl_jxy/article/details/61194024

2、Hash

The Redis Hash (hash) may store a mapping between the plurality of pairs. And String types,

The hash value may be stored in a string and may be a digital value, and the user may be a hash value of the digital

Incrementing or subtraction operation. Hash is most suitable for storing objects, take up less memory space and more convenient

Access the entire object.

Commonly used commands: https://blog.csdn.net/yhl_jxy/article/details/61429638

3, set (unordered collection)

Set String type is an unordered collection. set is achieved by the hash table.

Commonly used commands: https://blog.csdn.net/yhl_jxy/article/details/67692894

4, sorted set (ordered set)

sorted set is a set of upgraded version, it is to increase the order of the properties on the basis of the set.

Commonly used commands: https://blog.csdn.net/yhl_jxy/article/details/68146099

5、List

List is a list structure, the main function push, pop, etc., push, pop operations can list,

The head of the list may be, added tail removing elements, may be implemented as a stack operation list or queue.

Commonly used commands: https://blog.csdn.net/yhl_jxy/article/details/66475963

What are three Redis persistent way?

Cache data are stored in memory, if not persistent, restart or hang service, data will be lost.

Redis persistence (rdb, aof, mixed): https://blog.csdn.net/yhl_jxy/article/details/91879874

rdb: rdb is a binary snapshot file redis memory data, back up data regularly to dump.rdb file, you can set the frequency.

The advantage of this approach is that the data are periodically refreshed by a thread, to ensure high performance of redis restart recovery is faster, 5s downside is that if the backup set time,

If not to the next 5s Redis collapse, and this data is not backed up for a few seconds you will be lost.

aof: aof a write command to back up Redis achieve backup, the advantage is that the data will not be lost in general, the disadvantage is that restart recovery is too slow,

All the write command to back up files.

Redis 4.X mixing persistence: rdb and aof different, is a persistent hybrid combination of the two backup data together, the old data stored in binary,

The latest data write commands are stored, so that when the recovery of the dump recover quickly, only need to do a small amount of the write command to recover fast and hardly lose data.

 

Continuously updated perfected ......

Published 502 original articles · won praise 358 · Views 1.18 million +

Guess you like

Origin blog.csdn.net/yhl_jxy/article/details/105379126