Consistency between cached data and persistent data, this problem is summarized (I saw a good blog post), it is actually reading and writing, and there is also the issue of who comes first and who comes last. .
Theoretically, setting an expiration time for the cache is a solution to ensure eventual consistency. Under this solution, we can set the expiration time for the data stored in the cache. All write operations are subject to the database, and we only need to do our best for cache operations. (Recommended learning: Redis video tutorial)
That is to say, if the database write is successful and the cache update fails, then as long as the expiration time is reached, subsequent read requests will naturally read new values from the database and backfill the cache.
Redis is a high-performance key-value database. The emergence of redis has largely compensated for the shortcomings of key-value storage such as memcached, and can play a very good supplementary role to relational databases in some situations. It provides python, Ruby, Erlang, and PHP clients, which are very convenient to use.
According to our general scenario of using Redis, it should be like this:
In other words: we will go to redis first Determine whether the data exists. If it exists, return the cached data directly. If it does not exist, it will go to the database, read the data, and cache the data into Redis.
Applicable occasions: If the amount of data is relatively large, but it is not updated frequently (such as user rankings)
The use of the second type of Redis is completed with the first situation Different, please see the specific situation:
Here we will first go to redis to determine whether the data exists. If it exists, then directly update the corresponding data (this step The corresponding updated key will be recorded, for example, it will also be saved in redis. For example: the key is: save_update_keys [recorded with lpush list]), and the updated data will be returned to the page. If it does not exist, the content in the database will be updated first, and then a copy of the data will be saved in Redis.
NO10 Step: Later work: There will be relevant mechanisms in the background to read the keys stored in save_update_keys in Redis respectively, find the corresponding data, and update it to the DB.
Advantages: The main purpose of this process is to use Redis as a database, and updating and obtaining data is faster than DB. It is very suitable for frequent changes in large amounts of data (such as Weibo).
Disadvantages: It relies heavily on Redis, so it is necessary to save data during downtime. (However, you can use the snapshot AOF of redis. If you recover quickly, it should not have much impact, because even if Redis stops working, it will not affect subsequent data processing.)
Difficulty: planning the key in the early stage Format and storage type are important because they will affect whether the data can be synchronized to the DB.
The above is the detailed content of How to synchronize redis cache with database. For more information, please follow other related articles on the PHP Chinese website!

Redis是现在最热门的key-value数据库,Redis的最大特点是key-value存储所带来的简单和高性能;相较于MongoDB和Redis,晚一年发布的ES可能知名度要低一些,ES的特点是搜索,ES是围绕搜索设计的。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于redis的一些优势和特点,Redis 是一个开源的使用ANSI C语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式存储数据库,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis Cluster集群收缩主从节点的相关问题,包括了Cluster集群收缩概念、将6390主节点从集群中收缩、验证数据迁移过程是否导致数据异常等,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于原子操作中命令原子性的相关问题,包括了处理并发的方案、编程模型、多IO线程以及单命令的相关内容,下面一起看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了bitmap问题,Redis 为我们提供了位图这一数据结构,位图数据结构其实并不是一个全新的玩意,我们可以简单的认为就是个数组,只是里面的内容只能为0或1而已,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于实现秒杀的相关内容,包括了秒杀逻辑、存在的链接超时、超卖和库存遗留的问题,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
