Skip to content
Senior1 view

Design a distributed system for managing medical prescriptions with versioning, reminders, and offline mode.

Practice

AI answer

Architecture

Divide the system into several logical components:

  • API and authorization service — authenticate users and verify that patients, doctors, and staff have access to specific medical records.
  • Assignment service — stores the current version of an assignment and its change history.
  • Reminder service — schedules notifications and accounts for the patient’s time zone and preferences.
  • Synchronization service — sends changes to mobile clients and accepts offline operations.
  • Relational database as the source of truth; a message queue for events and background tasks. The cache and search index must not be sources of truth.

For example, you could use PostgreSQL for data, a message broker for events, and a transactional outbox to prevent changes to an assignment and publication of its event from getting out of sync in the event of a failure.

Data model and versions

Store assignments as immutable revisions: assignment_id, revision, author, time, status, content, reason for the change, and a link to the previous revision. A separate record or pointer identifies the current revision. A change creates a new revision rather than overwriting history. A cancellation is also recorded as an event or a new revision.

Use optimistic locking for updates: the client sends the expected revision, and the server checks it atomically, rejecting stale changes with a conflict response. The client loads the current version and offers the user a comparison of the changes. Medically significant fields must not be merged automatically; merge rules are appropriate only for safe metadata.

Offline mode and synchronization

The client stores an encrypted local copy and a queue of operations. Each operation receives a unique identifier to make retries idempotent. When the network is restored, the client sends the operations, and the server returns acknowledgments, current revisions, and conflicts. Use a cursor or sequential event number to retrieve changes; synchronization must tolerate retries and partial failures.

Show the time of the last synchronization and clearly mark local, unconfirmed changes. Offline data may be stale or revoked: the application must show its status and must not present an unconfirmed operation as having been accepted by a doctor.

Reminders

Schedule reminders based on the current revision of the assignment. A change or cancellation event must recalculate scheduled jobs; before sending, a worker checks again that the assignment is current to avoid sending a notification for a canceled version. Use idempotency keys, retries with backoff, and a dead-letter queue. Store the time zone, channels, and user consent; account for quiet hours and the option to disable notifications. Do not disclose a diagnosis or other sensitive information in push messages — show details only after authentication.

Reliability and security

Ensure TLS in transit and encryption at rest, strict role-based and object-level authorization, least-privilege access, auditing of reads and changes to assignments, backups, and tested recovery procedures. Store secrets separately from the code. Monitor synchronization latency, reminder delivery errors, and queues, and configure alerts for failures.

Before launch, define requirements for availability, acceptable notification delays, medical data retention, and applicable legislation. Test scenarios involving duplicate delivery, version conflicts, time zone changes, assignment cancellation, prolonged network outages, and restoration from backup.