Setup and configuration method for Redis database

  • 2020-05-10 23:07:24
  • OfStack

redis is a high performance key-value database. The emergence of redis has largely compensated for the deficiency of memcached and keyvalue storage, which can play a good supplementary role to relational databases in partial situations. It provides Python, Ruby, Erlang, PHP clients for easy use. The problem is that the project is still new, it may not be stable enough, and there are no practical examples for some large systems. In addition, the lack of batch get in mc is also a big problem. The network overhead of batch acquisition is not the same as that of multiple acquisition.

Performance test results:

SET operations per second 110,000 times, GET operations per second 81,000 times, the server configuration is as follows:

Linux 2.6, Xeon X3320 2.5Ghz.

The stackoverflow website USES Redis as the cache server.

Installation process:

Redis is an advanced key-value database. It is similar to memcached, but the data can be persisted and the data types supported are rich. There are strings, linked lists, sets, and ordered sets. Support for calculating the union, intersection, and complement of collections (difference) on the server side, as well as a variety of sorting functions. So Redis can also be viewed as a data structure server.

All of Redis's data is stored in memory and then stored asynchronously to disk from time to time (this is called "semi-persistent mode"). You can also write every data change to an append only file(aof) (this is called "full persistence mode").

1. Download the latest version

wget http://redis.googlecode.com/files/redis-2.0.0-rc4.tar.gz

2. The decompression

tar redis-2.0.0-rc4.tar.gz

3. Install C/C++ compilation components (not required)

apt-get install build-essential

4. Compile

cd redis-2.0.0-rc4
make

When the make command is completed, three executables, redis-server, redis-cli, redis-benchmark, and redis-stat, will be generated in the current directory. The functions of these executables are as follows:

redis-server: daemon starter for Redis server
redis-cli: Redis command line operation tool. Of course, you can also use telnet according to its plain text protocol
redis-benchmark: Redis performance testing tool that tests the read and write performance of Redis on your system and in your configuration
redis-stat: Redis state detection tool, which can detect Redis current state parameters and delay status  
There will be instructions for these commands in the back, copied from the Internet, of course...

5. Modify the configuration file

/etc/sysctl.conf
add

vm.overcommit_memory=1
Refresh the configuration to take effect

sysctl vm.overcommit_memory=1

Supplementary introduction:

  ** if memory is tight, you need to set kernel parameters:
echo 1 > /proc/sys/vm/overcommit_memory

The kernel parameters of   are described as follows:

The overcommit_memory file specifies the kernel's policy for memory allocation, which can be 0, 1, or 2.
0, means the kernel will check whether there is enough available memory for the process to use; If there is enough memory available, the memory request is allowed; Otherwise, the memory application fails and the error is returned to the application process.
1, means that the kernel allows all physical memory to be allocated, regardless of the current state of memory.
2, which means that the kernel allows more memory to be allocated than the sum of all physical memory and swap Spaces

  ** edit the redis.conf configuration file (/etc/ redis.conf) and make appropriate adjustments as required, such as:
daemonize yes # becomes a daemon, otherwise it will output a line of monitoring information every 5 seconds at startup
save 60 1000 # reduces the number of changes, which can be specified depending on the situation
maxmemory 256000000 # allocates 256M memory


After we have successfully installed Redis, we can run Redis by directly executing redis-server, which is running in the default configuration (the default configuration is not even a background line). We hope Redis run according to our requirement, we need to modify the configuration file, Redis configuration file is our second cp operation above redis. conf file, it was our copy to/usr local/redis/etc/directory. Modify it to configure our server. How to modify it? The following is the meaning of the main configuration parameter of redis.conf:
daemonize: whether to run in daemon mode
pidfile: pid file location
port: port number for listening
timeout: request timeout
loglevel: log information level
logfile: log file location
databases: number of databases opened
save * * : the frequency at which snapshots are saved, with the first * indicating how long, and the third * indicating how many writes are performed. Automatically saves snapshots when a fixed number of writes are performed in a fixed amount of time. Multiple conditions can be set.
rdbcompression: whether to use compression
dbfilename: data snapshot file name (file name only, not including directory)
dir: save directory for data snapshot (this is the directory)
appendonly: whether to turn on appendonlylog or not. If you turn on log, you will remember 1 log for each write operation, which will improve the data anti-risk ability but affect the efficiency.
appendfsync: how appendonlylog synchronizes to disk (3 options: force fsync on every write, enable fsync once per second, wait for the system to synchronize itself without calling fsync)
 

The following is a slightly modified configuration file:


daemonize yes
pidfile /usr/local/redis/var/redis.pid
port 6379
timeout 300
loglevel debug
logfile /usr/local/redis/var/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /usr/local/redis/var/
appendonly no
appendfsync always
glueoutputbuf yes
shareobjects no
shareobjectspoolsize 1024

. The above content written as redis conf and save to/usr local/redis etc/directory

Then execute it on the command line:

/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf

You can start the redis service in the background when you pass

telnet 127.0.0.1 6379

Connect to your redis service.

6. Start the service and verify

Start the server

./redis-server
or
$redis-server /etc/redis.conf  
See if it started successfully
$ ps -ef | grep redis    
or
./redis-cli ping
PONG

7. Start command line client assignment
 
redis-cli set mykey somevalue
./redis-cli get mykey

8. Turn off the service

$ redis-cli shutdown        
Close redis-server   on the specified port
$redis-cli -p 6380 shutdown

9. Clients can also connect using telnet.

[root@dbcache conf]# telnet 127.0.0.1 6379
Trying 127.0.0.1...
Connected to dbcache (127.0.0.1).
Escape character is '^]'.
set foo 3
bar
+OK
get foo
$3
bar
^]
telnet > quit
Connection closed.


Related articles: