In all seriousness

Interviewer: Young man, what do you think of Redis? Me: Ah, view ah, sit to see or lie down to see. Redis is small? Soon? But long lasting?

Interviewer: In all seriousness, I suspect you are driving a car, not only driving a car but also making color. Me:…

Interviewer: Go on, I don’t have much time, so don’t talk nonsense. Back to the subject, how much do you know about Redis? Me: Light weight, small size, very fast memory based, RDB and AOF persistence make it just as strong and persistent.

Interviewer: Tell me something specific. Me: Look at the text.

The body of the

Redis is an open source, high-performance, key-value pair-based cache and storage system. It provides multiple key-value data types to meet the cache and storage requirements in different scenarios. At the same time, Redis has many high-level functions that enable it to perform different roles such as message queue and task queue. In addition, Redis also supports external module extensions that can be used as the primary database in certain scenarios.

Because the memory read and write speed is much faster than the hard disk, even now the solid-state disk thinking is estimated to be toward memory that mode of thinking, perhaps I am a layman, but long-term storage or the use of mechanical disk. So all the data in the Redis database is stored in memory and that’s pretty fast. There is also a risk that data will be lost, but with RDB and AOF persistence the risk is reduced.

First acquaintance with Redis

1. Redhat7 series

1.1, install,

Prepared here is the source package, the version is not the latest, is stable application.

Other versions can be obtained from the official website or github, the hosted platform of Redis. The download address is as follows.

redis.io/download

Redis - 6.0.8. Tar. Gz# installationThe tar - ZXVF redis - 6.0.8. Tar. Gz# compiler
make && make install
Copy the code

1.2. Check for errors

Make [1]: *** [server.o] error 1Copy the code

1.3. Solutions

1.3.1 Installation depends on the environment

yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
Copy the code

1.3.2 Add environment variables and take effect

scl enable devtoolset-9 bash
echo "/opt/rh/devtoolset-9/enable" >> /etc/profile 
Copy the code

Re-read the environment variable configuration file

source /etc/profile
Copy the code

Recompile solves the problem

/usr/local/ = /usr/local/
cd/ opt/redis - 6.0.8 /# compiler
make && make install
Copy the code

You can refer to the rookie tutorial to practice common basic commands

www.runoob.com/redis/redis…

1.4. Startup and Login

Start the redis-server server

Start the Redis serviceNohup/opt/redis - 6.0.8 / SRC/redis - server &Copy the code

Log in to the Redis-CLI client

# login redis - cli/ opt/redis - 6.0.8 / SRC/redis - cliCopy the code

At this time, Redis under Linux officially started successfully. The basic usage will be introduced below.

ping
pong
Copy the code

1.5. Set a password

By default, the password is not open. You need to manually enable the commented out parameter configuration.

Edit the configuration fileVim/opt/redis - 6.0.8 / redis. Conf# Comment out the original password, copy a line and change it to the password you set
#requirepass foobared
requirepass 123456
Copy the code

2. Install in Windows

2.1, install,

Redis - x64-3.2.100. ZipCopy the code

2.1.1. Decompress in Windows or install mSI directly.

2.1.2. Set the service command (registered as a service, self-starting) to install the service

redis-server --service-install redis.windows-service.conf --loglevel verbose
Copy the code

Unloading service

redis-server --service-uninstall
Copy the code

2.2 start and close

redis-server redis.windows.conf
Copy the code

2.2.1. Enable the service

redis-server --service-start
Copy the code

2.2.2 Stop the service

redis-server --service-stop
Copy the code

2.3. Start redis service

Run CMD as administrator in the directory where redis was unzipped or installed
redis-server --service-start
Copy the code

2.4 run the test login under CMD

Run CMD as an administrator in the unzipped or installed directory of RedisRedis -cli.exe -h 127.0.0.1 -p 6379Or just execute it
redis-cli
# to perform
redis-cli
# login test
ping
Copy the code

5, RDM under Windows management tools, it is the visual interface redisdesktop.com/download

Two, basic knowledge

1. Often asked in interviews

Interviewer: What are the data types in Redis? Can you talk about them?

I: String, hash, list, set, zset, stream Stream is new in Redis5.0.

Interviewer: Wow, the guy has a few things, he knows a lot, even the type of stream.

Me: a face meng force…

Third, the advanced

1. Persistence

Interviewer: Do you know some advanced features of Redis?

Me: A little.

Interviewer: Can you tell me more about it?

Me: Fast in the brain searcher before the book summary. Caching, persistence.

Redis is used as the cache server, but after the cache is penetrated, the performance will be greatly affected. All the caches fail at the same time, and the cache avalanche makes the service unresponsive.

We want Redis to synchronize data from memory to disk in some form so that it can recover data from the records on disk after a restart. This process is called persistence.

