How Redis Helps You Make Fewer Requests to Your Database
Let me come straight to the point.
If you have ever built a CRUD application, you probably stored a USER ID as a unique identifier for each user. When a user logs in, the backend generates a session ID, and the browser stores that session ID in a cookie.
On the server side, that session ID is associated with the user’s identity.
Conceptually it looks like this:
Browser Cookie
sessionId = abc123Redis
session:abc123 → { userId: 42 }Now whenever the user navigates across the website, the browser automatically sends that session ID with every request.
The Problem Without Redis
Every time a user navigates to another page, the server might have to:
- receive the request
- check the authentication
- query the database
- retrieve the user
- send the response
User → Backend → Database → ResponseDoing this for every request can become expensive and slow.
Think about it this way:
If you had to show your ID card before entering every single room in a building, it would be annoying and inefficient.
How Redis Improves This
Instead of checking the database every time, the server can store session information in Redis.
When the user logs in:
Create sessionId
Store in RedisExample:
session:abc123 → { userId: 42 }Now when the user navigates to another page:
- browser sends the session ID
- backend checks Redis
- Redis returns the user ID
- request proceeds immediately
User → Backend → Redis → ResponseNo database query required.
This is similar to getting a verification stamp on your ID card.
After the first verification, security only checks the stamp instead of scanning your entire identity again.
Redis Also Helps With Caching
Redis is not only useful for sessions. It is widely used for caching frequently accessed data.
Get Jainananya’s stories in your inbox
Join Medium for free to get updates from this writer.
For example:
- homepage products
- trending items
- products on sale
- user shopping carts
Redis stores data as key-value pairs.
Example:
product:123 → { name: "Running Shoes", price: 2999 }If thousands of users visit the same product page, the application can fetch the data directly from Redis instead of hitting the database repeatedly.
User → Backend → Redis → ResponseThis reduces database load and significantly improves response time.
Example Redis Code (Node.js)
Here is a simple example of storing and retrieving sessions with Redis.
Connecting to Redis
import { createClient } from "redis";const redis = createClient();await redis.connect();
Storing a Session
When the user logs in:
const sessionId = "abc123";
const userId = 42;await redis.set(`session:${sessionId}`, userId, {
EX: 3600
});This stores the session in Redis for 1 hour.
Checking the Session
On each request:
const sessionId = req.cookies.sessionId;const userId = await redis.get(`session:${sessionId}`);if (userId) {
req.userId = userId;
next();
} else {
res.status(401).send("Unauthorized");
}
If Redis returns a user ID, the request proceeds as an authenticated user.
Example Redis Cache for Products
Caching a product query:
app.get("/product/:id", async (req, res) => {
const productId = req.params.id;const cached = await redis.get(`product:${productId}`); if (cached) {
return res.json(JSON.parse(cached));
} const product = await db.products.findById(productId); await redis.set(
`product:${productId}`,
JSON.stringify(product),
{ EX: 300 }
); res.json(product);
});
Now the first request hits the database, but later requests are served from Redis.
Expensive server RAM?
Redis uses RAM, which makes it more expensive per GB compared to traditional disk-based storage. However, the trade-off is speed. By serving frequently accessed data directly from memory, Redis reduces database load and drastically improves response times. In high-traffic systems, this often results in lower overall infrastructure costs because the database no longer needs to handle every request. In short, we pay a little more for memory so that users don’t have to wait in long queues for every page load.
So now do you think Redis is worth adding to your architecture?






