The problem
On a medical-equipment ordering platform, staff sometimes opened the same patient chart simultaneously. They reported duplicate orders, conflicting orders and edits overwriting one another. The UI did not show that a colleague was already working on that patient.
I keyed the lock by patient rather than order because two of those collisions happen during order creation, before an order record exists.
Design & architecture
The feature had three components:
- Presence: opening a chart joined a SignalR group so staff could see who else was viewing it.
- Lock registry: a singleton in the API process stored one lock per patient, including its owning connection. A check-and-set inside one mutex kept two simultaneous acquires from both succeeding. The form requested a lock before opening.
- Server conflict checks: create, update and submit endpoints checked the registry. If another user held the lock, the API returned HTTP 409. This prevented conflict even when a client missed a realtime notification.
Lock lifecycle and recovery
Locks were released on leaving the order form or chart, switching patients or disconnecting. Because a browser can disappear without sending a clean disconnect, the client sent a heartbeat every 15 seconds; a background sweep every 30 seconds released connections silent for 45 seconds. A server-checked administrative permission allowed force-release with a notification to the previous holder.
A 30-minute maximum age allowed another user to take over an old lock, with a notice to the previous holder. On reconnect, the client retried with backoff, rejoined the patient group and requested its lock again. The server could transfer ownership when the same user returned on a new connection; otherwise the client displayed that the lock was lost.
An API restart cleared the in-memory registry. A heartbeat from an unknown connection triggered a rejoin flow so the client could rebuild presence and request a lock without reloading the page.
Peer review changed the implementation
I designed the first version and was its primary API and UI implementer, but the final feature was a team effort. The day after my initial commits, a teammate identified race conditions and lock leaks: several release paths had not been synchronized and leaving a chart could retain a lock. We resolved some issues together; the teammate implemented other fixes, including the restart rejoin signal and client reconnect/dialog corrections. Those fix commits belong to them.
Another teammate later added the conflict check to a fourth write endpoint, and QA ran the manual acceptance pass. About a week after the first implementation, I added the 30-minute cap, corrected further client connection lifecycle leaks and consolidated lock acquisition.
Validation and observed outcome
The feature was shipped in April 2026 and validated through peer review and manual QA using two users on one patient. QA covered competing access, closing a tab, a network drop, an administrative force-release and expiry. There were no automated tests for this feature at that time.
After release, I don't recall further reports of these collisions from staff. That is a qualitative observation, not a measured reduction: I have no ticket counts, before/after incident rate or other impact metric to support a numerical claim.
Limits and what I'd improve
- Conflict prevention, not a mandatory lease: the API blocked a write when someone else held the lock; it did not require the writer to own a lock, and the guard and database write were not atomic. I'd enforce ownership and use a database row-version check as a backstop.
- Fixed maximum age: the 30-minute cap was checked during takeover, not renewed as a lease. I'd add a renewable lease with advance warning before expiration.
- Single instance: running more API instances would require shared atomic coordination through SQL or Redis. SignalR would remain the notification channel.
- Test coverage: I'd begin with unit tests for the registry, including simultaneous acquire attempts, lifecycle expiry, reconnect and administrative release, then add API-level conflict tests.
This write-up uses an anonymized architecture and intentionally excludes customer/patient data, internal identifiers, privileged routes and production logs. It distinguishes my implementation from teammates' contributions.