Home  >  Article  >  Database  >  What is redis generally used for in java web?

What is redis generally used for in java web?

(*-*)浩
(*-*)浩Original
2019-11-21 10:53:292575browse

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.

What is redis generally used for in java web?
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.


What is redis generally used for in java web?

We can know the following two points from Figure 1.

When reading data for the first time, reading Redis data will fail. At this time, the program will be triggered to read the database, read the data, and write it to Redis.

When the data is read for the second time and thereafter, Redis is read directly. After the data is read, the process ends, so the speed is greatly improved.

It can be seen from the above analysis that most operations are read operations. Using Redis to handle read operations will be very fast. It also reduces the dependence on the database and greatly reduces the burden on the database.

After analyzing the logic of the read operation, let’s analyze the process of the write operation, as shown in Figure 2.


What is redis generally used for in java web?

#As can be seen from the process, update or write operations require multiple Redis operations. If the number of business data writes is much greater than the number of reads, there is no need to use Redis.

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 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.

What is redis generally used for in java web?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn