Blog

    RTK Query in Redux for Healthcare Web Apps: Cache Lifetime, Logout Purge and Patient Data in the Browser

    By GauravSeptember 7, 20269 mins read
    On this page

    RTK Query, the data fetching layer in Redux Toolkit, keeps server responses in the Redux store so screens can share them without fetching again. In a healthcare web app those responses are often patient records, which raises questions general tutorials skip: how long they stay in memory, what clears them at logout and where they must never be written.

    Startup founders and engineering leaders building clinical or patient-facing web apps in React and Redux will find the cache settings, the logout purge and the browser storage rules we apply on client projects.

    How does RTK Query in Redux cache patient data?

    RTK Query in Redux caches each query's response in the store, keyed by endpoint and arguments, keeps it while any component subscribes, then removes it after keepUnusedDataFor seconds. Agnotic Technologies sets short lifetimes for patient endpoints, purges the whole cache at logout and never persists those responses to browser storage.

    Building a React app that shows patient data?

    We set the cache rules, the logout purge and the storage boundaries.

    The RTK Query documentation says that once the last subscription to cached data is removed, the data is removed from the cache after keepUnusedDataFor seconds, which defaults to 60 and can be set for the whole API or for one endpoint.

    What should be cached in the browser, and for how long?

    Cache what the user is actively working with, for as long as they are working with it, and nothing longer. In many care settings the browser runs on a shared device, so cache lifetime is a privacy setting as well as a performance one.

    Cache rules by endpoint type
    Endpoint typeExamplekeepUnusedDataForPersist to storage
    Reference dataCode lists, clinic locationsLong, or InfinityAllowed
    User and settingsThe signed-in user's profileThe defaultNo
    Patient listsToday's schedule, a ward listShortNo
    Patient recordsDemographics, results, notesShort, or zero after leaving the screenNever

    How short is a decision for each product, based on how users move between screens. What matters is that patient endpoints get their own setting instead of inheriting a global default.

    How do you purge the cache at logout?

    Purge the cache at logout by leaving the patient screens, dispatching resetApiState for every API slice and clearing any other slices that hold patient data. Doing only one of these leaves data behind.

    The RTK Query API reference says api.util.resetApiState immediately removes all existing cache entries and marks all queries as uninitialized, and notes that hooks also track state in local component state that resetApiState might not fully reset.

    • Step 1, navigate away from patient screens so their hooks unsubscribe and unmount.
    • Step 2, dispatch api.util.resetApiState() for each API created with createApi.
    • Step 3, reset other Redux slices that copied patient data, such as a selected-patient slice.
    • Step 4, clear tokens and in-memory session state, then redirect to sign-in.
    • Step 5, run the same purge on session timeout and on a 401 from the server, not only from the logout button.

    Where must patient data never be written in the browser?

    Patient data must never be written to localStorage, sessionStorage, IndexedDB or a persisted Redux store unless the product has a deliberate offline design with encryption and wipe rules. By default, Agnotic keeps PHI in memory only.

    • No persistence library on API slices or any slice holding patient data; allow-list only safe slices such as UI preferences.
    • No patient data in URLs or query strings, which end up in browser history, server logs and referrer headers.
    • No patient data in analytics events, error reports or console logs; scrub payloads before they leave the browser.
    • No patient data in service worker caches; exclude API routes from offline caching.

    How should the server's HTTP headers support this?

    The server should tell browsers and intermediaries not to store patient responses, so the HTTP cache does not keep a copy RTK Query never sees. The Redux cache and the HTTP cache are separate layers, and each needs its own rules.

    RFC 9111, the HTTP caching standard, says the no-store response directive means a cache MUST NOT store any part of the request or the response, and the unqualified private directive means a shared cache MUST NOT store the response.

    • Send Cache-Control: no-store on API responses that contain patient data.
    • Use private with a short max-age only for per-user responses that hold no PHI.
    • Keep reference data cacheable, since it is the same for every user.

    How do you keep cached patient data fresh?

    Keep it fresh with tags: queries provide tags such as Patient with an id, and mutations invalidate them, so a change saved on one screen refetches the affected data everywhere it is shown.

    • Tag each patient record and list by type and id.
    • Invalidate the matching tags on every mutation that changes those records.
    • Refetch on mount or argument change for screens where stale data is a clinical risk, such as medication lists.
    • Use refetch on focus or polling, with setupListeners configured, where other staff change the same records during the day.
    • Show when the data was loaded on screens where that matters.

    What about several users on one shared device?

    On shared workstations, assume the next person at the keyboard is someone else. The purge has to run whenever a session ends, including when a tab is simply left open.

    • An idle timeout that runs the full purge and returns to sign-in.
    • A switch-user flow that purges before the next person signs in, not after.
    • A check at sign-in that the store holds nothing from a previous session.

    How do you test that patient data is gone after sign-out?

    Test it directly: sign in, open patient screens, sign out by each route a session can end, then inspect the store and browser storage. A purge that is not tested tends to break the first time someone adds a new slice.

    • An integration test that opens a chart, signs out and asserts every API slice's queries are empty.
    • The same test triggered by idle timeout and by a mocked 401 response.
    • A check that localStorage, sessionStorage and IndexedDB hold no patient fields after any test run.
    • A review rule that new slices declare whether they hold patient data and are added to the purge.

    How should error and loading states treat patient data?

    Error and loading states should show nothing from a previous patient. A screen that briefly renders the last chart while the next one loads can show one patient's details to someone looking up another.

    • Key every patient query by patient id, so a new patient never reuses an old cache entry.
    • Render a loading state, not stale data, whenever the patient id changes.
    • Keep on-screen error messages generic, and send details without patient fields to the error tool.
    • Clear form state holding patient input when the user leaves the screen without saving.

    What should code review check in the data layer?

    Code review should check every new endpoint and slice against the cache and storage rules, because one careless line can undo them. Reviewers need a short list they can apply in minutes.

    • Does the new endpoint set its own keepUnusedDataFor, or rely on a default that does not suit patient data?
    • Does it provide and invalidate tags that match the records it changes?
    • Does any new slice copy patient data, and has it been added to the purge?
    • Is anything from the response written to storage, the URL, analytics or a log?
    • Does the matching server route send the agreed cache headers?

    What are the common mistakes with RTK Query and patient data?

    The common mistakes with RTK Query and patient data come from keeping defaults built for general apps, or from treating logout as a single button rather than every way a session can end.

    • Setting keepUnusedDataFor to Infinity globally to save requests, which keeps every opened patient in memory until reload.
    • Persisting the whole Redux store, API cache included.
    • Calling resetApiState only from the logout button and not on timeout or a 401.
    • Putting patient ids or names in route paths and query strings.
    • Sending Redux state to an error reporting tool as context on every error.

    A worked example: a clinic schedule and a patient chart

    A clinic web app shows the day's schedule and opens a patient chart from it. The schedule query has a short lifetime and refetches on focus, because appointments change during the day.

    The chart queries use a lifetime of zero, so a chart leaves memory as soon as the user closes it. Saving a note invalidates the Patient tag, and the schedule badge and the chart refresh together.

    When the receptionist steps away, the idle timeout navigates to sign-in, resets each API slice and the selected-patient slice, and the next user starts with an empty store.

    What does RTK Query patient data handling mean for your build plan?

    Decide the cache and storage rules before the first patient endpoint is written, and put them in code reviewers can see: per-endpoint lifetimes, one purge function and a rule against persisting API slices.

    • List endpoints by data type and agree a lifetime for each.
    • Write one purge function and call it from every path that ends a session.
    • Agree server cache headers with the backend team.
    • Add the sign-out tests before the second patient screen is built.

    How does Agnotic build React and Redux apps that show patient data?

    Agnotic sets up RTK Query with per-endpoint cache rules, a single purge on every session end, server headers that stop HTTP caching of PHI, and tests that prove the store is empty after sign-out. We build the screens and the data layer together, so the rules hold as the app grows.

    Patient data in memory only, and only while it is needed

    We design the data layer, the purge and the tests that prove it.

    Sources

    Frequently Asked Questions

    Yes, with settings chosen for it. The defaults suit general apps, so patient endpoints need short lifetimes, a purge at every session end and no persistence.

    Talk to an engineer about your project

    Share your API slices and how sessions end in your app. We will set the cache rules and the purge for each path.

    Go deeper

    Services and solutions on this topic