1. What is Redis?

Redis is a high performance key-value database based on memory.

2. The characteristics of the Reids

Redis is essentially a key-value in-memory database, much like memcached. The entire database is loaded into memory for operation, and periodically flushed to hard disk asynchronously.

Because it is a pure memory operation, Redis has excellent performance and can process more than 100,000 read and write operations per second, making it the fastest key-value DB known.

It’s not just performance that makes Redis great. Redis’s greatest appeal is the ability to store multiple data structures, with a maximum value limit of 1GB, unlike memcached, which can store 1MB of data, so Redis can be used to implement many useful functions.

For example, using his List to do FIFO bidirectional linked List, to achieve a lightweight high performance message queue service, with his Set can do high-performance tag system and so on. Redis can also set expire times for stored key-values, so it can be used as a more powerful version of memcached.

The main disadvantage of Redis is that the database capacity is limited by physical memory, so it cannot be used for high-performance read and write of massive data. Therefore, Redis is mainly suitable for high-performance operations and operations with small data volume.

3. What are the benefits of redis?

  1. Fast, because the data is in memory, similar to a HashMap, which has the advantage of having O(1) time for both lookup and operation.

  2. Support rich data types, support string, list, set, sorted set, hash

  3. Transactions are supported, and operations are atomic, meaning that changes to data are either all or none

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

4. What advantages does Redis have over memcached?

  1. As an alternative to memcached, where all values are simple strings, Redis supports richer data types

  2. Redis is much faster than memcached

  3. Redis can persist its data

5. What are the differences between Memcache and Redis?

  1. Storage mode Memecache Stores all data in the memory. After a power failure, the data will hang. The data cannot exceed the memory size. Redis is partially stored on hard disk to ensure data persistence.

  2. Data support types Memcache support for data types is relatively simple. Redis has complex data types.

  3. Different underlying models are used and the underlying implementation methods and application protocols used to communicate with clients are different. Redis directly built the VM mechanism itself, because normal system calls to system functions would waste a certain amount of time moving and requesting.

  

6. Redis Common performance Issues and solutions:

  1. When the Master writes a memory snapshot, the save command is used to schedule the rdbSave function, which blocks the main thread. When a large number of snapshots are taken, the performance of the main thread is greatly affected and the service is suspended intermittently. Therefore, the Master is advised not to write a memory snapshot.

  2. Master AOF persistence. If AOF files are not rewritten, the impact of this method on performance is minimal. However, the size of AOF files keeps increasing. The Master is advised not to do any persistent work, including memory snapshot and AOF log files. In particular, do not enable memory snapshot for persistence. If data is critical, a Slave enables AOF backup and synchronizes data once per second.

  3. The Master invokes BGREWRITEAOF to rewrite the AOF file. The AOF occupies a large amount of CPU and memory resources during the AOF rewrite. As a result, the load of services is too high and the service is temporarily suspended.

  4. Redis has a performance problem with master-slave replication. For the speed of master-slave replication and the stability of connection, it is better for the Slave and Master to reside in the same LAN

7. There are 2000W data in mySQL, but only 20W data in REDis. How to ensure that the data in REDis is hot data

When redis memory data sets grow to a certain size, a data obsolescence policy is implemented.

Redis offers six data elimination strategies:

  1. Volatile – lRU: Selects the least recently used expires data from a set with an expiration date (server.db[I].expires)

  2. Volatile – TTL: Selects expired data from a set (server.db[I].expires) to be discarded

  3. Volatile -random: Selects any data from a set with an expiration date (server.db[I].expires) to be discarded

  4. Allkeys-lru: Culls the least recently used data from the dataset (server.db[I].dict)

  5. Allkeys-random: Random selection of data from a dataset (server.db[I].dict)

  6. No-enviction: Data expulsion is prohibited

  

8. Please use Redis and any language to implement a malicious login protection code, limit each user Id within 1 hour can only log in 5 times. The specific login function or function can be empty function, do not write in detail.

Use the list to achieve: each element in the list represents the login time, as long as the difference between the last fifth login time and the present time is not more than 1 hour, the login is prohibited. The code in Python looks like this:

