How mobile occupational health screening fleets batch-render 500 patient dossiers per shift: 8-worker Chromium context pools, Modulo-10 check-digits, and memory recycling.
The 500-Dossier Shift Crunch
Medisan's mobile occupational health units conduct on-site screenings at manufacturing facilities and shipyards, screening 300 to 500 workers per 8-hour shift across physical and sensory disciplines (audiometry, spirometry, chest X-ray, vision, ECG) alongside laboratory blood draws for indicated worker groups. Each complete dossier spans 6 to 8 pages of clinical charts, reference intervals, and imagery — generating over 3,500 PDF pages in a single run.
At the end of a shift, the on-site team triggers batch generation of all patient dossiers, submitting thousands of complex document pages to background render queues.
Our initial rendering setup suffered from out-of-memory (OOM) crashes under concurrency, and manual paper tracking carried the risk of barcode misattribution. We rebuilt the pipeline around two core improvements: Modulo-10 (Luhn) check-digit validation in our barcode intake, and an asynchronous Playwright browser context pool.
Batch Rendering Throughput vs. Latency
A single 6–8 page dossier takes ~3.6 seconds to render end-to-end. With an 8-worker Chromium context pool running concurrently, the pipeline processes a full shift batch of 500 dossiers (3,500+ pages) in ~3.8 minutes — achieving an effective batch throughput of ~456ms per dossier (~2.2 dossiers/sec).
Modulo-10 (Luhn) Checksums for Barcode Integrity
In industrial screening environments, thermal barcode labels on specimen tubes are vulnerable to scuffing, chemical exposure, or smudging. Using sequential numeric IDs (e.g., 10001, 10002) creates a high risk of single-digit substitution: an optical misread of an '8' as a '3' silently attaches laboratory results to the wrong patient.
We structured all specimen barcodes around the format MDS-YYYYMMDD-NNNNNN-C, appending a Modulo-10 (Luhn) check-digit computed across the payload. For example, MDS-20261014-001518-3 uses '3' as the calculated validator.
The Luhn algorithm catches 100% of single-digit substitution errors and all adjacent transpositions (with the exception of 09 <-> 90 pairs). Most barcode scanners do not validate Luhn internally; we enforce checksum validation in our client scanner input handler before any database query executes, rejecting damaged scans immediately at intake.
utils/barcode_checksum.py (Modulo-10 Checksum)pythonExpand excerptCollapse excerpt
def luhn_checksum(digits_str: str) -> int:
"""Computes Modulo-10 check digit. Detects 100% of single-digit errors and all adjacent transpositions (except 09 <-> 90)."""
digits = [int(d) for d in digits_str]
for i in range(len(digits) - 1, -1, -2):
d = digits[i] * 2
digits[i] = d - 9 if d > 9 else d
return (10 - (sum(digits) % 10)) % 10Tuning the Playwright Browser Pool: Concurrency Without OOM
Spawning a fresh Chromium browser process via chromium.launch() requires 1.8 to 2.2 seconds and approximately 180MB of RAM. Under batch load, process churn quickly exhausts server memory and triggers kernel OOM kills. External CDN asset requests further degrade throughput.
We optimized the rendering backend by maintaining an 8-worker pool of persistent Chromium browser contexts. Calling browser.new_context() creates an isolated rendering environment in ~35ms rather than multiple seconds.
All CSS styles, fonts, and chart scripts are bundled and inlined locally, removing external HTTP round-trips. We elevated file descriptor limits using resource.setrlimit(resource.RLIMIT_NOFILE, (65536, 524288)) to accommodate Chromium's socket demands, and implemented a recycling mechanism that resets each context after 20 documents to prevent V8 memory bloat.
What Failed First: Missing Fonts in Lightweight Containers
Our first container build used Alpine Linux to keep image sizes under 80MB. In local tests with mock ASCII data, everything rendered smoothly.
During initial fleet testing, however, doctors noticed that Turkish characters (ş, ğ, ı, İ, ö, ü) were rendering as empty square boxes across printed health certificates. The base image lacked system TrueType font packages.
Switching the container base to Debian-slim and explicitly installing fonts-liberation and fontconfig resolved the issue completely. It added ~90MB to the image, but eliminated character rendering anomalies across all patient documents.
Process Cleanup & Orphaned Chromium Daemons
Another early issue was hung and orphaned browser processes. When complex SVG charts encountered render timeouts under heavy batch loads, Chromium worker threads occasionally hung indefinitely rather than terminating cleanly.
Over an 8-hour shift, orphaned processes accumulated until the server exhausted file descriptors. We solved this by wrapping each page render inside an asyncio timeout guard and enforcing process tree termination whenever a render times out.
