กลยุทธ์การแคชในหน่วยความจำและภายนอก
เปรียบเทียบแนวทางการแคชแบบต่าง ๆ ตั้งแต่แคชในหน่วยความจำอย่างง่ายไปจนถึงโซลูชันภายนอกที่มีความทนทาน เช่น Redis
กลยุทธ์การแคชในหน่วยความจำและภายนอก เป็นบทเรียน LLM Apps in Production (RAG + Vector DB + Caching) ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน LLM Apps in Production (RAG + Vector DB + Caching) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส LLM Apps in Production (RAG + Vector DB + Caching) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Intro to Caching Strategies
Caching is vital for making LLM applications faster and more cost-effective. But not all caches are built the same!
In this lesson, we'll dive into two primary strategies: in-memory caching and external caching. Each has unique benefits and drawbacks depending on your application's needs.
In-Memory Caching: The Basics
In-memory caching means storing data directly within your application's Random Access Memory (RAM). Think of it like a temporary notepad your app keeps handy.
- Speed: Accessing data from RAM is incredibly fast.
- Simplicity: Often easy to set up, using built-in language features (like dictionaries or hash maps).
- No External Dependencies: Your app doesn't need to connect to another service.
Simple In-Memory Cache (Python)
Here's a basic Python example using a dictionary to simulate an in-memory cache for LLM responses. Notice how subsequent requests for the same prompt hit the cache.
cache = {}
def get_llm_response(prompt):
if prompt in cache:
print("Cache hit!")
return cache[prompt]
else:
print("Cache miss, calling LLM...")
# Simulate a slow LLM call
response = f"LLM response for: {prompt}"
cache[prompt] = response
return response
if __name__ == "__main__":
print(get_llm_response("What is RAG?"))
print(get_llm_response("What is RAG?"))
print(get_llm_response("Tell me a joke."))
print(get_llm_response("Tell me a joke."))Limitations of In-Memory Caches
While fast and simple, in-memory caches have significant drawbacks for production LLM applications:
- Ephemeral Data: All cached data is lost if your application restarts or crashes.
- Limited Scale: Each instance of your application has its own separate cache. If you run multiple servers, they won't share data, leading to duplicated work.
- Memory Usage: Large caches can consume a lot of RAM, potentially impacting your application's overall performance.
Introducing External Caching
To overcome the limitations of in-memory caches, we use external caching solutions. These store cached data outside your application, typically in a dedicated server or service.
This allows multiple instances of your application to access and share the same cached data, making it ideal for scalable, distributed systems.
Redis: A Popular External Cache
Redis (Remote Dictionary Server) is a popular open-source, in-memory data store. It's widely used as a cache, database, and message broker due to its high performance and versatile data structures.
It's an excellent choice for external caching in LLM applications because it's incredibly fast and designed for network-based access.
Advantages of External Caching
External caches like Redis provide several key advantages:
- Distributed: Multiple application instances can share a single, consistent cache.
- Persistent: Data can be configured to be saved to disk, so it survives application or cache server restarts.
- Scalable: The cache can be scaled independently of your application, handling massive amounts of data and requests.
- Rich Features: Redis offers advanced features like Time-To-Live (TTL) for automatic cache expiration and various data structures.
Using Redis for LLM Caching (Python)
Here's how you might interact with Redis from Python using the redis-py library. This code assumes a Redis server is running locally on localhost:6379.
It demonstrates setting a key with an expiration (TTL) and retrieving it.
import redis
import json
# Connect to Redis. Ensure a Redis server is running!
# e.g., on Docker: docker run --name my-redis -p 6379:6379 -d redis
try:
r = redis.Redis(host='localhost', port=6379, db=0)
r.ping() # Check connection
print("Connected to Redis successfully!")
except redis.exceptions.ConnectionError as e:
print(f"Could not connect to Redis: {e}")
print("Please ensure a Redis server is running on localhost:6379")
r = None # Set r to None if connection fails
def get_llm_response_from_redis(prompt):
if r is None:
return {"error": "Redis not connected, cannot cache."}
cache_key = f"llm_response:{prompt}"
cached_data = r.get(cache_key)
if cached_data:
print("Redis Cache hit!")
return json.loads(cached_data.decode('utf-8'))
else:
print("Redis Cache miss, calling LLM...")
# Simulate LLM call and create a response structure
response_data = {"text": f"LLM response for: {prompt}", "source": "LLM"}
# Cache the response for 3600 seconds (1 hour)
r.setex(cache_key, 3600, json.dumps(response_data))
return response_data
if __name__ == "__main__":
print(get_llm_response_from_redis("What is the capital of France?"))
print(get_llm_response_from_redis("What is the capital of France?"))
print(get_llm_response_from_redis("Who invented the light bulb?"))
print(get_llm_response_from_redis("Who invented the light bulb?"))Choosing the Right Caching Strategy
Your choice of caching strategy depends on your application's requirements:
- Use In-Memory Caches if:
Your application runs as a single instance, data loss on restart is acceptable, or you're caching very small, temporary datasets. - Use External Caches (e.g., Redis) if:
You need distributed caching across multiple application instances, data persistence is critical, your dataset is large, or you require advanced caching features and scalability.
For most production LLM applications, external caching is the robust choice.
Caching Strategy Quiz
Test your understanding of caching strategies!
Recap: Cache Your Knowledge
We've explored the two main caching strategies for LLM applications:
- In-memory caches are fast and simple but are limited to a single application instance and lose data on restart.
- External caches like Redis offer persistence, distributed sharing, and independent scalability, making them ideal for robust production LLM systems.
Choosing the right strategy depends on your application's scale, data persistence needs, and operational complexity.
คำถามที่พบบ่อย
บทเรียน “กลยุทธ์การแคชในหน่วยความจำและภายนอก” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “กลยุทธ์การแคชในหน่วยความจำและภายนอก” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส LLM Apps in Production (RAG + Vector DB + Caching) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส LLM Apps in Production (RAG + Vector DB + Caching) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “กลยุทธ์การแคชในหน่วยความจำและภายนอก”
เปรียบเทียบแนวทางการแคชแบบต่าง ๆ ตั้งแต่แคชในหน่วยความจำอย่างง่ายไปจนถึงโซลูชันภายนอกที่มีความทนทาน เช่น Redis คุณปฏิบัติ LLM Apps in Production (RAG + Vector DB + Caching) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน LLM Apps in Production (RAG + Vector DB + Caching) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน LLM Apps in Production (RAG + Vector DB + Caching) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “กลยุทธ์การแคชในหน่วยความจำและภายนอก” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน LLM Apps in Production (RAG + Vector DB + Caching) นี้ได้ไหม
ได้ บทเรียน LLM Apps in Production (RAG + Vector DB + Caching) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ความสำคัญของการแคชการเรียกใช้ LLM
- กลยุทธ์การแคชในหน่วยความจำและภายนอก
- การผสานการแคชเข้ากับไปป์ไลน์ RAG
- การแคชเชิงความหมายสำหรับแอป LLM