#! /usr/bin/env python3Import redis import sys import time r = redis.StrictRedis(host= '127.0.0.1', port=6379, db=0) try: id = sys.argv[1]except: Print (' input argument error ') sys.exit(0) if r.len (id) >= 5 and time.time() -- float(r.index (id, 4)) <= 3600: print(' input argument error ') sys.exit(0) if r.len (id) >= 5 and time.time() -- float(r.index (id, 4)) <= 3600: Print (" you are forbidden logining ")else: print(' you are allowed to login ') R.l push(ID, time.time()) # login_func()Copy the code

9. Why does Redis need to put all data in memory?

Redis reads data to memory for the fastest read/write speed and writes data to disk asynchronously. So Redis is characterized by fast and persistent data. If data is not kept in memory, disk I/O speed severely affects Redis performance. As memory gets cheaper and cheaper, Redis will become more and more popular.

If the maximum memory usage is set, new values cannot be inserted after the number of existing records reaches the memory limit.

10.Redis is single-process, single-thread

Redis uses queue technology to turn concurrent access into serial access, eliminating the overhead of traditional database serial control

11. How to solve the concurrency competition problem of Redis?

Redis is a single-process single-thread mode, which uses queue mode to change concurrent access into serial access. Redis itself does not have the concept of locking, Redis does not compete for multiple client connections, but when Jedis clients concurrently access Redis, connection timeout, data conversion error, blocking, client closing and other problems will occur, all of which are caused by client connection disorder.

There are two solutions to this:

1. From the client side, in order to ensure the normal and orderly communication between each client and Redis, the connection is pooled, and the client reads and writes Redis using internal synchronized.

2. Server Angle, using setNX to achieve lock.

Note: For the first method, which requires the application to handle the resource synchronization itself, you can use a simple method, such as synchronized or lock. The second requires Redis’ setnx command, but there are a few caveats.

12. CAS(check-and-set operation for optimistic locking)?

Like many other databases, Redis as a NoSQL database also provides transaction mechanism. In Redis, the four commands MULTI/EXEC/DISCARD/WATCH are the cornerstones of our transaction implementation.

Although this concept is familiar to developers with relational database development experience, we will briefly outline the implementation characteristics of transactions in Redis:

  1. All commands in a transaction will be executed sequentially and Redis will not service any requests from other clients during the transaction, thus ensuring that all commands in the transaction will be executed atomically.

  2. In contrast to transactions in a relational database, if a command fails in a Redis transaction, subsequent commands are still executed.

  3. We can start a TRANSACTION with the MULTI command, which can be understood by those experienced in relational database development as a “BEGIN TRANSACTION” statement. Commands executed after this statement are considered operations within the transaction, and we can commit/roll back all operations within the transaction by executing EXEC/DISCARD commands. These two Redis commands can be considered equivalent to the COMMIT/ROLLBACK statements in a relational database.

  4. If communication between the client and the server fails and the network is disconnected before the transaction starts, all statements pending execution will not be executed by the server. However, if the network interruption event occurs after the EXEC command is executed by the client, all commands in the transaction will be executed by the server.

  5. When the appends-only mode is used, Redis calls the system function write to write all the write operations in the transaction to the disk in this call. However, if a system crash occurs during the writing process, such as a power outage, then only part of the data may be written to disk and some of the data may be lost.

The Redis server performs a series of necessary conformance checks upon restart and exits with an error message as soon as a similar problem is found.

In this case, we need to take full advantage of the Redis toolkit provided with the Redis-check-aof tool, which can help us locate data inconsistency errors and roll back some of the data that has been written. After the repair we can restart the Redis server again.

13.WATCH command and Cas-based optimistic locking:

In Redis transactions, the WATCH command can be used to provide the check-and-set (CAS) function. Suppose we monitor multiple Keys through the WATCH command prior to the transaction execution. If any Key value changes after the WATCH, the EXEC command will abandon the transaction and return a Null multi-bulk reply to notify the caller of the transaction

