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 overview, characteristics, different from Memached, master-slave configuration of production environment, redis configuration file parsing

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Redis Overview:

Is a Key-Value-based persistent database storage, supporting rich data types, written in C language, memory-based and persistent log-type, Key-Value database, and provides API in multiple languages

Characteristics of Redis

1. Key-Value healthy value type storage

2. Support reliable data storage and landing

3. Single-process, single-thread, high-performance server

4. Stand-alone qps (query rate per second) can reach 10w

5. Suitable for high-speed read-write access with small amount of data

The difference between Redis and Memached

1. Redis can persist data storage.

2. High performance, Redis can support more than 10W per second read and write frequency

3. Rich data types. Redis supports binary data type operations such as String, Lists, Hashes, Sets and sorted Sets (ordered set).

4. All operations of Redis are atomic

5. Support publish and subscribe function

6. Support asynchronous master-slave replication

[master-slave production environment configuration file]

Master profile

Daemonize yes

Pidfile / ryzc/redis/redis.pid

Port 30000

Timeout 0

Databases 16

Maxclients 10000

Dir / ryzc/redis/

Syslog-enabled no

Slowlog-log-slower-than-1

Appendonly no

Auto-aof-rewrite-percentage 0

Requirepass kik4asfljweiojlsfjlasjfc56uhj75Ojdw2fdf

Slave profile

Daemonize yes

Pidfile / ryzc/redis/redis.pid

Port 90022

Timeout 0

Databases 16

Maxclients 10000

Dir / ryzc/redis/

Syslog-enabled no

Slowlog-log-slower-than-1

Appendonly no

Auto-aof-rewrite-percentage 0

Requirepass kik4asfljweiojlsfjlasjfc56uhj75Ojdw2fdf

Masterauth kik4asfljweiojlsfjlasjfc56uhj75Ojdw2fdf

Slaveof 127.0.0.1 30000

[profile parsing]

1. Redis does not run as a daemon by default, but can be modified by this configuration item to enable the daemon using yes

Daemonize no

two。 When Redis runs as a daemon, Redis writes pid to the / var/run/redis.pid file by default, which can be specified through pidfile

Pidfile / var/run/redis.pid

3. Specify the Redis listening port, and the default port is 6379. In a blog post, the author explains why 6379 is chosen as the default port, because 6379 has the number corresponding to MERZ on the phone button, and MERZ is named after Italian singer Alessia Merz.

Port 6379

4. Bound host address

Bind 127.0.0.1

5. When the client has been idle for how long, the connection is closed. If specified as 0, the function is disabled.

Timeout 300

6. Specifies the logging level. Redis supports a total of four levels: debug, verbose, notice, and warning. The default is verbose.

Loglevel verbose

Debug: very detailed information, suitable for development and testing

Verbose: contains a lot of less useful information, but it's a little more refreshing than debug

Notice: more suitable for production environment

Warning: warning message

7. Logging method. Default is standard output. If Redis is configured to run in daemon mode, and here the logging mode is configured as standard output, the log will be sent to / dev/null.

Logfile stdout

8. Set the number of databases. The default database is 0. You can use the SELECT command to specify the database id on the connection.

Databases 16

9. Specify how long and how many update operations will synchronize the data to the data file, which can be matched by multiple conditions.

Save

Three conditions are provided in the Redis default configuration file:

Save 900 1

Save 300 10

Save 60 10000

It represents 1 change in 900 seconds (15 minutes), 10 changes in 300 seconds (5 minutes), and 10000 changes in 60 seconds.

# save action will be triggered after 900s and 1 key has changed

# after 300 seconds and 10 key changes, the save action will be triggered

# save action will also be triggered after 60 seconds and at least 10000 key have changed

10. Specify whether to compress data when stored in the local database. The default is that yes,Redis uses LZF compression. If you want to save CPU time, you can turn this option off, but it will cause the database file to become huge.

Rdbcompression yes

11. Specify the local database file name, the default is dump.rdb, and the data will be persisted to the file specified by 'dbfilename'' in this directory.

Dbfilename dump.rdb

twelve。 Specify the local database storage directory, which must be a directory, not a file.

Dir. /

13. Set the IP address and port of the master service when the machine is a slav service. When Redis starts, it automatically synchronizes data from master.

Slaveof

14. When the master service is password protected, the password of the slav service connection master

Masterauth

