Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Redis main memory database

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Introduction to Redis in-memory database:

Redis is completely open source and free, complies with the BSD protocol, and is a high-performance key-value database.

Characteristics of Redis:

-> based on memory

-> persistence: RDB, AOF

-> message mechanism: String is supported, and only Topic messages (broadcast) are supported.

-> rich data types

-> supports simple transactions

-> Master-slave replication is supported

-> HA (Sentinel): version is required. Version 2.4 or above is required.

Installation and configuration of Redis: compiler required for gcc

-> Redis standalone deployment:

Tar-zxvf redis-3.0.5.tar.gz make make PREFIX=/app/redis install

Commands related to-> Redis

Redis-benchmark provides a stress testing tool, simulation: 100000 operation

Redis-check-aof checks the AOF log file

Redis-check-dump checks RDB snapshot files

Redis-cli Command Line

Redis-sentinel Sentinel (HA of Redis)-> configuration file: sentinel.conf version 2.4 or above

Redis-server server

-> configuration file

-redis.conf

When daemonize yes is changed to yes, it will run in the background after startup.

Port 6379

-> Service startup command: redis-server conf/redis.conf

-> client connection command: redis-cli-p 6379 | redis-cli-h 192.168.10.210-p 6379

Installation and configuration of Redis cluster: (star architecture, linear architecture)

-> here we only talk about the star architecture (this architecture means that three services are started on a single server using different ports to form a cluster, and multiple servers are the same)

-Master: Port 6379 (shut down RDB and AOF)

-from: 6380, 6381 ports (enable RDB and AOF)

-> configuration file:

Redis6379.conf daemonize yes # save 900 1 # save 300 10 # save 60 10000 pidfile / app/redis/pids/redis_6379.pid logfile "/ app/redis/logs/redis_6379/redis.log" dbfilename dump.rdb Dir "/ data/redis_data/redis_6379" appendonly no appendfilename "appendonly6379.aof" redis6380.conf port 6380 slaveof 192.168.157.11 6380 Pidfile / app/redis/pids/redis_6380.pid logfile "/ app/redis/logs/redis_6380/redis.log" dbfilename dump.rdb dir "/ data/redis_data/redis_6380" appendonly yes appendfilename "appendonly6380.aof" Redis6381.conf port 6381 slaveof 192.168.157.11 6379 pidfile / app/redis/pids/redis_6381.pid logfile "/ app/redis/logs/redis_6381/redis.log" dbfilename dump.rdb dir "/ data/redis_data/redis_6381" Appendonly yes appendfilename "appendonly6381.aof"

(*) Note: default: read-only from node

(error) READONLY You can't write against a read only slave. Modify parameter: slave-read-only yes

-> HA of Redis: Sentinel mechanism (to copy sentinel.conf the file to / conf directory in the unzipped installation package)

Sentinel monitor mymaster 192.168.10.210 6379 1 # number of IP port sentinels sentinel auth-pass # configure the password sentinel down-after-milliseconds mymaster 30000 # to connect to the primary node, which means that if the heartbeat of the primary node is not received within 30 seconds The sentry thinks that after the master node down sentinel parallel-syncs mymaster 1 # elects a new master node, it can connect the number of other slave nodes at the same time sentinel failover-timeout mymaster 180000 # the maximum time allowed when the switch fails.

-Sentinel start command: redis-sentinel sentinel.conf

-> fragments of Redis:

-Twemproxy is a proxy sharding mechanism that is open source by Twitter

As a proxy, Twemproxy can accept access from multiple programs, forward it to each Redis server in the background according to routing rules, and then return it the same way.

-install Twemproxy

Git clone https://github.com/twitter/twemproxy.git cd twemproxy/ CFLAGS= "- ggdb3-O0" autoreconf-fvi & &. / configure-- prefix=/app/proxy-- enable-debug=log & & make & & make install

-configuration file: nutcracker.yml (need to copy under the unzipped installation package / conf directory to the conf directory of the installation directory)

