Redis Use Case: Session Caching and Storing

Leave your reply

Introduction

This use case centers around using Redis to cache website visitor session data. Redis is an excellent choice for session caching, because it is fast and light, and unlike other session store software platforms, Redis offers persistence.

Related articles:

Use Case: E-Commerce Site

For this use case, it is planned to build an e-commerce website. Within the scope of this project, the visitor's shopping cart should have persistence, so that it will persist if the visitor leaves the site.

The user is also interested in leveraging Redis's ability to cache sessions as a way to improve website performance.

Redis: Caching vs Storing

Broadly speaking, "caching" and "storing" refer to the same process: information is saved in Redis, in order to improve the user's experience. However, there are some subtle but key differences between these two methods.

Caching

Caching is ephemeral, and is not dependent on the user logging into the site. Caching is often used to speed up web page performance, by creating a cache of everything users see during their visit (HTML pages, scripts, image files, etc.).

When the user accesses a page, the web server first checks the cache to see if the files are stored there. If so, the server serves the files from the cache. If not, the server pulls the files from the web server.

Session caching typically only lasts for the visitor's session, and does not persist after the visitor leaves the site.

Storing

Stored session data is persistent, and requires the user to log into the site. Stored sessions are often use for functions like saving a user's place in a large file, or saving shopping cart data. Session data can also include personalized themes, lists of recently-viewed items, user-targeted promotional content, and other user-specific website components.

Scenario 1: Saving a Shopping Cart

Shopping carts are a classic example of session storing. In order to save a visitor's shopping cart, the visitor will need to log in to the site. After the visitor logs in, all of their shopping history can be stored and presented as needed.

Because e-commerce shopping carts need to have persistence, but will not need to be permanently stored, a flexible key-value store like Redis is a much better choice over a traditional relational database like MySQL.

Scenario 2: Improving Session Speed

The user's website is very image-heavy, and includes several client-side scripts which can be slow to load content. In order to increase their website's loading time, the user decides to implement session caching in Redis.

Redis is frequently used for session caching, as explained in this tutorial on the official Redis website. Redis offers speed and flexiblity, and is a particularly good choice over a traditional database because the session cache information is destroyed as soon as the session ends.