In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the application of redis in java web in detail for everyone, the example in this article is very detailed, the detailed explanation of picture and text is easy to learn, it is very suitable for beginners, interested friends can refer to it.
Generally speaking, there are two main scenarios of Redis in Java Web applications, one is to cache commonly used data, and the other is to use it to read / write quickly when high-speed read / write is needed, such as some situations where goods are snapped up and red packets are snatched.
Due to the need for high-speed read / write of data in the case of high concurrency, a core problem is data consistency and access control.
Caching
In the read / write operation to the database, the reality is that the number of read operations far exceeds the number of write operations, usually between 1:9 and 3:7, so there is much more possibility of reading than writing.
When sending SQL to the database for reading, the database will go to the disk to index the corresponding data back, and indexing disk is a relatively slow process. If you put the data directly on a Redis server running in memory, you don't need to read / write to the disk, but read the memory directly, which is obviously much faster and greatly reduces the pressure on the database.
And the use of memory for data storage overhead is also relatively large, because the disk can be TGB level, and very cheap, memory is generally hundreds of GB is quite remarkable, so memory although efficient but limited space, the price is much higher than the disk, so the cost of using memory is higher, not what you want to save, so we should consider conditional storage data.
Generally speaking, store some commonly used data, such as user login information; some major business information, such as banks will store some customer basic information, bank card information, recent transaction information and so on. Generally speaking, when using Redis storage, you need to consider three aspects.
Is business data commonly used? What's the hit rate? If the hit ratio is low, there is no need to write to the cache. Whether the business data has more read operations or more write operations, if there are many write operations, it needs to be written to the database frequently, and there is no need to use caching. What is the size of the business data? If you want to store hundreds of megabytes of files, it will put a lot of pressure on the cache, is it necessary?
After considering these issues, if you feel it is necessary to use caching, use it. The read logic using Redis as the cache is shown in figure 1.
The following two points can be seen from figure 1.
When the data is read for the first time, it fails to read the data from Redis, which triggers the program to read the database, read the data out, and write to the Redis.
When the data is read for the second time and later, the Redis is read directly, and the process is finished after reading the data, so the speed is greatly increased.
From the above analysis, we can see that most of the operations are read operations, using Redis to deal with read operations, the speed will be very fast, but also reduce the dependence on the database, greatly reduce the burden of the database.
After analyzing the logic of the read operation, let's analyze the flow of the write operation, as shown in figure 2.
As can be seen from the process, the operation of updating or writing requires multiple Redis operations. It is not necessary to use Redis if the number of business data writes is much greater than the number of reads.
If the number of reads is much greater than the number of writes, then using Redis has its value, because writing to Redis costs a certain amount of money, but its performance is good, which is almost negligible compared to the database.
High-speed read / write situation
In the application of the Internet, there are often some occasions that require high-speed reading / writing, such as the second kill of goods, the snatching of red envelopes, the Singles' Day activities of Taobao and JD.com, or the Spring Festival travel and so on.
In these situations, thousands of requests will reach the server in an instant. If you use a database, the database needs to execute thousands of SQL in an instant, which can easily cause the bottleneck of the database, seriously paralyze the database and cause the Java Web system service to crash.
In such situations, the response is often to consider writing to the database asynchronously, while in the case of high-speed read / write, only use Redis to deal with it, caching these data that need high-speed read / write to Redis, and under certain conditions, trigger these cached data to be written to the database. First take a look at the flow chart of a request operation, as shown in figure 3.
The above is a brief introduction to the application of redis in java web, and the details of the use still need to be used before you can understand it. If you want to know more, you are welcome to follow the industry information channel!
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.