Redis installation and debugging, Redis installation and debugging

Source: Internet
Author: User
Tags benchmark redis version redis windows install redis

Redis installation and debugging, Redis installation and debugging

Redis installation and debugging

Install and debug Redis linux: 64-bit CentOS 6.5

Redis version: 2.8.17 (updated to April October 31, 2014)

Redis Official Website: http://redis.io/

Redis Common commands: http://redis.io/commands

1. Install Redis

# Wget http://download.redis.io/releases/redis-2.8.17.tar.gz

# Tar xzf redis-2.8.17.tar.gz

# Cd redis-2.8.17

# Make

# Make install


If an error is reported

zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error: #error "Newer version of jemalloc required"
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/data0/src/redis-2.6.2/src'
make: *** [all] Error 2

Solution:

Make MALLOC = libc


Note: Redis does not implement its own memory pool and does not add its own stuff to the standard system memory distributor.

Redis-2.4 above comes with jemalloc, you do not need to add any parameters through zmalloc. in the c source code, we can see that Redis will first determine whether to use tcmalloc during compilation. If yes, it will replace the function implementation in the standard libc with the function implementation corresponding to tcmalloc. The second step is to determine whether jemalloc is enabled. If it is not used, the memory management function in the standard libc will be used. Therefore, we recommend that you use jemalloc to optimize tcmalloc.


To install tcmalloc, you can:

# Make USE_TCMALLOC = yes

Reference: Use TCMalloc to replace the malloc memory allocation of Nginx and Redis default glibc Libraries


Modify the CFLAGS parameter for debugging.

# Make CFLAGS = "-g-O0"

After the make command is executed, five executable files are generated in the src directory, they are redis-server, redis-cli, redis-benchmark, redis-check-aof, and redis-check-dump. Their functions are as follows:
Redis-server: daemon Startup Program of the Redis server
Redis-cli: Redis command line operation tool. Of course, you can also use telnet to operate based on its plain text protocol.
Redis-benchmark: Redis performance testing tool to test the read/write performance of Redis in your system and your configuration
Redis-check-aof: Update log check

Redis-check-dump: used for local database check

Why is the installation of three axes useless in standard Linux? The official wiki says this: Redis can run just fine without a configuration file (when executed without a config file a standard configuration is used ). with thedefault configuration Redis will log to the standard output so you can check what happens. later, you canchange the default settings.

You can also make install to copy the executable file to/usr/local/bin.

After make, a prompt will appear:

Hint: To run 'make test' is a good idea ;)


-----------------------------------------------------------------

In fact, it is not tested and can be used in general. But since people have suggested it, let's take a look at make test.

Run # make test

Error reported, prompting You need 'tclsh8. 5' in order to run the Redis test

Go to the official Tcl website http://www.tcl.tk/download Version 8.5

Then install tcl8.5:

(The configure and make locations are special. In the unix installation directory, the following is the official installation method of tcl)

# Tar xvzf tcl8.5.12-src.tar.gz

# Cd tcl8.5.13/unix/

#./Configure

# Make

# Make test

# Make install

Note: Of course, you can also use yum install tcl to install it.

After installing tcl, run make test in the redis directory. Tip:

\ O/All tests passed without errors!
Cleanup: may take some time... OK

This indicates that redis is properly installed. Yes.

-----------------------------------------------------------------

Install

# Make install



2. Run Redis

In version 2.8.17, The redis-server is stored in the src folder.

Start the Redis Server

#/Usr/redis-2.8.17/src/redis-server

If no daemonize no configuration is changed, the running information is displayed.


Note:

