In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "what are the advantages and characteristics of Redis". The editor shows you the operation process through actual cases, and the operation method is simple, fast and practical. I hope this article "what are the advantages and characteristics of Redis" can help you solve the problem.
What is redis?
Remote DIctionary Server (Redis) is a key-value storage system written by Salvatore Sanfilippo, which is a cross-platform non-relational database.
Redis is an open source key-value pair (Key-Value) database that is written in ANSI C, complies with the BSD protocol, supports the network, can be based on memory, distributed, and optional persistence, and provides API in multiple languages.
Redis is often referred to as a data structure server because value can be of types such as String, Hash, list, sets, and sorted sets.
Characteristics of Redis:
The in-memory database is fast and supports data persistence. The data in memory can be saved on disk and can be loaded and used again when rebooting.
Redis not only supports simple key-value type data, but also provides storage of data structures such as list,set,zset,hash.
Redis supports data backup, that is, data backup in master-slave mode.
Support transaction
Advantages of Redis:
Extremely high performance-Redis can read at a speed of 110000 times per second and write at a speed of 81000 times per second.
Rich data types-Redis supports Strings, Lists, Hashes, Sets and Ordered Sets data type operations for binary cases.
Atom-all operations of Redis are atomic, while Redis also supports the atomicity of several merged operations. (business)
Rich features-Redis also supports publish/subscribe, notification, key expiration, and so on.
How is Redis different from other key-value stores?
Redis has more complex data structures and provides atomic operations on them, which is an evolutionary path different from other databases. The data types of Redis are based on basic data structures while being transparent to programmers without additional abstraction.
Redis runs in memory but can be persisted to disk, so memory needs to be weighed when reading and writing high-speed data sets, because the amount of data cannot be larger than hardware memory. Another advantage of in-memory databases is that compared to the same complex data structures on disk, it is very easy to operate in memory, so Redis can do a lot of things with strong internal complexity. At the same time, they are compact and appended in terms of disk format, because they do not need random access.
What are the differences between Memcache and Redis
Storage mode Memecache stores all the data in memory and will hang up when the power is off. The data cannot exceed the memory size. Part of Redis is stored on hard disk, and redis can persist its data.
Data support type memcached all values are simple strings. Redis, as its substitute, supports richer data types and provides storage of data structures such as list,set,zset,hash.
Using the underlying model, the underlying implementation between them and the application protocol for communicating with the client are different. Redis directly builds its own VM mechanism, because the general system calls system functions, it will waste a certain amount of time to move and request.
Value values vary: Redis can reach a maximum of 512m Redis memcache only 1mb.
Redis is much faster than memcached.
Redis supports data backup, that is, data backup in master-slave mode.
Why is Redis so fast?
1. Based entirely on memory, most of the requests are purely memory operations, which is very fast. The data is stored in memory, and the advantage similar to HashMap,HashMap is that the time complexity of search and operation is O (1).
2. The data structure is simple and the data operation is simple. The data structure in Redis is specially designed.
3. Single thread avoids unnecessary context switching and competition conditions, and there is no CPU consumption caused by multi-process or multi-thread switching, there is no need to consider all kinds of locks, there is no lock release operation, and there is no performance consumption caused by possible deadlocks.
4. Using the multi-channel Istroke O multiplexing model, non-blocking IO
5. Different underlying models are used, and the underlying implementation between them and the application protocols for communication with clients are different. Redis directly builds its own VM mechanism, because the general system calls system functions, it will waste a certain amount of time to move and request.
6. Multi-channel I / O multiplexing model
The multiplexing model of multiple streams uses the ability of select, poll, and epoll to monitor multiple streams' Icano events at the same time, blocking the current thread when idle, and waking up from the blocked state when one or more streams have Icano events, so the program polls all streams once (epoll polls only those streams that actually emit events) and processes only those that are ready in sequence. This practice avoids a large number of useless operations.
* * here "multiplex" refers to multiple network connections, and "multiplexing" refers to the reuse of the same thread. * * the use of multi-channel IO O multiplexing technology allows a single thread to process multiple connection requests efficiently (minimizing the time consumption of network IO), and Redis operates data in memory very quickly, which means that in-memory operations will not become a bottleneck affecting Redis performance. The high throughput of Redis is mainly due to the above points.
So why is Redis single-threaded
First of all, we have to understand that the above analysis is to create a very fast atmosphere of Redis! According to the official FAQ, because Redis is a memory-based operation, CPU is not the bottleneck of Redis, and the bottleneck of Redis is most likely to be the size of machine memory or network bandwidth. Since single-threading is easy to implement, and CPU won't be a bottleneck, it makes sense to adopt a single-threaded solution (after all, multithreading can be troublesome! ).
Redis data types and commands
1. String (String) redis 127.0.0.1 SET rediskey redisOKredis 6379 > GET rediskey "redis" 2. Hash (Hash)
Redis hash is a mapping table of field (field) and value (value) of type string, and hash is particularly suitable for storing objects.
Each hash in Redis can store 232-1 key-value pairs (more than 4 billion)
3. List (List)
The Redis list is a simple list of strings sorted in the order in which they are inserted. You can add an element to the head (left) or tail (right) of the list.
A list can contain up to 232-1 elements (4294967295, each list has more than 4 billion elements).
Redis 127.0.0.1 redis 6379 > LPUSH rediskey redis (integer) 1redis 127.0.0.1 1redis 6379 > LPUSH rediskey mongodb (integer) 2redis 127.0.0.1 1redis 6379 > LPUSH rediskey mysql (integer) 3redis 127.0.1 LRANGE rediskey 0101) "mysql" 2) "mongodb" 3) "redis" 4. Collection (Set)
The Set of Redis is an unordered collection of type String. Collection members are unique, which means that there can be no duplicate data in the collection.
The encoding of the collection object can be intset or hashtable.
Collections in Redis are implemented through hash tables, so the complexity of adding, deleting and searching is O (1).
The maximum number of members in the collection is 4294967295 (each collection can store more than 4 billion members).
Redis 127.0.0.1 redis 6379 > SADD rediskey redis (integer) 1redis 127.0.0.1 1redis 6379 > SADD rediskey mongodb (integer) 1redis 127.0.0.1 1redis 6379 > SADD rediskey mysql (integer) 1redis 127.0.0.1 1redis 6379 > SADD rediskey mysql (integer) 0redis 127.0.0.1 1redis 6379 > SMEMBERS rediskey1) "mysql" 2) "mongodb" 3) "redis" 5. Ordered set (sorted set)
Redis ordered collections, like collections, are collections of elements of type string, and duplicate members are not allowed.
The difference is that each element is associated with a score of type double. Redis sorts the members of the collection from small to large by scores.
The members of an ordered set are unique, but the score can be repeated.
The collection is implemented through a hash table, so the complexity of adding, deleting, and finding is all O (1). The maximum number of members in the collection is 4294967295 (each collection can store more than 4 billion members).
6. HyperLogLog
Redis added the HyperLogLog structure in version 2.8.9.
Redis HyperLogLog is an algorithm used to do cardinality statistics. The advantage of HyperLogLog is that when the number or volume of input elements is very large, the space needed to calculate the cardinality is always fixed and small.
In Redis, each HyperLogLog key costs only 12 KB of memory to calculate the cardinality of nearly 2 ^ 64 different elements. This is in sharp contrast to collections that consume more memory with more elements when calculating the cardinality.
However, because HyperLogLog only calculates the cardinality based on the input elements, and does not store the input elements themselves, HyperLogLog cannot return each input element like a collection.
What is the cardinality?
For example, if the dataset {1, 3, 5, 7, 5, 7, 8}, then the cardinality of this dataset is {1, 3, 5, 7, 8}, and the cardinality (non-repeating elements) is 5. The cardinality estimation is to calculate the cardinality quickly within the acceptable range of error.
Example
The following example demonstrates how HyperLogLog works:
/ / add the specified element to the HyperLogLog. Redis 127.0.0.1 redis 6379 > PFADD rediskey "redis" 1) (integer) 1redis 127.0.0.1 1redis 6379 > PFADD rediskey "mongodb" 1) (integer) 1redis 127.0.0.1 redis 6379 > PFADD rediskey "mysql" 1) (integer) 1 integer / add specified elements to HyperLogLog. Redis 127.0.0.1 redis 6379 > PFCOUNT rediskey (integer) 37. Publish and subscribe
Redis publish subscription (pub/sub) is a mode of message communication: the sender (pub) sends the message and the subscriber (sub) receives the message.
Redis clients can subscribe to any number of channels.
The following figure shows the relationship between the channel channel1 and the three clients that subscribe to the channel-client2, client5, and client1:
Example
The following example demonstrates how publish and subscribe works, requiring two redis-cli clients to be opened.
In our example, we created a subscription channel named runoobChat:
The first redis-cli client redis 127.0.0.1 SUBSCRIBE runoobChatReading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "runoobChat" 3) (integer) 1
Now, let's reopen a redis client, then post the message twice on the same channel runoobChat, and the subscriber can receive the message.
The second redis-cli client, redis 127.0.0.1 integer 6379 > PUBLISH runoobChat "Redis PUBLISH test" (integer) 1redis 127.0.0.1 integer 6379 > PUBLISH runoobChat "Learn redis by runoob.com" (integer) subscriber's client will display the following message: 1) "message" 2) "runoobChat" 3) "Redis PUBLISH test" 1) "message" 2) "runoobChat" 3) "Learn redis by runoob.com"
The gif demo is as follows:
Open the local Redis service and open two redis-cli clients.
Enter SUBSCRIBE runoobChat in the first redis-cli client, which means subscribing to the runoobChat channel.
Type PUBLISH runoobChat "Redis PUBLISH test" in the second redis-cli client to send the message to the runoobChat channel, and the test message sent by the second redis-cli client will be seen on the first redis-cli client.
8. Business
Redis transactions can execute more than one command at a time with three important guarantees:
Bulk operations are cached in queues before sending EXEC commands.
After receiving the EXEC command, enter the transaction execution, any command execution in the transaction fails, and the rest of the commands are still executed.
During transaction execution, command requests submitted by other clients are not inserted into the transaction execution command sequence.
A transaction goes through the following three stages from start to execution:
Start the business.
Order to join the team.
Execute the transaction.
Example
The following is an example of a transaction, which starts a transaction with MULTI, then queues multiple commands into the transaction, and finally triggers the transaction by the EXEC command to execute all the commands in the transaction:
Redis 127.0.0.1 Mastering in 21 days QUEUEDredis 127.0.0.1 in 6379 > GET book-nameQUEUEDredis 127.0.1 QUEUEDredis 127.0.1 QUEUEDredis 127.0.1 OK2 OK2) Mastering C++ in 21 days 3) (integer) 34) 1) "Mastering Series" 2) "C++" 3) "Programming"
The execution of a single Redis command is atomic, but Redis does not add any mechanism to maintain atomicity on the transaction, so the execution of the Redis transaction is not atomic.
A transaction can be understood as a packaged batch execution script, but a batch instruction is not an atomized operation, and the failure of an intermediate instruction will not cause a rollback of the previously done instruction, nor will it cause subsequent instructions not to be done.
This is the description on the official website From redis docs on transactions:
It's important to note that even when a command fails, all the other commands in the queue are processed-Redis will not stop the processing of commands.
For example:
Redis 127.0.0.1 redis 7000 > multiOKredis 127.0.0.1 redis 7000 > set an aaaQUEUEDredis 127.0.0.1 set an aaaQUEUEDredis 7000 > set b bbbQUEUEDredis 127.0.1 Veg7000 > set c cccQUEUEDredis 127.0.0.1 Veg7000 > exec1) OK2) OK
If you fail at set b bbb, set a has succeeded and will not be rolled back, and set c will continue to execute.
9. Script
The Redis script uses the Lua interpreter to execute the script. Redis 2.6 supports the Lua environment through built-in support. The common command to execute a script is EVAL.
Redis 127.0.0.1 redis 6379 > EVAL "return {KEYS [1], KEYS [2], ARGV [1], ARGV [2]}" 2 key1 key2 first second1) "key1" 2) "key2" 3) "first" 4) "second" 10 GEO
Redis GEO is mainly used to store geolocation information and operate on the stored information, which is new in Redis 3.2.
The operation methods of Redis GEO are:
Geoadd: add the coordinates of the geographic location.
Geopos: gets the coordinates of the geographic location.
Geodist: calculates the distance between two positions.
Georadius: gets a collection of geographic locations within a specified range according to the latitude and longitude coordinates given by the user.
Georadiusbymember: gets a set of geolocations within a specified range based on a location stored in the location collection.
Geohash: returns the geohash value of one or more location objects.
Redis > GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania" (integer) 2redis > GEODIST Sicily Palermo Catania "166274.1516" redis > GEORADIUS Sicily 1537100 km1) "Catania" redis > GEORADIUS Sicily 1537200 km1) "Palermo" 2) "Catania" redis > 11 Redis Stream
Redis Stream is a new data structure added in Redis version 5.0.
Redis Stream is mainly used for message queuing (MQ,Message Queue). Redis itself has a Redis publish subscription (pub/sub) to achieve the function of message queue, but it has a disadvantage that the message cannot be persisted. If there is a network disconnection, Redis downtime, etc., the message will be discarded.
Simply put, publish subscriptions (pub/sub) can distribute messages, but cannot record historical messages.
Redis Stream provides message persistence and active and standby replication functions, which allows any client to access data at any time, and can remember the access location of each client, but also can ensure that the message is not lost.
The structure of Redis Stream is as follows. It has a linked list of messages, all joined messages are strung together, and each message has a unique ID and corresponding content:
Each Stream has a unique name, which is the key of Redis, which is automatically created the first time we append a message using the xadd directive.
The above figure illustrates:
Consumer Group: a consumer group, created with the XGROUP CREATE command, with multiple consumers (Consumer) in a consumer group.
Last_delivered_id: cursor, each consumer group will have a cursor last_delivered_id, any consumer reading the message will move the cursor last_delivered_id forward.
Pending_ids: the state variable of the consumer (Consumer), whose function is to maintain the consumer's unrecognized id. Pending_ids records messages that have currently been read by the client, but there is no ack (Acknowledge character: confirmation character).
Redis pipeline technology
Redis is a TCP service based on client-server model and request / response protocol. This means that typically a request follows these steps:
The client sends a query request to the server and listens for Socket returns, usually in blocking mode, waiting for the server to respond.
The server processes the command and returns the result to the client.
Redis pipeline technology
Redis pipeline technology allows the client to continue to send requests to the server when the server does not respond, and finally read the responses of all servers at once.
This is the end of the content about "what are the advantages and characteristics of Redis". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
Import com.alibaba.fastjson.JSON Configuration conf = HBaseConfiguration.create (); HTableInterfa
© 2024 shulou.com SLNews company. All rights reserved.