Home Database Redis redis introduces the principle of distributed database CAP

redis introduces the principle of distributed database CAP

Feb 25, 2021 am 10:02 AM
redis

redis introduces the principle of distributed database CAP

Recommended (free):redis

The traditional ACID are What

A (Atomicity) Atomicity
C (Consistency) Consistency
I (Isolation) Independence
D (Durability) Durability

Relational database follows ACID rules. Transaction in English is very similar to transactions in the real world. It has the following four characteristics:

1. A (Atomicity) Atomicity

Atomicity is easy to understand, that is to say, All operations in the transaction are either completed or none. The condition for the success of the transaction is that all operations in the transaction are successful. As long as there is If one operation fails, the entire transaction fails and needs to be rolled back. For example, a bank transfer to transfer 100 yuan from account A to account B is divided into two steps: 1) Withdraw 100 yuan from account A; 2) Deposit 100 yuan into account B. These two steps are either completed together or not completed together. If only the first step is completed and the second step fails, the money will be 100 yuan less for no reason.

2. C (Consistency) Consistency

  Consistency is also easier to understand, which means that the database must always be in a consistent state, The running of the transaction will not change the original consistency constraints of the database.

3. I (Isolation) Independence

The so-called independence means that concurrent transactions will not affect each other,If the data to be accessed by a transaction is being modified by another transaction, as long as the other transaction is not committed, the data it accesses will not be affected by the uncommitted transaction. For example, there is a transaction that transfers 100 yuan from account A to account B. If the transaction is not completed yet, if B checks his account at this time, he will not see the newly added 100 yuan.

4. D (Durability) Durability

Persistence means that once a transaction is committed, the modifications it makes will be permanently saved in the database , it will not be lost even if there is a downtime.

CAP

C:Consistency A:Availability
P:Partition tolerance ) or distributed tolerance
The CAP theory means that in a distributed storage system, at most the above two points can only be achieved.
Strong consistency: For example, what is in the data is what it is.
All data backups in the distributed system have the same value at the same time. (Equivalent to all nodes accessing the same latest copy of data) Availability: For example, it is impossible not to use Taobao Double Eleven.
After some nodes in the cluster fail, can the entire cluster still respond to the client's read and write requests. (High availability for data updates) Partition fault tolerance: In practical terms, partitioning is equivalent to the time limit requirement for communication.
If the system cannot achieve data consistency within the time limit, it means that a partition has occurred, and a choice must be made between C and A for the current operation. For example: Taobao bags
For strong consistency, we require that the number of likes for this bag is 141, which must not be wrong. Precise guidance is required, but it is difficult to ensure data uniformity in times of high concurrency.
For high availability: There can be weak consistency, such as allowing errors in the number of likes and views, but it cannot cause website paralysis.
So most website architectures use AP. Weak consistency and high availability

For Nosql,

Partition tolerance must be achieved. The distributed system may not be in the same city, such as Taobao, and content distribution is closest to you. Taobao servers may have servers in Hangzhou, Shanghai and Suzhou. Since the current network hardware will definitely have problems such as delayed packet loss,
so partition tolerance is what we must achieve. So we can only make a trade-off between consistency and availability. No NoSQL system can guarantee these three points at the same time. CA Traditional Oracle Database

AP Choice of Most Website Architectures

CP Redis, Mongodb
Note: Trade-offs must be made in distributed architecture.

Strike a balance between consistency and availability. Most web applications do not actually require strong consistency. Therefore, sacrificing C for P is the current direction of distributed database products.

The choice between consistency and availability
For web2.0 websites, many of the main features of relational databases are often useless
Database transaction consistency requirements
Many web real-time systems do not require strict database transactions and have very low requirements for read consistency. In some cases, the requirements for write consistency are not high. Allows for eventual consistency.
Requirements for real-time writing and reading of databases
For relational databases, if you insert a piece of data and query it immediately, you can definitely read the data, but for many web applications For example, it does not require such high real-time performance. For example, after posting a message on Weibo, it is completely acceptable for my subscribers to see this news after a few seconds or even more than ten seconds.
Requirements for complex SQL queries, especially multi-table related queries
Any web system with a large amount of data is very taboo about related queries on multiple large tables, as well as complex data analysis Types of report queries, especially SNS type websites, avoid this situation from the perspective of demand and product design. Often there are only primary key queries of a single table and simple conditional paging queries of a single table. The function of SQL is greatly weakened.

Classic CAP diagram

The core of CAP theory is: a distributed system cannot satisfy consistency, availability and partition fault tolerance at the same time. Of the three needs, at most two can be satisfied well at the same time.
Therefore, according to the CAP principle, NoSQL databases are divided into three categories: satisfying the CA principle, satisfying the CP principle and satisfying the AP principle:

CA - single-point cluster, system that meets consistency and availability , are generally less scalable.
CP - A system that satisfies consistency and must tolerate partitioning. Usually the performance is not particularly high.
AP - A system that meets availability, partition tolerance, and may generally have lower consistency requirements.
redis introduces the principle of distributed database CAP

BASE

BASE is a solution proposed to solve the problems caused by the strong consistency of relational databases and the reduced availability.

 BASE is actually the abbreviation of the following three terms:

 Basically Available
 Soft state
 Eventually consistent
It The idea is to improve the overall scalability and performance of the system by allowing the system to relax its requirements for data consistency at a certain moment. Why do we say this? The reason is that large systems often cannot use distributed transactions to complete these indicators due to geographical distribution and extremely high performance requirements. To obtain these indicators, we must use another way to complete them. Here is BASE This is the solution to this problem

Introduction to distributed cluster

Distributed system
consists of multiple computers and communication software components through a computer network connection (local network or wide area network). Distributed systems are software systems built on the network. It is precisely because of the characteristics of software that distributed systems have a high degree of cohesion and transparency. Therefore, the difference between networks and distributed systems lies more in the high-level software (especially the operating system) than in the hardware. Distributed systems can be applied on different platforms such as PCs, workstations, LANs and WANs, etc.
To put it simply:
Distributed: Different service modules (projects) are deployed on multiple servers. They communicate and call through RPC/RMI to provide external services and intra-group services. cooperation.
Cluster: The same service module is deployed on multiple different servers, and unified scheduling is performed through distributed scheduling software to provide external services and access.

The above is the detailed content of redis introduces the principle of distributed database CAP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

How to configure Lua script execution time in centos redis How to configure Lua script execution time in centos redis Apr 14, 2025 pm 02:12 PM

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

How to use the redis command line How to use the redis command line Apr 10, 2025 pm 10:18 PM

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

How to set the redis expiration policy How to set the redis expiration policy Apr 10, 2025 pm 10:03 PM

There are two types of Redis data expiration strategies: periodic deletion: periodic scan to delete the expired key, which can be set through expired-time-cap-remove-count and expired-time-cap-remove-delay parameters. Lazy Deletion: Check for deletion expired keys only when keys are read or written. They can be set through lazyfree-lazy-eviction, lazyfree-lazy-expire, lazyfree-lazy-user-del parameters.

How to optimize the performance of debian readdir How to optimize the performance of debian readdir Apr 13, 2025 am 08:48 AM

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information

How to implement redis counter How to implement redis counter Apr 10, 2025 pm 10:21 PM

Redis counter is a mechanism that uses Redis key-value pair storage to implement counting operations, including the following steps: creating counter keys, increasing counts, decreasing counts, resetting counts, and obtaining counts. The advantages of Redis counters include fast speed, high concurrency, durability and simplicity and ease of use. It can be used in scenarios such as user access counting, real-time metric tracking, game scores and rankings, and order processing counting.

See all articles