Interviewer: Do you know the common persistence methods of Redis?

Redis enables RDB persistence by default. AOF persistence needs to be manually enabled.

Redis supports two types of persistence. One is RDB, the other is AOF. The former “periodically” stores the data in memory to the hard disk according to the specified rules, while the latter records the command book after each command is executed. You can use either of these persistence methods separately, but in most cases the two are tightly combined.

At this time, the interviewer looked at me with a look of expectation on his face. Please continue.

2. RDB mode

To continue, the RDB adopts the snapshot mode. The default setting is automatic synchronization. The default Settings are as follows.

Manual synchronization is also possible

Not recommended in production environments
SAVE
Copy the code
# asynchronous form
BGSAVE
Copy the code
# Based on custom snapshots
FLASHALL
Copy the code

3. AOF method

When Redis is used to store non-temporary data, AOF persistence is typically turned on to reduce data loss due to process termination. AOF can append every command Redis executes to a hard disk file, which obviously degrades Redis performance, but in most cases it is acceptable. It is important to note that AOF performance can be improved by using a fast read/write hard disk.

It is not enabled by default, you need to manually enable AOF. When you view the redis.conf file, you will find that appendOnly is set to no

appendonly yes
Copy the code

After AOF persistence is enabled, executing one command at a time changes the directory of data in Redis, and Redis writes the command to an AOF file on disk. The directory where the AOF file is saved is the same as that of the RDB file. The default filename is appendone. AOF, which can be changed using appendfilename.

appendfilename "appendonly.aof
Copy the code

In fact, Redis does just that. Redis automatically overwrites the AOF file whenever a certain condition is reached, which can be set in the redis.conf configuration file:

auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
Copy the code

At startup, Redis will execute the commands in the AOF file line by line to load the data from the hard disk into memory, which is slower than RDB.

Although AOF records the command in an AOF file every time an operation is performed to change the contents of the database. But in fact, due to the operating system cache mechanism, the data is not actually written to the disk, but into the operating system disk cache. By default, the operating system performs synchronization every 30 seconds to write the contents of the disk cache to the disk.

In Redis you can set the timing of synchronization with appendfsync:

# appendfsync always
The default is everysec
appendfsync everysec
# appendfsync no
Copy the code

Redis allows both AOF and RDB to be enabled. In this way, data security is ensured and operations such as backup are friendly. After restarting Redis, the AOF file will be used to restore the data. Because of AOF persistence, the probability that data will be lost is minimized.

4. Redis copy

With persistence, Redis guarantees that data will not be lost (or lost) even if the server restarts. However, the database is stored on a single server, so it is hard to avoid all kinds of emergencies, such as hard disk failure, server downtime, etc., which will also lead to data loss.

To avoid failures as much as possible, it is common practice to make multiple copies of the database to be deployed on different servers. So that even if one fails, the other servers can still provide service. For this purpose, Redis provides replication. That is, after the data in one database is updated, the updated data is automatically synchronized to other databases.

If you are familiar with MySQL, it is similar to the primary/secondary replication of MySQL.

Redis is much easier to use than MySQL. Just add slaveof slave database address when starting from slave library.

Configure in the slave library
slaveof master_database_ip_addr
# test, add nohup & is put in the background, and output the log to the /root/ directory nohup.outNohup /opt/redis-6.0.8/ SRC /redis-server --6380 --slaveof 192.168.245.147 6379&Copy the code

Principle of 4.1,

Replication initialization. The main principle here is to boot from the library and send SYNC to the master library. At the same time, after receiving the SYNC command, the master library will start to save the snapshot in the background, which is the RDB persistence process, and will cache the command received during the snapshot. When the snapshot is complete, Redis sends the snapshot file and all cached commands to the slave database. Once received from the database, the snapshot file is loaded and the cache command received is executed.

The replication synchronization phase lasts throughout the master/slave synchronization process until the master/slave relationship terminates. Snapshots play a crucial role in the replication process. Snapshots are taken whenever replication is performed, even if RDB-style persistence is turned off, by removing all save parameters.

4.2 optimistic Replication

Redis adopts a replication strategy of Optimistic replication. It is tolerated that the contents of the master and slave databases are different at some point in time, but eventually their data will be synchronized. Specifically, in the process of master-slave replication data between database Redis itself is asynchronous, this means that the primary database after the client request will immediately command in the main database of execution result feedback to the client, and asynchronous data synchronization to from the library, will not wait for receives the command from the database in the returned to the client.

Data is writable when synchronized to at least a specified number of slave libraries, as specified by the argument:

# Set a minimum of 3
min-slaves-to-write 3
Set the maximum disconnection time for slave data
min-slaves-max-lag 10
Copy the code

4.3 Incremental Replication

