Embedded Redis for Spring Boot
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Embedded Redis is mainly useful for local development and automated tests where you want Redis behavior without managing a separate server process by hand. In a Spring Boot project, the usual pattern is to start a lightweight Redis instance before tests run, point Spring at that port, and shut it down automatically afterward.
When Embedded Redis Makes Sense
Using an embedded server keeps tests self-contained. That is helpful when the code under test depends on Redis for caching, distributed locks, or session storage and you want repeatable integration tests.
It is less appropriate for production-like performance testing. Embedded Redis is a convenience tool, not a substitute for a real deployment. If your team needs stronger environment fidelity, a container-based approach is often a better choice.
Wiring Embedded Redis Into Spring Boot Tests
A common setup is to start Redis in a test configuration and inject the chosen port into Spring Boot properties. The exact dependency depends on the embedded Redis library you choose, but the Spring pattern stays the same.
In a test, import that configuration and use the normal Spring Redis APIs:
This gives you a real Redis-backed integration path without requiring developers to install Redis manually.
Property-Based Configuration
If the port cannot be fixed because of conflicts, choose a free port and feed it into Spring properties. The exact mechanism varies, but the goal is always the same: the embedded server and the Spring RedisConnectionFactory must agree on the same host and port.
Keeping that value centralized avoids mysterious test failures where the server is running on one port while Spring is trying to connect somewhere else.
Why Tests Fail With Embedded Redis
Most failures are operational rather than code-level. Typical examples are:
- the port is already in use
- the embedded binary is not compatible with the local operating system
- Redis starts too late and the application context connects too early
- tests share state because keys are not cleaned up between runs
To keep tests reliable, start Redis before the context needs it, use isolated keys, and clear data between test cases when necessary.
Common Pitfalls
The biggest mistake is treating embedded Redis as a production replacement. It is for tests and local convenience, not for deployment.
Another issue is hardcoding a port that conflicts on developer machines or CI workers. When reliability matters, make the port configurable or dynamically allocated.
Developers also forget teardown. If the embedded process is not stopped cleanly, later tests can fail with port-binding errors.
Summary
- Embedded Redis is most useful for local integration tests and development workflows.
- Start the Redis process before Spring Boot creates its Redis connection beans.
- Point Spring at the same host and port used by the embedded server.
- Keep tests isolated by cleaning up keys and avoiding shared mutable state.
- Use a more production-like setup when you need realistic networking or performance behavior.
Related reading
- Enable logical replication on Google Cloud Postgres
- Encrypting the Hadoop Distributed Cache file
- End to end integration test for multiple spring boot applications under Maven
- Entity Listener and caching for distributed system
- EmbeddedCassandra Cannot run unit tests
- Enable binary mode while restoring a Database from an SQL dump
- Enable HAL serialization in Spring Boot for custom controller method
- Enable hibernate filter globally with spring-boot spring-data

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.