▲The default port number of redis is 6379. (According to the redis author antirez's blog post, 6379 is the MERZ number on the mobile phone button, and MERZ is taken from the name of Alessia Merz, the Italian karaoke girl. MERZ has long been synonymous with antirez and its friends as stupid .)

▲There are two storage methods for Redis. The default method is the snapshot method. The implementation method is to regularly save the snapshot of the memory to the hard disk, the disadvantage of this method is that if a crash occurs after persistence, a piece of data will be lost. Therefore, driven by the perfectionist, the author adds the aof method. Aof is append only mode. When writing memory data, the Operation Command is saved to the log file.

 

Run Redis later

You need to read the configuration file to start

Note: by defaultRedis. confFileDaemonizeThe parameter isNoSo redis will not run in the background. We can modify the redis. conf file, which is under the unzipped redis root directory.

Daemonize yes

If you want to provide your redis. conf, you have to run it using an additional
Parameter (the path of the configuration file ):

% Cd src
%./Redis-server/path/to/redis. conf

#/Usr/redis-2.8.17/src/redis-server/usr/redis-2.8.17/redis. conf

View redis Processes

# Ps aux | grep redis


Start Multiple redis instances

Copy the default redis. conf file to redis6383.conf, open the redis6383.conf configuration file, find port 6379, and change 6379 to 6383.

#/Usr/redis-2.8.17/src/redis-server

#/Usr/redis-2.8.17/src/redis-server/usr/redis-2.8.17/redis6383.conf


Call Service:

#/Usr/redis-2.8.17/src/redis-cli

New

Redis> set foo bar

Obtain

Redis> get foo "bar"

Delete

Redis> del foo



Fuzzy search

Redis> keys f *

Redis> keys f? O?


View info Information

Enter the # info command.Memory fragmentation Rate: Mem_fragmentation_ratio = 2.59.

The jemalloc memory distributor is used by default.



3. Configure Redis

The redis configuration file is in your installation directory. The name is redis. conf.

To put it simply, redis. conf:

Redis is not used by default.DaemonIf you want to change the value, change "daemonize no" to "daemonize yes. (You can check the printed information without changing it during the test .)

If you are not comfortable with redis's default port 6379, you can change port 6379.

If you wantPut data files in a specified folder, Change dir/opt/data/

The default value is dir./, that is, it is stored in the installation directory by default.

Connection timeout, timeout 300, no changes to the header ......

Dir is the data file path. By default, it is in the installation directory.

* Select either of the following configurations. For more information, see section 2 in this article.

###### SNAPSHOTTING ##### memory snapshot method:

The default memory snapshot policy is,

There should be at least one data change within 900 seconds (15 minutes;

Or at least 10 data changes within 300 seconds;

Or within 60 seconds, there are at least 1000 data changes. The time + number of data changes affect the appearance of memory snapshots.

###### Append only mode ###### AOF Method

Appendfsync everysec synchronization per second. Comment out the following option: appendfsync no

For other configurations, the comments in the conf file are quite clear, so I will not talk nonsense. Let's just look at your configuration.

 

You can copy the configuration file to etc.

Mkdir/etc/redis

Cp redis. conf/etc/redis. conf

Mkdir/var/lib/redis

1. redis. conf configuration parameters:

# Running as a daemon

Daemonize yes

# If a later process runs, you must specify a pid. The default value is/var/run/redis. pid.

Pidfile redis. pid

# Bind the Host IP address. The default value is 127.0.0.1.

# Bind 127.0.0.1

# Redis default listening port

Port 6379

# How many seconds after the client is idle, disconnect. The default value is 300 (seconds)

Timeout 300

# Log record level, with four optional values: debug, verbose (default), notice, and warning

Loglevel verbose

# Specify the log output file name. The default value is stdout. You can also set it to/dev/null to shield logs.

Logfile stdout

# Number of available databases; default value: 16; default value: 0

Databases 16

# Policy for saving data to disk

# If one of the Keys data is changed, it will be refreshed to disk once every 900 seconds.

Save 900 1

# When 10 Keys data items are changed, refresh them to disk once every 300 seconds

Save 300 10

# When pieces of keys data are changed, refresh to disk once every 60 seconds

Save 60 10000

# Whether to compress data objects when dump. rdb Databases

Rdbcompression yes

# Local database file name, default value: dump. rdb

Dbfilename dump. rdb

# Local database storage path. The default value is ./

Dir/var/lib/redis/

########### Replication #####################

# Redis replication Configuration

# Slaveof <masterip> <masterport> when the local machine is a slave service, set the master service IP address and port

# Masterauth <master-password> when the local machine is a slave service, set the master service connection password

# Connection password

# Requirepass foobared

# Maximum number of client connections, unlimited by default

# Maxclients 128

# Maximum memory usage settings. When the maximum memory is reached, Redis will first try to clear expired or expiring keys. After this method is processed, any Key that has reached the maximum memory settings will be cleared, no more write operations can be performed.

# Maxmemory <bytes>

# Whether to record logs after each update operation. If not enabled, data may be lost for a period of time during power failure. Because redis synchronizes data files according to the save conditions above, some data will only exist in the memory for a period of time. The default value is no.

Appendonly no

# Update the log file name. The default value is appendonly. aof.

# Appendfilename

# Update log conditions. There are three optional values. "No" indicates that data is cached and synchronized to the disk by the operating system. "always" indicates that data is manually written to the disk by calling fsync () after each update operation. "everysec" indicates that data is synchronized once per second (default ).

# Appendfsync always

Appendfsync everysec

# Appendfsync no

############### Virtual memory ###########

# Whether to enable the VM function. The default value is no.

Vm-enabled no

# Vm-enabled yes

# Virtual memory file path. The default value is/tmp/redis. swap. It cannot be shared by multiple Redis instances.

Vm-swap-file/tmp/redis. swap

# Store all data greater than vm-max-memory into the virtual memory. No matter how small the vm-max-memory settings are, all the index data is stored in the memory (Redis's index data is keys ), that is to say, when vm-max-memory is set to 0, all values exist on the disk. The default value is 0.

Vm-max-memory 0

Vm-page-size 32

Vm-pages 134217728

Vm-max-threads 4

############ Advanced config ###############

Glueoutputbuf yes

Hash-max-zipmap-entries 64

Hash-max-zipmap-value 512

# Whether to reset the Hash table

Activerehashing yes

Note: The official Redis documentation provides some suggestions on VM usage:

  • When your key is very small and the value is very large, it will be better to use the VM, because the memory saved is relatively large.
  • When your key is not hour, you can consider using some very method to convert a large key into a large value. For example, you can consider combining the key and value into a new value.
  • It is best to use linux ext3 and other file systems that support sparse files to save your swap files.
  • The vm-max-threads parameter can be used to set the number of threads accessing the swap file. It is best to set the number of threads not to exceed the number of machine cores. if it is set to 0, all operations on swap files are serial. it may cause a long delay, but it guarantees data integrity.

 

2. Adjust System Kernel Parameters

If the memory is insufficient, you need to set the kernel parameters:

Echo 1>/proc/sys/vm/overcommit_memory

Here we will talk about the meaning of this configuration:/proc/sys/vm/overcommit_memory
This file specifies the kernel memory allocation policy. The value can be 0, 1, or 2.
0 indicates that the kernel will check whether there is enough available memory for use by the process. If there is enough available memory, the memory application will be allowed; otherwise, the memory application will fail, and return the error to the application process.
1 indicates that the kernel allows all physical memory allocation regardless of the current memory status.
2. indicates that the kernel is allowed to allocate more memory than the total physical memory and swap space.
Redis generates a sub-process when dumping data. In theory, the memory occupied by the child process is the same as that of the parent. For example, the parent occupies 8 GB of memory, at this time, 8 GB of memory should also be allocated to the child. If the memory is not enough, it will often cause the redis server to go down or the IO load is too high, and the efficiency is reduced. Therefore, the optimized memory allocation policy should be set to 1 (indicating that the kernel allows allocation of all physical memory, regardless of the current memory status)

 

4. debug

Note: Because redis enables memory optimization by default, you must modify the compilation option. Otherwise, the prompt "<value optimized out>" is displayed when variables are printed in gdb. This is mostly due to gcc optimization. we can add the-O0 option to forcibly disable gcc compilation optimization.

Compiling Redis without optimizations

By default Redis is compiled with-O2Switch, this means that compiler optimizations are enabled. This makes the Redis executable faster, but at the same time it makes Redis (like any other program) harder to inspect using GDB.

Therefore, you need to modify the Makefile, which is in the/src directory.OPTIMIZATION? =-O2Option to O0

OPTIMIZATION? =-O0

 

Delete the original redis, modify makefile, and make again to start redis.

 

Debugging tips: View pointer Variables

View redis Processes

# Ps aux | grep redis

Attach gdb to process

# Gdb-p process id

(Gdb) r start again, otherwise it will not start from the main function.

(Gdb) break main sets breakpoints

(Gdb) list to view code

(Gdb) p variable name to view the variable content, use p to view the variable, this time can be viewed

 

Redis debugging skills

Redis will process events cyclically in the aeMain function of AE. c:

// Void aeMain (aeEventLoop * eventLoop) {eventLoop-> stop = 0; while (! EventLoop-> stop) {// if a function needs to be executed before event processing, run if (eventLoop-> beforesleep! = NULL) eventLoop-> beforesleep (eventLoop); // start to process the event aeProcessEvents (eventLoop, AE _ALL_EVENTS );}}

#/Usr/redis-2.6.14/src/redis-cli

Redis> set foo bar

OK

Redis> get foo "bar"

The aeMain function intercepts the request from redis-cli. Only when the aeMain function processes the request will redis-cli returnOK,Otherwise, you will always wait.


 

5. Benchmark Test

First, start redis with the startup command.

Then run the following command in any directory:

# Redis-benchmark-h localhost-p 6379-c 100-n 100000

Simulate 100 concurrent connections and 100000 requests, and check the performance of the redis server whose host is localhost and port is 6379.


Address: http://blog.csdn.net/unix21/article/details/9526295

------------------------------------------------------------------------

More references

For details about the configuration, refer to Redis 2.2.14 installation and configuration in CentOS.

Installation Reference: Redis (1) Installation

Debugging reference: http://redis.io/topics/debugging using gdb to debug redis-server

Benchmark parameter: Redis installation and deployment


In addition, to use redis in PHP, you need to install PHP extensions. For details, refer:

Php extension installation for Redis and Redis

Phpredis usage

Common redis commands



How to install redis in Windows 7

Github.com/dmajkic/redis/downloads. The downloaded Redis supports 32bit and 64bit. Select 32bit based on your actual situation. Copy the 32bit file content to the directory to be installed, such as: D: \ dev \ redis-2.4.5.
Open a cmd window and use the cd command to switch to the specified directory (D: \ dev \ redis-2.4.5) and run the redis-server.exe redis. conf.

Reopen a cmd window and use the cd command to switch to the specified directory (D: \ dev \ redis-2.4.5) and run redis-cli.exe-h 127.0.0.1-p 6379, where 127.0.0.1 is a local ip, 6379 is the default port of the redis server.

In this way, the Redis windows environment has been built.

The environment has been set up. You have to test it. For example, store a string whose key is test and value is hello word, and then obtain the key value.
 
How to configure redis

/Etc/sysctl. conf
Add
Vm. overcommit_memory = 1
Refresh configuration to make it take effect
Sysctl vm. overcommit_memory = 1
Additional information:
** If the memory is insufficient, you need to set the Kernel Parameter echo 1>/proc/sys/vm/overcommit_memory.
The kernel parameters are described as follows:
The overcommit_memory file specifies the kernel's memory allocation policy. The value can be 0, 1, or 2. 0 indicates that the kernel will check whether there is enough available memory for use by the process. If there is enough available memory, the memory application will be allowed; otherwise, the memory application will fail, and return the error to the application process. 1 indicates that the kernel allows all physical memory allocation regardless of the current memory status. 2. indicates that the kernel is allowed to allocate more memory than the total physical memory and swap space.
** Edit redis. conf configuration file (/etc/redis. conf), make appropriate adjustments as needed, such as: daemonize yes # convert to daemon; otherwise, a line of monitoring information will be output every five seconds at startup save 60 1000 # reduce the number of changes, in fact, you can specify maxmemory 256000000 # allocate MB of memory as needed.
After we successfully install Redis, we can directly execute redis-server to run Redis. At this time, it runs according to the default configuration (the default configuration is not even the background operation ). If we want Redis to run as required, we need to modify the configuration file. The Redis configuration file is the redis operated by the second cp above. conf file, which is copied to the/usr/local/redis/etc/directory. Modify it to configure our server. How to modify it? The following figure shows the meaning of parameters to be configured for redis. conf:
Daemonize: whether to run in daemon mode
Pidfile: pid File Location
Port: the port number of the listener.
Timeout: Request timeout
Loglevel: log information level
Logfile: Location of the log file
Databases: number of databases Enabled
Save **: the frequency at which snapshots are saved. The first "*" indicates the duration and the third "indicates the number of write operations performed. Snapshots are automatically saved when a certain number of write operations are performed within a certain period of time. You can set multiple conditions.
Rdbcompression: whether to use Compression
Dbfilename: Data snapshot file name (only file name, excluding directory)
Dir: directory for storing data snapshots (this is the Directory)
Appendonly: whether to enable appendonlylog. If it is enabled, a log is recorded for each write operation, which improves data risk resistance but affects efficiency.
Appendfsync: How to synchronize appendonlylog to the disk (three options are force-call fsync for each write, enable fsync once per second, and do not call fsync to wait for the system to synchronize itself)

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.