Yesterday, I wrote an article about setting up redis cluster and using it in my own project. This morning, I found that Redis was often asked in the interview when I read the interviews written by others (mainly Java). So check out the official documentation and its artificial wheels to summarize some of the questions you need to know when interviewing and studying at Redis. It’s impossible to include everything. Try to include the most common ones.

What is Redis?

  • Redis is an open source key-value database written in C language. Like Memcached, it supports storing a relatively large number of value types, including String (string), list(linked list), set(collection), zset(sorted set — ordered set), and hash (hash). All of these data types support push/ POP, add/remove, intersection union and difference sets, and richer operations, all of which are atomic. On this basis, Redis supports sorting in various ways. As with memcached, the data is cached in memory for efficiency. The difference is that Redis periodically writes the updated data to disk or the modified operation to the appended record file, and on this basis realizes master-slave synchronization. Vmware is funding the development and maintenance of the Redis project.

Redis vs. Memcached

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

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

3, Redis support data persistence, you can keep the data in memory in disk, when the restart can be loaded again for use, while Memecache stores all the data in memory

Redis is much faster than memcached

5. Memcached is a multi-threaded, non-blocking IO reuse network model; Redis uses a single-threaded IO reuse model. Redis vs. Memcached

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

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

3, Redis support data persistence, you can keep the data in memory in disk, when the restart can be loaded again for use, while Memecache stores all the data in memory

Redis is much faster than memcached

5. Memcached is a multi-threaded, non-blocking IO reuse network model; Redis uses a single-threaded IO reuse model.

Redis vs. Memcached

The ultimate strategy: You can replace everything you do with Redis strings with Memcached in exchange for better performance. In addition, Redis is preferred;

What are the benefits of using 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

Redis common data structure usage scenarios

1. String

  1. String

Common commands: set,get, DECr, INCr,mget, etc. The String data structure is a simple key-value type. A value can be a number as well as a String. General key-value caching applications; General count: number of micro blog, number of fans, etc.

2.Hash

Common commands: hget,hset,hgetall, etc.Copy the code

The String data structure is a simple key-value type. A value can be a number as well as a String. General key-value caching applications; General count: number of micro blog, number of fans, etc.

2.Hash

Common commands: hget,hset,hgetall, etc.Copy the code

A Hash is a mapping of fields and values of string type. Hash is especially useful for storing objects. For example, we can use Hash data structures to store user information, product information and so on.

For example, a recent e-commerce website project used redis hash data structure to cache the homepage. Because the homepage of a website has the most visitors, the homepage of a website can be improved by Redis cache to improve performance and concurrency. I use Jedis client to connect and operate the REDis cluster or single REDis I set up, using Jedis can be very easy to conduct related operations on Redis, in general, from building a simple cluster to achieve redis as a cache of the whole step is not difficult.

3.List

Common commands: lpush, rpush lpop, rpop, lrange, etcCopy the code

List is linked list. Redis List has many application scenarios and is also one of the most important data structures of Redis. For example, functions such as weibo following list, fan list and latest news ranking can be realized by using the List structure of Redis.

Redis List is implemented as a bidirectional linked list, that is, it can support reverse lookup and traversal, which is easier to operate, but brings some extra memory overhead.

4.Set

Common commands: sadd, spop smembers, sunion, etcCopy the code

A set provides functions similar to a list, except that it can automatically assign weights.

Set is a good choice when you need to store a list without duplicating it, and sets provide an important interface for determining whether a member is in a set that lists don’t provide.

In the microblog application, all the followers of a user can be stored in a collection, and all the fans can be stored in a collection. Redis is very convenient to implement such functions as common following, common preferences, second friends and so on.

5.Sorted Set

Common commands: zadd,zrange,zrem,zcard, etcCopy the code

Compared with set, sorted set adds a weight parameter score, so that the elements in the set can be sorted according to score.

For example, in the live broadcast system, the real-time ranking information includes the list of online users in the live broadcast room, the list of various gifts, and the bullet-screen messages (which can be understood as the list of messages according to the message dimension). It is suitable to use the SortedSet structure in Redis for storage.

MySQL has 2000W data, Redis only 20W data, how to ensure that Redis data are hot data (Redis has what data weeding strategy??

When redis memory data sets grow to a certain size, a data obsolescence policy is implemented. Redis offers six data elimination strategies:

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

Volatile – TTL: Discard data that will expire from a set of data sets (server.db[I].expires)

Volatile -random: Selects any data from a set of expired data sets (server.db[I].expires) to be discarded

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

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

6. No-enviction: Data expulsion is prohibited

How to solve Redis concurrency competition problem?

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.

Redis Common performance Issues and solutions:

It is better for the Master not to do any persistent work, such as RDB memory snapshots and AOF log files. If data is important, a Slave will enable AOF backup data. The policy is set to synchronize data once per second. It is best for Master and Slave to live in the same LAN to avoid adding Slave libraries to the stressed Master library

Welcome to learn and communicate with me to build Java cloud architecture. I will record the building process and essence of Java cloud architecture recently developed, so as to help more friends who are interested in developing Java advanced architecture to discuss the building process of Java advanced architecture and how to apply it to enterprise projects.

I personally invite all BATJ architecture masters to create a Java Advanced Architecture Communication community Group (group number: 673043639), committed to providing a free Java architecture industry communication platform, through this platform to let everyone learn and grow from each other, improve their skills, make their level to a higher level, and successfully become a Java architecture technology leader or architect development.

Hope this article can help you at the same time, also listen to your point of view. Welcome to comment, add attention, share your ideas! Keep updating!

To-morin Java architectureCopy the code

Share the latest Internet articles to pay attention to the latest development of the Internet