Redis detailed and common problems solutions

Source: Internet
Author: User
Tags lzf disk usage password protection

About Redis

Redis is a key-value storage system. Similar to memcached, it supports storing more value types, including string (string), list (linked list), set (set), Zset (SortedSet-ordered collection), and hash (hash type). These data types support Push/pop, Add/remove, and intersection-set and difference sets, and richer operations, and these operations are atomic. Based on this, Redis supports sorting in a variety of different ways. As with memcached, data is cached in memory to ensure efficiency. The difference is that Redis periodically writes the updated data to disk or writes the modified operation to the appended record file, and Master-slave (Master-Slave) synchronization is implemented on this basis.

Installation:

Installing Redis

Official website: http://redis.io/

Official download: Http://redis.io/download can download different versions as needed

Windows Edition: Https://github.com/ServiceStack/redis-windows

To download the installation package:

Unzip startup Redis

After extracting the redis64-2.8.17 version, locate the redis.windows.conf and modify the configuration file contents as follows.

Support Data structure

Redis offers five types of data: String,hash,list,set and Zset (SortedSet).

Redis Profile Main parameter configuration

1, specify the Redis listening port, the default port is 6379.

Port 6379

2, the host address of the binding

Bind 127.0.0.1

3, when the client is idle for how long to close the connection, if specified as 0, indicating that the function is turned off

Timeout 300

4. Specify logging level, Redis supports four levels: Debug, verbose, notice, warning, default is verbose.

LogLevel verbose

5, logging mode, the default is standard output, if you configure Redis to run as a daemon, and here is configured to log records as standard output, the log will be sent to/dev/null.

logfile stdout

6, set the number of databases, the default database is 0, you can use the Select <dbid> command to specify the database ID on the connection.

Databases 16

7, specify how long, how many times the update operation, the data synchronization to the data file, multiple conditions can be combined.

Save <seconds><changes>

8. Three conditions are available in the Redis default configuration file:

Save 900 1

Save 300 10

Save 60 10000

Represents 1 changes in 900 seconds (15 minutes), 5 changes in 300 seconds (10 minutes), and 60 changes in 10,000 seconds.

9, specify whether to compress the data when stored to the local database, the default is Yes,redis with LZF compression, if in order to save CPU time, you can turn off this option, but will cause the database file becomes huge.

Rdbcompression Yes

10, specify the local database file name, the default value is Dump.rdb.

Dbfilename Dump.rdb

11. Specify the local database to store the directory.

Dir./

12. Set the IP address and port of the master service when this machine is slave service, and it will automatically synchronize data from Master when Redis is started.

Slaveof <masterip><masterport>

13. When the master service has password protection set, the slave service connects to the master password.

Masterauth <master-password>

14, set up the Redis connection password, if the connection password is configured, the client will need to provide the password via auth <password> command when connecting Redis, it is turned off by default.

Requirepass foobared

15, set the maximum number of client connections at the same time, the default is unlimited, Redis can open the number of client connections for the Redis process can open the maximum number of file descriptors, if set maxclients 0, indicating no restrictions. When the number of client connections reaches the limit, Redis closes the new connection and returns the max number of clientsreached error message to the client.

MaxClients 128

16. Specify Redis maximum memory limit, Redis will load data into memory when booting, Redis will try to clear expired or expiring key first, when this method processing, still reach the maximum memory setting, will no longer write operation, but still can read operation. Redis's new VM mechanism will store the key in memory, and value will be stored in the swap area.

MaxMemory <bytes>

17, specify whether to log records after each update operation, Redis asynchronously writes data to disk by default and, if not turned on, may cause data loss for a period of time when power is lost. Because the Redis itself synchronizes data files in sync with the save conditions above, some data will only exist in memory for a period of time. The default is No.

AppendOnly No

18. Specify the update log file name, default to Appendonly.aof

Appendfilename appendonly.aof

19, specify the update log conditions, a total of 3 optional values:
No: Indicates that the data cache of the operating system is synchronized to disk (fast)
always: Represents a manual call to Fsync () to write data to disk (slow, secure) after each update operation
everysec: Indicates synchronization once per second (trade-offs, default values)

Appendfsync everysec

20, set when replying to the client, whether the smaller package is merged into a package send, the default is open

Glueoutputbuf Yes

21. A special hashing algorithm is used to specify that more than a certain number or maximum element exceeds a certain threshold.

Hash-max-zipmap-entries 64

Hash-max-zipmap-value 512

22. Specifies whether to activate the reset hash, which is on by default (described later in the introduction of the Redis hashing algorithm).

activerehashing Yes

23. Slave sends a PING request to the server based on the specified interval. The time interval can be set by Repl_ping_slave_period. Default 10 seconds

Repl-ping-slave-period10

