redi + keepalive brief

Redis 概念

Redis is an open source (BSD license), the data structure stored in the memory system that can be used as a database, cache and messaging middleware. It supports multiple types of data structures, such as strings (strings), hashes (hashes), lists (lists), the set (sets), ordered collection (sorted sets) and range queries, bitmaps, hyperloglogs and geospatial ( geospatial) radius of the index query. Redis built-in replication (replication), LUA scripting (Lua scripting), LRU-driven events (LRU eviction), transaction (transactions) and different levels of disk persistence (persistence), and by Redis Sentinel (Sentinel) and automatic partitioning (Cluster ) provides high availability (high availability).

Redis 优势

1. The fast speed
official data read and write performance of 100,000 / s, reason: The database is stored in the memory, redis using the c language, single-threaded multi-threaded architecture prevents competition,
2. Based on the data structure of the server key-value pairs
almost all programming languages provide similar functionality dictionary, similar to the way this organization is called data-based key way, with many of the key database is different, not only in the value of Redis can be strings, but also a specific data structure, so that not only facilitate the development in many application scenarios, but also to improve development efficiency. Redis stands REmote Dictionary Server, which provides five main data structures: strings, hashes, lists, sets, ordered set, while the evolution of the string on the basis of a bitmap (Bitmaps) and two HyperLogLog kind of magic "data structure", and with the LBS (location based service, location-based services) continues to develop.
3. The feature-rich
addition to five kinds of data structures, Redis also provides many additional features:
• It provides key expiration functionality can be used to implement caching.
It provides a publish-subscribe feature can be used to implement messaging system.
Support for Lua scripting capabilities, you can use Redis Lua to create a new command.
Provides a simple transaction capabilities to ensure that the transaction characteristics to a certain extent.
Provide a pipeline (Pipeline) function, so that the client can order a number of one-time passed
Redis, reducing the overhead of the network.
4. Simple and stable
simple Redis mainly in three aspects. First, Redis source rarely, secondly, Redis use single-threaded model, and finally, Redis does not depend on the operating system libraries (such as Memcache need to rely on such systems libevent library), Redis own implementation process related to the event Features.
The multi-client language
Redis provides a simple TCP communication protocol, many programming languages can easily access the Redis
6. The persistence
usually watch, the data in memory is not secure, in the event of power failure or machine failure, important data may be lost, so persistence Redis provides two ways: RDB and AOF, namely two strategies can be used to save memory data to the hard disk (shown in Figure 1-1), so to ensure the sustainability of data.
redi + keepalive brief
7. From the master copy
provides a copy function Redis realized Redis multiple copies of the same data (shown in Figure 1-2), is the basis for replication of the distributed Redis.
redi + keepalive brief

8. high availability and distributed
Redis from the official version 2.8 provides high availability to achieve Redis Sentinel, it is possible to ensure Redis node failure detection and automatic fault transfer. Redis from the official version 3.0 offers a distributed implementation Redis Cluster, it is truly distributed realization Redis provides a scalable high availability, literacy and capacity.

Redis集群介绍

Redis cluster assembly is a shared data among a plurality of nodes provided between Redis. Redis cluster does not support the processing of multiple command keys, because it requires moving data between different nodes, and thus does not reach such performance as Redis, in the case of high load may cause unexpected errors.
Redis cluster by partitions to provide a degree of availability, in a real environment in the case where a node is down or unreachable continues to process commands advantage of Redis cluster: automatic segmentation data to different nodes. Could continue to handle the case of partial command of the entire cluster node fails or is not reachable.
Cluster data pieces Redis
Redis cluster does not use the hash consistency, but the introduction of the concept of cluster .Redis 16384 groove hash hash slots, each key by the CRC16 checksum of modulo 16384 to determine which channel is placed each node of the cluster is responsible for a portion of the slot hash, for example, such as the current cluster has three nodes, then:
the node a 5500 containing the hash slot number to 0.
node B 5501 to contain the hash slot number 11000.
node C comprising No. 1638411001 to hash slot.
this structure can easily add or delete nodes. For example, if I want to add a new node D, I need from node A, B, C in the portion of the slot to get on D. If I want to remove node a, a needs to move in a groove on the node B and C, then a is not any node can be removed from the groove in the cluster. Since a hash slot from the mobile node to another node and will not stop service, so whether to add or delete a node to change the hash slot number will not cause the state of the cluster unavailable.

