Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Learn/LangChain Mastery/Part 7 · 6. Memory & State Management
Lesson 52 of 68

Persistence Layers (Redis / DB)

Surviving the restart. Durability and Scale.

Derpx0610 Jan · 1 min read
LangChain Mastery
Why Memory Breaks SystemsConversationBuffer & Window MemoryConversationSummaryMemoryEntity MemorySession IsolationPersistence Layers (Redis / DB)
Contents
1. Why RAM is Not Enough2. RedisChatMessageHistory3. Databases (Postgres/SQL)4. Common Failure Modes 🚧A. Memory BloatB. Summary DriftC. Agent Loops5. Final Thoughts: Memory Defines Behavior
0% read · 1 min leftBack to top ↑
Course outline & contents0%
LangChain Mastery
Why Memory Breaks SystemsConversationBuffer & Window MemoryConversationSummaryMemoryEntity MemorySession IsolationPersistence Layers (Redis / DB)
Contents
1. Why RAM is Not Enough2. RedisChatMessageHistory3. Databases (Postgres/SQL)4. Common Failure Modes 🚧A. Memory BloatB. Summary DriftC. Agent Loops5. Final Thoughts: Memory Defines Behavior

1. Why RAM is Not Enough

If you store memory in a Python Dictionary (In-Memory), what happens when:

  1. You redeploy your server? -> Memory wiped.
  2. Your server crashes? -> Memory wiped.
  3. You scale to 2 servers behind a Load Balancer? -> Request 1 hits Server A, Request 2 hits Server B (which has no memory).

In-Memory Memory is a toy. Production systems need External Persistence.

2. RedisChatMessageHistory

Redis is the industry standard for LLM memory. It is fast (sub-millisecond) and durable.

redis_memory.py
from langchain_community.chat_message_histories import RedisChatMessageHistory

history = RedisChatMessageHistory(
  session_id="user_123",
  url="redis://localhost:6379"
)

history.add_user_message("Hi")
# Data is now in Redis, not Python RAM.

3. Databases (Postgres/SQL)

For long-term storage (Entity Memory), SQL is better. Redis works well for "Recent Chat History" (Hot Storage) with a TTL (Time To Live). Postgres works well for "User Facts" (Cold Storage) that must survive for years.

4. Common Failure Modes 🚧

Even with persistence, things go wrong.

A. Memory Bloat

You store everything forever. Your Redis bill explodes. Your prompt costs explode. Fix: Set TTLs on session keys (e.g., expire after 24 hours of inactivity).

B. Summary Drift

As you summarize summaries of summaries, the facts distort. "Bob likes coding" becomes "User likes computers." Fix: Periodically refresh/reset summaries based on raw logs.

C. Agent Loops

Agents generate "Intermediate Steps" (Thinking). If you treat those as Memory, you pollute the history with debug logs. Fix: Only store the Final Answer in long-term memory.

5. Final Thoughts: Memory Defines Behavior

LLMs don't remember. Systems do. If you design your memory layer correctly, your AI feels like a trusted companion. If you stick to "Append to List," it feels like a demo.

Memory design is where correctness, privacy, and reliability converge.

Key Intuition: "The brain is transient. The database is eternal."

Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonSession IsolationNext lesson →Designing Tool Schemas