HAPI FHIR history count mode and expensive totals

Accurate _history totals can become a heavy database query as resource history grows, so teams need to choose a count mode deliberately. This page explains the cached, accurate and disabled options and how to set them.

On this page

Why HAPI FHIR history count mode matters on large databases

If the history endpoints of your HAPI FHIR JPA server slow down as the history table grows, the likely cause is the total count the server computes for each _history response. Counting the rows behind a history request means scanning every version that matches the request, so the cost rises with the size of the table rather than with the size of the page you asked for. The fix is to choose a count behavior deliberately: keep an accurate count where clients need one, use a cached or approximate figure where a rough number is enough, or disable the total where nobody reads it. The right choice depends on what your clients do with the total, not on a default.

A _history total is the count of versions returned by a history query, reported alongside the page of results. Because the database must count every matching row before the server can report the total, the work grows with the number of stored versions for the resource type, instance or server, plus any filters such as _since or _at. A page of 20 results can therefore cost as much as a full table scan on a deployment with a large history table. Teams usually notice the problem first on servers that keep long version histories, that receive frequent updates to the same resources, or that serve paging UIs and export tools which request totals on every call.

Change the count behavior when your monitoring shows history queries dominating database time, when paging clients never use the total, or when a total is needed only for reporting that can run as a separate, scheduled job. Before changing anything, confirm with your application owners which clients read the total, how stale a cached figure can be before it misleads them, and which database engine you run, since the query plans differ between PostgreSQL and SQL Server. Your deployment runs on a JDK version that matches your HAPI FHIR release, for example JDK 17 for the 8.2.0 line, and the count settings should be tested on that same stack before production.

Choosing between cached, accurate and disabled counts

This table compares cached, accurate and disabled total counts for _history on a HAPI FHIR JPA server across freshness, database load, client expectations and fit. Read each row as a trade-off: the freshest totals cost the most query work on large tables, and the cheapest option gives clients the least information. Confirm option names and defaults in the configuration reference for your deployed release before you change anything.

Count modeFreshness and database loadClient expectations and fit
AccurateEach request runs a count against the history table, so cost grows with the number of stored versions and can dominate response time on large databases.Fits small or mid-size repositories, audit-style reviews, or clients that must show exact version totals to users.
CachedTotals are reused for a set period, so the database runs the expensive count far less often, at the cost of totals that can lag recent writes.Fits high-volume servers where approximate totals are acceptable and paging works from links rather than counts.
DisabledNo count query runs, so database load from totals is removed, and the response carries no total.Fits clients that page with next links and never display totals. Confirm that every client and application handles a missing total.
Freshness of totalsAccurate is current to the last write, cached is stale by design, and disabled has no figure to be stale.Ask clients whether a slightly out-of-date number would mislead a user or a downstream process.
Effect of table sizeCount cost rises with the history table, so the gap between modes widens as the server stores more versions.Measure a representative dataset before choosing, because the same mode can behave very differently at small and large volumes.
Option names and defaultsThe setting names, cache duration and default value depend on the release you run and must be confirmed in the configuration reference.Record the chosen mode in your runbook so that upgrades and load tests use the same behavior.

What drives the choice of count mode

Total counts on a _history search look like a small detail, but they sit on the most expensive part of the query plan. The factors below pull in different directions, so the right count mode depends on how your clients use totals, how large and how volatile your history is, and what infrastructure you can spend on reads.

Exact totals for paging UIs

If a client renders page numbers or a "of N results" label, it depends on an exact total. Exact counts force the database to scan every matching history row before it can answer, and that cost grows with the table. Relaxing the count gives faster first pages but means the UI shows an estimate or no total at all. Check which screens actually display totals and whether users navigate to the last page.

Size of the history table

Every update to a resource adds a row to the history table, so _history counts scale with total versions, not with current resources. A server that has accumulated years of updates can have a history table many times the size of its current data. Measure the row count and the time a count takes on your own database before deciding, because the cost is specific to your data shape and indexes.

Write rate and data churn

A cached total stays valid only as long as the data is stable. On a server that takes frequent writes, a cached count drifts from the true number within minutes, and the drift is visible to anyone comparing the total with the rows returned. Estimate how many versions your system writes per hour, and decide how stale a total can be before a client or auditor notices.

Read replica availability

Counting history on the primary database competes with transactional writes and ingest jobs for the same resources. If you run a read replica, count queries can be routed there, which protects write latency but introduces replication lag into the number a user sees. Confirm with your platform team whether replicas exist in each environment and how the HAPI FHIR configuration routes read traffic to them.

Reporting and audit use of totals

When totals feed operational reports, billing or audit evidence, a rounded or cached figure may not be acceptable even if clients never see it. Reporting that needs exact history volumes is usually better served by a separate query against the database or an export than by pushing count load into the FHIR API. Ask who consumes the number, how often they consume it, and whether they need reproducibility.

Client search patterns

Clients that always page from the first result, or that only fetch the latest version of a resource, rarely need a total at all. Clients that jump to arbitrary pages or export full histories depend on it. Review your access logs or client code to see which _history requests include _count and whether any client reads the total field, since that is the usage you must preserve.

