Our Java application had a performance problem. Every request hit the database, even for data that changed once an hour. Redis as a cache layer reduced response time from 200 ms to 2 ms and offloaded 95% of queries from the database.
Why Redis, not Memcached¶
Redis does more than key-value — hashes, lists, sets, sorted sets. It also offers persistence (RDB, AOF), replication, and Sentinel for HA. More than a cache — a legitimate database for specific use cases.
Cache for the REST API¶
public List<Product> getProducts() {
String cached = jedis.get("products:all");
if (cached != null) {
return objectMapper.readValue(cached, productListType);
}
List<Product> products = productRepository.findAll();
jedis.setex("products:all", 3600, objectMapper.writeValueAsString(products));
return products;
}
Session store and rate limiting¶
With Spring Session, Redis as a session store is trivial — one annotation. Sessions survive a restart and are shared across instances. Redis INCR+EXPIRE for rate limiting: key = IP + minute, limit 100 req/min.
Operational tips¶
- Set maxmemory with the allkeys-lru eviction policy
- Do not use KEYS * in production — use SCAN
- Set TTL on cache keys
- Monitor the memory fragmentation ratio
Redis belongs in every stack¶
Simple to operate, fast, versatile. As a cache, session store, or message broker — Redis has a place in every application that needs speed.
Need help with implementation?
Our experts can help with design, implementation, and operations. From architecture to production.
Contact us