1. What is Redis?

  • 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. One of Redis’s biggest attractions is the ability to store multiple data structures. In addition, the maximum limit for a single value is 1GB, unlike memcached, which can only store 1MB of data. 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.

PHP architect tutorial directory, as long as you can see the guaranteed salary rise a step (continue to update)

What are the advantages of Redis 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

3. What data types does Redis support?

String, List, Set, Sorted Set, hash

4. What physical resources do Redis mainly consume?

Redis is an in-memory based database with high performance – it relies on in-memory memory.

5. What is the full name of Redis?

Remote Dictionary Server

6. What kinds of data elimination strategies does Redis have?

  • Memory limit is reached and the client tries to execute commands that will allow more memory to be used (most write instructions, but DEL and a few exceptions)

  • Allkeys-lru: Attempts to reclaim the least-used key (LRU) to make room for newly added data.

  • Volatile – LRU: Attempts to reclaim the least-used key (LRU), but only the key in the expired set, so that newly added data has space to store.

  • Allkeys-random: Retrieves random keys to make room for newly added data.

  • Volatile -random: Retrievals random keys to make room for newly added data, but only for keys in expired collections.

  • Volatile – TTL: Retrievals the keys in the expired set, and prioritized the keys with a shorter TTL to make room for newly added data.

7. Why doesn’t Redis officially provide Windows version?

Because the current Linux version has been quite stable, and a large number of users, there is no need to develop a Windows version, but will bring compatibility issues.

What is the maximum capacity of a string value?

512M

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. What should Redis cluster solution do? What are the options?

(1) TwemProxy, the general concept is that it is similar to a proxy method, and the use method is no different from ordinary Redis. After setting up multiple Redis instances under it, twemProxy should be connected where redis is needed. It receives the request as a proxy and, using a consistent hash algorithm, forwards the request to the concrete Redis and returns the result to twemProxy. Easy to use (only need to modify the connection port relative to REDis), the first choice for the expansion of old projects. Problem: The pressure of twemproxy’s own single-port instance and the change of the calculated value when the number of Redis nodes changes after using consistent hash, the data cannot be automatically moved to the new node.

(2) CODIS, the most commonly used cluster scheme at present, 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.

(3) 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.

(4) Implementation in the business code layer, set up several unrelated Redis instances, in the code layer, hash the key, and then go to the corresponding Redis instances to operate data. 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.

11. When will Redis cluster solution make the whole cluster unavailable?

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.

12, MySQL has 2000W data, redis only 20W data, how to ensure that redis data is hot data?

When the redis memory data set grows to a certain size, a data obsolescence strategy is implemented.

13. What are the suitable scenarios for Redis?

One of the most common scenarios where Redis is used is the 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 Caching (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) One of the advantages of queue Reids in the field of memory storage engines is that they provide list and set operations, which makes Redis a good platform for message queuing.

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.

(4) leaderboard/counter Redis in memory to increase or decrease the operation of the number to achieve very good.

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 sort set — we call it “user_scores” — we simply do as follows: this assumes, of course, that you sort incrementally by your users’ scores. ZRANGE user_scores 0 10 WITHSCORESAgora Games is a good example of this, implemented in Ruby, whose leaderboards are stored in Redis, as you can see here.

Last (but certainly not least) is Redis’s publish/subscribe feature.

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).

14. What are the benefits of Redis?

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

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

(3) Support transactions, operations are atomicity, the so-called atomicity of the data is either all executed, or not all executed

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

15. What are the differences between Memcache and Redis?

(1) Storage mode

  • The Memecache stores all data in the memory. After a power failure, the Memecache hangs. The data cannot exceed the memory size.

  • Redis is partially stored on hard disk to ensure data persistence.

(2) Data support type

  • Memcache support for data types is relatively simple.

  • Redis has complex data types.

(3) Different underlying models are used

  • The underlying implementation mode and application protocol of communication between them and the client 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.

(4) Value

  • Redis can be up to 1GB, while Memcache is only 1MB

Redis is 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.

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

To set the password: config set requirepass 123456

Authorization password: auth 123456

18, 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.

19. 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 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.

21. How are Redis clusters replicated?

Asynchronous replication

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

16384.

Redis cluster how to select a database?

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

24, How to test the connectivity of Redis?

ping

25. 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.

26, 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

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

EXPIRE and PERSIST commands.

29, 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.

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. 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.

31. What algorithm does Redis recycle?

LRU algorithm

32. How does Redis do mass data insertion?

Redis2.6 Start Redis – CLI supports a new mode called Pipe mode for performing large data inserts.

33, Why do Redis partition?

Partitioning allows Redis to manage more memory, and Redis will be able to use all the machine’s memory. Without partitions, you can only use the memory of one machine. Partitioning allows Redis computing power to be multiplied by simply adding computers, and Redis network bandwidth to be multiplied by adding computers and network cards.

34, Do you know what Redis partition implementation scheme?

  • Client partitioning is where the client determines which Redis node data will be stored or read from. Most clients already implement client partitioning.
  • Proxy partitioning means that the client sends requests to the proxy, which then decides which node to write or read data to. The broker decides which Redis instances to request based on partitioning rules and then returns the Redis response to the client.
  • A proxy implementation of Redis and memcached is Twemproxy Query Routing, which means that the client requests any random Redis instance and Redis forwards the request to the correct Redis node.
  • Redis Cluster implements a hybrid form of query routing, but rather than directly forwarding requests from one Redis node to another Redis node, it redirects directly to the correct Redis node with the help of the client.

35. What are the disadvantages of Redis zoning?

  • Operations involving more than one key are generally not supported.

For example, you can’t intersect two collections, because they might be stored in different Redis instances (there are ways to do this, but you can’t use intersection directives directly).

  • You cannot use Redis transactions if you operate on multiple keys simultaneously. The granularity used for partitioning is key, and you cannot store a data set with a very long sorted key.

