Module 8 Lesson 4: External Memory (Redis & Postgres)
Production State. How to move your memory from local RAM to persistent databases for multi-user applications.
External Memory: Scales for the World
If you store message history in a Python variable (memory = ...), that memory dies as soon as your script stops. In a real website, your server might restart, or you might have 10,000 users. You need a Database to store the state.
1. The Redis Advantage
Redis is an in-memory database that is extremely fast. It is the most popular choice for "Hot" conversation state.
from langchain_community.chat_message_histories import RedisChatMessageHistory
# Every conversation gets a unique key
history = RedisChatMessageHistory(
session_id="user_123_conversation_1",
url="redis://localhost:6379"
)
history.add_user_message("Hi!")
print(history.messages)
2. Using Session IDs for Isolation
In your backend code, you typically extract the session_id from a cookie or a JWT token. You then initialize the LangChain memory using that ID. This ensures the agent only "remembers" what happened with that specific user.
3. Visualizing Distributed Memory
graph TD
U1[User A] --> API[FastAPI Server]
U2[User B] --> API
API -->|Session 1| R[(Redis DB)]
API -->|Session 2| R
R -->|Memory 1| LLM[Agent Brain]
4. Why use Postgres?
If you want the memory to last for years (e.g., a personal companion bot), Redis might be too expensive or risky. Use Postgres (SQL) for long-term, cold storage of chat history.
5. Engineering Tip: TTL (Time to Live)
Most chat history has an "Expiration Date." In Redis, you can set a TTL of 24 hours. If the user doesn't come back in a day, the memory is deleted automatically. This saves space and improves privacy.
Key Takeaways
- Python-local memory is only for testing; never for production.
- Redis is the standard for fast, ephemeral chat history.
- Session IDs are mandatory for separating user conversations.
- TTL helps manage database size and data privacy.