Similarities and differences between redis + MySQL mode and memory + hard disk Mode

Source: Internet
Author: User

Learning any new knowledge is a step-by-step process. From Initial ignorance to simple familiarity, and then suddenly enlightened, the results are ecstatic and will be happy for a long time.

Similar to redis + MySQL and memory + Hard Disk

First, see the figure:

First of all, we know that MySQL is a persistent storage and stored in a disk. Retrieval involves some io. To solve this bottleneck, a cache occurs, for example, the most commonly used memcached (MC ). First, the user accesses MC. If it does not hit MySQL, the user accesses MySQL, and then copies the data to a part of MC like the memory and hard disk.

Redis and MC are both cached and run in the memory, which greatly improves the access speed for Web access with high data volumes. However, MC only provides a simple data structure, such as string storage. redis provides a large number of data structures, such as string, list, set, hashset, and sorted set, this makes it much easier for users. After all, it encapsulates a practical layer of functions and achieves the same effect at the same time. Of course, it is easy to discard Mc with redis.

The relationship between memory and hard disk. The main data stored on the hard disk is used for persistent storage, while the memory is the part of data currently running. The CPU accesses the memory instead of the disk, which greatly improves the running speed, of course, this is a program-based principle of local access.

Reasoning to redis + MySQL, It is a ing of memory + disk relationships. MySQL is put on disk and redis is put in memory. In this way, the Web application only accesses redis at a time. If no data is found, to access MySQL.

However, the usage of redis + MySQL and memory + disk is preferably different.

Redis + MySQL and memory + hard disk running modes are different for those who have learned about the memory and hard disk running processes. They both know that they use the Page Replacement Algorithm for scheduling, that is to say, each time data is switched from the hard disk into or out of memory by block, for example, the hard disk has a GB file. If you want to read this file, only 10 MB of the file is stored in the memory each time (this is what small blocks in Figure 1 mean ).

Someone may guess that MySQL stores 1000 GB of data. When a user accesses MySQL, 10 MB of data is copied to redis, for example, select a data with ID =, put the data from id = 10 to id = 9999 in redis for next access. However, the key lies in the access to MySQL data, which is not a local principle of files. Different users access completely different things and have nothing to do with the order of IDs.

In fact, redis's strength is not here. It is good at saving metadata-type data, that is, descriptive rather than data itself.

In this regard, I have assumed several application scenarios of redis. Please criticize and correct them:

  • Number of counters
  • ID list for storing search keywords (without content)
  • Store the follow relationship between users (non-user information)
  • Store simple static html, not all CSS and JS

In short, we found thatRedis stores a large number of index fields in the data table,If the matching information is used, you can search for it in MySQL Based on the index field. For example, you can search for the keyword "redis". The first step is to obtain redis-related information from MySQL and return it to the user, record A zset and use redis as the name. Each ID is saved in order. Someone will search for "redis" next time ", you can directly find the corresponding ID information in MySQL based on the list, which greatly improves the access speed.

Is a retrieval flowchart:

 

I have my thoughts on redis. I hope you will criticize and correct me. Thank you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.