Redis caching concepts and techineques used in full stack application
Redis is an in-memory data store that stores frequently accessed data in RAM (Random Access Memory) instead of retrieving it from the database every time.
When a client sends a request, fetching data directly from the database for every request can increase response time and database load. To improve performance, we use Redis as a caching layer.
Caching concepts and techineques used in full stack application
- > Cache Hit
- A Cache Hit occurs when an application requests data from Redis and the requested data is already available in the Redis cache.
The data is stored in redis
await redis.set( "product:101",
JSON.stringify({
id: 101,
name: "Laptop",
price: 50000
})
);- > Cache Miss
- A Cache Miss occurs when an application requests data from Redis, but the requested data is not available in the Redis cache.
- Backend queries to the Data base and DB send the data to the server and backend store the data in the Redis backend sends the response to the frontend.
> TTL (Time To Live )
In Redis, TTL defines how long a key should remain available in the cache before it automatically expires.
A. SETEX
Stores a value with an expiration time.
SETEX product:101 300 "Laptop"B. SET with EX
SET product:101 "Laptop" EX 300C. EXPIRE
SET product:101 "Laptop"
EXPIRE product:101 300D. PEXPIRE
PEXPIRE product:101 5000> Cache Evaction
Get Rohith’s stories in your inbox
Join Medium for free to get updates from this writer.
When Redis memory becomes full, it needs to decide which cached data should be removed to make space for new data. This process is called Cache Eviction.
>Cache Invalidation
Cache Invalidation is the process of removing or updating outdated data stored in Redis when the original data in the database changes.
> Cache Aside
Cache-Aside is a caching technique in which the application itself manages the cache.
The application first checks whether the requested data exists in Redis. If the data is available, it returns it directly. Otherwise, it fetches the data from the database, stores it in Redis, and returns it to the user.
>Write through(Update Cache and DB together)
Write-Through Caching is a caching technique in which the application updates both the database and the cache whenever data is created or modified.
> Write Behind (Update Cache DB later)
Write-Behind Caching, also called Write-Back Caching, is a caching strategy in which the application writes data to the cache first and updates the database in the background.
>LRU (Least recently used)
LRU is a cache eviction technique that removes data that has not been accessed for the longest period when Redis needs to free memory.
>LFU(Least frequently used)
LFU stands for Least Frequently Used. It is a cache eviction strategy that removes the keys that have been accessed the fewest times when Redis needs to free memory.
> Redis Peristance
Persistence means saving Redis data to disk so that data can survive a Redis restart or crash.
Normally, Redis stores data in RAM, which makes it very fast. But RAM is volatile. If Redis shuts down unexpectedly, data can be lost unless persistence is configured.
Redis supports two main persistence mechanisms:
A. RDB (Redis Database Backup)
RDB creates a snapshot of your Redis data at a particular point in time and saves it to a file.
Redis Memory
|
| Snapshot
v
dump.rdb fileB. AOF (Append Only File)
AOF records write operations performed on Redis.
>Redis Replication
Replication means maintaining copies of data from one Redis server on one or more other Redis servers.