15. Set the Redis connection password. If the connection password is configured, the client needs to provide the password through the AUTH command when connecting to the Redis. It is disabled by default.

Requirepass foobared

16. Set the maximum number of client connections at a time. By default, there is no limit. The number of client connections that Redis can open at the same time is the maximum number of file descriptors that can be opened by Redis processes. If you set maxclients 0, there is no limit. When the number of client connections reaches the limit, Redis closes the new connection and returns a max number of clients reached error message to the client

Maxclients 128

17. Specify the maximum memory limit of Redis. Redis will load data into memory at startup. After reaching the maximum memory, Redis will first try to clear the expired or expiring Key. When this method is processed, the maximum memory setting will still be reached, and the write operation will no longer be performed, but the read operation can still be performed. Redis's new vm mechanism stores Key in memory and Value in swap area.

Maxmemory

18. Specifies whether to log after each update operation. Redis writes data to disk asynchronously by default. If it is not enabled, it may result in data loss for a period of time in the event of a power outage. Because redis itself synchronizes data files according to the above save conditions, some data will only exist in memory for a period of time. Default is no

Appendonly no

19. Specify the update log file name. Default is appendonly.aof.

Appendfilename appendonly.aof

20. Specify the update log condition. There are 3 optional values:

No: indicates that the operating system synchronizes the data cache to disk (fast)

Always: means to manually call fsync () after each update operation to write data to disk (slow, secure)

Everysec: indicates synchronization once per second (eclectic, default)

Appendfsync everysec

21. Specify whether to enable the virtual memory mechanism. The default value is no. For a brief introduction, the VM mechanism stores the data in pages. Redis will swap the cold data on the disk, which is less visited, and the pages that visit more will be automatically swapped out to memory by the disk (I will carefully analyze the VM mechanism of Redis in a later article).

Vm-enabled no

twenty-two。 Virtual memory file path. Default is / tmp/redis.swap, which cannot be shared by multiple Redis instances.

Vm-swap-file / tmp/redis.swap

23. All data larger than vm-max-memory is stored in virtual memory, no matter how small the vm-max-memory setting is, all index data is stored in memory (Redis's index data is keys), that is, when vm-max-memory is set to 0, all value actually exists on disk. Default value is 0

Vm-max-memory 0

24. Redis swap files are divided into many page. An object can be stored on multiple page, but a page cannot be shared by multiple objects. Vm-page-size should be set according to the size of the stored data. The author suggests that if many small objects are stored, the page size should be set to 32 or 64bytes. If large objects are stored, a larger page can be used. If you are not sure, use the default value.

Vm-page-size 32

25. Set the number of page in the swap file, because the page table (a type of bitmap that indicates that the page is idle or used) is placed in memory, every 8 pages on disk will consume 1byte's memory.

Vm-pages 134217728

twenty-six。 Set the number of threads to access the swap file, it is best not to exceed the number of cores of the machine, if set to 0, then all operations on the swap file are serial, which may cause a long delay. The default value is 4

Vm-max-threads 4

twenty-seven。 Sets whether to merge smaller packets into a single packet when replying to the client. Default is on.

Glueoutputbuf yes

twenty-eight。 Specifies that a special hash algorithm is used when the number of elements exceeds a certain number or the largest element exceeds a critical value.

Hash-max-zipmap-entries 64

Hash-max-zipmap-value 512

twenty-nine。 Specifies whether to activate reset hashing, which is enabled by default (more on later when introducing Redis's hashing algorithm)

Activerehashing yes

thirty。 Specify to include other profiles that can use the same profile between multiple Redis instances on the same host, while each instance has its own specific profile

Include / path/to/local.conf

thirty-one。 'syslog-enabled' is set to yes to output the log to the system log. Default is no.

Syslog-enabled no

thirty-two。 Specifies the identifier of the syslog, which has no effect if 'syslog-enabled' is no.

Syslog-ident redis

thirty-three。 Specifies the syslog device (facility), which must be user or local0 to local7.

Syslog-facility local

thirty-four。 Replication redundancy

Master-Slave replication. Use slaveof to set one Redis instance as a slave library (hot backup) for another Redis server. Note: # configuration is only valid for the current slave.

So you can configure a slave to use different intervals to save data, listen on other ports, and so on.

Command format:

Slaveof

If master is password protected, password verification is required before slave synchronizes with master, otherwise master will reject slave's request.

Masterauth

When slave loses its connection to master, or when slave is still in master for data synchronization (not yet consistent with master), # slave can respond to client requests in two ways:

1) if slave-serve-stale-data is set to 'yes' (the default) slave will still respond to client requests, there may be a problem at this time.

2) if slave-serve-stale data is set to 'no' slave, an error message such as "SYNC with master in progress" will be returned. Except for the INFO and SLAVEOF commands.

