Generally speaking, there are two main scenarios for 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 reading/writing is required. For example, there are occasions when it is necessary to rush to buy goods and grab red envelopes.
Due to high-speed reading/writing of data in high-concurrency situations, one of the core issues is data consistency and access control.
# (Recommended learning: Redis Video Tutorial )
## In the reading/writing operation of the database, the real situation is The number of read operations far exceeds the number of write operations, generally at a ratio of 1:9 to 3:7, so the possibility of reading is much greater than the possibility of writing. When sending SQL to the database for reading, the database will go to the disk to index the corresponding data back, and indexing the disk is a relatively slow process. If the data is placed directly on the Redis server running in the memory, then there is no need to read/write the disk, but directly read the memory, which will obviously be much faster and will greatly reduce the pressure on the database. The use of memory to store data is also relatively expensive, because the disk can be TGB level, and it is very cheap. The memory is generally a few hundred GB, which is quite remarkable. Therefore, although the memory is efficient, the space is limited and the price It is also much higher than the disk, so the cost of using memory is high. It is not possible to store whatever you want, so we should consider conditional storage of data. Generally speaking, some commonly used data is stored, such as user login information; some major business information, such as banks will store some basic customer information, bank card information, recent transaction information, etc. 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 rate is low, there is no need to write to the cache. Whether the business data has a lot of read operations or a lot of write operations? If there are a lot of write operations, it needs to be written to the database frequently, and there is no need to use cache. What is the size of 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, then use it. The read logic for using Redis as cache is shown in Figure 1.High-speed reading/writing occasions
In Internet applications, there are often some occasions that require high-speed reading/writing, such as flash sales of products, grabbing red envelopes, Taobao, JD.com’s Double Eleven event or Spring Festival ticket grabs, etc. In the above situations, thousands of requests will reach the server in an instant. If a database is used, the database needs to execute thousands of SQLs in an instant, which can easily cause a bottleneck in the database. In severe cases, it will cause database paralysis and cause the Java Web system service to crash. The response to such situations is often to consider asynchronous writing to the database, and in high-speed reading/writing situations, use Redis alone to cache the data that requires high-speed reading/writing into Redis. , and when certain conditions are met, these cached data are triggered to be written into the database. Let’s first look at the flow chart of a request operation, as shown in Figure 3.The above is the detailed content of What is redis generally used for in java web?. For more information, please follow other related articles on the PHP Chinese website!