跳到正文
DEV Community· Adil Ben Moussa·· 1 小時前AI 評分64

企業應用程式的網絡與快取:寫入端須負責更新快取

Networking and caching in enterprise apps: own the stale data

AI 導讀

Adil Ben Moussa 提出企業前端的網絡與快取設計原則,主張由執行寫入的程式碼負責令相關快取失效,並重新載入或套用伺服器回覆。每個業務域應由單一資料存取層連接後端,請求需處理取消、合併在途重複請求及逾時;重試只適用於讀取,寫入須有伺服器去重的 idempotency key 才可自動重試。手機網絡不穩時可先顯示快取並在背景更新,按連線情況和每項資料的 TTL 決定何時重新整理。

正文

This is part 3 of 8 of the free series Frontend system design for enterprise applications, first published on tt-tech.io.

A customer makes a transfer, goes back to the dashboard, and sees the old balance. The transfer worked. The dashboard read a cache that nobody told about the write. In a bank, that is a support call, and sometimes a second transfer.

Caching sits in this part, next to networking, because in an enterprise app they are one question: where does this data live, and when is it wrong? A cache without an owner is a bug with a delay.

One boundary to the backend

Components never fetch. One data-access layer per domain talks to the backend, and everything else reads from it. That gives you one place to add auth, one place to cancel, one place to invalidate.

When the screens need a shape the domain APIs do not have, put a backend-for-frontend (BFF) between them, owned by the frontend team. Do it for shape and for secrets, not to avoid talking to the API team.

One boundary to the backend. Five widgets, the balance card, the accounts list, the transfer form, a dashboard chart and the header, all ask the data-access layer for the accounts. AccountsStore.load() shares one request in flight, so one request goes out, through an optional backend-for-frontend owned by the frontend team, to the domain APIs for accounts, payments and cards. One place for auth, cancel and invalidate

The contract is generated, and required is the truth

Generate the types from the OpenAPI document instead of writing them by hand. OpenAPI 3.1 schemas are "a superset of the JSON Schema Specification Draft 2020-12",[1] and in JSON Schema "the properties defined by the properties keyword are not required" unless a required list names them.[2] So read the required array, not the field list, to know what will be there.

Cancel, dedupe, time out

Three things every request needs. Cancel what the user left: Angular's httpResource "cancels the outstanding request before issuing a new one" when its inputs change.[3] Without that, React's docs describe the result, a race condition where an older response arrives last and the screen shows the wrong results.[4] In plain fetch, AbortController lets you "abort one or more Web requests as and when desired".[5]

Dedupe what is already in flight, so five widgets asking for accounts cause one request. And give every call a timeout, so a slow backend becomes an error message, not a spinner.

After a write, who refreshes the cache?

To invalidate a cache entry is to mark it as out of date, so the next read goes to the server again. Every cache needs an owner, and the owner is the code that performs the write. After a transfer, the accounts store either refetches or applies the server's answer. A widget that keeps its own copy will never hear about it.

In React without a framework, the data layer is yours to choose or build. Its docs list caching, server fetching and network waterfalls as the hard parts, and point to frameworks' built-in data fetching over effects.[4] Whatever library you pick, the decision stays the same: which keys does this write make wrong?

The code that writes the data owns the cache of it.

On mobile in Morocco, cache first

On mobile in Morocco, caching is not an optimisation. Outside the big cities the connection is often slow or gone, and a screen that waits for the network is a screen people close. In Qismi, our school app, we solved that with heavy caching:

  • Two levels. Every list sits in memory and in the phone's storage, each entry with a time to live (TTL): five minutes for message threads, one hour for announcements.
  • Cache first, refresh behind. The screen shows the cached copy at once and fetches a fresh one in the background. This pattern is called stale-while-revalidate.
  • The network decides when. On Wi-Fi a cached list counts as stale after 20% of its TTL, on 3G after 80%, on 2G after 95%, and offline it is never refreshed. Slow connections get speed, fast ones get fresh data.
  • The write still owns the cache. After a message is sent, the code that sent it clears four entries: both people's thread lists, the conversation, and the receiver's unread count.

A timeline for Cache-Control: max-age=1, stale-while-revalidate=59. From 0 to 1 second the cached copy is fresh and used as is, with no revalidation; from 1 to 60 seconds it is stale but still answers at once while the network revalidates it in the background; after 60 seconds it is not used and the request waits for the network, whose response fills the cache

Image: tt-tech.io, adapted from web.dev, Keeping things fresh with stale-while-revalidate, CC BY 4.0.

The picture shows the same pattern for an HTTP response with a one-second max-age and a 59-second stale-while-revalidate window.

The same rule as above, just with more caches to own.

HTTP caching headers

The browser cache is the second cache, and in banking the one that leaks. no-store means "any caches of any kind (private or shared) should not store this response"; private is for "user-personalized content".[6] Personalized content in a shared cache "may cause unintentional information leakage".[7] Hashed build assets are the opposite case: long max-age plus immutable.[6]

A flowchart for choosing Cache-Control: a response that cannot be reused gets no-store, for example the account balance; one that must be revalidated every time gets no-cache; then private if only the browser may keep it or public if shared caches may, then a max-age, with hashed JS and CSS files getting max-age=31536000 and immutable, and finally an ETag so a stale copy can be revalidated with a small 304 Not Modified

Image: tt-tech.io, adapted from web.dev, Prevent unnecessary network requests with the HTTP Cache, CC BY 4.0.

