About Redis related issues and Memcached

What is Redis?

The code for installing Redis Redis is written in ANSI-C and can be installed and run on all POSIX systems (Linux, *BSD, Mac OS X, Solaris, etc.). And Redis doesn't rely on any non-standard library, and no compilation parameters have to be added. The installation of redis is surprisingly simple, and this may be one of the reasons why he is very popular. It is easy to get started. Redis is an open source key-value database written in C language. . Similar to Memcached, it supports the storage of more value types, including string, list, set, zset, and hash. These data types support push/pop, add/remove, and intersection sets and difference sets and richer operations, and these operations are atomic. Based on this, redis supports sorting in a variety of different ways. Like memcached, data is cached in memory for efficiency. The difference is that redis periodically writes updated data to disk or writes modified operations to additional log files, and master-slave synchronization is implemented on this basis. Currently, VMware is funding the development and maintenance of redis projects.

Differences and Comparison between Redis and Memcached

Redis not only supports simple k/v type data, but also provides storage of data structures such as list, set, zset, and hash. Memcache supports simple data types, String.

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

3, Redis supports data persistence, can keep the data in the memory on the disk, restart can be loaded again for use, and Memecache all the data in memory

4, redis is much faster than memcached

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

Questions Related to Redis Contrast to Memcached

If you want to know in more detail, you can check this handbook (very recommended) on the MOOC website: "The Perplexity of Pedaling Two Boats - Memcached and Redis": TIcle/235...

The choice of Redis and Memcached

The ultimate strategy: Use Redis's String type to do things, you can use Memcached instead, in exchange for better performance improvements; In addition, priority to Redis;

What are the advantages of using redis?

(1) Fast, because the data exists in memory, similar to HashMap. The advantage of HashMap is that the time complexity of finding and operating is O(1).

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

(3) to support transactions, operations are atomic, the so-called atomicity is that the changes to the data are either all executed, or all are not executed

(4) Rich features: Can be used for caching, messages, set expiration time by key, will be automatically deleted after expiration

Redis common data structure usage scenarios

1. String

Commonly used commands: set, get, decr, incr, mget, etc.

The String data structure is a simple key-value type. In fact, value can not only be a String, but also a number. Regular key-value cache application; regular count: microblog number, fan number, etc.

Questions Related to Redis Contrast to Memcached

2.Hash

Commonly used commands: hget, hset, hgetall, etc.

Hash is a mapping table of field and value of type string. Hash is particularly suitable for storing objects. For example, we can use Hash data structures to store user information, product information, and so on.

For example: The homepage of an e-commerce website project recently done uses the redis hash data structure for caching, because a website's home page traffic is the largest, so usually the website's home page can be redis cached to improve performance and concurrency. the amount. I use the jedis client to connect and operate the redis cluster or standalone redis I built. It is easy to use jedis to perform related operations on redis. Generally speaking, it is not difficult to go from a simple cluster to the whole step of implementing redis as a cache. . Interested in the article I wrote yesterday:

"One easy to understand redis cluster theory and construction and use": juejin.im/post/5ad54d...

3.List

Commonly used commands: lpush, rpush, lpop, rpop, lrange, etc.

List is a linked list. Redis list has many application scenarios and is one of the most important data structures of Redis. For example, Weibo's attention list, fan list, and latest news rankings can all be implemented using Redis's list structure.

The implementation of the Redis list is a doubly linked list, which can support reverse lookup and traversal, which is more convenient to operate, but brings some extra memory overhead.

4.Set

Commonly used commands: sadd, spop, smembers, sunion, etc.

The functions externally provided by set are similar to list, which is a list function. The special point is that set can be automatically re-ranked. When you need to store a list of data, and do not want duplicate data, set is a good choice, and set provides an important interface to determine whether a member is in a set collection, this is not the list can not provide.

In the Weibo application, it is possible to have all of the user's followers in one set and have all of their fans in one set. Redis can be very convenient to achieve such as common concerns, common preferences, second friends and other functions.

5.Sorted Set

Commonly used commands: zadd, zrange, zrem, zcard, etc.

Compared to set, sorted set adds a weight parameter score, so that the elements in the collection can be ordered by score.

For example: In the live broadcast system, the real-time ranking information includes online user list of the live studio, various gift rankings, and barrage message (can be understood as message ranking according to the message dimension), and is suitable for storage using the SortedSet structure in Redis. .

There are 2000w data in MySQL, there are only 20w data in Redis, how to ensure that the data in Redis are hot data (redis what data out of the strategy???)

Related knowledge: When the size of a redis memory data set rises to a certain size, a data elimination strategy (recycling strategy) is implemented. Redis provides six data elimination strategies:

