Skip to content

Performance Tuning

OctoFHIR includes an index advisor and configurable tuning options.

For the current query/index execution model, see Search Indexing.

The server has a cache for prepared query templates, but search does not use it yet. Each search builds its SQL from scratch, because renderers can embed values in the SQL and a key based on query structure does not prove that a template fits.

  • Cache key: based on parameter structure (resource type, parameters, modifiers), not values
  • Eviction: TTL cleanup. An entry goes stale after an hour; at capacity an insert removes stale entries with a 1% chance, and above 1.5 times the capacity on every insert
[search]
default_count = 10
max_count = 100
cache_capacity = 1000 # Maximum cached queries

The cache lives in memory, per process, so a restart empties it.

In most setups, you do not need to handcraft indexes on day one. A resource table has:

  1. The primary key on id.
  2. A whole-document GIN index on resource, for searches that run as JSON containment.
  3. Covering and GIN indexes on the generated profile column, for _profile.
  4. Functional indexes for the parameters in search.indexed_params, built at startup.

There are no side tables of search values, and updated_at has no index by default. See Search Indexing for what each parameter type gets.

The index advisor reports on one resource table, from the table’s shape, from pg_stat_statements when it is available, and from request URLs you give it. It is read-only.

Terminal window
GET /api/db-console/index-advisor?resourceType=Patient

See the index advisor for the request and what it suggests. In the web console it is Index advice on the Database page.

To index a search parameter, add it to search.indexed_params. Add other indexes by hand only after measuring a real hotspot, and check with EXPLAIN that the planner uses them: an index helps only if its expression matches the predicate the search generates.

  • The Explain panel of the Requests page shows the SQL and plan behind a search.
  • allow_debug_search_plan turns on debug plans over HTTP.
  • pg_stat_statements in PostgreSQL gives the slowest statements, and the SQL console can query it.
[storage.postgres]
url = "postgres://user:pass@localhost/octofhir"
pool_size = 20
connect_timeout_ms = 5000
idle_timeout_ms = 600000
Concurrent Users Pool Size
< 50 10-20
50-200 20-50
200-1000 50-100
> 1000 Use PgBouncer

The metrics db_pool_connections_active, db_pool_connections_idle and db_pool_connections_max show the pool of an instance, and pg_connections the connections to the database. See Metrics & Dashboards.

For read-heavy workloads, route search and read operations to a PostgreSQL read replica:

[storage.postgres.read_replica]
url = "postgres://user:pass@replica:5432/octofhir"
pool_size = 30 # Defaults to primary pool_size
connect_timeout_ms = 5000 # Defaults to primary value
idle_timeout_ms = 300000 # Defaults to primary value

When configured, the following operations use the replica pool:

  • GET /fhir/{type}?... (search)
  • GET /fhir/{type}/{id} (read)
  • GET /fhir/{type}/{id}/_history/{vid} (vread)
  • GET /fhir/{type}/{id}/_history (history)
  • GET /fhir/{type}/_history (type history)
  • GET /fhir/_history (system history)

Write operations (create, update, delete, transactions) always go to the primary.

[redis]
enabled = true
url = "redis://localhost:6379"
pool_size = 10
timeout_ms = 1000
  1. Local cache (in-memory DashMap): Fastest, per-instance
  2. Redis cache: Shared across instances
  3. Database: Source of truth

When resources are modified:

  1. Local cache entry removed
  2. Redis PUBLISH sent to invalidation channel
  3. Other instances receive notification and clear local cache
Terminal window
# Slower: scans all observations
GET /fhir/Observation?status=final
# Faster: filters by code first (likely indexed)
GET /fhir/Observation?code=http://loinc.org|8867-4&status=final
Terminal window
# Returns up to 10 results (default)
GET /fhir/Patient?family=Smith
# Explicitly limit
GET /fhir/Patient?family=Smith&_count=50
# Just get the count
GET /fhir/Patient?family=Smith&_summary=count
Terminal window
# Returns full resources
GET /fhir/Patient?family=Smith
# Returns only needed fields (less data transfer)
GET /fhir/Patient?family=Smith&_elements=id,name,birthDate
Terminal window
# Can return many resources
GET /fhir/Patient?_revinclude=Observation:subject
# Better: paginate and fetch separately
GET /fhir/Patient?family=Smith&_count=10
GET /fhir/Observation?subject=Patient/123&_count=50

OctoFHIR exposes Prometheus metrics at /metrics. Metrics & Dashboards lists them, with the built-in dashboards.

Enable distributed tracing:

[otel]
enabled = true
endpoint = "http://jaeger:4318"

Spans include:

  • http.request - Full request lifecycle
  • fhir.* - One per REST interaction, such as fhir.read, fhir.search, fhir.create, fhir.update, fhir.bundle
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
stages: [
{ duration: '30s', target: 50 },
{ duration: '1m', target: 100 },
{ duration: '30s', target: 0 },
],
};
export default function () {
const res = http.get('http://localhost:8888/fhir/Patient?family=Smith', {
headers: { 'Accept': 'application/fhir+json' },
});
check(res, {
'status is 200': (r) => r.status === 200,
'response time < 100ms': (r) => r.timings.duration < 100,
});
sleep(0.1);
}

Run:

Terminal window
k6 run loadtest.js
  1. Look at the plan of the search, in the Explain panel of the Requests page or with a debug plan.

  2. Check whether the parameter is in search.indexed_params, and ask the index advisor:

    Terminal window
    GET /api/db-console/index-advisor?resourceType=Patient
  3. Bound what the search returns with _count, _elements and date windows.

  1. Check for large result sets: lower search.max_count and search.include_limits.
  1. Increase pool size:

    [storage.postgres]
    pool_size = 50
  2. Check for long-running queries:

    Terminal window
    GET /api/db-console/active-queries
  3. Consider using PgBouncer for connection pooling