AlgoViz

Write-through

medium 22 min

With write-through, a save goes to the cache and to the database in the same request, and the caller hears success only when both are done.

See it run

Every save goes to the cache and to the database before the caller hears that it worked.

Operations

Write, then read

Nine steps: `user:123` is saved through the cache layer, then read back from it.

Step 1 of 9

1. A write request

Our systemCallerAppCache layerDatabaseJohn Doe

The app is asked to change user:123 from John Doe to Jane Doe.

Step 1: 1. A write request

def write(key, value):    cache.set(key, value)         # 1 ms    try:        db.write(key, value)      # 10 ms, we wait    except DbError:        cache.delete(key)         # take it back        raise    return "saved"def read(key):    return cache.get(key) or load(key)
Caller waits
11 ms, then 1 ms
Writes
2 per save
Write-back
1 ms, but the database is later

The two writes are not one transaction. Say how you handle one working and the other not.

In the real world

The same write-through doing real jobs. Pick one and step through it.

Someone changes their settings and the page reloads at once.

Step 1 of 3

Save

Our systemCallerAppCache layerdarkDatabasedark

Dark mode is switched on.

Step 1: Save

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Reading only from the cache

    critical

    Entries are evicted, expire, or were written before the cache existed. A missing copy is not a missing user.

    Wrong
    def read(key):    return cache.get(key)         # None: user not found?
    Right
    def read(key):    value = cache.get(key)    return value if value is not None else load_and_fill(key)
  • Writing around it

    critical

    One script that updates the database directly leaves the old copy in the cache.

    Wrong
    db.execute("UPDATE prices SET ...")   # nightly job
    Right
    for key, value in changes:    write_through(key, value)         # same door for all
  • Calling it a transaction

    major

    Two systems, two writes. A crash between them leaves them different.

    Wrong
    # always consistentcache.set(k, v); db.write(k, v)
    Right
    db.write(k, v)cache.set(k, v, ttl=300)   # bounded if the second is lost
  • Using it for counters that change all the time

    minor

    Every increment pays for a database write. The database is still the limit.

    Wrong
    write_through("views:7", views + 1)   # 5,000 a second
    Right
    write_back.incr("views:7")            # flushed in batches

Interview tips

  • How to explain it

    • A save goes to the cache and the database in one request.
    • The caller waits for both.
    • The next read finds the new value in the cache.
  • Memory trick: BOTH

    • Before returning, write to the cache.
    • Only then write to the database.
    • Together they must succeed.
    • Hot cache after every save.
    • If your own code makes both writes, swap the first two.
  • Reach for it when

    • People read what they just saved.
    • Reads outnumber writes and must be fresh.
    • One layer can own all writes.
  • Move on when

    • Writes are many and reads are few.
    • The save must be very fast.
    • Other programs write to the database directly.