Write Back (Write Behind)

Cache is the one that sync to the database directly

sequenceDiagram
    participant App as Application
    participant Cache as Cache
    participant DB as Database (Master)

    Note over App, Cache: Instant Response
    App->>Cache: 1. Write Data
    Cache-->>App: 2. Confirm Success (Immediately!)
    
    Note over Cache, DB: Background Sync (Later)
    Note right of Cache: Data is "Dirty" (not yet in DB)
    Cache->>DB: 3. Eventually Write/Sync Data
    DB-->>Cache: 4. Confirm Success

Pros

  • Fast since writing to ram is much faster than writing to disk
    Cons:
  • Single point of failure:
    • If cache is down then database will lost data
  • DB has stale data