The execution failed. Procedure For example, let’s again assume that Incr is not provided in Redis to do atomic increment of key values. We would have to write our own code to do this. Its pseudo-code is as follows:

val = GET mykeyval = val + 1SET mykey $valCopy the code

The above code is only guaranteed to be correct in the case of a single connection, because if more than one client is executing the code at the same time, an error scenario often seen in multithreaded programs is called race condition.

For example, if both clients A and B read the original value of mykey at the same time, assuming the value is 10, and then both clients increment the value by one and set it back to the Redis server, the result of mykey would be 11, not 12. To solve similar problems, we need the help of the WATCH command, as shown in the following code:

WATCH mykeyval = GET mykeyval = val + 1MULTISET mykey $valEXECCopy the code

Different from the previous code, the new code monitors the key through WATCH command before obtaining the value of mykey, and then surrounds the set command in the transaction, so that each connection can be effectively guaranteed before executing EXEC. If the value of mykey obtained by the current connection is modified by other connected clients, The EXEC command for the current connection will fail. This lets the caller know if val was successfully reset after judging the return value.

14. Several ways to persist redis

1. Snapshots.

By default, Redis stores data snapshots in a binary file on disk called dump.rdb. You can configure Redis persistence policies, such as writing data to disk if there are more than M updates in the data set every N seconds. Or you can manually invoke the command SAVE or BGSAVE.

The working principle of

  • Redis forks.

  • The child process starts writing data to a temporary RDB file.

  • When the child process finishes writing to the RDB file, it replaces the old file with the new one.

  • This allows Redis to use copy-on-write technology.

2, AOF

Snapshot mode is not very robust, and when the system stops or Redis is accidentally killed, the last data written to Redis is lost.

This may not be a big deal for some applications, but for applications that require high reliability, Redis is not a good choice. Append-only file mode is another option. You can turn on AOF mode in the configuration file

3. Virtual memory mode

Using a VM works better when your key is small and your value is large. Because it saves a lot of memory.

When your key is small, you can use some unusual methods to turn large keys into large values. For example, you can consider combining key and value into a new value.

  

The vM-max-threads parameter is used to set the number of threads that can access the swap file. The value should not exceed the number of cores on the machine. If set to 0, all operations on the swap file will be serial. There may be long delays, but data integrity is well guaranteed.

In my own tests, VIRTUAL memory also performed well. If you have a large amount of data, consider a distributed or other database.

15. Redis cache invalidation policy and primary key invalidation mechanism

As a caching system to periodically clean up invalid data, a primary key invalidation and elimination strategy is required.

  

In Redis, live keys are called volatile. When the cache is created, a lifetime is set for a given key, and when the key expires (lifetime 0), it may be removed.

  

1. Some operations that affect the survival time

The lifetime can be removed by using the DEL command to delete the entire key, or by overwriting the original data with the SET and GETSET commands. That is, the lifetime of the current data is different after changing the value corresponding to the key and overwriting the same key and value.

  

For example, executing INCR on a key, LPUSH on a list, or HSET on a hash table does not change the lifetime of the key itself. On the other hand, if RENAME is used to RENAME a key, the renamed key has the same lifetime as before.

  

Another possibility of the RENAME command is to try to RENAME a key with a lifetime to another key with a lifetime, another_key. In this case, the old another_key (and its lifetime) is deleted, and the old key is renamed another_key, so, The new another_key also has the same lifetime as the original key. Run the PERSIST command to remove the lifetime of a key without deleting the key, so that the key becomes a persistent key again.

  

2. How to update the survival time

You can run the EXPIRE command on a key that already has a lifetime, and the new lifetime will replace the old one. The accuracy of the expiration time is controlled within 1ms. The time complexity of primary key expiration is O (1). The EXPIRE and TTL commands are used together. Returns 1 on success; If the key does not exist or cannot be set, 0 is returned.

  

Maximum cache configuration:

Maxmemory defaults to 0 and does not specify a maximum cache. If new data is added, exceeding the maximum memory will crash Redis, so be sure to set this. When redis memory data sets grow to a certain size, a data obsolescence strategy is implemented.