Alpha: listen: 127.0.0.1 hash 22121 hash: fnvla_64 distribution: ketama auto_eject_hosts: true redis: true server_retry_timeout: 2000 server_failure_limit: 1 servers:-192.168.10.210

-check that the configuration file is correct:. / nutcracker-t conf/nutcracker.yml

-start the proxy server:. / nutcracker-d-c conf/nutcracker.yml

-connection to Redis requires a port connection configured by Twemproxy: redis-cli-p 22121

Operation of Redis: based on key-value form

->

-

Transaction of Redis: not a real transaction

-> the nature of Redis transactions: put a set of operations into a queue and execute them in batches

-> compare transactions between Oracle and Redis

-> transaction example of Redis: (bank transfer)

Set tom 1000 set mike 1000 multi decrby tom 100 incrby mike 100 exec

= = > locking mechanism of Redis

-> Core: when a transaction commits, if the monitored value changes, the commit fails

Command: watch

-> Redis lock instance: (ticket purchase)

Set ticket 1 set tom 1000 user 1: tom multi decr ticket decrby tom 100exec-> when submitting, it is a little slow 1) (integer)-1 = > the number of votes cannot be-1 / 2) (integer) 900user 2: before the tom is submitted, the ticket has been bought decr ticket

= = > message mechanism of Redis

-> Redis message system type

-synchronous messaging system: requires an answer from the other party

-Asynchronous messaging system: there is no need to wait for the other party to answer

-> Redis message type

-Queue: queue (peer to peer)

-Topic: topic (broadcast)

(*) Redis and Kafka: only Topic is supported

(*) JMS: Java Message Service-> both support: Queue and Topic

Recommended: Weblogic

-> commands related to Redis messages

Publish: post messages to specify channels

Subscribe: subscribe to message-specified channels

Psubscribe: subscription messages specify channels using wildcards

= = > persistence of Redis: RDB, AOF

-> persistence: recovery

-> RDB: snapshot method. Write the data in memory to the rdb file at regular intervals

-advantages: fast recovery

-disadvantages: if a power outage occurs between two RDB, the data must be lost

-parameters:

Rule: from bottom to top

Save 9001 within 15 minutes, if the value of 1 key changes, the RDB file is generated.

Save 30010 if the value of 10 key changes within 5 minutes, a RDB file is generated

Save 60 10000 if the value of 1w key is changed within 60 seconds, the RDB file is generated.

Stop-writes-on-bgsave-error yes stops writing new data if there is an error while writing the RDB file

Whether rdbcompression yes is compressed (advantages: space saving disadvantages: low recovery efficiency) can be set to: no

Rdbchecksum yes checksum to check whether the RDB file is good

Dbfilename dump.rdb if multiple instances of Redis are running on a host in a cluster, it is recommended to distinguish between rdb files

Dir. / saved directory

-> AOF:append only file (logging)

-advantages: high data security

-disadvantages: slow recovery, need to redo the log to achieve the purpose of recovery

-disabled by default

-parameters:

Appendonly no = = > yes # enable AOF appendfilename "appendonly.aof" # if multiple instances of Redis are running on a host under a cluster, it is recommended to distinguish between aof files # when to log? # appendfsync always # logs are recorded for each operation. Safest, worst performance appendfsync everysec # default: per second # appendfsync no # it is up to the operating system to decide whether to write a new log of aof when a no-appendfsync-on-rewrite no # rewrite occurs. When will AOF rewrite be performed? Auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb # what is the rewriting of AOF: rewrite set i 0 incr i incr i = > Problem: AOF file is too big 100 times * incr I finally: I = 100 # demo: rewrite simulation of AOF: 100000 operations bin/redis-benchmark-n 100000

= = > Redis question links: (find some classic problem handling links, save them as favorites, and reserve them later)

-> http://blog.sina.com.cn/s/blog_a1e9c7910102vmx5.html

-> http://blog.csdn.net/a491857321/article/details/52006376

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report