What are the main functions of Redis?

1. Sentinel and Replication

Redis server strikes without warning are a nuisance, how to ensure that the backup machine is a complete backup of the original server? This is where sentry and replication are needed.

Sentinel can manage multiple Redis servers, providing monitoring, alerting, and automatic failover, while Replication is responsible for making multiple backup servers available to a single Redis server.

Redis also uses these two features to ensure that Redis is highly available

2. The transaction

In many cases we need to execute more than one command at a time, and both of them need to succeed or fail. Redis’s support for transactions also stems from this requirement, namely the ability to execute multiple commands in sequence at once, while maintaining atomicity.

3. The LUA script

On a transactional basis, lua comes in handy if we need to perform more complex operations (including some logical judgments) on the server at once

4. Persistence

Redis persistence means that Redis writes data from memory to hard disk and loads the data upon redis restart to minimize the impact of cache loss.

5. Cluster

The resources of a single server are always limited. CPU resources and I/O resources can be separated by master/slave replication to transfer some OF the CPU and I/O pressure to the slave server. This is similar to the master/slave synchronization of mysql database.

Before the official distributed scheme of Redis comes out, there are twemProxy and CODIS schemes, both of which rely on proxy for distribution in general.

What data types does Redis support?

Support for multiple types of data structures

1. String: indicates the basic data type. It is a binary safe string with a maximum of 512 MB.

2. List: A list of strings in the order they were added.

3. Set: Unordered collection of strings, with no duplicate elements.

4. Sorted set: a sorted collection of strings.

5. Hash: a collection of key-value pairs.

Is Redis single process single thread?

Redis is single-process single-thread, Redis uses queue technology to change concurrent access into serial access, eliminating the overhead of traditional database serial control.

Why is Redis single threaded?

Multithreading involves locking, and it consumes CPU by switching threads. Since CPU is not the bottleneck of Redis, the bottleneck of Redis is most likely machine memory or network bandwidth. Multi-core CPU performance cannot be achieved with a single thread, but this can be solved by opening multiple Instances of Redis on a single machine.

Models used by other open source software

Nginx: Multi-process single-thread model

Memcached: Single-process multithreaded model

Advantages of using Redis?

1. Fast speed, because the data is stored in memory, similar to HashMap. The advantage of HashMap is that the time complexity of search and operation is O(1).

  • Support rich data types, support string, list, set, sorted set, hash

3. Support transactions, operations are atomic, the so-called atomic is the data changes either all executed or not executed

  • Rich features: can be used for cache, message, by key set expiration time, will be automatically deleted after expiration

Redis single point throughput

Single-point TPS reaches 80,000 / SEC and QPS reaches 100,000 / SEC, to supplement the concepts of TPS and QPS

1.QPS: The maximum number of user visits per second acceptable to the application system

The number of times a request is processed per second. Note that this is the number of times a request is processed successfully. You can understand that there is a counter in the server, for each request processed by 1,1 second counter=QPS.

2.TPS: Maximum number of requests that can be processed per second

The number of transactions processed per second, the number of transactions processed by an application system in 1s, a transaction in distributed processing, may correspond to multiple requests, for measuring the processing capacity of a single interface service, QPS is more reasonable.

What advantages does Redis have over memcached?

1. All memcached values are simple strings. Redis is an alternative to these, supporting richer data types

2.Redis is much faster than memcached

3.Redis can persist its data

4.Redis supports data backup, that is, data backup in master-slave mode.

What data elimination strategies does Redis have?

In Redis, the user is allowed to set the maximum memory size server. Maxmemory, when the Redis memory data set size increases to a certain size, the data obsolescence strategy is implemented.

Volatile – LRU: Selects the least recently used discard from a set that has been set to expire

2. Volatile – TTR: Selects data from a set that has been set to expire to be eliminated

3. Volatile -random: Randomly selects data from expired data sets

4. Allkeys-lru: Select the least recently used data from the dataset for elimination

5. Allkeys-random: Randomly select data from the data set for elimination

6. Noenviction: Data elimination is prohibited

Redis also synchronizes data to AOF when it is weeded out

What should Redis cluster solution do? What are the options?

1.twemproxy

2. Codis, the most widely used cluster scheme, has basically the same effect as Twemproxy, but it supports data of old nodes to be restored to new hash nodes when the number of nodes changes.

3.Redis cluster3.0 comes with its own set, which is characterized by the concept of hash slot instead of consistent hash algorithm and its own support for setting slave nodes.

Redis read-write separation model

By increasing the number of Slave DBS, the read performance increases linearly. To avoid a single point of failure of the Master DB, two Master DBS are deployed in the cluster in dual-system hot backup mode, ensuring high read and write availability in the entire cluster.

The disadvantage of read/Write separation architecture is that each node, whether Master or Slave, must store complete data. In case of a large amount of data, the expansion capability of the cluster is limited by the storage capacity of a single node. Moreover, for write-intensive applications, read/Write separation architecture is not suitable.