Redis 集群的主从复制模型

In order to cluster remain available in case of partial failure node or most nodes can not communicate, it uses the cluster from the master copy model, each node has the N-1 replicas.
With A in our example, B, C three nodes of the cluster, in the absence of replication model, if node B fails, then the entire cluster will think that this lack of 5501-11000 range of slots is not available.
However, if time is created in a cluster (or over a period of time ) we add a node from A1, B1, C1, then the entire cluster will have three master node and three slave nodes for each node, so that after the failure node B, B1 cluster will be elections for the new primary node continue to serve , the entire cluster will not be used because the tank can not be found without a
but when the B and B1 have failed, the cluster is not available.

Redis 一致性保证

Redis does not guarantee strong consistency of data which means that in practice the cluster may be lost when operating under certain conditions first reason is because the cluster is to use asynchronous replication write process:... The client to the master node B a write command master node B replies to the client status command master node to write his copy operation to obtain from the node B1, B2 and B3. master node copy command occurs after return command responses, as if each times processing command requests need to wait for replication to complete, then the master node processes the requested command speed will greatly reduce - we have to make a trade-off between performance and consistency. Note: Redis cluster may provide a method of synchronizing write in the future. Redis Cluster another situation that could be lost command of the emergence of a cluster network partition, and at least one client and includes a master node, including a few isolated instances.
For example assumes that the cluster contains A, B, C, A1, B1, C1 six nodes, wherein A, B, C master node, A1, B1, C1 of A, B, C from the node, as well as a customer Z1 side is assumed that a network partition occurs cluster, the cluster may be split into two parties, one of which includes most of the nodes a, C, A1, B1 and C1, one containing a small portion of the node B and the client is still able to Z1 .Z1 write to the master node B, if a network partition occurs a short time, then the cluster will continue to work, if the partition is enough time to make the most of the B1 party elections for the new master, then the Z1 was written in the data B will be lost.
Note that the network division during the Z1 client can send the write command to the master node B the maximum time is limited appearance, this time limit is called a node timeout (node timeout), is a cluster of Redis important configuration options.

Redis 高并发

1.redis is memory-based, very fast read and write speeds of memory;
2.redis is single-threaded, eliminating the need for a lot of time thread context switch;
connection 3.redis using multiplexing techniques, it can be processed concurrently. Internal achieve non-blocking IO using epoll, epoll + uses a simple event framework for their own implementation. epoll in reading, writing, close, connections are transformed into an event, and then use epoll multiplexing characteristics, and never wasted little time on the io.
The reason focuses on the following single-threaded design and fast IO multiplexing core design

为什么Redis是单线程的

1. The official answer
because Redis is based on the memory bottleneck operation, CPU is not the Redis, Redis bottlenecks are most likely to be the size of the machine memory or network bandwidth. Since the single-threaded easy to implement, and the CPU does not become a bottleneck, it is logical to adopt a single-threaded program.
2. Performance Indicators
performance on redis, the official data processing hundreds of thousands of requests per second.
3. The detailed reasons
1) does not require the consumption of various performance lock
data structures are not all Redis simple Key-Value, as well as list, hash and other complex structures which are likely to undergo a very fine-grained operation such as add an element to the back of a long list, add or delete an object in the hash them. These operations may need to add a lot of locks, resulting in synchronization overhead is greatly increased.
In short, in the single-threaded case, we will not have to consider various issues lock, lock lock release operation does not exist, not because of a deadlock may occur and cause performance overhead.
2) single-threaded multi-process program cluster
single-threaded power is actually very powerful, very high efficiency per core, multi-threaded nature can have higher performance than single-threaded cap, but in today's computing environment, even if the stand-alone multi-threaded cap often can not meet the need for further exploration of multi-server clustered programs that are multithreaded technology is still unobtainable. So single-threaded, multi-process clusters would be a stylish solution.
3) CPU consumption
using a single-threaded, to avoid unnecessary context switches and competitive conditions, there is no multi-process or multi-threaded switching lead is consumed CPU.
However, if the CPU becomes the bottleneck Redis, or do not want to let other server CUP nuclear idle, how to do?
Can be considered more than a few Redis process, Redis is a key-value database, there is no constraint between is not a relational database, data. As long as the client distinguish which key on which Redis process it.

