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.
More Related questions...