Redis series (a): Redis Introduction

A, Redis Overview

  Redis is an open source (BSD follow protocol) Key-Value memory storage system data structure, as a database, cache and message broker. It supports five data structures: strings string, hash hash, list of list, an ordered collection set and collection zset. Redis Lua scripting support, high availability and clustering mechanism Sentinel. Applicable scene: cache, voting, sweepstakes, distributed session, charts, counting, queue, subscription publishing and so on; the specific description, see Redis official website .

Two, Redis installed

   Download: https: //redis.io/download

     install gcc: yum install gcc

    to download a good first step redis-5.0.2.tar.gz uploaded to the server / root / svr / packages directory or direct cd / root / svr / packages and then executed: wget http://download.redis.io /releases/redis-5.0.2.tar.gz

    execution cp redis-5.0.2.tar.gz ../

    CD / the root / SVR then execute: tar -xvf redis-5.0.2.tar.gz:

   cd-repeat 5.0.2:

    执行:make install PREFIX=/root/svr/redis-5.0.2

    start redis execution: bin / redis-server ../redis.conf ( Note: If you need to start background redis.conf configuration inside daemonize changed to yes )

    verify the successful launch ps -ef | grep redis

    into the redis client: bin / redis-cli

    Exit Client: quit

 Three, redis.conf main configuration in detail

parameter Explanation
bind Redis specified only receives requests from the IP address, if not set, then process all requests
port Listening port, default 6379
timeout When the timeout setting client connection seconds. When the client does not issue any command during this time, then close the connection
daemonize By default, redis is not running in the background, if you need to run in the background, to change the value of the item to yes
loglevel log divided into three grades 4, debug, verbose, notice, and warning. Production environment in general turned notice
logfile Configuration log file address, the standard default output, i.e., printing on the window displayed on the terminal
save save <seconds> <changes> value of at least 10,000 key changes which come within the meaning save 60 10000 example 60 seconds (1 minute) (the database stored - persistent RDB)
dbfilename Rdb file name
to you Data directory, two kinds of persistence rdb, aof files in this directory
replicaof replicaof <masterip> <masterport>: The master-slave configuration is the configuration represented example is redis masterip: masterport from node
masterauth master password connection
replica-serve-stale-data

When the slave master with lost connections or data is being synchronized, slave run in two ways:

1) If the replica-serve-stale-data set yes (the default setting), slave will continue to respond to client requests.

2) If the replica-serve-stale-data set to no, will remove any request other than the specified command returns an error "SYNC with master in progress"

replica-read-only Whether to set a read-only slave
repl-diskless-sync Synchronization strategy: disk or socket, the default disk mode
repl-diskless-sync-delay If a non-synchronized disk mode is turned on, you can configure the synchronization delay time to wait for a child process to produce master socket transmission by RDB data to the slave. The default value is 5 seconds, is set to 0.0 seconds per transmission without delay
repl-ping-replica-period ping requests to slave to master according to the specified interval. 10 seconds by default
repl-timeout Synchronization timeout
repl-disable-tcp-nodelay Whether disabled after the slave socket send SYNC TCP_NODELAY
repl-backlog-size backlog size setting data backup
repl-backlog-ttl After disconnecting slave starts counting the number of seconds, backlog buffer will be released
replica-priority slave priority, when linked to a master, a smaller priority number, priority will be given to enhance the salve is master, 0 as a special priority, the slave can not be identified as a master
requirepass The client should be password validation when processing any command
rename-command Rename command, you can change the name to dangerous command
maxclients Setting a maximum number of clients simultaneously connected, the default limit is 10,000 clients.
maxmemory Set the maximum memory, once the memory usage reaches the maximum memory, redis deletes the key according to the selected recovery strategy (maxmemmory-policy)
maxmemory-policy

最大内存策略:如果达到内存限制了,redis如何选择删除key:
1)volatile-lru -> 根据LRU算法删除设置过期时间的key
2)allkeys-lru -> 根据LRU算法删除任何key
3)volatile-random -> 随机移除设置过过期时间的key
4)allkeys-random -> 随机移除任何key
5)volatile-ttl -> 移除即将过期的key(minor TTL)
6)noeviction -> 不移除任何key,只返回一个写错误

maxmemory-samples

设置样本量的个数

appendonly

是否开启AOF,如果开启那么在启动时Redis将加载AOF文件,它更能保证数据的可靠性,aof的文件内容就是RESP协议

appendfilename

AOF文件名(默认:"appendonly.aof")

appendfsync

配置 Redis 多久才将数据 fsync 到磁盘一次
Redis支持三种不同的模式:
1)no:不要立刻刷,只有在操作系统需要刷的时候再刷。比较快。
2)always:每次写操作都立刻写入到aof文件。慢,但是最安全。
3)everysec:每秒写一次。折中方案。

auto-aof-rewrite-percentage

自动重写AOF文件。如果AOF日志文件增大到指定百分比,默认100。Redis能够通过 BGREWRITEAOF 自动重写AOF日志文件

auto-aof-rewrite-min-size

自动重写AOF文件。如果AOF日志文件到达最小的指定大小,默认64mb

aof-use-rdb-preamble

Redis 4.0之后配置混合持久化,需要配置 aof-use-rdb-preamble yes

lua-time-limit

Lua脚本的最大执行时间,单位为毫秒

cluster-enabled

是否开启集群 cluster-enabled yes

cluster-config-file

redis自动生成集群配置信息的文件名

cluster-node-timeout

集群节点超时毫秒数。超时的节点将被视为不可用状态。

aof-rewrite-incremental-fsync

当一个子进程重写AOF文件时,如果配置aof-rewrite-incremental-fsync yes,则文件每生成32M,数据会被同步

rdb-save-incremental-fsync

当redis保存RDB文件时,如果启用了以下选项,每生成32MB数据,文件将被fsync到磁盘

四、Redis核心数据结构

五、Redis核心原理

  1、Redis单线程为什么还能这么快

  Redis所有的数据都是在内存中,所有的运算都是内存级别的运算,而且单线程避免了多线程的切换性能损耗的问题。正因为Redis是单线程,所以要小心使用Redis指令,对于那些耗时的指令(比如keys),一定要谨慎使用,一不小心就可能会导致 Redis 卡顿。

  2、Redis单线程为何能处理那么多的并发客户端连接

  Redis的IO多路复用:redis利用epoll来实现IO多路复用,将连接信息和事件放到队列中,依次放到文件事件分派器,事件分派器将事件分发给事件处理器。(IO多路复用在后续的netty系列里面详细讲解)

  总结:Redis是一个Key-Value数据结构的内存存储系统,他支持5种数据结构,分别是String结构、Hash结构、List结构、Set结构、Zset结构;Redis支持Lua脚本,哨兵机制和集群实现高可用;介绍了安装Redis的步骤,详细的解释了redis.conf的主要配置,以及Redis的核心原理。

Guess you like

Origin www.cnblogs.com/toby-xu/p/11715704.html