Dragon Pastoral

凯龘牧歌

Understanding Cache vs Cookie vs Session in 100 seconds.

Posted at # Dev

Table of Contents

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 differences much more clearly for sure.

What is Cache vs Cookie vs 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 is a small text file stored by a website on the client-side (user’s browser). It is used to store user-specific information, such as user preferences, login status, and browsing history. Cookies are sent back to the server with each HTTP request, allowing the server to identify the user. Each cookie has an expiration date, after which it is 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 data is stored on the server-side, and a session ID is stored in a cookie on the client-side to identify the user. Sessions have a limited lifetime and are usually destroyed when the user logs out or the session expires.

How do they work?

This example explains how these three work when shopping online:

Conclusion

FeatureCacheCookieSession
LocationBrowserBrowserServer
PurposeSpeed up page load timesPreserve user settings, manage login sessionsMaintain user-specific session data
LifespanUntil manually cleared or expiredSet expiration timeEnds with browser session
CapacityLargeSmall (about 4KB)Large