Consistency of cache and database

       As your business grows, traffic will become more and more, we are also increasing the pressure of DB, we will take a lot of means to reduce the pressure of DB. This means there are many, complicated balancing, CDN, caching, read and write separation, cluster. Cache more common here, but also a means higher performance. But to use the cache, often used for data does not change frequently, but we also need the presence and consistent database cached scene. For example, product properties modified, should be promptly displayed on the user interface. In order to prevent dirty reads, reading the user misleading cause, we need to do caching and data consistency.

  In ENODE group which, Tang Xuehua teachers are given two options. A first timing job + Cache record. We are the first to read normal DB data, then the data cached. Then we can each perform a DB (CRUD) operations while the cache key is stored in the database. Key operating and data insertion operation is a thing, so that you can guarantee that if DB operation is successful, then the corresponding key will succeed. This time, we open a timed task according to the DB Key to delete the cache, then the user requests over, it will re-read DB, and re-cached. This is a mechanism lazy loading, and easy to implement. If your changes are frequent, then you can consider Job, when directly update the cache rather than deleted, so as not to reduce the cache hit rate, to prevent a sudden overwhelmed DB. JOB whether to delete or update the cache, depending on your number of concurrent.

  The second scheme uses the message queue implementation, somewhat similar to the feeling of the CAP. Modify the data when not directly update the DB, but to send a message. This message has to do is update the DB at the same time, to update the cache. If the update DB fails, then consider this news failed. If the update DB success, the failure to update the cache, then the message will be retried until the cache is updated successfully, so that eventual consistency. Then your news consumption power and other methods must be done to ensure that you can try again.

  

Guess you like

Origin www.cnblogs.com/learnEver/p/10953027.html