MySQL and Redis cache architecture

Source: Internet
Author: User
Tags memcached mysql update

"How to use Redis for MySQL cache"

Application of Redis to read and write data, while using the queue processor timing to write data to MySQL.

At the same time to avoid conflict, at the Redis boot to MySQL read all table key values into Redis, to Redis write data, the Redis primary key to self-increment and read, if the MySQL update fails, you need to clear the cache and synchronize the Redis primary key in a timely manner.

This process, mainly in real-time to read and write Redis, and MySQL data through the queue asynchronous processing, to alleviate the MySQL pressure, but this method is mainly based on high concurrency, and Redis's highly available cluster architecture is relatively more complex, generally not recommended.

The synchronization mechanism of memory database and MySQL

How Redis synchronizes with MySQL database

"Plan One" http://www.zhihu.com/question/23401553?sort=created

The program implements MySQL update, add, delete to delete redis data.

The program queries Redis, does not exist, queries MySQL and saves Redis

The synchronization of Redis and MySQL data, the code level can roughly do this:

READ: Read redis-> No, read mysql-> write MySQL data back to Redis

Write: Write mysql-> success, write Redis (capture all MySQL changes, write and delete events, operate on Redis)

"Scheme II" http://www.linuxidc.com/Linux/2015-01/111380.htm

Get MySQL Binlog in real time for parsing, then modify Redis

MySQL to Redis data replication scenario

Whether MySQL or Redis, itself with the mechanism of data synchronization, like the more commonly used MySQL Master/slave mode, is the slave-side analysis of Master Binlog to achieve, such data replication is actually an asynchronous process, Only when the server is in the same intranet, the asynchronous delay can almost be ignored.

So theoretically we can also analyze MySQL's Binlog file and insert data into Redis in the same way. However, this requires a very deep understanding of binlog files as well as MySQL, and because Binlog exists statement/row/mixedlevel many forms, it is very important to analyze binlog to achieve synchronization.

So here's a cheaper way to borrow the more mature MySQL UDF, put the MySQL data into Gearman first, and then sync the data to Redis with a PHP Gearman Worker that you write. There are many more processes than the analysis of Binlog, but the implementation costs are lower and easier to operate.

"Programme III"

To use the MySQL UDF, see MySQL: MySQL 5.1 Reference Manual:: 22.3 Adding New Functions to MySQL and then through trigger function after table update and insert The call is written to Redis. This is roughly what it looks like.

"http://www.zhihu.com/question/27738066"

1. First of all clear is not sure to cache, the current structure of the bottleneck where, if the bottleneck is really database operations, and then continue to look down.

2. Identify the difference between memcached and Redis, and which one to use. The former is a cache, it is impossible to permanently save data (LRU mechanism), support distributed, the latter in addition to caching and support to persist data to disk, etc., Redis to implement the distributed cache (seemingly the latest version of the integration), their own to achieve consistency hash. Because I do not know your application scenario, it is difficult to say that you must use Memcache or Redis, perhaps MongoDB will be better, such as in the storage of logs.

3. Cache large But infrequently changing data, such as comments.

4. Your idea is correct, clear, read the cache before reading, if there is a direct return, if you do not read the DB again, then write to the cache layer and return.

5. Consider whether you need master-slave, read-write separation, consider whether distributed deployment, consider whether the subsequent horizontal scaling.

6. For once and for all, follow-up maintenance and expansion is convenient, then the existing code architecture optimization, as you say replace the database components need to change a lot of code, explaining the current schema problems. You can use some of the existing frameworks, such as SPRINGMVC, to decouple your application layer from the business layer and the database layer. Do this before you cache it.

7. Make the read cache operation as a service component, provide service to the business layer, and serve the application layer with the business layer.

8. Preserve the original database components and optimize them as service components, allowing the subsequent business layers to call the cache or the database flexibly.

9. Do not recommend a one-time full-scale cache, the start of the core business, the edge of the business can be replaced by the cache components, step-to-core business.

10. Refresh the memory, take memcached as an example, add, modify and delete operations, generally using the lazy load strategy, that is, the new only write to the database, not immediately update the memcached, but wait until re-read the load into memcached, The Modify and delete operations also update the database, and then mark the data in the memcached as invalid, waiting for the next read to load again.

MySQL and Redis cache architecture

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.