Design patterns and optimization · Intermediate

Write-through caching

Define what a synchronous cached write acknowledges and reason about partial failure across cache and origin.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Define the acknowledgement boundary
  2. Trace a partial failure
  3. Account for every writer and reader
  4. Evaluate latency and availability costs
  5. Ask Logeia about this articleCheck your understanding

Define the acknowledgement boundary

A write-through abstraction propagates a write to its backing store synchronously rather than acknowledging only an in-memory update and persisting later. The intended benefit is a clear successful-write boundary that includes the origin operation. This should not be confused with an automatic distributed transaction. If cache and database are independent services, their state transitions can still be separated by network failures. Read the actual provider contract instead of inferring atomicity from the pattern name.

Trace a partial failure

Consider changing a balance from 100 to 80. The database commits, but the subsequent cache update fails. The database may now contain 80 while the cache still exposes 100. Repeating an instruction to subtract 20 can charge twice unless the operation is idempotent. A request identifier, invalidation policy and reconciliation procedure address different parts of this problem. An error response alone does not prove that the database transaction was rolled back.

Account for every writer and reader

The synchronous wrapper can maintain only the writes that pass through it unless another invalidation mechanism exists. Administrative tools, batch jobs and direct SQL updates may bypass it. Track those writers or arrange change events and bounded expiry to repair stale entries. Likewise, a successful origin write does not establish immediate visibility through an asynchronously replicated reader. Consistency depends on the complete read/write routing, not merely on one service using a write-through API.

Evaluate latency and availability costs

The write response waits for the origin, so database latency remains on the critical path. A cache does not make all writes cheap. Refusing success while the origin is unavailable may improve the acknowledgement guarantee at the cost of write availability. This can be a reasonable choice for read-heavy records with manageable write volume. Evaluate failure injection, duplicate requests, invalidation recovery and tail latency before deciding whether the operational complexity is justified.

A user-facing response should also distinguish a confirmed rejection from an uncertain outcome. An operation identifier can let the caller query the result instead of submitting a fresh debit. This complements server-side idempotency rather than replacing it. Test the case where the origin commits successfully but the reply is lost, because network failure is not the same event as business-operation failure.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Understand the systems behind the web · STEP 12

Choose a learning path