How to Set Up/configure the redis server and use the Jedis Client

Source: Internet
Author: User
Tags allkeys benchmark syslog redis server
Setup and installation of redis Server

Run the following command on the command line:

$ wget http://download.redis.io/releases/redis-2.8.13.tar.gz$ tar xzf redis-2.8.13.tar.gz$ cd redis-2.8.13$ make

After compilation, six files are generated:
redis-server: This is the redis server.
redis-cli: This is the redis client.
redis-check-aof: This is a tool for checking aof files.
redis-check-dump: This is a local data check tool.
redis-benchmark: Performance Benchmark Testing Tool. After installation, you can test the current redis performance.
redis-sentinel: Redis monitoring tool and cluster management tool

Configuration File

The redis configuration file is:redis.conf
Common configuration items:
daemonize: Whether to run a later process. The default value is no.
pidfile /var/run/redis.pid: PID file path
port 6379: Listening port
bind 127.0.0.1: Bind host IP Address
unixsocket /tmp/redis.sock: Sock file path
timeout 300: Timeout time. The default value is 300 s.
loglevel verbose: Log level. The options include debug: a large amount of information, useful for development and testing; verbose: A lot of extremely useful information, but not as messy as debug; notice: the information you want to use in the production environment; warning: the most critical and important information is printed. The default value is erbose.
logfile stdout: Logging method. The default value is stdout.
syslog-enabled no: Logs are recorded in system logs. The default value is no.
syslog-ident redis: System log ID
syslog-facility local0: Specify the System Log device, which must be user or local0 ~ Local7. The default value is local0.
databases 16: Number of databases. The default database is db 0. You can use select to select different databases. The dbid range is 0 ~ (The value you set is-1)

save <seconds> <changes>: How long does RDB synchronize data to data files when there are multiple update operations.
save 900 1: At least one key is changed within 15 minutes.
save 300 10: At least 300 keys are changed in 5 minutes.
save 60 10000: At least 10000 keys are changed within 60 s.
rdbcompression yes: Whether to compress the data stored in the local database. The default value is yes.
dbfilename dump.rdb: Name of the local database file. The default value is dump. RDB.
dir ./: Local database storage path. The default value is ./
slaveof <masterip> <masterport>: When the local machine is a slave service, set the master service IP address and port
masterauth <master-password>: Master service connection password

When the slave node loses connection with the master node or is being copied, the slave node processes client requests:
slave-serve-stale-data yes: Yes: The slave node continues to respond to the client request, but the data may be inaccurate or empty. No: Except info and slaveof, other commands return "sync with Master in progress"

requirepass foobared: Connection password foobared
maxclients 128: Maximum number of connections, unlimited by default
maxmemory <bytes>: Set the maximum memory. When the maximum memory is reached, redis will first try to clear expired or expiring keys. After this method is processed, it will still reach the maximum memory setting, no more write operations will be performed

The following figure shows the maxmemory policy.
maxmemory-policy volatile-lru: Maxmemory setting policy. The default value is volatile-LRU.
Volatile-LRU: uses the LRU algorithm to remove from the expiration set.
Allkeys-LRU: remove key based on LRU Algorithm
Volatile-random: randomly move one from the expiration set
Allkeys-random: Randomly removes one
Volatile-TTL: The Key is removed based on the latest expiration time.
Noeviction: No data is removed. The client returns the error don't expire at all, just return an error on write operations.

maxmemory-samples 3

appendonly no: Whether to record logs after each update operation. If this function is disabled, 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.
appendfilename appendonly.aof: Update the log file name. The default value is appendonly. Aof.
Redis supports three different Synchronization Methods:
No: Don't fsync, just let the OS flush the data when it wants. Faster. // wait for the OS to synchronize data cache to the hard disk
Always: fsync after every write to the append only log. Slow, safest. // call fsync () after each update operation to write data to the disk
Everysec: fsync only if one second passed since the last fsync. Compromise. // synchronize once per second
appendfsync everysec// Update the log condition. The default value is everysec.
no-appendfsync-on-rewrite no

slowlog-log-slower-than 10000: Set the redis slow log time, which only includes the command execution time, excluding the IO operation time, such as the client connection and response time. The Unit is microseconds (1 million s). The default value is 10000. A negative value indicates that slow log is disabled, and 0 indicates that all commands are logged.
slowlog-max-len 1024: The maximum length of slowlog is 1024. This will consume memory, and slowlog reset is used to reclaim slowlog memory.