Data processing can be very complicated when using partitions, for example you have to collect RDB/AOF files from different Redis instances and hosts simultaneously for backup.

  • Dynamic capacity expansion or reduction while partitioning can be complex.

Redis clusters can transparently rebalance data to users by adding or removing Redis nodes at run time, but some other client or proxy partitioning methods do not support this feature. However, there is a pre-slice technology can also better solve this problem.

Redis persistent data and cache how to do capacity expansion?

If Redis is used as a cache, use consistent hashing for dynamic scaling and scaling.

If Redis is used as a persistent store, a fixed keys-to-nodes mapping must be used, and the number of nodes cannot be changed once determined. Otherwise (that is, if the Redis nodes need to change dynamically), you must use a system that can rebalance data at run time, which currently only the Redis cluster can do.

37, distributed Redis is early or late scale to do a good job? Why is that?

Since Redis is so lightweight (a single instance uses only 1M memory), it is best to start many instances at first to prevent future expansion. Even if you only have one server, you can start with Redis running in a distributed fashion, using partitions and launching multiple instances on the same server. Setting up a few more Redis instances to start with, such as 32 or 64 instances, may seem cumbersome to most users, but the sacrifice is worth it in the long run. That way, when your data is growing and you need more Redis servers, all you need to do is simply migrate Redis instances from one service to another (without worrying about repartitioning). Once you add another server, you need to migrate half of your Redis instances from the first machine to the second machine.

38. What is Twemproxy?

  • Twemproxy is a proxy system maintained by Twitter for Memcached ASCII and Redis protocols. It is a single-threaded program written in C and runs very fast. It is open source software under the Apache 2.0 license.

  • Twemproxy supports automatic partitioning, so if one of its proxy Redis nodes is unavailable, it will automatically exclude that node (this will change the original keys-Instances mapping, so you should only use Twemproxy when caching Redis).

  • There is no single point of Twemproxy itself, because you can start multiple Twemproxy instances and then have your clients connect to any one of them.

  • Twemproxy is an intermediate layer between Redis client and server, so it should not be complicated and should be reliable to handle partitioning functionality.

Which clients support consistent hashing?

Redis-rb, Predis, etc.

40. How is Redis different from other key-value stores?

  • Redis has more complex data structures and provides atomic operations on them, which is a different evolutionary path from other databases.
  • Redis data types are transparent to programmers while being based on basic data structures without additional abstractions.
  • Redis runs in memory but can persist to disk, so there is a memory tradeoff for high-speed reads and writes to different data sets, since the amount of data cannot be larger than hardware memory. Another advantage of an in-memory database is that it is much easier to operate in memory than the same complex data structures on disk, allowing Redis to do a lot of things with a lot of internal complexity.
  • At the same time, they are compact in terms of disk formats and are produced in an appending manner because they do not require random access.

What is the memory usage of Redis?

To give you an example: 1 million key-value pairs (the keys are 0 to 999999 and the values are the string “Hello world”) used 100MB on my 32-bit Mac laptop. The same data can be put into a key for only 16MB, because the key value has a high overhead. Executing on Memcached gives a similar result, but is less expensive than Redis, which records type information, reference counts, and so on. Of course, the ratio is much better for big key pairs. 64-bit systems require more memory overhead than 32-bit systems, especially since key-value pairs are smaller because Pointers take up eight bytes on 64-bit systems. But, of course, 64-bit systems support more memory, so more or less a 64-bit system is needed to run a large Redis server.

What are some ways to reduce the memory usage of Redis?

If you are using a 32-bit Redis instance, you can make good use of the Hash,list,sorted set,set, etc., as many small key-values can often be grouped together in a more compact way.

What command is used to view the usage and status information of Redis?

info

What happens when Redis runs out of memory?

If the upper limit is reached, Redis write commands will return an error message (but read commands will return normally). Or you can use the configuration flush mechanism by using Redis as a cache, flushing out old content when Redis reaches its memory limit.

Redis is a single thread, how to improve the utilization of multi-core CPU?

You can deploy multiple instances of Redis on the same server and use them as different servers. At some point, one server is not enough anyway, so if you want to use more than one CPU, you can consider shards.

46, How many keys can a Redis instance hold?

List, Set, Sorted Set How many elements can they store at most? In theory Redis can handle up to 232 keys and has been tested in practice with at least 250 million keys stored in each instance. We are testing some larger values. Any list, set, and sorted set can have 232 elements. In other words, the storage limit of Redis is the amount of available memory in the system.

Redis common performance problems 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.

48. What 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.

49, How to choose the appropriate persistence mode?

  • In general, if you want to achieve data security comparable to PostgreSQL, you should use both persistence features.
  • If you care deeply about your data, but can still afford to lose it within minutes, you can use RDB persistence only.
  • Many users use AOF persistence only, but this is not recommended: periodic SNAPSHOTS are very convenient for database backups, RDB can restore data sets faster than AOF, and RDB can avoid the aforementioned AOF bugs.

50. Does Redis take effect in real time without restarting the configuration?

For running instances, there are many configuration options that can be modified through the CONFIG SET command without any kind of reboot. Starting with Redis 2.2, you can switch from AOF to RDB’s snapshot persistence or other means without rebooting Redis. Retrieve the ‘CONFIG GET *’ command for more information. Occasional reboots are necessary, such as when you need to update the Redis program to a new version, or when you need to modify some configuration parameters that are not currently supported by the CONFIG command.

As long as you can guarantee your salary to rise a step (constantly updated)

I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx and many other knowledge points can be shared free of charge to everyone, you can join my PHP technology exchange group 953224940

>>> Architect growth path