Redis offers six data elimination strategies:

  1. Volatile – lRU: Selects the least recently used expires data from a set with an expiration date (server.db[I].expires)

  2. Volatile – TTL: Selects expired data from a set (server.db[I].expires) to be discarded

  3. Volatile -random: Selects any data from a set with an expiration date (server.db[I].expires) to be discarded

  4. Allkeys-lru: Culls the least recently used data from the dataset (server.db[I].dict)

  5. Allkeys-random: Random selection of data from a dataset (server.db[I].dict)

  6. No-enviction: Data expulsion is prohibited

  

Note the six mechanisms here. Volatile and Allkeys specify whether to eliminate data from a set with an expiration date or from the entire set. Lru, TTL, and RANDOM are three different elimination strategies, plus a no-enviction policy that never retrievals data.

  

Using policy rules:

  1. Allkeys-lru is used if the data has a power-law distribution, that is, some data is accessed with high frequency and some data is accessed with low frequency

  2. Allkeys-random is used if the data is equally distributed, that is, all data are accessed at the same frequency

  

Three data elimination strategies:

TTL and RANDOM are easier to understand and implement. The main reason is that Lru has used the elimination strategy least recently. The design will sort the keys according to the failure time, and then select the key that fails first for elimination

16. Redis is the most suitable scenario

Redis is most suitable for all in-momory scenarios. Although Redis also provides persistence, it is more of a disk-backed replication, so you might be surprised if Redis is more like Memcached. So when to use Memcached and when to use Redis?

If you simply compare Redis with Memcached, most of you will get the following:

  1. Redis not only supports simple K/V type data, but also provides the storage of list, set, zset, hash and other data structures.

  2. Redis supports data backup, namely, data backup in master-slave mode.

  3. Redis supports persistent data, which can keep data in memory on disk and be reloaded for use upon restart.

Session Cache

One of the most common scenarios for using Redis is session cache. The advantage of caching sessions with Redis over other stores, such as Memcached, is that Redis provides persistence. When maintaining a cache that is not strictly consistent, most people would be unhappy if all of their shopping cart information was lost. Now, would they be?

  

Fortunately, as Redis has improved over the years, it’s easy to figure out how to use Redis appropriately to cache documents for sessions. Even Magento, a popular commercial platform, offers plug-ins for Redis.

2. Full page Cache (FPC)

In addition to the basic session token, Redis also provides a very simple FPC platform. Returning to the consistency issue, even if the Redis instance is restarted, users will not see a drop in page loading speed due to disk persistence, which is a huge improvement, similar to PHP native FPC.

Again, Magento provides a plug-in to use Redis as a full-page caching back end.

Also, for WordPress users, Pantheon has a great plugin called WP-Redis that will help you load pages you’ve viewed as quickly as possible.

3, the queue

One advantage of Reids in the memory storage engine world is that it provides list and set operations, which makes Redis a good message queue platform to use. The operation Redis uses as a queue is similar to the push/pop operation of a list in a native programming language such as Python.

If you do a quick Google search for “Redis Queues”, you will immediately find a number of open source projects designed to use Redis to build great back-end tools for queues. For example, Celery has a background that uses Redis as a broker, which you can view from here.

Leaderboards/counters

Redis does a great job of incrementing or decrementing numbers in memory. Set and Sorted Set also make it very easy to perform these operations. Redis just provides these two data structures.

So, to get the top 10 users from the sorted set — we call it “user_scores” — we just need to do the following:

Of course, this assumes that you are doing an ascending order based on your users’ scores. If you want to return users and their scores, you need to do this:

ZRANGE user_scores 0 10 WITHSCORESCopy the code

A good example is Agora Games, implemented in Ruby, whose leaderboards are stored in Redis, as you can see here.

Publish/subscribe

Last (but certainly not least) is Redis’s publish/subscribe capabilities. There are a lot of publish/subscribe usage scenarios. I’ve seen people use it for social networking connections, as publish/subscribe based script triggers, and even Redis’s publish/subscribe feature for chat systems! (No, it’s true, you can check it out).

  

Of all the features that Redis offers, I feel this one is the least popular, even though it does offer this functionality to users.