vm-enabled no// Whether to use virtual memory. The default value is no. Virtual Memory is not recommended in redis2.4.
vm-swap-file /tmp/redis.swap// Virtual memory file path. The default path is/tmp/redis. Swap. You cannot share virtual memory files with multiple redis instances.
vm-max-memory 0// Set the maximum Vm value. The default value is 0. All values exist in the disk.
vm-page-size 32// Set the page size of the VM. The default value is 32.
vm-pages 134217728// Set the maximum memory pages in the swap file. The default value is 134217728. Swap size = VM-page-size * VM-pages
vm-max-threads 4// The maximum I/O thread that the VM runs simultaneously

When a certain number or maximum element exceeds a critical value, a special hash algorithm is used:
hash-max-zipmap-entries 512
hash-max-zipmap-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
activerehashing yes// Whether to reset the hash table

include /path/to/other.conf: Reference other configuration files

Set the Linux kernel memory allocation policy
$ sudo vim /etc/sysctl.conf
vm.overcommit_memory = 1// Specify 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 allocation of all physical memory regardless of the current memory status. 2 indicates that the kernel can allocate more memory than the total physical memory and swap space.
Start

./redis-server redis.conf
If you want to build multiple instances on one server, you can use the following directory results:

Redis | -- place files under the root directory | -- instance-1 instance 1 directory | -- redis. conf instance 1 configuration file | -- instance-2 instance 2 Directory | -- redis. conf instance 2 configuration file | -- instance-3 instance 3 directory | -- redis. configuration file of conf instance 3

You can run the following command at startup:
./redis-server ./instance-1/redis.conf: Start instance 1
./redis-server ./instance-2/redis.conf: Start instance 2
./redis-server ./instance-3/redis.conf: Start instance 3

Benchmark Testing

./redis-benchmark
The benchmark test can be used to test the performance of the current redis server. My test result on my VM is that the number of writes per second is more than 40 thousand, and the number of reads per second is more than 80 thousand.

Set the master-slave structure

Add the following configuration to the configuration file of the redis server as a copy:
slaveof 192.168.66.41 6379
Note:Salveof <master node ip> <master node port>

Obtain the Jedis Client

Jedis GitHub address: https://github.com/xetorthio/jedis
You can obtain the Jedis jar package in the following two ways:

Directly download the jar package

Https://github.com/xetorthio/jedis/releases

Obtain the jar package from the maven Repository
<dependency>        <groupId>redis.clients</groupId>        <artifactId>jedis</artifactId>        <version>2.5.2</version>        <type>jar</type>        <scope>compile</scope>    </dependency>
Easy to use
Jedis jedis = new Jedis("localhost");jedis.set("foo", "bar");String value = jedis.get("foo");

Of course, this method is not good, because every time you use it, you need to create a new connection, and Jedis is NOT thread-safe. It is easy to be surprised when concurrent access occurs. The following method should be used:Use pool.

Jedispool pool = new jedispool (New jedispoolconfig (), "localhost"); Jedis = pool. getresource (); try {// start using Jedis. set ("foo", "bar"); string foobar = Jedis. get ("foo"); Jedis. zadd ("sose", 0, "car"); Jedis. zadd ("sose", 0, "Bike"); set <string> sose = Jedis. zrange ("sose", 0,-1);} finally {// after use, put the connection back into the connection pool if (null! = Jedis) {Jedis. Close () ;}/// when the application exits, close the connection pool: pool. Destroy ();

This pool method can generally meet our requirements, but sometimes when we use multiple redis servers, we need to put different keys on different redis servers, in this case, we can choose different redis servers based on different services. This can solve the problem to a certain extent, but there will be another problem, such as: we are not easy to determine which business product has big data. This will make the data not evenly distributed to multiple redis servers.
In this case, we need to use the sharding technology. The Code is as follows:

// Shard information list <jedisshardinfo> shards = new arraylist <jedisshardinfo> (); jedisshardinfo Si = new jedisshardinfo ("localhost", 6379); SI. setpassword ("foobared"); shards. add (SI); SI = new jedisshardinfo ("localhost", 6380); SI. setpassword ("foobared"); shards. add (SI); // pool object shardedjedispool pool = new shardedjedispool (New config (), shards); // start to use shardedjedis Jedis = pool. getresource (); Jedis. set ("A", "foo ");.... // do your work herepool. returnresource (Jedis );.... // a few moments latershardedjedis jedis2 = pool. getresource (); Jedis. set ("Z", "bar"); pool. returnresource (Jedis); pool. destroy ();
Cluster
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();//Jedis Cluster will attempt to discover cluster nodes automaticallyjedisClusterNodes.add(new HostAndPort("127.0.0.1", 7379));JedisCluster jc = new JedisCluster(jedisClusterNodes);jc.set("foo", "bar");String value = jc.get("foo");

How to Set Up/configure the redis server and use the Jedis Client

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.