24, when a slave loss and master connection, or synchronization is in progress, slave behavior has two possibilities:

(1) If Slave-serve-stale-data is set to "Yes" (the default), slave will continue to respond to client requests, which may be normal or empty data that has not yet obtained a value.

(2) If Slave-serve-stale-data is set to "no", Slave will reply "Synchronizing from master (Syncwith Master in progress)" to handle various requests, in addition to the INFO and slaveof commands.

Slave-serve-stale-datayes

25. Redis Master-slave configuration

Redis supports Master-slave's master-slave configuration by specifying the slaveof parameter as the host's IP and port in the slave configuration file.

Slaveof 192.168.1.23 6379

26. Specify other profiles that can use the same configuration file across multiple Redis instances on the same host, while each instance has its own specific configuration file.

include/path/to/local.conf

include/path/to/other.conf

Common exception handling in Redis and memory CPU optimizations

Redis often comes up with a problem summary:

1. When the change occurs--maxheap and--heapdir

A RedisQFork.dat file is created when you start these two versions, and I'm not sure if the Redisqfork file is smaller, but I'm sure you can adjust the location of the file by setting the Redis boot parameters heapdir.

I search for "Heapdir" in the redis.windows.conf file, and then add a line below the default commented out, specifying the path to the Redis memory-mapped file (storage mapped files):

2, Redis common exception handling

1

2

3

# heapdir指定内存映射文件路径名,不能是文件名

# heapdir <directory path(absoluteorrelative)>

heapdir D:/temp/redis_heapdir/

Specific files and directories please specify according to your own disk. My symptoms and situations are this, 16GB memory, Windows version 64bit Redis, boot Redis after C:\Windows\ServiceProfiles\NetworkService\AppData\Local\ on C drive The Redis directory generates a 16GB size Redisqfork_8792.dat file (8792 is the process PID that starts, and this file is automatically deleted if Redis is turned off). Originally in order to save money only on the 64GB solid-state drive, the result 16G a eat down, c disk only 16GB free space, since set to D disk, after restarting, feeling obsessive-compulsive disorder is much better.

3. Common exception Handling

If you encounter java.net.SocketTimeoutException:Readtimed out exception exception information

Please try to set your own time-out value when constructing jedispool. Jedispool default Time-out is 2 seconds (in milliseconds)

Pool = new Jedispool (config, ip,port,redisconfig.gettimeout ());

4. Optimization of CPU usage efficiency in Redis

Setting the configuration of No-appendfsync-on-rewrite to Yes can alleviate this problem, set to Yes to indicate that a new write operation is not fsync during rewrite, exists in memory temporarily, and then writes after rewrite is complete.

It is best not to turn on the master aof backup feature.

Redis can also turn off automatic persistence, comment out these save configurations, or save ""

If there is an error in the background save to disk, the write operation will stop.

Stop-writes-on-bgsave-error Yes.

Compressing an RDB file with Lzf consumes the CPU, but can reduce disk usage.

Rdbcompression Yes

When you save an RDB and load an RDB file, you can prevent errors, but with about 10% performance, you can turn them off and improve performance.

Rdbchecksum Yes

5. When the following error occurs:

Virtualalloc/cowalloc fail! Out of Memory allocating 42bytes!

Modified: MaxMemory 1024000000

Maxheap 1024000000

Redis Client Common commands

Get key

Set key value

Clear

Exists key

Del key

Dbsize

Lpush Key value

Info All

Info

Monitor

Hgetall Key

Redis 127.0.0.1:6379> Info #查看server版本内存使用连接等信息

Redis 127.0.0.1:6379> Client List #获取客户连接列表

Redis 127.0.0.1:6379> client Kill 127.0.0.1:33441 #终止某个客户端连接

Redis 127.0.0.1:6379> dbsize #当前保存key的数量

Redis 127.0.0.1:6379> Save #立即保存数据到硬盘

Redis 127.0.0.1:6379> Bgsave #异步保存数据到硬盘

Redis 127.0.0.1:6379> flushdb #当前库中移除所有key

Redis 127.0.0.1:6379> Flushall #移除所有key从所有库中

Redis 127.0.0.1:6379> Lastsave #获取上次成功保存到硬盘的时间戳

Redis 127.0.0.1:6379> Monitor #实时监测服务器接收到的请求

Redis 127.0.0.1:6379> Slowlog Len #查询慢查询日志条数

Redis 127.0.0.1:6379> Slowlog Get #返回所有的慢查询日志, maximum depends on Slowlog-max-len configuration

Redis 127.0.0.1:6379> Slowlog get 2 #打印两条慢查询日志

Redis 127.0.0.1:6379> slowlog Reset #清空慢查询日志信息


Redis detailed and common problems solutions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.