What Redis- that? Why fast? Why single-threaded?

Reference: https://blog.csdn.net/weixin_42189604/article/details/82703997

Redis Introduction

There are two databases in use today:

  • Relational databases: RDB
    • Storing a data structure formatted
    • Mysql,Oracle,SqlServer
  • Non-relational databases: NoSql
    • All data stored in accordance with a configuration of the key storage, mainly dedicated context-specific database, the API provides a particular outwardly, instead of providing a common interface to Sql, therefore more efficient , ( simply understood NoSql can not write any statement can be achieved Sql storage and query data)
    • repeat?

Redis is an open source database kv, is Nosql, in order to ensure efficiency, the data is cached in memory of them, and a memory-based operation, high performance, he supports many types of data structures,

String String

Hash Hash

List List

Set collection

Ordered set Zset

Range queries

Several features:

  1. redis will be periodically updated data is written to disk or to modify the operation of the additional write log files, and to achieve master-slave synchronization based on this. So: redis support master-slave synchronization, data from the Vinegar weapons any number of synchronization from the server, the server may be associated with the primary server to other servers.
  2. Redis provides persistence options, these options allows users to save their data to disk for storage above, according to the actual situation, may from time to practice exporting data sets to disk (snapshot), or append them to the command log, persistent feature may be turned off, the cache data redis function as an efficient network use.
    1. This is a magical place: Redis support persistence, which can store data persistently on disk; when closed the persistence function, then Redis has become efficient network cache.
  3. Redis is not using the table: his database will not be forced to predefined or user requirements for different Redis data store associate, after all, kv database ah
In accordance with the operating mode of the database storage can be divided into:
Hard disk database and in-memory database
Redis to store data in memory, when reading and writing data will not be subjected to hard disk I / O limitations, so fast !!!
Ah Note: When you open a can of persistence option, the data will periodically be saved on disk, this also developed a database of the master-slave synchronization
 

Redis Why so fast?

First, the working mode first look at the hard disk database

(1) start with the database to read data into memory,

(2) data in memory and then saved to the hard disk

(3) Change the hard drive data and then saved to the database

Many steps, read to read to ( Database -> Memory ---> hard disk)

Second, the pattern memory database

Redis as a cache why so fast?

kv database

C language

Data stored in memory (hard to avoid i / O problems slow)

  1. Based entirely on memory:
    1. Most of the request is pure memory operation, very fast
    2. Data stored in memory, similar to HashMap, but the HashMap is is to find and operation time is O (1)
  2. Simple data structure
    1. Also simple operations on the data, the data structure Redis is specifically designed
  3. Using a single-threaded,
    1. Avoid unnecessary context switches and competitive conditions, there was no switch or multi-threaded multi-conduct caused consumes cpu, do not consider all kinds of locks, no lock would not release the lock, not because of a deadlock caused by the performance obtained consumption.
  4. Using multiple I / O multiplexing model
    1. Non-blocking IO;
  5. Use different underlying model
    1. The underlying application protocol between them to achieve the communication between the client and the way is not the same, Redis VM build their own direct mechanism, because most of the system calls the function, it will waste some time and request to move
  • Multiple I / O multiplexing model

Multiplexing using select poll epoll model can simultaneously monitor multiple streams of capacity I / O events, in his spare time, will the current thread blocked out, when one or more streams have I / O event, wakes from blocking states, so the program will again poll all streams (epoll is the only poll that really sent a stream of events), and only sequential order of processing-ready streams, this approach would avoid a lot of useless operation.

  • Multiple: multiple network connections
  • Multiplexing: refers to the same thread
    • Multiple I / O multiplexing: a single thread allows efficient processing a plurality of connection requests (to minimize the time spent in the network IO)
    • Redis and manipulate data in memory is very fast, which means that operations within the memory does not become a bottleneck Redis performance,
    • So Redis with high throughput.

Why use a single thread? Multithreaded not better?

Has been said above, Redis has been very fast!

Official : Because the Redis-based memory bottleneck operation, CPU is not the Redis, Redis bottleneck is likely to be the memory of the machine size, or network bandwidth (. And these two factors is your user's question, I single-threaded back fast enough the bottlenecks in you instead of me here)

 

But then, the way we use single-threaded performance is unable to play multi-core cpu's (after all, the thread is the basic unit of cpu scheduling and dispatching tasks)

We can improve this shortcoming by opening multiple Redis on a single machine.

Another point: This refers to a single thread is: there is only one thread to handle in processing requests our network when a formal Redis is certainly more than one thread ( focused attention)

For example, when Redis persistence will be executed in a manner the child or the child thread ( which obviously is not a single-threaded, single-threaded so clearly refers to a single-threaded processing network requests! (Multiple IO multiplexing))

 

Expansion: Several until the model you should

  1. Single-process multi-threaded model: mysql, oracle (win version)
  2. Multi-process model: Oracle (Linux)

 

Guess you like

Origin www.cnblogs.com/renshujia/p/12367697.html