In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the detailed introduction and application of Redis". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the detailed introduction and application of Redis.
What is Redis?
Redis is an open source (BSD licensed) in-memory data structure storage used as database, cache, and messaging middleware. It supports a variety of data structures, such as string, hash table, list, unordered set, range query of ordered set, bitmap, cardinality statistics and geospatial index and query. Redis has built-in replication, Lua scripting, LRU recycling, transactions, and different levels of disk persistence, and provides high-availability clusters through sentinels and automatic partitions.
Single thread structure
Pure memory database, the bottleneck does not lie in memory, but in network IO
Single thread to avoid frequent context switching
Asynchronous blocking IBO (Multiplexing)
Persistence RDB (Redis DataBase) persistence
Snapshot
Advantages: suitable for backup, restore and recovery of data quickly, maximize the performance of Redis
Disadvantages: fork () can be very time-consuming when data is lost between snapshots and when the dataset is large
AOF (Append Only File) persistence
Journal
Advantages: high data integrity, high readability, and rewritable (the new rewritten AOF file contains the minimum set of commands needed to recover the current dataset)
Disadvantages: large, slower than RDB, with bug
Business
Multi starts the transaction, and exec executes the transaction
As you can see, the principle of redis transaction implementation is that the commands to be executed are stored in a queue, executed in turn, stop and cancel the transaction when an error is reported, and commit the transaction if the error is not reported.
Exception: cases where there is no rollback:
When one or more commands in a transaction join the queue without reporting errors, the error will only be reported when it is executed, and redis will ignore the error and continue to execute.
Use watch to monitor one (or more) key, and if the key (or these) is changed by other commands before the transaction is executed, the transaction will be interrupted. When exec is called, monitoring of all keys is canceled regardless of whether the transaction is executed successfully or not. Or call unwatch to cancel the monitoring manually.
Pipeline
Pipeline reduces the round-trip delay time by reducing the number of communications between the client and redis, and the principle of Pipeline is queue, while the principle of queue is time-first-in, first-out, so as to ensure the order of data.
Applicable scenarios: batch operation, low reliability requirements,
Lua script
Lua is an efficient lightweight scripting language, written in the standard C language and open in source code, and is designed to be embedded in the application so as to provide flexible extension and customization for the application. By definition, the script in Redis is a transaction, so anything that can be done in a transaction can be done in a script. And in general, it's easier and faster to use scripts.
Multiple redis commands can be executed atomically through lua scripts
All command operations are blocked during the execution of the lua script
Benefits of using scripts
Reduce network overhead and run multiple commands in the same script in a Lua script
Atomic operation, redis will execute the entire script as a whole, and will not be inserted by other commands. In other words, there is no need to worry about race conditions during scripting
Reusability, the script sent by the client will always be stored in redis, which means that other clients can reuse this script to complete the same logic.
Multi-database support
16 databases are supported by default; it can be understood as a namespace
Something different from a relational database
Redis does not support custom database nouns
Authorization cannot be set separately for each database
Each database is not completely isolated. You can empty the data in all databases on the redis instance side by using the flushall command
Select different database namespaces through select dbid. The default value range of dbid is 0-15.
Distributed cluster
In Redis Cluster, Sharding uses the concept of slot (slot), which is divided into 16384 slots, which is a bit similar to the pre sharding idea mentioned earlier. For each key-value pair entering the Redis, it is hashed according to key and assigned to one of the 16384 slot. The hash algorithm used is also relatively simple, that is, 16384 modules are taken after CRC16. Each node (node) in the Redis cluster is responsible for allocating some of the 16384 slot, that is, each slot corresponds to a node for processing. When dynamically adding or decreasing node nodes, 16384 slots need to be redistributed, and the key values in the slots need to be migrated. Of course, this process, in the current implementation, is still in a semi-automatic state and requires human intervention. Redis cluster, to ensure that the node corresponding to 16384 slots is working properly. If a node fails, the slots it is responsible for will also fail, and the whole cluster will not work. In order to increase the accessibility of the cluster, the officially recommended solution is to configure node as a master-slave structure, that is, a master master node with n slave slave nodes. At this time, if the primary node fails, Redis Cluster will select a rising primary node from the slave node according to the election algorithm, and the whole cluster will continue to provide services. This is very similar to the server node through the Sentinel monitor rack to form a master-slave structure, but the Redis Cluster itself provides the ability to fail over and fault tolerance.
Redis sharding
Codis
Twemproxy
Supported data types, common commands, common scenarios String
The default maximum storage capacity is 512m
Common commands: set, get, incr, decr, append, strlen, mget, setnx
Set+get: cache, single sign-on
Bitmap: statistics on the number of users going online
Incr: counter, speed limiter
List
Orderly and repeatable
Common commands: lpush, rpush, lpop, rpop, llen, lrange, lrem, lset
Lpush+lpop:Stack (stack)
Lpush+rpop:Queue (queue)
Lpush+ltrim:Capped Collection (finite set)
Lpush+brpop:Message Queue (message queuing)
Blpop: event reminder (alternative polling)
Hash
Nesting of data types is not supported
Suitable for storing objects
Common commands: hset, hget, [hmset] (http://doc.redisfans.com/hash/hmset, .html), hmget, hgetall, hexists, hincrby, hsetnx, hdel
Set
Disorder and non-repetition
Common commands: sadd, srem, smembers, sdiff, sunion, sinter
Sadd: label
Sinter: intersection
Sunion: Union
SortedSet
Orderly and non-repetitive
Common commands: zadd, zrange
Zcount: statistics
Zrevrange: ranking
Key
Common commands: expire, ttl
Script
Common command: eval
Redis installation installation
First of all, go to the redis official website to find the redis version to be installed and the Redis download page. We select v4.0.11 here and execute the following command:
# wget http://download.redis.io/releases/redis-4.0.11.tar.gz# tar xzf redis-4.0.11.tar.gz# cd redis-4.0.11# make
At this point the installation is complete, and then you can test the compilation status through make test
# make test
The error-free completion compilation should have this output:
Error report: tcl 8.5 or above is required to run redis test
You need tcl 8.5or newer in order to run the Redis test make: * [test] Error 1
Install tcl8.6.1 below:
# wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz# sudo tar xzvf tcl8.6.1-src.tar.gz# cd tcl8.6.1/unix/# sudo. / configure# sudo make# sudo make install
Run make test again, and after no problem, run the last step to complete the installation:
# make install
Start directly:
#. / redis-server.. / redis.conf
To start redis in the background, just modify the daemonize yes of the redis.conf configuration file and start it again.
Install the startup command to start the redis server: #. / redis-server.. / redis.conf stop the redis service: #. / redis-cli shutdown connection local startup redis:# redis-cli according to the ip port connection redis:# redis-cli-h 127.0.0.1-p 6379 to check whether the password 127.0.0.1redis.conf 6379 > config get requirepass1) "requirepass" 2) "set the password 127.127. 0.0.1 config set requirepass 6379 > key 123456 / / password is a summary of 123456OK usage about key
It is recommended that key should not be too long and should not exceed 1024 bytes, which takes up memory and reduces query efficiency.
Uniform naming rules are recommended, for example: String:001:zhangsan:age
Using bitmap to realize the Statistics of the number of users going online
Bitmap is very effective for certain types of calculations.
Suppose we now want to record the frequency of users on our site, for example, calculating how many days user A has been online, how many days user B has been online, and so on, as data to determine which users should participate in activities such as beta testing-a pattern that can be implemented using SETBIT and BITCOUNT.
For example, whenever a user goes online on a certain day, we use SETBIT, take the user name as the key, take the launch date of the website represented on that day as the offset parameter, and set the offset to 1.
For example, if today is the 100th day of the launch of the site, and user peter has read the site today, execute the command SETBIT peter 101; if peter continues to read the site tomorrow, execute the command SETBIT peter 1001, and so on.
When you want to calculate the total number of times peter has been online, use the BITCOUNT command: execute BITCOUNT peter, and the result is the total number of days peter has been online.
For more detailed implementation, please refer to the blog post (outside the wall) Fast, easy, realtime metrics using Redis bitmaps.
Cache cache consistency
Update the library data before deleting the cache
Implementation of cache breakdown and cache avalanche distributed lock
Setnx+lua implementation
Public class RedisTool {private static final String LOCK_SUCCESS = "OK"; private static final String SET_IF_NOT_EXIST = "NX"; private static final String SET_WITH_EXPIRE_TIME = "PX"; private static final Long RELEASE_SUCCESS = 1L; / / acquire lock public static boolean getLock (Jedis jedis, String lockKey, String requestId, int expireTime) {String result = jedis.set (lockKey, requestId, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expireTime) If (LOCK_SUCCESS.equals (result)) {return true;} return false;} / / release lock public static boolean releaseLock (Jedis jedis, String lockKey, String requestId) {String script = "if redis.call ('get', KEYS [1]) = ARGV [1] then return redis.call (' del', KEYS [1]) else return 0 end" Object result = jedis.eval (script, Collections.singletonList (lockKey), Collections.singletonList (requestId)); if (RELEASE_SUCCESS.equals (result)) {return true;} return false;}} so far, I believe you have a deeper understanding of "detailed introduction and application of Redis". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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
© 2024 shulou.com SLNews company. All rights reserved.