How to set up and verify a count mode

This sequence confirms the HAPI FHIR release and Java runtime, applies the history count mode you chose, and measures how that mode affects _history response times and client paging. It ends with a recorded configuration and test results that your team can compare against production expectations before go-live.

  1. Confirm the release and JDK version

    The platform engineer records the HAPI FHIR version and the JDK the server runs on, then checks both against the HAPI FHIR versions page. For example, the 8.2.0 line requires JDK 17, so an older JVM on a test host would block the setup before any tuning begins. Check that the test host's JDK matches the production runtime, and confirm whether a newer release such as 8.8.2 is in your upgrade plan. The step is done when a written version record exists and the JVM check passes on the test host.

  2. Identify the history count setting

    Using the JPA server documentation for your exact release, the platform engineer finds the setting that controls history totals and lists the count modes that release supports. Record the setting name, its default value, and where it is read, whether in the starter's properties file, environment variables or a Java configuration class. Confirm the name against your version, because it can change between releases. The step is done when the team has a short written note covering the name, default and location, and a test instance shows the value it was given at startup.

  3. Apply the chosen mode in a test environment

    The platform engineer sets the mode selected in the decision step on a non-production instance that uses the same database engine as production, either PostgreSQL or Microsoft SQL Server. After a restart, the startup logs or the server's configuration output should show the chosen value. Send a _history request against a small test dataset and confirm that the bundle contains the total behavior the mode promises. The step is done when the logged value matches the decision record and the test request returns as expected.

  4. Load representative history volume

    The data engineer loads resource versions that match production in resource types, the number of versions per resource and the rate of updates. Use transactions and the bulk loading practices your team has documented, so the history tables grow the way they will in production. Confirm the row counts in the history tables and refresh database statistics before measuring. Illustrative example: if production resources average many updates each, the test data should show the same pattern rather than one version per resource. The step is done when the row counts match the agreed target and the database statistics are current.

  5. Measure _history response times

    The performance engineer runs a fixed set of _history requests at system, type and instance level, with and without _count and offset parameters. Each request runs cold and then warm, and the team records response times as percentiles for each count mode they are comparing. The team should also note whether the total element appears in each response. The step is done when results for every mode are stored in one table, the query set is saved for repeat runs, and the team agrees on the acceptable latency with the owners of the applications that depend on the server.

  6. Confirm client paging behavior

    The integration engineer tests each client application against the chosen mode, using the client libraries the applications actually use. The test confirms that next links are present, that the total is returned or omitted as the client expects, and that each client walks every page and stops at the last one. Ask each application owner what their client does when a total is missing, because some clients depend on it for progress displays. The step is done when every client completes a full walk of a test history, and the mode, the test results and any client constraints are written into the change record.

How a _history request flows through the server and database

The request starts at the client and passes through the reverse proxy to the HAPI FHIR JPA server, which is backed by a relational database. The server first decides whether a total is needed based on the configured count mode, then queries the database for the history entries. Check where the count decision happens in your version, and confirm whether the database receives a separate count query, because that second query is where the cost of accurate totals concentrates on large tables.

Where the count decision and the database count query sit in a _history request.

Security and operational considerations for count queries

Total counts on _history touch the same tables as the data they describe, so they inherit the same access and performance risks. The cards below cover who can see counts, how to watch the queries that produce them, how to bound database load, and how to record changes to count settings.

Access control on history endpoints

A count is a small disclosure. If a caller can read a total of history entries for a resource type or compartment, they learn how much activity exists even when they cannot read the entries. HAPI FHIR provides building blocks such as the AuthorizationInterceptor, consent and search narrowing interceptors, but it does not supply a complete security layer. Check that every _history request, including the system-level and type-level forms, passes through the same authorization rules as the reads it summarizes, and test with a restricted account to confirm the total value is filtered.

HAPI FHIR security docs

Monitoring slow count queries

Count queries over large history tables are often the slowest statements in a database. Track execution time for queries against the history table separately from reads and writes, so a rise in count cost shows up before users report slow responses. Use database-level slow query logging or the statistics views your database provides, and compare plans before and after any change to the count mode. Confirm with your DBA which thresholds trigger an alert.

Database resource limits

Count work competes with clinical reads and writes for connections, memory and I/O. Set connection pool limits in the JPA configuration so that a burst of _history requests cannot exhaust the pool that serves other traffic. Ask your database team whether statement timeouts, per-role resource limits or a read replica can isolate reporting-style counts from the primary write path, and test the behavior under concurrent load.

Logging configuration changes

Changing the count mode or its cache settings changes what clients see and how much load the database carries. Record every change to these settings in version control or your change management system, with the reviewer, the reason and the date. Keep the audit trail for the same period your other FHIR server configuration is kept, and confirm with your compliance team how that period is set.

Reviewing counts in access reviews

Counts can expose patterns, such as a sudden spike in activity for one patient or resource type, that a reviewer would not see in an access log alone. Include _history requests in periodic reviews of audit records produced by BALP or your own logging. Decide in advance who receives alerts about unusual count volume, and document that decision in the operating procedures.