Redis单线程的优劣势

1, the code clearer and easier processing logic do not have to consider various issues lock, lock lock release operation does not exist, not because of possible deadlocks caused by consumption of performance does not exist or switch multi-threaded multi-process and resulting consumption CPU
single-process single-threaded multi-core CPU performance drawbacks can not play, but can open multiple instances of Redis single to perfect;
2, IO multiplexing redis a network IO multiplexing technology to ensure that when multiple connections, high throughput systems.
Multiple - refers to a plurality of socket connections, multiplexing - refers to a multiplex thread. There are three main multiplexing technology: select, poll, epoll. epoll is the latest multiplexing technology is currently the best.
Here "multiplexing" refers to multiple network connections, "multiplexing" refers to reuse the same thread. Multi-channel I / O multiplexing technique that allows efficient processing a plurality of single thread connection request (IO minimize the time consumed by the network), and the data in memory Redis operation is very fast (within a memory operation will not be here performance bottleneck), the above two main Redis created with high throughput.

Redis cluster迁移

redis clusters migrate divided into two methods:
1, migration scenarios using the cluster's own cluster management tools redis-trib.rb lossless migration scenarios.
2, migration scenarios use Redis cluster management tool itself, stop the old cluster nodes, file archiving by rdb
first way Description:
deploy new node redis instance. (Note that after the deployment of a single instance, do not have to create a cluster)
to add a new node redis to the old cluster. (Add a master node)
will add a new node redis to the old cluster. (Adding slave nodes)
cluster migration
will evenly slice slots to the new master node remaining
advantages: data integrity
defects: Migration Source redis cluster is unavailable.
The second way of introduction:
old cluster node generates RDB file (Prerequisite: The disconnect Redis connection)
get old cluster nodes slot information, and records the
deployment of new Redis cluster migration environment
for the new slot allocation Redis cluster (corresponding to the old cluster nodes slots)
will rdb files are copies of the old cluster nodes at the same slot.
Advantages: redis ensure the availability of source, applications can switch at any time.
Defects: slot must always correspond to the migration process.

keepalived

What is Keepalived it, keepalived watch his name known, remain viable, which is to keep the network online, that is, so-called high-availability or hot standby, to prevent a single point of failure (single point of failure means that once a certain point has failed to It will lead to unusable) the occurrence of the entire system architecture.
Redis Master-Slave + Keepalive + VIP .
This is a classic db architecture can also be used to switch from the main mysql. The basic principle is: Keepalive survival by detecting master script, and then complete the main drift by switching from VIP (Virtual IP).
Infrastructure Figure
redi + keepalive brief

基本构建与原理

1) Keepalive + VIP: keepalived deployed on redis master-slave, redis instance survival detection script, as well as alarm notification script.
2) When redis master failure, VIP drift from the master to the slave, complete ms roles and configuration changes.
Parameter 3) redis client connection is provided in the host VIP, the entire switching process transparent to the client.

优缺点与适用场景

Advantages: simple, low cost.
Disadvantages: The maximum throughput of the entire cluster is limited to the processing capability of a single instance redis, unless an application using the multiple sets of such Keepalive + VIP scheme.
Thus poor scalability, and is not suitable for the current deployment of deployment scenarios plurality redis single instances, at least 8 redis deployed our example a physical machine.
To suit the scene: the application of concurrent requests is not very high.

Guess you like

Origin blog.51cto.com/11298469/2453291