Spring / Spring AI interview questions
How does conversation memory work in Spring AI?
Conversation memory in Spring AI gives a ChatClient awareness of what was said earlier in a session — the model receives prior turns as part of every new request without the caller manually tracking message history. Without memory, every call is completely stateless from the model's perspective.
The mechanism is the ChatMemory interface, which stores and retrieves lists of Message objects keyed by a conversation ID. The MessageChatMemoryAdvisor uses this interface in its request hook to prepend stored messages, and in its response hook to save the new exchange.
Built-in ChatMemory implementations:
- InMemoryChatMemory — stores history in a JVM Map. Fast, no dependencies, but lost on restart and not shareable across pods.
- JdbcChatMemory — persists to any JDBC-compatible database (H2 for tests, Postgres/MySQL for production).
- CassandraChatMemory — persists to Apache Cassandra for high-throughput scenarios.
- Neo4jChatMemory — stores conversation graphs in Neo4j.
ChatMemory memory = new InMemoryChatMemory();
ChatClient client = ChatClient.builder(chatModel)
.defaultAdvisors(new MessageChatMemoryAdvisor(memory))
.build();
String sessionId = "user-42";
// Turn 1
client.prompt()
.advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, sessionId))
.user("My favourite language is Kotlin.").call().content();
// Turn 2 — model remembers turn 1
String reply = client.prompt()
.advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, sessionId))
.user("What language did I mention?").call().content();
// reply: "You mentioned Kotlin."The conversation ID is the multi-user isolation key. Each user session gets a unique ID; the memory store returns only that session's history, so conversations never bleed into each other.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