Questions teams ask about _history counts

How much does it cost to change the history count mode?

The software itself carries no license fee, because HAPI FHIR is licensed under the Apache Software License 2.0. Cost comes from labor: the engineers and database administrators who measure, change, test and document the setting, plus a staging environment that holds a realistic copy of your data. Most of this cost is one-time. Recurring cost appears only if you add monitoring, change how clients page through results or buy ongoing support. DeployOpen does not publish prices. A quote follows from a written scope, so ask for each part to be listed: the HAPI FHIR version, the database engine, the data volume tested, the client applications reviewed, the load test, the rollback plan and the handover documentation.

How long does testing a history count change take?

No duration is fixed by the software, so the answer depends on your environment. The main drivers are the size of your history data in a copy of production, the number of client applications that read totals, and how long your staging environment takes to refresh. A sound test plan has clear stages: a baseline timing of current queries, the change in staging, a load test with realistic read traffic, client regression checks and a sign-off step. Confirm the schedule against your change calendar and any freeze windows. Ask any outside partner to tie the timeline to a written scope and to the data volume they will test against.

What does our internal team need to provide for this work?

Your team supplies the facts that decide the count mode, so plan their time before the work starts. Typically you will need:

  • An owner for the HAPI FHIR server configuration and its deployment pipeline.
  • A database administrator who can read query plans and track history table growth on PostgreSQL or Microsoft SQL Server, the engines the JPA server starter supports for production.
  • A staging environment with a copy of production-sized data and settings that match production.
  • Product or integration owners who can list which client applications use history totals and how those clients page.
  • A change approver who accepts the rollback plan and the test results before the setting reaches production.
Can our own team change the count mode without outside help?

Often yes, if your team already runs the JPA server and can operate its database. HAPI FHIR is open source under the Apache Software License 2.0, so your engineers can read the documentation, change the configuration and test it in their own environment. The factors that decide whether to do it alone are how much history data you hold, whether clients depend on totals for paging, and whether your test environment matches production. The HAPI FHIR README points to Smile CDR for commercial support if you want a vendor under a support contract. DeployOpen can plan, test and document the change if your team prefers an outside partner working to a written scope.

What should a quote for this work include?

A quote should follow a written scope that states what is measured, tested and handed over. Ask for these items:

  • The HAPI FHIR version, the JDK version and the database engine, since these set the test matrix. The version documentation lists JDK 17 for HAPI FHIR 8.2.0, so confirm the requirement for the version you run.
  • The data volume and the history tables to be measured.
  • The count mode options being compared and the criteria for choosing between them.
  • The client applications to review and the test cases for each.
  • The load test scope, rollback steps and handover documentation.
  • Which items are one-time and which recur, such as monitoring or a retainer for later reviews.
Why do _history totals slow down on large databases?

Counting is the expensive part. To return an accurate total, the database must find and count every history row that matches the request, so the work grows as the table grows. A count that is fast on a small test server can become a slow query in production. The cost also depends on how the query is filtered and indexed, which is why engineers should capture query plans on the database engine in use before changing any setting. Count handling is a server setting that can change between releases, so confirm the exact behavior in the documentation for your HAPI FHIR version. The JPA server documentation explains the server model in general terms.

Which history count mode suits a large HAPI FHIR server?

No single mode suits every server, so choose by testing each option against your clients. An accurate count gives exact totals at the highest database cost. A cached count reduces repeated work but can show a total that lags behind recent writes. Disabling counts removes the database work, but any client that uses a total to show page numbers or decide when to stop paging will need changes. Pick the mode that matches client needs, then measure query time and staleness under load. Record the decision with the clients' test results so the reason for the setting stays clear during later upgrades.

How DeployOpen scopes and delivers history count tuning

DeployOpen works on HAPI FHIR history count mode as a scoped change to a self-hosted JPA server in your own infrastructure. The work starts with measuring how _history totals behave on your database, then agrees on a count mode, a test plan and a handover before any configuration changes reach production.

How we scope

  • Written scope covers which resource types, clients and _history patterns are in or out of the count-mode change.
  • Scope records the current database size, JPA server version and the count behavior clients currently depend on.
  • Engagements are fixed-scope, with deliverables and timelines agreed in writing before work starts.

How we build

  • Query timing on the history tables is measured under realistic read load before and after each count mode is tried.
  • Configuration changes are made in a non-production copy of your deployment, with the JPA server version pinned.
  • Each change is tested against client paging and total-count expectations, including clients that rely on exact totals.

How we hand over

  • Handover includes the chosen count mode, the reasoning behind it and the measurements that supported the decision.
  • Runbook documents how to revert the setting, verify the change and watch database load after deployment.
  • Client teams receive written guidance on how to request totals and how paging responses will differ from today.

How we support

  • Ongoing support covers reviewing count behavior after JPA server upgrades, since defaults and options can change between releases.
  • Retainers can include periodic retesting of history queries as data volume grows on your database.
  • Confirm with DeployOpen during scoping which support hours, response channels and upgrade assistance your retainer includes.

Sources and further reading

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.