Redis Basics Introduction

First, understand Redis

1. What is Redis?

(1) Redis is an open source (BSD license), the data structure stored in the system memory, which 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).

(2) Redis is an open source written in ANSI C, support network, also based on the persistent log memory type, Key-Value database, and provides multilingual API. From March 15, 2010, Redis development work was hosted by VMware. From May 2013 began, Redis development sponsored by Pivotal.

(3) redis is a key-value storage system. And Memcached Similarly, it supports relatively more stored value type, comprising a string (string), List (list), SET (set), zset (sorted set - ordered set) and hash (hash type). These data types are supported push / pop, add / remove and on the intersection and union, and difference richer operation, and these operations are atomic. On this basis, redis support a variety of different ways of sorting. Like with memcached, in order to ensure efficiency, the data is cached in memory. Redis difference is periodically updated in the data written to disk or to modify the operation of writing additional log file, and on this basis realize the master-slave (master and slave) synchronization.

(4) Redis is a key-value high-performance database. redis appears, to a large extent compensate for the lack of such memcached key / value store, it can play a very good complement to relational database in some situations. It provides Java, C / C ++, C #, PHP, JavaScript, Perl, Object-C, Python, Ruby, Erlang and other clients, very easy to use.

(5) Redis supports master-slave synchronization. Data can be synchronized from the primary server to an arbitrary number from the server, the server may be associated with the primary server from another server. This allows the tree Redis single executable copy. Intentionally or unintentionally, can save the data write operation. Since the full implementation of the publish / subscribe mechanism, so that the database synchronization from a tree anywhere, can subscribe to a channel and receive the full message master server publishing record. Synchronization scalability and redundancy data useful for a read operation.
(6) redis official website address is very easy to remember, is redis.io. (Specially checked, the domain name suffix io domain name owned by the state, is a british Indian Ocean territory, namely the British Indian Ocean Territory) Currently, Vmware in financing the development and maintenance redis projects.

2. Why use Redis?

From a performance and concurrency two considerations:

In our daily Java Web development, all are using the database to store data, due to the general system tasks typically do not exist in the case of high concurrency,
so this seems to be no problem, but when it comes to large amounts of data demand for some commodities such as buying the scene, or home visits moment when the larger,
single-use database to store system data because of problems facing the disk, disk read / write speed is relatively slow and serious performance drawbacks, thousands of instant request comes in,
the need to complete the system of thousands of read / write operations in a very short period of time, this time more often than not be able to withstand the database, the database system is extremely easy to cause paralysis,
eventually leading service dang serious production problems machine.

Therefore, in the case of large concurrent, all requests directly access the database, the database connection will appear abnormal. This time, we need to do to use Redis a buffer operation, so that the first request access to Redis, rather than directly accessing the database.
Here Insert Picture Description
Features:

Pure memory operation
core is based on a non-blocking IO multiplexing mechanism of
single-threaded but avoid the frequent context switching multithreading problem

The difference 3.sql and nosql

(1) concept

SQL (Structured Query Language) database, refers to a relational database. The main representatives: SQL Server, Oracle, MySQL (open source), PostgreSQL (Open Source)
NoSQL (Not Only SQL) refers to non-relational database. The main representatives: MongoDB, Redis, CouchDB

(2) the difference between
storage

SQL data tables exist specific structure; and NoSQL is more flexible and scalable storage can save a JSON document, the hash table or other means.
SQL typically stores data in a database table form

Relational data table / data set

To add data in SQL tables and fields of the structure must be well defined, such as the definition table's primary key (primary key), the index (index), the trigger (Trigger), stored procedures (stored procedure) and the like. Table structure may be defined after the update, but if there is a relatively large structural changes, then it will become more complicated.
In NoSQL, the data can be anywhere at any time to add, you do not need to define the table.
NoSQL can also be centralized index data. With MongoDB, for example, automatically creates a unique value _id field after the data set creation,
so you can increase the index after the data set is created.
From this point of view the project, NoSQL may be more appropriate initialization data is unclear or undecided in.

External data storage

