Understanding Cache vs Cookie vs Session in 100 seconds.
Published: 2024-04-13 21:54:50

## Table of Contents

1. **At the beginning**
2. **What is Cache vs Cookie vs Session?**
3. **How do they work?**
4. **Conclusion**

## At the beginning
Have you tried to figure out the difference between Cache vs Cookies vs Session? The operations of these three objects are quite similar. However, what precisely are they, what issues are they addressing, and how are they unique? In this article, It makes you understand those difference much clearly for sure.


## What is Cache, Cookies, Session?
### Cache
Cache is a temporary storage used by web browsers or web servers to store frequently accessed data. The purpose of cache is **to improve performance by reducing the time needed to fetch data**. Cache data is stored locally and is automatically cleared **after a certain period of time**. Cache does not store any user-specific information.


### Cookie
Cookie is small text file stored by a website on the **client-side (user's browser)**. Cookie is used to store **user-specific information**, such as user preferences, login status, and browsing history. Cookie is sent back to the server with each HTTP request, allowing the server to identify the user. Cookie has an **expiration date**, after which they are automatically deleted.


### Session
Session is a way **to maintain state between a user's interactions with a web application**. It is typically used to store user-specific information, such as login status, shopping cart contents, and user preferences.
Session datas are stored on **the server-side**, and a session ID is stored in a cookie on the client-side to identify the user. It has a **limited lifetime** and are usually destroyed when the user logs out or the session expires.


### The difference between Cache and Cookie
* **Cache**’s function is **to make the web page load faster** while **Cookie**’s function is **to track user’s different browsing activities**.
* **Cache** is composed of **files, images and videos or something of the web pages** you visited. **Cookie** stores **information related to the user’s web browsing activity** like user’s preferences, login information, etc.

### The difference between Cookie and Session
* Both Cookie and Session contain **user information**, but **Cookie** is stored at **the client side** while **Session** is stored on **the server-side**.
* **Cookie** expire **after a period of time** while **Session** end **when a user closes the browser**.
* The maximum size of **Cookie** is **4KB**. However, there is **no limit on the size or number** of **Session**.


## How do they work?
This is an example when you are doing shopping online.
Let's try to think about it.

Before you were doing shopping using one e-commerce website. Now, you want to use this e-commerce website for searching clothes again. When you open the website, it can be displayed earlier than before. It is because of **Cache**. Cache can be stored temporary the data which you see before to improve the performance.

After that you try to login in the app and realize that it has already inputed your account information on login form. It is because of **Cookie**. The website is stored the user information on the user browser.

When you start doing shopping, the product which you choose is stored **Session**. That's how this information can be stored while you are seeing the different page. But after you close the page, it will disappear.


## Conclusion
| **Feature** | **Cache** | **Cookie** | **Session** |
|---------|---------|---------|---------|
| **Location** | Browser | Browser | Server |
| **Purpose** | Speed up page load times | Preserve user settings, manage login sessions | Maintain user-specific session data | 
| **Lifespan** | Until manually cleared or expired | Set expiration time | Ends with browser session | 
| **Capacity** | Large | Small (about 4KB) | Large |