What is the difference between buffering and caching? Cache and Buffer are both caches, what is the main difference

Author: Shen Wanma
Link: https://www.zhihu.com/question/26190832/answer/146259979
Source: Zhihu
The copyright belongs to the author, please contact the author for authorization.

 

Two English words, don't you look at the original meaning first? Although they are all abstract words, they have concrete applications everywhere.

Buffer common is this:



 
(Source: train stop buffer bumper )

Yes, it's something like that huge spring at the end of the railroad. The function is that in case the car does not stop, the speed of the collision spring will slow down and the danger will be less. called buffering .

Cache common is this:



 

(Source: kipedia )


Yes, it is a safe deposit box. See the rusted Food Cache on the right? This is a safe deposit box for emergency supplies deployed in the forest. The function is to put the things you need in easier reach. Although the accurate translation is often called cache , I personally think that the meaning is not expressed correctly, and half of the functions are lost. Taiwanese translation is better, called Cache .

I believe that after reading these, I don't need to tell the difference?

Oops, let's show it off.

Simply put, the core role of Buffer is to buffer and soften shocks. For example, you have to write to the hard disk 100 times per second, which has a great impact on the system, and wastes a lot of time to deal with the two things of starting and ending writing. Use a buffer to temporarily store it into a hard disk write every 10 seconds, the impact on the system is very small, the writing efficiency is high, and the life is pleasant. Greatly softened the impact.

The core function of Cache is to speed up the access speed. For example, if you have finished a very complicated calculation, and you want to use the result next time, you should put the result in a handy place and save it, so you don’t need to do the calculation next time. Speed ​​up data retrieval.

Therefore, if you pay attention to the storage system, you will find that the names of the read and write buffer/cache of the hard disk are different, called write-buffer and read-cache. The difference between the two is clearly stated.

Of course, in many cases, the two may be mixed. For example, in fact, many people use memcached for reading and writing. Quite a few times the same goes for Non-SQL databases. Strictly speaking, the L2 and L3 caches in the CPU are also both read and write - because you can't simply define whether the CPU uses them to read or write. The hard disk is also a typical example. Both buffer and cache are in the same space. Is it buffer or cache?

But think about it carefully, you said that it is OK to use the cache as a buffer? Of course, as long as the cache elimination logic can be controlled, there is no problem. So what about using buffer as cache? It seems that in very special cases, when the access order can be determined, it is also possible. Just think about it - does a buffer need random storage by definition? Generally not needed. But the cache must be. So most of the time you can use cache instead of buffer, and vice versa is more limited. This is technically the key difference between cache and buffer.

——————
Supplement 1: Do not misunderstand that Buffer is for writing, and Cache is for reading. Can I use Buffer for reading? Of course you can. For example, you can use read buffers when you want to process reads in batches instead of processing anything. Of course, you can also use cache for writes, such as when your writes have high randomness. Which scenario to use Buffer and which scenario to use Cache depends on the specific needs of the scenario.

Supplement 2: Do not misunderstand that Cache or Buffer must be memory or something on a high-speed medium. As long as it is relatively fast. I can completely store the cache on the hard disk. For example, some games will create a precompiled shader (exposure age) at runtime, which is essentially a cache, which exists on a slow hard disk, because reading the hard disk is still more than recompiling. quick. The same is true for Buffer. For example, the NTFS file system itself has a Logging Buffer, which even explicitly refuses to be placed in any volatile cache.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326455852&siteId=291194637