How to need to increase the external SQL data associated with it, standardized approach is to add a foreign key in the original table, the associated external data table.
In addition to this NoSQL in standardized external data table approach, we can put external data directly into the original data set with non-standardized way to improve query performance.

JOIN in SQL queries

You can use SQL JOIN table chaining data from multiple relational data tables in a query out with a simple query.
NoSQL yet provide similar JOIN query for multiple data sets of data for queries. So most NoSQL data storage using non-standardized way to store data.

Data coupling

SQL is not allowed to delete external data has been used to ensure data integrity. The NoSQL do not have this concept of strong coupling, you can delete any data at any time.

Affairs

If multiple SQL tables with lots of data needs to be updated, that is, if one fails to update a table other tables can not be updated successfully.
This scenario can be controlled by a transaction, commit the transaction can be unified in order to complete after all.
But this concept is not NoSQL transaction, the operation of each data set is the atomic level.

Query Performance

Under the premise the same level of system design because NoSQL omitted consumed JOIN query, it is theoretically better than the performance of the SQL.

4.redis application scenarios:

(1) redis since the data read operation and operations among memory, reading and writing of more efficient, it is often used for cached data. Some data needs frequent access, but will not change in a short time, put redis in operation. Thereby improving the speed and reduce the user's load request sites, reducing the number of read and write the database, the data put into the cache.

(2) some common features real-time computing. Need real-time changes and display functions, you can put data on the redis in operation. Greatly improve efficiency.

(3) message queue, is often used to construct functions like live chat system, greatly improving the availability of applications

5.Redis event mechanism

Redis using event-driven mechanism to handle large amounts of network IO.
Redis event-driven library focusing exclusively on web IO, and a timer. The event library handles the following two types of events:

Event file (file event): used to handle network IO Redis between the server and the client.
Event time (time eveat): Redis server Some operations (such as function serverCron) needs at a given point in time execution, the time event processing is the timing of such operations.

aeEventLoop (Event Manager) is the core of the whole event-driven, which manages the file list of events and event table time,
continuous cycle processing with an event-ready files and time of the event due.

(1) document the event handler

Redis based Reactor pattern has developed its own network event handler, which is the file event handler. File IO event handler using multiplexing technology, while monitoring multiple sockets and handlers for different socket associated events. When the socket is readable or writable event is triggered, it will call the appropriate event handler.

Redis IO multiplexing technique used mainly: select, epoll, evport kqueue and the like.

, The file event handler has four components as shown below, which are sockets, I / O multiplexing program, file event and the event dispatcher processor. Here Insert Picture Description
①redis reactor model is based on the development of a network event processor, this processor is called the document an event handler (file event Handler). This file event handler is single-threaded, so redis it is called a single-threaded mode, using the IO multiplexing mechanism to simultaneously monitor multiple socket, select the corresponding event handler according to the time on the socket to handle this event.

② If the listening socket is ready for execution accept, read, write, close other operations when operating with the corresponding file event will be generated prior to this time will call the associated document processor a good event handler to deal with this event.

③ file event handler is single-threaded mode, but listening through the mechanism of IO multiplexing multiple socket, can achieve high-performance network communication model, and can be docked with other single-threaded internal modules to ensure that the internal redis the simplicity of threading model.

④ event configuration file processor consists of four parts: a plurality of socket, IO multiplexing program, file event dispatcher, the event processor (processor command request, reply command processor, the processor connection response, and the like) .

⑤ plurality of socket may have different simultaneous operations, each corresponding to a different event file, but the program will monitor the plurality of IO multiplexing socket, but the socket will be placed in a queue to process, from each queue remove the event dispatcher to a socket, the socket event dispatcher processor corresponding to the event.

⑥ Then after a socket of the event is finished processing, IO multiplexing program will be a queue of a socket next to the event dispatcher. Event dispatcher for each event based on the current generated socket, the corresponding event handler is selected to handle.

(2) file events
Here Insert Picture Description
so Redis a client and server connect for sending a command and the process as shown in FIG.

The client sends a request to establish a socket connection to the server, then the listening socket will produce AE_READABLE event, the connection is triggered response processor. The processor will be the client's connection request response, and then create client socket, and client status and the client socket AE_READABLE events associated with the command requesting processor.