17. When will Redis cluster solution make the whole cluster unusable?

In A cluster with three nodes A, B, and C, if node B fails without the replication model, the whole cluster will be unavailable as it lacks slots in the range 5501-11000.

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

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

19. What does Redis have to do with Redisson?

Redisson is an advanced distributed coordination Redis customer service, Can help users in the distributed environment to easily implement some Java objects (Bloom filter, BitSet, Set, SetMultimap, ScoredSortedSet, SortedSet, Map, ConcurrentMap, List, ListMultimap, Queue, BlockingQueue, Deque, BlockingDeque, Semaphore, Lock, ReadWriteLock, AtomicLong, CountDownLatch, Publish/Subscribe, HyperLogLog).

20. What are the advantages and disadvantages of Jedis versus Redisson?

Jedis is the client of Java implementation of Redis. Its API provides comprehensive support for Redis commands.

Redisson implements distributed and extensible Java data structures. Compared with Jedis, Redisson has relatively simple functions. It does not support string manipulation, sorting, transaction, pipeline, partitioning and other Redis features. The goal of Redisson is to promote a separation of focus from Redisso that users can focus more on processing business logic.

21. How to set and verify the password in Redis?

To set the password: config set requirepass 123456

Authorization password: auth 123456

22, What about the Redis hash slot?

The Redis cluster does not use consistent hash, but introduces the hash slot concept. The Redis cluster has 16384 hash slots. After each key is verified by CRC16, the model of 16384 is taken to determine which slot to place.

23. What is the master/slave replication model of Redis cluster?

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 Redis cluster write operations be lost? Why is that?

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

25. How are Redis clusters replicated?

Asynchronous replication

26. What is the maximum number of nodes in Redis cluster?

16384.

27. How to select database for Redis cluster?

The Redis cluster cannot make database selection at present, default is 0 database.

How do I test the connectivity of Redis?

ping

29. What is the use of pipes in Redis?

A one-time request/response server can be implemented to process new requests even if the old one has not yet been responded to. This allows you to send multiple commands to the server without waiting for a reply, which is finally read in one step.

This is pipelining, a technology that has been widely used for decades. For example, many POP3 protocols have been implemented to support this feature, greatly speeding up the process of downloading new messages from the server.

How to understand Redis transaction?

A transaction is a single isolated operation: all commands in the transaction are serialized and executed sequentially. The transaction will not be interrupted by command requests from other clients during execution.

A transaction is an atomic operation: all or none of the commands in a transaction are executed.

What are the Redis transaction related commands?

MULTI, EXEC, DISCARD, and WATCH

32. How to set the expiration time and permanent validity of Redis key respectively?

EXPIRE and PERSIST commands.

33, How does Redis do memory optimization?

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.

34, How does 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.

A new command is executed, etc.

So we keep crossing the boundary of the memory limit, by constantly reaching the boundary and then constantly reclaiming back below the boundary.

If the result of a command is that a large amount of memory is used (for example, the intersection of a large set is saved to a new key), it does not take long for the memory limit to be exceeded by this memory usage.

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

1. The codis.

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

2. Redis cluster3.0 comes with its own cluster, which is characterized by the concept of hash slot rather than consistent hash algorithm and its own support for setting slave nodes. See the official documentation for details.

3. Implement in the business code layer, set up several unrelated Redis instances, hash the key in the code layer, and then operate the data in the corresponding Redis instances. This method has high requirements on hash layer code, including alternative algorithm scheme after node failure, automatic script recovery after data flapping, instance monitoring and so on.

Recommended reading

Carefully arrange | public number article directory

Performance optimization | Tomcat service optimization

Highly recommended | The must-know performance optimization insider

Enumerate the weirdest reasons programmers quit their jobs

How can you learn MySQL database quickly

Big and full | Introduction to Logstash technology

Docker container technology Docker file

, end,

— Writing is not easy, your forwarding is the biggest support for me —

Let’s have fun together

At present, more than 40,000 people are interested in joining us

             

             

Click on the menu “wechat group” to join the group and communicate with your partners!