Redis data sharding model

In order to solve the defect of read/write separation model, data sharding model can be applied.

Each node can be viewed as an independent master, and then data sharding is implemented by business.

Combining the above two models, each master can be designed as a model consisting of one master and multiple slaves.

What kinds of persistence methods does Redis provide?

RDB persistence allows you to take snapshots of your data at specified intervals

The AOF persistence mode records each write operation to the server. When the server is restarted, these commands will be executed again to restore the original data. The AOF command saves each write operation to the end of the file using the Redis protocol. Redis can also rewrite AOF files in the background so that AOF files are not too large.

If you only want your data to exist as long as the server is running, you can do so without any persistence.

You can also enable both persistence methods. In this case, when Redis restarts, AOF files will be loaded first to recover the original data, since AOF files usually hold more complete data sets than RDB files.

The most important thing to understand is the difference between RDB and AOF persistence, so let’s start with RDB persistence.

How to choose the right persistence method?

  • Redis provides two main persistence mechanisms: **RDB and AOF;

2,RDB

If this function is enabled by default, the system snapshots the data in the memory to the disk at the specified time, creates a dump. RDB file, and restores the data to the memory when Redis starts.

Redis forks () a separate child process, copies the current parent process’s database data into the child process’s memory, and then writes the child process to a temporary file. After the persistence process ends, the child process replaces the last snapshot file with this temporary file. Then the child process exits and the memory is freed.

Note that each snapshot persistence duplicates the database data of the main process, doubling the memory overhead. If the memory is insufficient, the server will be blocked until the memory is released after the replication. Therefore, if there is a large amount of data and write operations are frequent, a large amount of DISK I/O operations will inevitably occur, seriously affecting the performance, and data may be lost after the last persistent operation.

3.AOF

Each write operation is recorded in the form of log (read operation is not recorded), only files can be appended but files can not be rewritten. Redis will start from beginning to end to complete data recovery. FlushDB is also executed.

There are two main ways to trigger: write whenever there is a write operation, and write every second at a time (also losing data).

Because AOF uses the method of append, so the file will be bigger and bigger, to solve this problem, a new rewriting mechanism is added, that is, when the log file is large enough, a new process will be forked to traverse the data in the process memory, each record corresponds to a set statement, write to the temporary file, Then replace it with the old log file (rDB-like operation). The default trigger is triggered when the aOF file size is double the size since the last rewrite and the file is larger than 64M.

When both methods are enabled, Redis preferentially selects AOF for data recovery. In general, just use RDB, which is enabled by default, because RDB makes database backups easier and restores data sets much faster than AOF.

Enabling persistent caching can have some impact on performance, especially when the set memory is full, and even down to several hundred reqs/s. So if you’re just using it for caching, you can turn persistence off.

Redis Common performance issues and solutions?

(1) It is best for the Master not to do any persistent work, such as RDB memory snapshots and AOF log files

(2) If the data is important, a Slave enables AOF backup, and the policy is set to synchronize data once per second

(3) For the speed of Master/Slave replication and connection stability, it is better for Master and Slave to reside in the same LAN

(4) Try to avoid adding slave libraries to the master library under great pressure

Master < -slave1 < -slave2 < -slave3…

In this way, the single point of failure can be easily solved and the Slave can replace the Master. If the Master fails, you can immediately enable Slave1 as Master.

What Java clients are supported by Redis? Which is the official recommendation?

Redisson, Jedis, lettuce, etc. Redisson is officially recommended.

Redis hash slot concept?

The Redis cluster does not use consistent hash, but introduces the hash slot concept. When a key-value needs to be placed in the Redis cluster, the value of CRC16(Key) mod 16384 determines which bucket to place a key in.

What is the maximum number of nodes in Redis cluster?

Redis cluster pre-sorted 16384 buckets (hash slot)

What is the master-slave replication model for Redis clusters?

In order to make the cluster usable even if some nodes fail or most nodes fail to communicate, the cluster uses a master-slave replication model, with n-1 replicas per node.

Will there be write losses in the Redis cluster? Why is that?

Redis does not guarantee strong data consistency, which means that in practice the cluster may lose writes under certain conditions.

How are Redis clusters replicated?

Asynchronous replication

How does Redis optimize memory?

Use hashes whenever possible. Hashes use very little memory, so you should abstract your data model into a hash as much as possible. For example, if you have a user object in your Web system, do not set a separate key for the user’s name, last name, email address, and password. Instead, store all of the user’s information in a hash table.

How does the Redis recycle process work?

A client runs a new command to add new data.

Redi checks the memory usage, and if it exceeds the maxMemory limit, reclaims it according to the preset policy.

What algorithm does Redis recycle use?

LRU algorithm

What scenarios are suitable for Redis?

1) Session sharing (single sign-on)

2) Page caching

3) queue

4) Leaderboards/counters

5) Publish/subscribe

This article is published by OpenWrite!