After the client establishes a connection, sends a command to the server, the client socket will produce AE_READABLE event, triggering command requesting processor, the processor reads the client command, and then passed to the relevant program execution.

Execute commands to obtain the corresponding command reply, in order to respond to the command passed to the client, the server client socket AE_WRITEABLE events associated with the command reply processor. When a client attempts to read command response, the client socket generate AE_WRITEABLE event, the processor will trigger commands reply Reply All commands written to the socket.

(3) Time event
Redis time events are divided into the following two categories:

Timed Events: make a program executed once after a specified time.
Periodic events: every specified time so that a program is executed once.

6.redis threaded + multiplex I / O multiplexing model

redis internal use file event handler file event handler, the event handler file is single-threaded, so it is called redis single-threaded model.
It uses a plurality of IO multiplexer mechanism simultaneously listening socket, to select the corresponding event handler for processing according to the event on the socket.

Single-threaded model to solve the ills of data storage: Data concurrency-safe, no run multiple threads simultaneously access the database will have this problem, so have the mysql mvcc and locks, Memcached's cas optimistic locking to ensure data concurrency does not appear data problems caused, but redis using a single thread does not have this problem: 1, single-threaded simple enough, both in the realization redis or as a caller, you do not need to live in fear of concurrent data, no lock. 2. does not appear unnecessary thread scheduling, multi-threaded, you know, frequent switching contexts, will bring a lot of performance overhead

What is to switch contexts?

Competition multitasking CPU, cpu conversion task context switch when a CPU (context switch). CPU tasks There are four ways: processes, threads, or cause interruption of the hardware signal by triggering calls.
When a task switch, the task needs to record the current status of and access to information and address of the next task (pointer), which is the content of the context. Therefore, the context refers to the contents of the CPU registers a point in time (CPU register) and program counter (PC), and also broadly includes a virtual memory address in the process of mapping information.

Context switching process:

(1) Record the current task context (i.e., registers and counters of all states);
(2) to find and load the new task context;
(3) switches to a new task program counter position, to restore its tasks.

According to the form of execution of the task, the corresponding switch under the above, there is a process context switch, thread context switches, interrupts and context switching three categories.

Processes and threads difference:

Process is the basic unit of resource allocation and execution; thread is the basic unit of task scheduling and running. Thread does not have resources to provide pointers to process virtual memory, stack, variables and other shared resources, and threads can share resources processes.

Context of the process of the handover process:

(a) receiving a switching signal to suspend the process, the virtual memory records the current process, resources such as memory stack;
(B) the state in the context of the process of the CPU is stored in it;
© then retrieve the next process in memory context;
(D) and loaded into a register of the CPU recovery;
(E) have to refresh the virtual memory and the user process stack;
(F) jumps to the last position of the program counter pointed to recover the process.

Thread context switching:

Between (1) the process is different from the thread context switching, the process is substantially the same, and a process context switch.
Within the context of the thread (2) the process of switching. User resource without switching process, only need to switch thread private data and registers. This process will be less than the process context switching consumes resources, so compared to the advantages of multi-threaded multi-process.

Interrupt context switching:

Rapid response to hardware events, scheduling and interrupt the normal execution process will interrupt the process. The same CPU, hardware interrupt higher priority than process. Switching process is similar to the system calls when the user runs do not involve state resources. But a lot of the same interrupt context switching can cause performance problems.

Multiple I / O multiplexing model, the IO model used is the java NIO system, also linux one of many IO model, it means later when a request to access redis, redis to organize the data to be returned to the request, this time, the request redis entrance is not blocked, the other can continue to send requests to redis request until after redis io stream is completed before the caller returns data, this way, a single thread is not afraid to affect the speed of
this "multi Road "refers to multiple network connections
" multiplex "refers to reuse the same thread
the way we use single-threaded multi-core CPU performance is unable to play, but we can open multiple instances of Redis single to perfect

Published 130 original articles · won praise 1 · views 2471

Guess you like

Origin blog.csdn.net/qq_36417677/article/details/104896578