Location of Caching
Client (Browser) Caching
Cache data in browser.
You can do it manually, store things like user info such as profile pic or email. You don’t want to request that very time. Or some data for offline working or faster rendering.
There are libraries doing this automatically, such as Apollo GraphQL Client. It can cache GraphQL queries automatically, so that repetitive requests aren’t made.
Server-Side Caching
Reverse Proxy can cache data.
CDN Caching
Application Caching
There are applications like Redis that cache in memory. Cache Invalidation is required. There are algorithms like LRU.
Database Caching
Databases support caching, to save time from queries.
-
Query Level Caching
- Use query hash as key to cache
- Gets complicated when query is complex, changing a single cell may affect many cache.
-
Object Level Caching primer
Types of Caching
Cache-Aside
graph LR; Client-->Cache Client-->Storage
Write-Through
sequenceDiagram participant Client participant Cache participant DB Client->>Cache: 1. Write to Cache Cache->>DB: 2. Store in DB Cache->>Client: 3. Return to user
Write-Begind (write-back)
Write data to database asynchronously. Good for situations that have super high frequency database write.
sequenceDiagram participant Client participant Cache participant Queue participant Event Processor participant DB Client->>Cache: 1. Write to Cache Cache->>Event Processor: 2. Add to queue (as publisher) Cache->>Client: 3. Return to user Event Processor->>DB: 4. Async: select and execute event (as Consumer)
Refresh-Ahead
graph LR; Cache-->Client Database-->Cache
Database automatically refresh recently accessed cache prior to expiration.
- Reduced Latency
- Drawback: Have to accurately predict which items are going to be queried.