Retries and idempotency

Retrying a GET is safe. Retrying a transfer is not. POST and PATCH "are not guaranteed to be idempotent", and a client can safely retry a request "that uses an idempotent method".[8] So a write either carries a key the server deduplicates, or it is never retried automatically. When the server answers 409 Conflict, "a request conflict with the current state of the target resource",[9] show it to the user and reload; do not retry.

Three requests that time out and are retried. GET /accounts: retrying is safe, reading twice changes nothing. POST /transfers without a key: the server already sent 500,00 MAD, the answer is lost, the automatic retry sends it again, and the money leaves twice. POST /transfers with an idempotency key: the retry carries the same key, the server answers that it is already done, and the money leaves once

Interceptors are where the cross-cutting parts live: auth headers, retries, timeouts, logging.[10] Keep the retries to reads. I would add a correlation id there too, so an audit log on the server can be matched to what the user clicked.

On Atlas Bank

Atlas has no real server. FakeBackend in libs/data-access/banking is the boundary (its comment says "nothing else in the app talks to 'the server'"), with 250 ms of simulated latency so loading states are real. AccountsStore.load() dedupes:

  /** Idempotent: concurrent callers share one in-flight request; later callers resolve immediately. */
  load(): Promise<void> {
    if (this.loadedState()) return Promise.resolve();
    this.inFlight ??= this.fetchAll();
    return this.inFlight;
  }

After a transfer, the same store adds the transaction the backend returns and subtracts the amount from the balance, so every screen that reads it is right. Lint fails on a fetch to an absolute URL, because "the browser never calls a third party directly". What Atlas does not have yet: cancellation, timeouts, and an idempotency key on transfer(). Those come with a real HTTP layer.

What I left out, and why

  • GraphQL versus REST. In enterprise, the backend usually decides.
  • WebSockets, SSE, long polling as a tour. When you need real-time, it is one decision about one feed.
  • Service workers and offline-first on the web. Rare in regulated web apps. On mobile it is a different story, as Qismi shows.
  • CDN edge caching. Operations, not frontend design.

On Monday

  • Search your app for fetch( and HttpClient outside the data-access layer. Each hit is a missing boundary.
  • Pick your most important write. Write down which cached data it makes wrong, and who refreshes it.
  • Check the Cache-Control header on your most sensitive response.

Part 4, on rendering, is next.

For your coding agent

The rules of this article, written for an agent. Paste them into your AGENTS.md, save them as a skill, or point your agent at the file.

# Networking and caching rules for enterprise apps

Source: https://tt-tech.io/blog/enterprise-frontend-networking-caching
For Angular and React frontends. Add to AGENTS.md or save as a skill. Your project's own rules win where they differ.

## Rules

1. Components never fetch. All backend calls go through one data-access layer per domain. No `fetch(` or `HttpClient` outside it.
2. Add a backend for frontend (BFF), owned by the frontend team, only when the screens need a shape the domain APIs do not have, or to keep secrets out of the browser.
3. Generate API types from the OpenAPI document. Never write them by hand. Treat a field as always present only when the schema's `required` array names it.
4. Cancel requests the user left: `httpResource` in Angular, `AbortController` with `fetch`. An older response must never overwrite a newer one.
5. Dedupe requests already in flight, so several widgets asking for the same data cause one request.
6. Give every call a timeout, so a slow backend becomes an error message, not an endless spinner.
7. The code that performs a write owns the cache of that data. After the write, refetch or apply the server's answer in the owning store. Never keep a private copy in a widget.
8. Set `Cache-Control` on purpose: `no-store` for responses that must not be reused (an account balance), `private` for user-personalised content, never personalised content in a shared cache, and a long `max-age` plus `immutable` for hashed build assets.
9. Retry reads only. Never retry a `POST` or `PATCH` automatically unless it carries an idempotency key that the server deduplicates.
10. On `409 Conflict`, show the conflict to the user and reload. Do not retry.
11. Put cross-cutting concerns in interceptors: auth headers, timeouts, read retries, logging, and a correlation id per request so server logs can be matched to what the user clicked.
12. On mobile, where the connection is often slow or gone, cache first: show the cached copy at once and refresh it in the background (stale-while-revalidate), with a time to live per entry. The write still clears what it makes wrong.

## Before you say done

- Search the diff for `fetch(` and `HttpClient` outside the data-access layer. Move each hit behind the boundary.
- For every write in the diff, list in the pull request which cached data it makes wrong and where it is refreshed.
- Check every retry in the diff: it is on a read, or the write carries an idempotency key.
- For every new or changed sensitive response, state its `Cache-Control` header.

The same rules as a file: https://tt-tech.io/agents/enterprise-frontend-networking-caching.md

Sources

  1. OpenAPI Initiative, OpenAPI Specification v3.1.1, 2024
  2. JSON Schema, object, docs
  3. Angular, Reactive data fetching with httpResource, docs
  4. React, You Might Not Need an Effect, docs
  5. MDN, AbortController
  6. MDN, Cache-Control header
  7. MDN, HTTP caching
  8. MDN, Idempotent
  9. MDN, 409 Conflict
  10. Angular, Intercepting requests and responses, docs
  11. web.dev, Keeping things fresh with stale-while-revalidate
  12. web.dev, Prevent unnecessary network requests with the HTTP Cache

Part 3 of 8 of the free series Frontend system design for enterprise applications on tt-tech.io, also in French and Arabic.

來源:DEV Community · dev.to