Scalability Workflow

System Dataflows, Queues & Scale-Out Architecture

1. High-Throughput Concurrent Dataflow Architecture

The system optimizes for the highly asynchronous nature of a busy Kenyan Level 4 facility. During peak mid-morning hours, registration, triage, labs, and pharmacies generate complex read/write operations that must be handled smoothly without freezing the primary UI threads.

Rendering Chart

2. Multi-Tiered Redis Caching Framework

To reduce database roundtrips, the system implements a strict caching design. Read-heavy, static, or slow-changing domain data maps directly to multi-layered Redis cache stores.

2.1 Cache Retention & Invalidation Strategy

Cache Key PatternStored Data ObjectsEviction StrategyTTLInvalidation Triggers
keml:drugs:v1Complete Kenya Essential Medicines List reference arrayvolatile-lru72 HoursProcurement updates or KEML version updates.
bed:occupancy:globalActive bed maps, ward counts, availability metricsnoeviction5 MinutesDynamic bed allocations, ward transfers, patient discharges.
patient:session:auth:*Active encrypted RBAC access tokens, session vectorsallkeys-lru15 MinutesExplicit logout event or security termination block.
tariff:nhif_sha:pricesApproved SHA procedures compensation values matrixvolatile-lru24 HoursStatutory adjustment updates from the Ministry of Health.

2.2 Defensive Cache Lookaside Implementation

The system wraps query reads in a declarative lookaside pattern to prevent database traffic spikes during sudden power restorations or system reboots:

import json
from typing import Dict, Optional
from django.core.cache import cache
from apps.ancillary.models import KEMLInventory

def fetch_drug_metadata_defensive(item_code: str) -> Optional[Dict]:
    """
    Lookaside caching pattern ensuring sub-millisecond inventory reads 
    while shielding PostgreSQL from heavy concurrent query traffic.
    """
    cache_key = f"inventory:item:{item_code}"
    
    # Attempt immediate memory cache hit
    cached_data = cache.get(cache_key)
    if cached_data:
        return json.loads(cached_data)
        
    try:
        # Fallback to physical database engine read on cache miss
        db_record = KEMLInventory.objects.get(code=item_code, is_active=True)
        serialized_payload = {
            "id": str(db_record.id),
            "name": db_record.name,
            "code": db_record.code,
            "current_stock": db_record.quantity,
            "unit_price_kes": float(db_record.unit_price)
        }
        
        # Hydrate the cache memory store with a protective 1-hour TTL
        cache.set(cache_key, json.dumps(serialized_payload), timeout=3600)
        return serialized_payload
        
    except KEMLInventory.DoesNotExist:
        return None

3. Asynchronous Queue Processing via Huey

Background tasks, heavy analytics calculation engines, and downstream integrations are completely separated from the HTTP request-response thread lifecycle using Huey.

Rendering Chart

3.1 Production Worker Ingestion Configuration

The Huey queue infrastructure is tuned to run efficiently on our dual physical server setup. It uses task prioritization to ensure that patient-facing services (like printing prescriptions) are processed ahead of backend tasks (like running accounting reports).

# huey_config.py
from huey import RedisHuey
from django.conf import settings

# Initialize high-performance Huey engine backed by local Redis instance
huey_engine = RedisHuey(
    'hmis-task-scheduler', 
    url=settings.REDIS_URL,
    connection_pool_kwargs={'max_connections': 100}
)

# Task Priority Configuration Boundaries
PRIORITY_CRITICAL = 3   # e.g., Lab machine results ingestion, eMAR push
PRIORITY_STANDARD = 2   # e.g., M-Pesa STK Callback processing, SMS notifications
PRIORITY_LOW      = 1   # e.g., MoH Monthly indicator batch parsing, system data sync

4. Front-End Performance Optimization Layers

The Nuxt 4 frontend application minimizes local memory consumption and stays responsive even on older clinical client terminals (such as thin clients or refurbished core-i3 desktops common in regional facilities).

4.1 Real-Time Dashboard Updates via Server-Sent Events (SSE)

Rather than overloading the network with constant HTTP polling loops for queue status updates, the system uses lightweight, unidirectional Server-Sent Events (SSE). This approach maintains real-time active patient lists with minimal CPU overhead.

4.2 Local Storage Strategy for Client-Side Resiliency

  • State Management Security: All user states are managed using lightweight, reactive Pinia stores inside Nuxt 4.

Document Verification Block

Author: Ian Wataka - Backend DeveloperTarget Scope: Level 4 Hospital Production Infrastructure