Redis in Spring Boot: Caching and Locks in Production

Redis makes Spring Boot APIs faster until load, expiry or lock timing changes. These articles cover cache behaviour under real traffic, expiry-driven latency spikes, memory growth from caches without limits, and distributed locks that expire while the job is still running.

All articles on this page are also collected in a Medium list you can save and follow.

Start here

Distributed locks

Caching under load

Setup and fundamentals

Also on bytzecho.com

Versions of these articles on this site, with extended code and notes.

Frequently asked questions

What happens when a Redis lock expires while the job is still running?

Redis removes the key when its TTL ends, so a second instance can acquire the lock and run the same job concurrently. Renew the lease only while the original owner holds it, make the job’s writes idempotent, or use fencing tokens when stale writes would corrupt data. See Our Redis Lock Expired. The Report Job Kept Running.

Why can Redis caching make a Spring Boot API slower?

Every cache lookup adds a network round trip and serialization. When hit rates are low or cached values are large, that overhead can exceed the cost of the original query. Measure hit rate and latency before and after adding the cache.

Why do cache expirations cause latency spikes?

When many keys share the same TTL they expire together, and every request falls through to the database at once. Adding random jitter to TTLs and letting one request rebuild a missing entry spreads that load out.

New articles are published on Medium first. Subscribe to the newsletter to get production lessons by email.