search
HomeBackend DevelopmentPHP Tutorial响应版主号召,发点入门教学文章

写在前面:

虽然和php没啥直接关系,但我想现在redis是非常主流的,看着很多朋友在抱怨效率低下且还在实时查着mysql。因此介绍下redis,毕竟是内存,读写速度超过磁盘n个档次,并且redis学习成本比起mysql、mongo、cassandra这些低很多。绝对值得你去尝试。

如果您是新手,您可以仔细看下redis的同步方式,了解个大概。
如果您是redis的老用户,就当我给您提个醒,那个细节很不起眼,但确实可能会在将来影响到您。

此文并不是redis的教程文章,也不是深入redis源代码分析的核心原理介绍。我只是想从一个特别的角度让大家看到redis的好处,以及一些需要注意的地方。如果有需要,我以后会重点介绍一些redis的使用细节,参数设置之类的。跟一些精通linux c、擅长分析源码的大牛比起来我还是入门级的,如果有错误之处,请直接指出,不甚感激!



redis的数据持久化有两种方案:

1 写aof文件
2 写快照rdb文件

如果两种同时打开,redis启动后会优先尝试从aof文件中恢复数据。另外如果做主从,redis以slave会向master拉aof文件来实现主从同步的目的。

快照rdb就不说了,这里主要说一下aof。当你在redis中执行了一条命令后,redis就会以追加方式写入aof文件中。

我在本机先开好两个redis server,一个监听12345端口做master。另一个监听12346端口做slave。


然后redis-cli连接redis-server master,并写入一个测试用的值:


同样再来个redis-cli连接redis-server slave,可以看到,这个值被同步过来了。


现在我们看一下master和redis的aof文件,可以看到,完全一致。
master:

slave:


现在我从master中删了这个mytestkey,也会写入aof文件,并且会同步到slave。
master中执行del mytestkey:

查看slave的aof文件,注意最后三行,已经同步过来了。


这就是redis主从同步的基本原理。
在redis中执行的命令,会被写入aof文件,并且在master中执行的命令,会将aof文件同步到slave(准确的说是由slave向master发送sync命令拉aof文件)。

但也有例外:

刚才我新建了一个key,又把它删除了,那么库里应该啥都没了。这时如果我再删除,就等于是删除一个不存在的key,redis会返回:(integer) 0。flushdb是清库操作,无论库里是否有数据,都会返回ok。
那么这条两条命令在库里没有数据的情况下,是否会被写入aof文件并且同步呢?

答案是否定的,它并没有第二次写入del mytestkey,也没有写入flushdb。同样更不会同步给slave的aof文件。

我之所以要提这一点,是因为前两天因为这个性质导致线上业务出了个重大bug。我们线上业务是多台php web server+多台redis。redis同样也做了主从,主从关系采用单向链表式结构。即master->slave->slave->slave...master中存储着所有slave需要的公共数据。而每一台slave,都需要写自己独特的cache,因为一台php web server读一台redis slave。这样看似完美,即做到了redis的负载均衡,又避免了内存数据冗余。我们为了避免cache的冗余。也为了避免cache key的冲突(其实这个很好解决,加个key前缀就可以解决了),就违背了主从同步的核心理念 - 数据一致性,每台php web server都向自己对应的那台slave中写入数据。cache毕竟是cache,这些cache会造成数据更新的问题,我没有在set的时候给key设置失效时间。而是采用事件驱动的方式来统一一次性清除掉所有机器上的cache。

每天晚上有一系列脚本,用来更新所有redis中的公共数据,公共数据和cache存储在不同的分区中,假如公共数据存储在0库,那cache就存储在1库。我向master执行
select 0
flushdb
select 1
flushdb

接下来问题就来了,由于master的cache分区1库中一条数据都没有。所以这条flushdb实际并不会写入本机aof文件,更不会被同步到各个slave。结果就是cache根本没被清理掉,造成第二天数据没更新的bug,这里我也没设置monitor,第二天被n多部门追问起来才知道出了问题,找了一下午才找到原因。被一通谴责。。。。

解决方案嘛也很简单:
A 修改为统一向master写数据
B 晚上脚本在master上清理cache分区时,先随便set一个key,然后再执行flushdb

为了数据不冗余,也为了偷懒(这样改动最小),我采取了Plan B。
最后为了追根究底,我去看了redis的源码,发现在写aof模块中,果然有那么一段,大概意思就是执行del或flushdb之类的命令时发现没什么可释放的,就不向aof文件中追加此命令。另外,info、monitor这种和监控相关的,不对数据产生影响的命令也不会写入aof文件。


回复讨论(解决方案)

用了Mysql的内存表 深深的知道 内存和硬盘上数据读取的差距.redis还没部署 基本上都是memcache

不错 mark

灰常不错,了解一个。。。

赞,有机会也想学学PHP编程

支持一个 :)



多谢 ShadowSniper大哥!

感谢楼主分享

php新手路过

不错,有机会学习一下

了解啦,谢谢分享!

了解啦,谢谢分享!

刚好用到这个

刚好准备看redis了...赞一个

赞。。。。。。。。。。。。

谢谢楼主分享! 

谢谢楼主,学习一下

都系偶。。。。多谢 门口 

支持分享。。。


新手路过。。。

呵呵,谢谢了,新手路过

有机会也想学学PHP编程

有机会也想学学PHP编程

good good


谢谢分享呀!

能否跟你学学技术呢?

2013春运日历2013春运日历

收藏!收藏一下!

写得也不错,可以参考一下

收藏一下! 刚好准备看redis了...赞一个

编程什么的最麻烦了!!!想想就头疼...

主帖介绍的机制完全是错的,由表面原因推内部机制,抽空读读源码吧

谢谢楼主分享

不错,学习学习

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you set the session cookie parameters in PHP?How do you set the session cookie parameters in PHP?Apr 22, 2025 pm 05:33 PM

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

What is the main purpose of using sessions in PHP?What is the main purpose of using sessions in PHP?Apr 22, 2025 pm 05:25 PM

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How can you share sessions across subdomains?How can you share sessions across subdomains?Apr 22, 2025 pm 05:21 PM

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.

How does using HTTPS affect session security?How does using HTTPS affect session security?Apr 22, 2025 pm 05:13 PM

HTTPS significantly improves the security of sessions by encrypting data transmission, preventing man-in-the-middle attacks and providing authentication. 1) Encrypted data transmission: HTTPS uses SSL/TLS protocol to encrypt data to ensure that the data is not stolen or tampered during transmission. 2) Prevent man-in-the-middle attacks: Through the SSL/TLS handshake process, the client verifies the server certificate to ensure the connection legitimacy. 3) Provide authentication: HTTPS ensures that the connection is a legitimate server and protects data integrity and confidentiality.

The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

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 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools