As the cache is used more and more widely in Web services, Redis can be said to be the most popular NoSQL database today! The biggest difference between Redis and memcached is that Redis supports more data types, including string, hash, list, set, sorted list, and so on, so Redis is developing very quickly and many companies have replaced memcached with Redis. I have also done some redis development, now do some summary.
1. Redis Common Configuration
daemonize No//redis is not run as a daemon by default, it can be modified by this configuration item and the daemon is enabled with Yes
pidfile/var/run/redis.pid//When Redis is running as daemon, the files written by the PID can be specified by Pidfile
Port 6379//Specify Redis listening port, default port is 6379
Bind 127.0.0.1//Only accept requests coming to an IP
Save <seconds> <changes>//Specify how many times the update action is
rdbcompression Yes //Specifies whether the data is compressed when stored to a local database, the default is Yes,redis with 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
dbfilename dump.rdb//Specify local database file name, default value is Dump.rdb
dir./data//Specify the local database to hold the directory
Tcp-keepalive 60//Active detection of client links is normal, the official recommendation 60s
slaveof <masterip> <masterport>/Set the IP address and port of the master service are set when the machine is Slav, and it automatically synchronizes data from master when Redis boots
appendonly No// Specifies whether logging occurs after each update operation, Redis asynchronously writes data to the disk by default and, if not turned on, may result in data loss over a period of time when power is lost.
appendfilename appendonly.aof//Specify update log file name, default is Appendonly.aof
vm-enabled No //Specifies whether the virtual memory mechanism is enabled, the default value is no, a simple introduction, the VM mechanism to store data paging, by Redis will be less accessible pages, such as cold data swap to disk, access to more pages from the disk automatically swapped out into memory
2. Redis.clients.jedis.exceptions.JedisException:Could not return the resource to the pool exception
In the development I encountered this anomaly, over and over to see the code did not find where to write the wrong, finally helpless restart the machine, the Redis also restarted, the results do not throw an exception!
Oh, I found that I was in the same database of Redis put string string, and want to put byte[] caused, remember the same database to put the same data structure!
3. Spring and Jedis integration
Jedis is the official Redis-Preferred Java client development package. Although the Spring-data-redis package is better, but I prefer to use the Jedis directly, so flexibility is better. The following describes the spring and Jedis configurations:
Spring configuration file configuration bean, primarily creating Jedis required objects in the spring container
<BeanID= "Jedispoolconfig"class= "Redis.clients.jedis.JedisPoolConfig"> < Propertyname= "Maxtotal"value= " the" /> < Propertyname= "Testonborrow"value= "true" /> < Propertyname= "Maxidle"value= "Ten" /> < Propertyname= "Maxwaitmillis"value= "+" /> </Bean> <BeanID= "Shardedjedispool"class= "Redis.clients.jedis.ShardedJedisPool"> <Constructor-argIndex= "0"ref= "Jedispoolconfig" /> <Constructor-argIndex= "1"> <List> <Beanclass= "Redis.clients.jedis.JedisShardInfo"> <Constructor-argIndex= "0"value= "127.0.0.1" /> <Constructor-argIndex= "1"value= "6379"type= "int" /> </Bean> </List> </Constructor-arg> </Bean>
Next, you can automatically inject the Jedis object into the Java persistence Layer or the service layer:
Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.stereotype.Repository;ImportRedis.clients.jedis.ShardedJedis;ImportRedis.clients.jedis.ShardedJedisPool; @Repository Public classRedisdao {@AutowiredPrivateShardedjedispool Pool; Publicstring getString (String key) {Shardedjedis Jedis=NULL; Try{Jedis=Pool.getresource (); returnJedis.get (key); } Catch(Exception e) {e.printstacktrace (); }finally{pool.returnresourceobject (Jedis); } return NULL; }}
4. Redis Monitoring
To view Redis memory usage:
Use REDIS-CLI to connect to Redis and use info memory to see the footprint
$./src/redis-CLI127.0.0.1:6379>Infomemory# memoryused_memory:37583536Used_memory_human:35.84MUsed_memory_rss:40407040Used_memory_peak:38312160Used_memory_peak_human:36.54MUsed_memory_lua:35840Mem_fragmentation_ratio:1.08Mem_allocator:jemalloc-3.6.0
Used_memory:redis the memory used by the current data, which may include swap virtual memory.
Used_memory_rss:redis the physical memory currently occupied by the process that is viewed by the top command is memory-intensive.
mem_fragmentation_ratio:used_memory_rss/used_memory ratio, this value is more important when
Mem_fragmentation_ratio<1, it shows that Redis is already using swap and the performance of the operation can be greatly affected. Typically, this value is larger than 1.
How to avoid mem_fragmentation_ratio<1? You need to properly configure three parameters in the redis.conf file: MaxMemory, Maxmemory-policy, Maxmemory-samples.
MaxMemory: Default is 0, in which case Redis uses physical memory whenever possible and uses virtual memory whenever possible. When using swap, performance can drop sharply and redis may be down in the extreme.
This is the primary cause of the Redis service outage in the running environment. Therefore, it is important to set a reasonable value for this parameter in the production environment. When memory usage reaches the threshold, Redis frees up memory using the purge policy.
Maxmemory-policy: Defines the purge policy.
5. Redis Master-slave replication
Redis replication supports master-slave replication, where a master can hang multiple slave libraries to support read and write separations. Cascade replication from the library can be hung from the library at the same time.
Redis Development Summary