Redis is a high-performance Key-value database. The emergence of redis largely compensates for the shortage of keyValue storage such as memcached, and can play a very good complementary role in relational databases. It provides python, Ruby, Erlang, and PHP clients for ease of use. The problem is that this project is still quite new and may not be stable enough, and there are no actual instances for large-scale system applications. In addition, the lack of batch get in MC is also a big problem. The network overhead of batch get is different from that of multiple get.
Performance test results:
The set operation is performed 110000 times per second, and the get operation is performed 81000 times per second. The server configuration is as follows:
Linux 2.6, Xeon x3320 2.5 GHz.
The stackoverflow website uses redis as the cache server.
Installation Process:
Redis is an advanced key-value database. It is similar to memcached, but data can be persistent and supports a wide range of data types. There are string, linked list, set and ordered set. Allows you to compute, merge, and merge sets (difference) on the server side. It also supports multiple sorting functions. So redis can also be seen as a data structure server.
All redis data is stored in the memory and then asynchronously stored to the disk (this is called the "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
Ii. Decompression
TarRedis-2.0.0-rc4.tar.gz
3. Install the C/C ++ compilation component (optional)
Apt-GetInstallBuild-essential
Iv. Compilation
CDRedis-2.0.0-rc4
Make
After the make command is executed, an executable file is generated under the current directory, including redis-server, redis-CLI, redis-benchmark, and redis-Stat. Their functions are as follows:
- Redis-server: Daemon startup of the redis ServerProgram
- 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-stat: redis status detection tool that can detect redis's current status parameters and latency
These commands will be explained later. Of course they are copied from the Internet...
5. modify the configuration file
/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 parameters:
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 the redis. conf configuration file (/etc/redis. conf) and make appropriate adjustments as needed, for example:
Daemonize yes # convert to daemon; otherwise, a monitoring information line is output every 5 seconds at startup.
Save 60 1000 # reduce the number of changes, which can be specified as needed
Maxmemory 256000000 # allocate MB of memory
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)
The following is a slightly modified configuration file:
Daemonize yespidfile/usr/local/redis/var/redis. pidport 6379 timeout 300 loglevel debuglogfile/usr/local/redis/var/redis. logdatabases 16 save 900 1 save 300 10 save 60 10000 rdbcompression yesdbfilename dump. rdbdir/usr/local/redis/var/appendonly noappendfsync alwaysglueoutputbuf yes1_objects no1_objectspoolsize 1024
Write the above content as redis. conf and save it to the/usr/local/redis/etc/directory.
Then run the following command in the command line:
1 |
/Usr/local/redis/bin/redis-server/usr/local/redis/etc/redis. conf |
You can start the redis service in the background.
Connect to your redis service.
6. Start and verify the service
Start the server
./Redis-Server
Or
$ Redis-server/etc/redis. conf
Check whether startup is successful
$ PS-Ef | grep redis
Or
./Redis-cli Ping
Pong
7. Start the command line client and assign values
Redis-cli Set Mykey somevalue
./Redis-cli get mykey
8. Close the service
$ Redis-cli Shutdown
# Disable the redis-server on the specified port
$ Redis-cli-P6380Shutdown
9. The client can also be connected through 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 3
bar
^]
telnet> quit
Connection closed.