#Redis is an indispensable service for current web programming. Its characteristics are obvious. Compared with memcached, it can be cached and restarted without losing data, which is very easy to use. So the question is, how does it do it?
RDB
RDB is a means of persistence, which writes data in memory to disk under certain conditions. So under what conditions is it written? It is impossible to write without thinking. Writing one by one will affect the performance. You can't wait for a long time before writing one. If there is a downtime in the middle and all the data is lost, it is better to use memcached. There is such a configuration in the redis configuration:
save 900 1
save 300 10
save 60 10000
A very critical piece of configuration, this is the core of RDB persistence. Meaning:
1. If 1 key changes (insert or update) in 900 seconds, I will synchronize it to the disk
2. If in 300 seconds, there is If 10 keys change (insert or update), I will synchronize it to the disk
3. If there are 10,000 key changes (insert or update) in 60 seconds, I will synchronize it to the disk
How do we know these time points and the number of changes? There are two other extremely critical things at this time, one is called the dirty counter, and the other is called lastsave (the time of the last save). The dirty counter specifically records the time since the last save. Change the number of keys. Lastsave records the time when the save is executed. For example, the initial time is time1 and dirty is 0. At this time, 20 keys have changed, dirty is 20, and then the current time is time2, time2-time1 > ;= 300, if the second condition is met, the data in the memory will be saved, and dirty is cleared to 0, and then wait for the condition to be triggered.
If I have 100,000 keys within 60 seconds, then the problem comes. When a large number of disk IO comes, the redis main process will be blocked, and all the commands during the period will not be executed. How can this be done? , so there came a one called bgsave, which is a sub-process forked out of the main redis process and is specialized in performing RDB persistence work.
The saved file format is in binary format. If the database goes down, human intervention is not required for recovery, and redis will automatically read the disk file.
AOF
Different from RDB, AOF stores the commands you execute. When the aof function is turned on, the executed update command will not be written directly to the aof file. Instead of writing to an aof buf first, we know that we cannot always write to buf. buf is also memory, so when can it be synchronized to the disk? There is also such a configuration in redis
appendfsync always
appendfsync everysec
appendfsync no
means:
1. As long as there is an update I will synchronize the command
2. If the last synchronization time is more than one second from now, synchronize
3. If it is not synchronized, wait for the operating system to judge by itself (I will synchronize when I am free) )
Under analysis, the first type of io is frequent and has high io pressure, but the probability of losing data is the smallest. The second type of io is not very stressful and only loses about 1 second of data at most. The third type of io is There is little pressure and the probability of losing data is too high. All things considered, generally the second option. But there is still a question. I executed INCR num 100 times. Logically, num is 100. There are 100 same commands in aof. There is nothing wrong with it. So what is the difference between executing INCR num 100 times and SET num 100? The same result. The former takes 99 times more space, which is very wasteful, so AOF rewriting appeared. How is it done? It's very simple: first read the current value from the database, and then replace it with a record. This is the principle of AOF rewriting. Rewriting takes time, so it is handled by a child process. During the rewriting process, what should I do if a new command comes? The old method is to write the buf buffer. After the rewriting is completed, append the commands in the buf to the new aof, and then replace the old aof with the new aof. Implemented the rewrite.
This article comes from redis tutorial, welcome to learn.
The above is the detailed content of How to achieve persistence in redis. For more information, please follow other related articles on the PHP Chinese website!

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

Redis is a NoSQL database suitable for efficient storage and access of large-scale data. 1.Redis is an open source memory data structure storage system that supports multiple data structures. 2. It provides extremely fast read and write speeds, suitable for caching, session management, etc. 3.Redis supports persistence and ensures data security through RDB and AOF. 4. Usage examples include basic key-value pair operations and advanced collection deduplication functions. 5. Common errors include connection problems, data type mismatch and memory overflow, so you need to pay attention to debugging. 6. Performance optimization suggestions include selecting the appropriate data structure and setting up memory elimination strategies.

The applications of Redis in the real world include: 1. As a cache system, accelerate database query, 2. To store the session data of web applications, 3. To implement real-time rankings, 4. To simplify message delivery as a message queue. Redis's versatility and high performance make it shine in these scenarios.

Redis stands out because of its high speed, versatility and rich data structure. 1) Redis supports data structures such as strings, lists, collections, hashs and ordered collections. 2) It stores data through memory and supports RDB and AOF persistence. 3) Starting from Redis 6.0, multi-threaded I/O operations have been introduced, which has improved performance in high concurrency scenarios.

RedisisclassifiedasaNoSQLdatabasebecauseitusesakey-valuedatamodelinsteadofthetraditionalrelationaldatabasemodel.Itoffersspeedandflexibility,makingitidealforreal-timeapplicationsandcaching,butitmaynotbesuitableforscenariosrequiringstrictdataintegrityo

Redis improves application performance and scalability by caching data, implementing distributed locking and data persistence. 1) Cache data: Use Redis to cache frequently accessed data to improve data access speed. 2) Distributed lock: Use Redis to implement distributed locks to ensure the security of operation in a distributed environment. 3) Data persistence: Ensure data security through RDB and AOF mechanisms to prevent data loss.

Redis's data model and structure include five main types: 1. String: used to store text or binary data, and supports atomic operations. 2. List: Ordered elements collection, suitable for queues and stacks. 3. Set: Unordered unique elements set, supporting set operation. 4. Ordered Set (SortedSet): A unique set of elements with scores, suitable for rankings. 5. Hash table (Hash): a collection of key-value pairs, suitable for storing objects.

Redis's database methods include in-memory databases and key-value storage. 1) Redis stores data in memory, and reads and writes fast. 2) It uses key-value pairs to store data, supports complex data structures such as lists, collections, hash tables and ordered collections, suitable for caches and NoSQL databases.


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

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

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),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.