Thursday, April 24, 2014

Difference between Cache and Buffer

Key difference: A cache transparently stores data so that future requests for that data can be served faster. A buffer, on the other hand, temporarily stores data while the data is the process of moving from one place to another.

Both cache and buffer are types of temporary storage that are utilized in computer science. However, they differ in the methods and the capabilities in which they are used. A cache transparently stores data so that future requests for that data can be served faster. A buffer, on the other hand, temporarily stores data while the data is the process of moving from one place to another, i.e. the input device to the output device.

There are two main types of caches, memory caching and disk caching. Memory caching is when the cache is part of the main memory, whereas disk caching is when the cache is part of some other separate storage area, such as a hard disk. Caching is the process of storing data in a cache so that the data can be accessed faster in the future. The data that is stored within a cache might be values that have been computed earlier or duplicates of original values that are stored elsewhere. When some data is requested, the cache is first checked to see whether it contains that data. The data can be retrieved more quickly from the cache than from its source origin.

An easy example to understand caching is to look at web caching. A web cache is a mechanism for the temporary storage (caching) of web documents, such as HTML pages and images. This is mainly done to reduce bandwidth usage, server load, and perceived lag. When a web page is loaded, the data on the pages is cached; hence the next time the page is loaded it is quicker, as data is already present, and only the changes made to the page need to be loaded, which are in turn cached for next time. Google's cache link in its search results provides a way of retrieving information from websites that have recently gone down and a way of retrieving data more quickly than by clicking the direct link.

The buffer, on the other hand, is found mainly in the RAM and acts as an area where the CPU can store data temporarily. This area is used mainly when the computer and the other devices have different processing speeds. Typically, the data is stored in a buffer as it is retrieved from an input device (such as a mouse) or just before it is sent to an output device (such as speakers). However, the buffer may also be used when moving data between processes within a computer.

So, the computer writes the data up into a buffer, from where the device can access the data, as its own speed. This allows the computer to be able to focus on other matters after it writes up the data in the buffer; as oppose to constantly focus on the data, until the device is done.

Buffers can be implemented in a fixed memory location in hardware or by using a virtual data buffer in software, which points to a data buffer are stored on a physical storage medium. Majority of the buffers are utilized in the software. These buffers typically use the faster RAM to store temporary data, as RAM has a much faster access time than hard disk drives. A buffer often adjusts timing by implementing a queue or FIFO algorithm in memory. Hence, it is often writing data into the queue at one rate and reading it at another rate.

A common example of this is streaming videos online, such as YouTube. While, watching a video on YouTube, one may notice that a gray bar tends to load before the red bar of the video stream can play. The gray bar is the buffer. It downloads the data of the video and saves it so that the video may play at an uninterrupted rate. As you might have noticed that when the red bar catches up to the gray bar, the video stops, in order to load the rest of the video.

Buffers are also often used with I/O to hardware, such as disk drives, sending or receiving data to or from a network, or playing sound on a speaker. Buffers are used for many purposes, such as interconnecting two digital circuits operating at different rates, holding data for use at a later time, allowing timing corrections to be made on a data stream, collecting binary data bits into groups that can then be operated on as a unit, and delaying the transit time of a signal in order to allow other operations to occur.

However, a buffer cannot be used to instantaneously move your location in the data stream, unless the new part has already been moved to the buffer. Similar to the YouTube video, which cannot be forwarded to a part that is not covered by the gray bar.  If you do, the buffer will relocate and restart from the new location.

Still, the functions of a cache and buffer are not mutually exclusive and are often combined for an ideal performance.