Performance Tuning
OctoFHIR includes an index advisor and configurable tuning options.
For the current query/index execution model, see Search Indexing.
Query Caching
Section titled “Query Caching”Overview
Section titled “Overview”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
Configuration
Section titled “Configuration”[search]default_count = 10max_count = 100cache_capacity = 1000 # Maximum cached queriesThe cache lives in memory, per process, so a restart empties it.
Index Optimization
Section titled “Index Optimization”Built-In Indexes (Default)
Section titled “Built-In Indexes (Default)”In most setups, you do not need to handcraft indexes on day one. A resource table has:
- The primary key on
id. - A whole-document GIN index on
resource, for searches that run as JSON containment. - Covering and GIN indexes on the generated
profilecolumn, for_profile. - 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.
Index Advice
Section titled “Index Advice”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.
GET /api/db-console/index-advisor?resourceType=PatientSee the index advisor for the request and what it suggests. In the web console it is Index advice on the Database page.
Optional Custom Indexes
Section titled “Optional Custom Indexes”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.
Analyzing Query Performance
Section titled “Analyzing Query Performance”- The Explain panel of the Requests page shows the SQL and plan behind a search.
allow_debug_search_planturns on debug plans over HTTP.pg_stat_statementsin PostgreSQL gives the slowest statements, and the SQL console can query it.
Connection Pooling
Section titled “Connection Pooling”Configuration
Section titled “Configuration”[storage.postgres]url = "postgres://user:pass@localhost/octofhir"pool_size = 20connect_timeout_ms = 5000idle_timeout_ms = 600000Sizing Guidelines
Section titled “Sizing Guidelines”| Concurrent Users | Pool Size |
|---|---|
| < 50 | 10-20 |
| 50-200 | 20-50 |
| 200-1000 | 50-100 |
| > 1000 | Use PgBouncer |
Monitoring
Section titled “Monitoring”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.
Read Replicas
Section titled “Read Replicas”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_sizeconnect_timeout_ms = 5000 # Defaults to primary valueidle_timeout_ms = 300000 # Defaults to primary valueWhen 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 Caching
Section titled “Redis Caching”Configuration
Section titled “Configuration”[redis]enabled = trueurl = "redis://localhost:6379"pool_size = 10timeout_ms = 1000Cache Layers
Section titled “Cache Layers”- Local cache (in-memory DashMap): Fastest, per-instance
- Redis cache: Shared across instances
- Database: Source of truth
Cache Invalidation
Section titled “Cache Invalidation”When resources are modified:
- Local cache entry removed
- Redis
PUBLISHsent to invalidation channel - Other instances receive notification and clear local cache
Search Optimization
Section titled “Search Optimization”Use Specific Parameters
Section titled “Use Specific Parameters”# Slower: scans all observationsGET /fhir/Observation?status=final
# Faster: filters by code first (likely indexed)GET /fhir/Observation?code=http://loinc.org|8867-4&status=finalLimit Results
Section titled “Limit Results”# Returns up to 10 results (default)GET /fhir/Patient?family=Smith
# Explicitly limitGET /fhir/Patient?family=Smith&_count=50
# Just get the countGET /fhir/Patient?family=Smith&_summary=countUse _elements
Section titled “Use _elements”# Returns full resourcesGET /fhir/Patient?family=Smith
# Returns only needed fields (less data transfer)GET /fhir/Patient?family=Smith&_elements=id,name,birthDateAvoid Large _include
Section titled “Avoid Large _include”# Can return many resourcesGET /fhir/Patient?_revinclude=Observation:subject
# Better: paginate and fetch separatelyGET /fhir/Patient?family=Smith&_count=10GET /fhir/Observation?subject=Patient/123&_count=50Monitoring and Metrics
Section titled “Monitoring and Metrics”Prometheus Metrics
Section titled “Prometheus Metrics”OctoFHIR exposes Prometheus metrics at /metrics. Metrics & Dashboards lists them, with
the built-in dashboards.
OpenTelemetry
Section titled “OpenTelemetry”Enable distributed tracing:
[otel]enabled = trueendpoint = "http://jaeger:4318"Spans include:
http.request- Full request lifecyclefhir.*- One per REST interaction, such asfhir.read,fhir.search,fhir.create,fhir.update,fhir.bundle
Benchmarking
Section titled “Benchmarking”Load Testing with k6
Section titled “Load Testing with k6”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:
k6 run loadtest.jsTroubleshooting
Section titled “Troubleshooting”Slow Queries
Section titled “Slow Queries”-
Look at the plan of the search, in the Explain panel of the Requests page or with a debug plan.
-
Check whether the parameter is in
search.indexed_params, and ask the index advisor:Terminal window GET /api/db-console/index-advisor?resourceType=Patient -
Bound what the search returns with
_count,_elementsand date windows.
High Memory Usage
Section titled “High Memory Usage”- Check for large result sets: lower
search.max_countandsearch.include_limits.
Connection Pool Exhaustion
Section titled “Connection Pool Exhaustion”-
Increase pool size:
[storage.postgres]pool_size = 50 -
Check for long-running queries:
Terminal window GET /api/db-console/active-queries -
Consider using PgBouncer for connection pooling