Slave-serve-stale-data yes

thirty-five。 Rename the command and change the get command to the wget command

Rename-command get wget

thirty-six。 You can also completely kill a command by renaming it to an empty string, blocking the set command, such as:

Rename-command set ""

thirty-seven。 Maximum available memory. If exceeded, Redis will try to remove the keys from the EXPIRE collection by Redis trying to release the expiring # keys while protecting the keys with a long life cycle.

If this does not work, Redis will report an error, but query requests such as GET will still be answered.

Warning: if you want to think of Redis as a real DB, don't set it, only you want to use Redis as a cache or

It only needs to be set when there is a stateful server ('state' server).

Maxmemory

Memory cleanup strategy: if maxmemory is reached, you can take the following actions:

Volatile-lru-> use the LRU algorithm to delete expired set

Allkeys-lru-> Delete any key that follows the LRU algorithm

Volatile-random-> randomly delete key from expired set

Allkeys- > random-> randomly delete a key

Volatile-ttl-> Delete the key that is about to expire recently (the nearest expire time (minor TTL))

Noeviction-> does not expire at all. Write operations report errors directly.

Default policy:

Maxmemory-policy volatile-lru

For processing redis memory, the LRU and minor TTL algorithms are not accurate, but approximate (estimated) algorithms. So we will check some samples # to achieve the purpose of memory check. The default number of samples is 3, and you can modify it.

Maxmemory-samples 3

thirty-eight。 By default, Redis saves data to the hard drive asynchronously. If your application scenario allows the loss of the latest data due to extreme situations such as a system crash, then this is already very ok. Otherwise, you should turn on the 'appendonly' mode. When turned on, Redis will add every write to the # appendonly.aof file, which will be read when Redis starts to rebuild the dataset in memory.

Note: if you want, you can turn on both 'appendonly' mode and asynchronous dumps mode (you need to comment out the 'save' expression above to disable dumps). In this case, Redis will prefer to use appendonly.aof to ignore dump.rdb when rebuilding the dataset

Appendonly no

Appendonly file name (default: "appendonly.aof")

Appendfilename appendonly.aof

Call the fsync () function to tell the operating system to write data to the hard disk immediately.

Redis supports modes in 3:

No: no fsync, just tell OS that you can flush the data, and whether flush depends on OS. The performance is better.

Always: every time an append only log file is written, it will be fsync. Poor performance, but very safe.

Everysec: there is no interval of 1 second for fsync. Compromise.

The default is "everysec"

Appendfsync always

Appendfsync everysec

Appendfsync no

When the AOF fsync policy is set to always or everysec and the background save process (saving process) is performing a large number of Imax O operations

Redis may block the fsync () call for too long

No-appendfsync-on-rewrite no

thirty-nine。 Automatic rewriting of append only files

When the AOF log file is about to grow to a specified percentage, Redis can automatically rewrite the append only file by calling BGREWRITEAOF.

Here's what it does: Redis remembers the last rewritten AOF file, size. It then compares the size with the current size, and if the current # size is larger than the specified percentage, it will trigger an override. Similarly, you need to specify the minimum size for the AOF file to be rewritten, which is useful to avoid situations where the percentage reaches # but the file size is actually very small (which is not necessary to rewrite), which leads to AOF file rewriting.

The AOF rewriting function can be turned off when auto-aof-rewrite-percentage is set to 0

Auto-aof-rewrite-percentage 100

Auto-aof-rewrite-min-size 64mb

forty。 Slow log

Redis slow log is used to record queries that exceed the specified execution time.

You can specify two parameters: one is the slow query threshold in milliseconds, and the other is the length of slow log, which is equivalent to a queue.

Turning off slow log,0 with a negative number causes every command to be recorded.

Slowlog-log-slower-than 10000

# No setting will consume too much memory, so you still need to set it up. You can use the SLOWLOG RESET command to reclaim memory used by slow log

Slowlog-max-len 1024

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