0
0
Supabasecloud~10 mins

Caching strategies in Supabase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Caching strategies
Client Request
Check Cache
Return
Store in Cache
Return Data
When a client requests data, the system first checks the cache. If data is found (cache hit), it returns immediately. If not (cache miss), it fetches from the database, stores the result in cache, then returns it.
Execution Sample
Supabase
cache = {}
key = 'user:123'
if key in cache:
  data = cache[key]
else:
  data = db_fetch(key)
  cache[key] = data
This code checks if user data is in cache; if not, it fetches from the database and caches it.
Process Table
StepActionCache StateData SourceResult
1Check if 'user:123' in cache{}N/ACache miss
2Fetch 'user:123' from DB{}DatabaseData fetched
3Store 'user:123' data in cache{'user:123': 'UserData'}N/ACache updated
4Return data to client{'user:123': 'UserData'}CacheData returned
5Next request: Check cache{'user:123': 'UserData'}N/ACache hit
6Return cached data{'user:123': 'UserData'}CacheData returned
💡 Execution stops after data is returned to client on cache hit or miss.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 6
cache{}{}{'user:123': 'UserData'}{'user:123': 'UserData'}
dataundefinedUserDataUserDataUserData
Key Moments - 3 Insights
Why do we check the cache before the database?
Checking the cache first (see Step 1 in execution_table) avoids slower database calls if data is already stored, improving speed.
What happens if data is not in the cache?
If cache miss occurs (Step 1), data is fetched from the database (Step 2) and then stored in cache (Step 3) for future requests.
Does the cache update every time we fetch data?
No, the cache updates only on a miss (Step 3). On a hit (Step 5), data is returned directly without updating cache.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the cache state after Step 3?
A{}
Bundefined
C{'user:123': 'UserData'}
D{'user:124': 'UserData'}
💡 Hint
Check the 'Cache State' column in execution_table at Step 3.
At which step does the system fetch data from the database?
AStep 2
BStep 1
CStep 5
DStep 6
💡 Hint
Look at the 'Action' column in execution_table to find when DB fetch happens.
If the cache already contains 'user:123', which step returns data directly from cache?
AStep 2
BStep 5
CStep 3
DStep 1
💡 Hint
Check the 'Result' column for cache hit and data return.
Concept Snapshot
Caching strategies:
- Check cache first for requested data
- If cache hit, return data immediately
- If cache miss, fetch from database
- Store fetched data in cache
- Improves speed by reducing database calls
- Common in cloud apps like Supabase
Full Transcript
Caching strategies help speed up data access by storing data temporarily. When a client asks for data, the system first looks in the cache. If the data is there (cache hit), it returns it quickly. If not (cache miss), it fetches from the database, saves it in the cache, then returns it. This reduces slow database calls and improves performance. The example code shows checking the cache, fetching from the database if needed, and updating the cache. The execution table traces these steps clearly. Key moments include understanding why cache is checked first, what happens on a miss, and when cache updates occur. The quiz tests understanding of cache state and data flow steps.