| Look at the world in a grain of sand, a heaven in a wild flower. The infinite grip in the palm, the eternal grip in an instant. -- William Black
Before discussing cache, let's discuss another question: theory and practice. from the perspective of ahuaxuan programmers, some programmers tend to practice and some programmers tend to be theoretical, but this is not a good behavior. Theories and Practices are equally important. When we are doing many core algorithms, there is no theory at all, but in our years of practice, we cannot deepen our understanding without summing up the theory. therefore, theory and practice are equally important.
Cache is one of the indispensable technologies in today's various software or hardware systems, so it is very important for every programmer, as well as ahuaxuan. if we use DFA to implement text filtering from theory to practice, this article summarizes the theory from practice.
Before discussing the cache function, we should first take a look at the cache itself. ahuaxuan subdivided the cache problem into four categories based on its own experience.
1. Why does cache exist?
2. Where can the cache exist?
3 What are the cache attributes?
4 cache media?
After figuring out these four problems, we can determine the cache to be used through the Application Scenario at will.
The following is an analysis of the four problems.
1. Why does cache exist?
Generally, a website or an application requests the application server in the form of a browser. The application server then requests the database after performing a bunch of calculations, after receiving the request, the database returns the data to the application server after a heap of computation. Then, the application server returns the data to the browser after a heap of computation. this is a standard process. however, with the popularization of Internet connection, there are more and more people accessing the internet, and the amount of information on the Internet is also increasing. Under these two increasing circumstances, our applications need to support more and more concurrency. then, our application servers and database servers are making more and more calculations, but our application server resources are often limited, the number of requests received by the database per second is also limited (Which of the following is the limited hard drive speed ). if you use limited resources to provide as much throughput as possible, one way is to reduce the amount of computing, shorten the request process (reduce network I/O or hard disk I/O), then the cache can be very large. the basic principle of caching is to break the standard process described in. In this standard process, any stage can be cut off. requests can be retrieved from the cache and returned directly. this not only saves time, increases response speed, but also saves hardware resources. our limited hardware resources can serve more users.
2. Where can the cache exist? Java code
- Browser --- Between the browser and the app --- the app-database through the Layer
Browser --- Between the browser and the app --- the app-database through the Layer
In, we can see the general process of a request. Next we will redraw this figure to make our structure a little more complex.
(Layer Apps)
Browser --- Between the browser and the app --- the app-database through the Layer
Theoretically, any part of the request can be cached. in the first step, if the data in the browser exists, the speed is the fastest for the user, because there is no need for network requests at this time. in the second step, if the cache is added between the browser and the app, the cache is transparent to the app. in addition, this cache stores the complete page. the third node has several layers in the app, so the cache can be placed on different layers. This part is a complex part of the situation or scenario. exercise caution when selecting cache. in step 4, the database can also contain caches, such as MySQL's querycache.
That is to say, we can cache any point in the entire request process. but can all data be cached. of course not. The data to be cached always has some characteristics. to clearly determine whether the data can be cached or not, you must start with the data change features.
What are the changing features of data? There are two simplest options: change and change. we all know that data that will not change does not need to be computed every time. the problem is that all data will change theoretically. Change is the eternal topic of the world. that is to say, it is wrong to divide the data into two types: change and change. So let's add another condition: time. then we can sum up the data features to change or remain unchanged for a period of time. based on this data feature, we can cache the data in the appropriate location and cache type.
3. What are the cache attributes?
From the object-oriented perspective, the cache is an object, so the object must have attributes. next we will discuss the cache attributes. the following lists the three common attributes.
(1) hit rate
The hit rate is the ratio of the number of requests cached to the number of times the cache returns the correct results. The higher the proportion, the higher the cache usage.
The hit rate problem is a very important issue in the cache. We all hope that our cache hit rate can reach 100%, but it is often counterproductive. Besides, the cache hit rate is an important indicator to measure the cache validity.
(2) maximum element
The maximum number of elements that can be stored in the cache. Once the number of elements in the cache exceeds this value, the cache clearing policy will be used, setting the maximum element value based on different scenarios may increase the cache hit rate to a certain extent. cache more effectively.
(3) clearing Policy
1 FIFO, first in first out, the data first enters the cache. When the cache space is insufficient (when the maximum element limit is exceeded), the data is first cleared.
2 LFU, less frequently used, the elements that have been least used will be cleared. This requires that the cached element has an hit attribute. When the cache space is insufficient, the minimum hit value will be cleared.
2 LRU, least recently used, least recently used. The cached element has a timestamp. When the cache capacity is full and you need to make room for caching new elements, the elements whose timestamp is the farthest from the current time in the existing cache will be cleared out of the cache.
4. cache Media
There will be no more than two types of hardware media, memory and hard disk (corresponding to the Application Layer Program, do not consider the register and other issues ). however, we often do not divide data from hardware. The general division method is technically divided into several types, including memory and hard disk files. database.
(1) memory. putting the cache in the memory is the fastest option. Any program that directly operates on the memory is much faster than operating on the hard disk, but if you need to consider the break down problem, because the data stored in the memory is called non-persistent data, if there is no backup on the hard disk, it is difficult or cannot be restored after the machine is down.
(2) hard disk. in general, many cache frameworks use both memory and hard disk resources. For example, when the allocated memory space is full, the user will choose to persistently store the data to the hard disk. of course, you also choose to directly store one copy of data to the hard disk (one copy in the memory, one copy in the hard disk, not afraid to go down ). in addition, other caches directly store data on the hard disk.
(3) database. when it comes to databases, some people may wonder, aren't we talking about reducing the number of database queries and reducing the pressure on database computing? How can we use the database as the cache medium. this is because there are many types of databases, such as berkleydb, which does not support SQL statements, does not have an SQL engine, but only stores the key and value structures, so the speed is very fast, in contemporary PCs, there are no problems with queries over a dozen times per second (of course, this is determined based on business characteristics, if the data you access is evenly distributed, ahuaxuan cannot guarantee this speed ).
In addition to the cache medium, ahuaxuan divides the cache into local cache and remote cache based on the coupling between the cache and the application.
Local cache refers to the cache component contained in the application. remote cache refers to the cache component that is decoupled from the application outside the application. typical local caches include ehcache and Oscache, while remote cache has a well-known memcached.
The biggest advantage of localcache is that the application and cache are in the same process. The request cache is very fast and does not require network overhead. therefore, for a single application, it is more appropriate to use the local cache when the cache node does not need to notify each other in the case of a cluster or cluster. this is why ehcache and Oscache are so popular in Java.
However, local cache has some disadvantages. Generally, such cache frameworks (such as ehcache or Oscache in Java) are both local cache. that is to say, as applications follow, multiple applications cannot directly share the cache. In the case of Application Clusters, this problem becomes more obvious, of course, some cache components provide the function for cluster nodes to notify each other of cache updates. However, this function is broadcast or loop update, when the cache is updated frequently, the network I/O overhead will be very high, and the normal operation of the application will be affected in severe cases. in addition, if the cache contains a large amount of data, using localcache means that each application has such a large cache, which is definitely a waste of memory.
In this case, we often choose remote cache, such as memcached. in this way, in a cluster or distributed environment, each application can share data in memcached. These applications are directly connected to memcached through the socket and memcached protocol based on the TCP/IP protocol, one app updates the value in memcached, and all applications can get the latest value. although there is a lot of network overhead at this time, this solution is usually more common than localcache broadcast or loop update cache nodes, and the performance is higher than the latter. because only one copy of the data needs to be saved, the memory usage is also improved.
From the above analysis, we can see that both local cache and remote cache have their own places in the cache field, therefore, ahuaxuan recommends that you determine the cache to be used accurately based on the cache features and our business scenarios when selecting or using the cache. in this way, the cache function can be fully used.
Ahuaxuan believes that the use of cache is a required skill for architects. A good architect can accurately determine the type of cache to be used based on the data type and business scenario, and how to use this type of cache. there is no silver bullet in the cache world. Currently, there is no cache that can solve any business scenario or data type. If this technology emerges, the architect will be less valuable. haha.
|