volaTIle-lru: Picking out the least recently used data from the dataset for which the expiration time has been set (server.db[i].expires)

volaTIle-ttl: Elimination of expired data from the set of data (server.db[i].expires) for which the expiration time has been set

volaTIle-random: Eliminates data from any dataset that has an expiration time set (server.db[i].expires)

Allkeys-lru: pick the least recently used data from the data set (server.db[i].dict)

Allkeys-random: Eliminating data from any dataset (server.db[i].dict)

No-enviction: prohibit eviction of data

How does Redis's concurrent competition problem be solved?

Redis is a single-process, single-threaded mode that uses queue mode to change concurrent accesses to serial access. Redis itself has no concept of locks. Redis does not have competition for multiple client connections. However, when the Jedis client makes concurrent access to Redis, connection timeouts, data conversion errors, blocking, and client connections are closed. Both are due to the confusion of client connections. There are 2 solutions to this:

1. From the perspective of the client, in order to ensure that each client communicates with Redis normally, the connection is pooled and the client reads and writes Redis operations with internal locks.

2. From the server perspective, use setnx to implement the lock.

Note: For the first one, you need the application to handle the synchronization of resources, the methods you can use are more general, you can use synchronized or lock; the second need to use Redis setnx command, but need to pay attention to some problems.

Redis data structure

The author of redis, antirez, once called it a data structures server. This is a very accurate statement. All the functions of redis are to save the data in its several kinds of structures and provide it to users. Structured interface. We can imagine our inherent data types and their operations in various languages.

Redis currently provides four data types: string, list, set, and zset (sorted set) and Hash.

String is the simplest type. You can understand it as a type with Memcached. A key corresponds to a value, and the supported operations on it are similar to those of Memcached. But it's more feature-rich.

List is a linked list structure, the main function is push, pop, get a range of all the values ​​and so on. The key in operation is understood as the name of the linked list.

Set is a set, similar to the concept of collection in our mathematics. There are operations for adding and deleting elements of a set, and operations such as finding and subtracting multiple sets. The key in the operation is understood as the name of the collection.

The zset is an upgraded version of the set. It adds a sequence attribute to the set. This attribute can be specified when adding a modifier. After each assignment, the zset will automatically reorder the new value. It can be understood that there are two columns of mysql tables, one for storing value and one for storing order. The key in the operation is interpreted as the name of the zset.

The Hash data type allows the user to use Redis to store object types. An important advantage of the Hash data type is that when you store data objects with only a few key values, the memory consumption of the data store will be small. For more information on Hash data types see: http://code.google.com/p/redis/wiki/Hashes

Questions Related to Redis Contrast to Memcached

Redis data storage

Redis storage is divided into memory, disk storage and log files in three parts, the configuration file has three parameters to configure it.

Save seconds updates,save configuration, indicating how many update operations will synchronize the data to the data file. This can be combined with multiple conditions, such as the settings in the default configuration file, and three conditions are set.

Appendonly yes/no , appendonly configuration, indicating whether logging is performed after each update operation. If it is not enabled, data loss may occur during a period of power failure. Because redis itself synchronizes data files according to the above save conditions, some data will only exist in memory for a period of time.

Appendfsync no/always/everysec , appendfsync configuration, no indicates that the operating system performs data caching synchronization to the disk, always indicates that manually calling fsync() after each update operation writes data to the disk, everysec indicates that synchronization occurs once per second.

Amorphous Iron Core For Car Audio

Anyang Kayo Amorphous Technology Co.,Ltd is located on the ancient city-Anyang. It was founded in 2011 that specializes in producing the magnetic ring of amorphous nanocrystalline and pays attention to scientific research highly,matches manufacture correspondingly and sets the design,development,production and sale in a body.Our major product is the magnetic ring of amorphous nanocrystalline and current transformer which is applied to the communication, home appliances, electric power, automobile and new energy extensively. We are highly praised by our customers for our good quality,high efficiency,excellent scheme,low cost and perfect sale service.
Iron-based amorphous Filter Inductance Cores have high saturation magnetic induction, low coercivity, low loss ,excellent DC bias resistance and high permeability of 120 to 1200.So it can be widely used to the car audio choke, DMC filter and smooth output filter,differential mode filter,PFC correction inductance and filter coil.

Car Audio Amorphous Iron Core,Filter Inductance Iron Core,Amorphous Iron Ring,Filter Inductance Magnetic Core,Hot Sale Filter Inductance core

Anyang Kayo Amorphous Technology Co.,Ltd. , https://www.kayoamotech.com

Posted on