When two hospitals share patients, a one-way feed is rarely enough. A partner clinic updates an allergy, the main hospital corrects a demographic field, and within a week each side holds a slightly different version of the same patient with no record of who changed what.
This post is for integration teams asked to keep FHIR data aligned between two hospitals in both directions. It covers choosing a source of truth per resource type, version checks with ETag and If-Match, conflict rules when both sides edit, and the nightly job that finds drift.
What is FHIR data synchronization between hospitals?
Connecting two FHIR servers?
Our FHIR integration page covers the services we build for exchanging and keeping FHIR data aligned across organizations.
FHIR data synchronization between hospitals is a service that copies changes between two FHIR servers in both directions, applies each change only if the target still holds the expected version, resolves conflicts by agreed rules, and checks both sides for drift on a schedule. Agnotic Technologies builds these services for hospital partners sharing patients.
Sync is different from a referral or a query. Both copies are expected to stay correct over time, so the service has to know which side owns each piece of data.
When do two hospitals need two-way sync?
Two hospitals need two-way sync when clinicians on both sides edit the same patients' records as part of routine care. A shared service line, a hospital and its satellite clinic, or two sites in a merger before a single EHR goes live are common cases.
If only one side edits and the other reads, a one-way feed is simpler and safer. Ask how many resource types both sides really write before building anything bidirectional, because each one adds conflict rules and testing.
A source of truth per resource type
Decide an owner for every resource type, and sometimes for every field, before writing code. The owner's version wins by default; the other side may propose changes, but it does not silently overwrite.
- Patient demographics: owned by the hospital that registered the patient first, or by the master patient index if one exists.
- Allergies and problems: shared, with both sides allowed to add and a review rule for edits and removals.
- Encounters and orders: owned by the hospital where the visit or order happened, and read-only elsewhere.
- Results and reports: owned by the producing lab or department, never edited by the receiver.
Write the table down and get clinical sign-off. Most arguments during testing turn out to be ownership questions nobody answered at the start.
How do ETag and If-Match prevent lost updates?
ETag and If-Match prevent lost updates by making every write conditional on the version the sync service last saw. If someone changed the record in the meantime, the write fails instead of erasing their change.
HL7 FHIR R4 handles lost updates with version-aware updates: the server returns the resource's version id in an ETag header, the client sends it back in an If-Match header, and when the version does not match, the server returns 412 Precondition Failed instead of updating the resource.
The sync service stores the last version id it saw on each side for every linked resource. When a 412 comes back, it re-reads the target, applies the conflict rules and retries or sends the change to a review queue, never a blind overwrite.
Check that both servers support version-aware updates before relying on them. A server that ignores If-Match turns every concurrent edit into a silent loss.
How do you find the changes to send?
Find changes by polling each server's history since the last successful run, or by subscribing where the server supports it. Polling history is the dependable fallback because it also catches changes made while a subscription was down.
In HL7 FHIR R4, the history interaction returns the versions of a single resource, of all resources of one type, or of all resources on the server, and its _since parameter limits the result to versions created at or after a given instant.
- Watermark: store the instant of the last fully processed change per server and resource type.
- Overlap: start each poll slightly before the watermark and drop versions already processed.
- Order: apply changes in dependency order, so a Patient exists before its Encounter and Observations.
- Echo guard: tag every write the sync service makes, and skip those versions when they appear in history.
The echo guard matters most. Without it, a change copied from A to B shows up in B's history, gets copied back to A, and the two servers update each other forever.
What should happen when both hospitals edit the same record?
When both hospitals edit the same record between runs, the conflict rules decide, and anything the rules cannot settle goes to a person. The rules come from the ownership table, not from timestamps alone.
- Owner wins: for owned types, the owner's version is kept and the other edit becomes a review item.
- Union for lists: two new allergies added on different sides are both kept, with duplicates matched by code.
- Removal needs review: deleting or marking a shared entry inactive on one side is never copied without a check.
- Field merge only when agreed: for example, a phone number owned by one side and a preferred language by the other.
Do not resolve by last updated time. Server clocks differ, and the latest edit is not always the correct one.
The nightly reconciliation job
A nightly reconciliation job compares both servers for every linked patient and reports differences the change feed missed. It is the safety net that turns silent drift into a visible list.
For each linked resource, the job compares version ids against the sync service's record and compares content for the fields in scope. Differences are classed as pending, conflict or unexplained, and unexplained ones go to the integration team the next morning.
Run it outside clinical peak hours and page through results gently, because a full comparison reads every linked record on both sides.
What goes wrong in hospital-to-hospital sync?
Most failures are silent. Each side looks correct on its own, and the difference shows up only when a clinician compares them.
- Ping-pong updates: writes echo back and forth because the service does not recognize its own changes.
- Lost edits: one side's server ignores If-Match, so concurrent changes overwrite each other.
- Broken references: an Observation arrives before its Patient link exists, and the write fails or attaches to the wrong record.
- Id confusion: local ids differ on each side, and a missing link table maps a record to the wrong patient.
- Watermark gaps: a failed run advances the watermark anyway, and a batch of changes is never sent.
Each of these is cheaper to prevent than to clean up. Fail the run loudly on a broken reference, move the watermark only after every change in the batch is written or queued, and alert when the review queue stops shrinking.
A worked example: a shared cardiology service
Two hospitals run a joint cardiology service. Patients are registered at the main hospital, seen in clinics at both sites, and allergies and problems are maintained by both teams.
The ownership table makes the main hospital the owner of demographics, each site the owner of its own encounters and results, and allergies and problems shared. The sync service polls history every few minutes, writes with If-Match and tags its writes so they are skipped on the next poll.
One morning, a nurse at the satellite clinic adds a penicillin allergy while a doctor at the main hospital adds a latex allergy. Both are kept by the union rule. Later, someone marks the latex allergy inactive at the clinic; the removal rule sends it to review.
Who owns what?
Ownership of the service spreads across both hospitals and the team that runs the sync. Each sign-off belongs to one named role.
- Clinical leads from both hospitals: the ownership table and the conflict rules.
- Health information management: the link table between local patient ids and the review queue.
- Each hospital's FHIR server team: endpoint access, version support and notice of upgrades.
- Integration team: change detection, writes, the echo guard, reconciliation and alerts.
- Privacy officers: the data sharing agreement that sets which resource types are in scope.
How do you test a two-way sync?
Test a two-way sync by scripting concurrent edits, outages and replays against two test servers, then checking both sides match. Happy-path tests prove little, because conflicts only appear under timing pressure.
- Concurrency: edit the same resource on both sides within one polling interval and confirm the rules decide.
- Outage: stop one server mid-run, restart it and confirm nothing is skipped or duplicated.
- Echo: confirm a single edit produces exactly one write on the other side.
- Reconciliation: plant a difference directly on one server and confirm the nightly job reports it.
Measure the sync in production too: changes sent, 412 responses, review queue size and the age of the oldest unexplained difference.
What are FHIR synchronization best practices?
Agree an owner per resource type before building, write with If-Match every time, and never resolve conflicts by timestamp alone. Tag your own writes so they are not copied back.
Poll history with a watermark that only moves after success, apply changes in dependency order, and run a nightly comparison that makes drift visible.
How Agnotic can help
Agnotic designs and builds two-way FHIR sync services: ownership tables, change detection, version-aware writes, conflict queues and reconciliation reports. We work with both hospitals' clinical and technical teams so the rules match how care is shared.
Plan a two-way FHIR sync with Agnotic
We build sync services that keep shared patient records aligned and visible.
Related reading on Agnotic
FHIR exchange, subscriptions and patient identity.