Based on the following three aspects

  • The slave library stores the run ID of the master library. Each Redis run instance will have a unique run ID, and a new run ID will be automatically generated whenever the instance restarts. Similar to the unique ID of the secondary node configured in MySQL.
  • During the replication synchronization phase, when a command is transferred from the master to the slave, the command is stored in a backlog that records the offsets of the commands currently stored in the backlog.
  • When a command is received from the master library, the offset of the command is recorded.

4.4, pay attention to

The situation is slightly more complicated when the primary database crashes. To manually restore primary database data from a secondary database, follow the following principles:

  • Use from the databaseSLAVEOF NO ONEThe command will be promoted from the master library to continue service.
  • Start the main library that crashed before, and then useSLAVEOFCommand to set it as the slave of the new master library.

Note: When replication is enabled and persistence is turned off for the database, do not use Supervisor or similar process management tools to crash the main library and restart it. Also avoid direct restarts when the server where the master library resides is down due to a failure. When the main database is restarted, persistence is not enabled and all data in the database is cleared. The slave library will still receive data from the master library, which will cause all slave libraries to be emptied, resulting in a lonely database persistence.

Manual maintenance can be a hassle, but Redis provides an automated solution: Sentry to implement this process and avoid the error prone problem of manual maintenance.

5. Sentinel

From the copy history of Redis, we know that in a typical Redis system with one master and many slaves, slave library plays a role of redundancy and backup as well as read and write separation in the whole system. When the master library encounters an abnormal interruption of service, the developer manually deactivates the master to enable the system to continue service. The process is relatively complex and difficult to automate. Use the sentry tool.

The role of sentinels

  • Monitor the operation of Redis system
  • Monitor master and slave libraries for normal operation
  • The main library GG Smida, automatically from the library to the main library, happy

Of course, there are multiple sentinels monitoring the master/slave database schema, and sentinels monitoring each other, as shown below:

First, you need to build a master and many slave model, and then turn on the configuration sentry.

# main librarySentinel Monitor Master 127.0.0.1 6379 1Create a configuration file, such as sentinel.conf
redis-sentinel /opt/path/to/sentinel.conf
Copy the code

So much for the sentry, now it’s in my head. At least you know there’s something you can do with a pretty interviewer.

6. Cluster

The clustering feature has been added since Redis3.0.

Even with sentry, each database in the Redis cluster still holds all the data in the cluster, resulting in the total storage capacity of the cluster being limited to the database node with the smallest available memory, resulting in the barrel effect. Because Redis is memory based for all data, problems are already evident, especially when Redis is used as a persistent storage service.

Here’s a scenario. In terms of capacity expansion, after client sharding, if you want to add more nodes, you need to manually migrate the database. In the process of migration, in order to ensure the consistency of data, it is necessary to take the group offline temporarily, which is relatively complicated.

Considering the small size of Redis, it is a lightweight feature. Presharding can be used to avoid problems to a certain extent. In other words, at the beginning of the deployment, consider the storage scale ahead of time and create enough instances.

From the above theoretical knowledge, sentinels are similar to clusters, but sentinels and clusters are two separate functions. If you want to scale horizontally, clusters are a good choice.

To configure the cluster, enable cluster-enabled in the redis.conf configuration file

cluster-enabled yes
Copy the code

Configure a different working directory for each node in the cluster, or modify the persistence file

cluster-config-file nodes-6379.conf
Copy the code

Cluster testing you can perform configuration, refer to other books can also, implementation is not difficult. As long as you know how it works.

Redis for Java

The sample

package com.jedis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class Test {

	@org.junit.Test
	public void demo(a) {
		Jedis jedis = new Jedis("127.0.0.1".6379);
		jedis.set("name"."sky");
		String params = jedis.get("jedis");
		System.out.println(params);
		jedis.close();
	}
	
	@org.junit.Test
	public void config(a) {
		// Get the connection pool configuration object
		JedisPoolConfig config = new JedisPoolConfig();
		// Set the maximum number of connections
		config.setMaxTotal(30);
		// Set the maximum number of idle connections
		config.setMaxIdle(10);
		// Get the connection pool
		JedisPool pool = new JedisPool(config, "127.0.0.1".6379);
		// Get the core object
		Jedis jedis = null;
		try {
			// Get the connection from the connection pool
			jedis = pool.getResource();
			// Set the object
			jedis.set("poolname"."pool");
			// Get the object
			String pools = jedis.get("poolname");
			System.out.println("values:"+pools);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			// Release resources
			if(jedis ! =null){
				jedis.close();
			}
			if(pool ! =null){ pool.close(); }}}}Copy the code

At the end, I put up a very crude mind map, and on a whim, I wrote so much.

By Longteng Wanli Sky original is not easy, white piao addiction