DEV Community

Ilya Sib
Ilya Sib

Posted on Originally published at privacyscrubber.com

Microsoft Presidio vs. Local In-Memory Sanitization: Measuring PII Redaction Latency Without Docker or Cloud Proxies

Engineering teams adding Personally Identifiable Information (PII) redaction in front of Large Language Models usually start with the same architecture: spin up Microsoft Presidio inside Docker, load spaCy NER weights, and deploy a Python microservice between the application and the model API.

In production, that design carries an infrastructure tax that outweighs the detection logic itself:

  1. Cold container images exceeding 1.5 GB just to mask emails, tokens, IBANs, and SSNs before calling chat.completions.
  2. Network hops across an internal microservice or external cloud proxy that add serialization latency and interrupt Server-Sent Event (SSE) token streaming.
  3. Unpredictable entity sprawl when a second inference service holds raw cleartext data in memory or HTTP logs, expanding GDPR Article 28 Data Processing Agreement (DPA) audit scope.

An alternative pattern is Zero-Trust Data Sanitization (ZTDS) (IETF draft-sibiryakov-ztds-protocol-02, ztds.ai), where sensitive entities are replaced in local process memory with typed, deterministic surrogates ([EMAIL_1], [IBAN_1], [API_SECRET_1]) before any byte leaves the local runtime, and restored locally after the model responds.

This writeup reports a reproducible local micro-benchmark of the ZTDS Python reference engine (pip install ztds) across 2,000 scoped sessions and compares operational trade-offs against Docker-backed microservice redaction.


Reproducible Local Benchmark (ztds==1.1.2)

We tested 40 100%-synthetic documents spanning four enterprise payload profiles (devsecops_secrets, healthcare_phi, finance_iban_cards, corporate_legal_pii), scaling from short single alerts (~350 bytes) up to multi-record ~40 KB logs and DSAR extracts. Every payload ran 500 scoped iterations on Apple Silicon Python 3 with zero network requests and strict round-trip verification (restore(sanitize(text)) == text).

Workload Profile Mean Payload Size sanitize + reveal p50 sanitize + reveal p95 (~40 KB batch) Full Session w/ RAM Wipe p50 Throughput Peak Traced Memory Bijective Round-Trip
devsecops_secrets 3,528.5 B 0.1109 ms 4.4501 ms 0.6392 ms 4.43 MB/s 98.60 KB 100.0%
healthcare_phi 3,334.3 B 0.1399 ms 7.4701 ms 0.6655 ms 2.55 MB/s 115.45 KB 100.0%
finance_iban_cards 2,978.5 B 0.1291 ms 6.7256 ms 0.6574 ms 2.52 MB/s 111.17 KB 100.0%
corporate_legal_pii 3,588.5 B 0.1514 ms 8.0026 ms 0.6929 ms 2.54 MB/s 122.37 KB 100.0%

Three practical engineering takeaways stand out from the traces:

  • Sub-millisecond hot path: Median in-memory tokenization plus restoration stays between 0.11 ms and 0.15 ms on single prompts and standard clinical/treasury documents. Even the heaviest ~40 KB batch payloads stay under 8.01 ms at p95, compared against 180–720 ms end-to-end round trips documented for remote cloud DLP gateways in our earlier OSF latency study.
  • Zero-container footprint: Peak Python traced memory (tracemalloc) stayed between 98.6 KB and 122.4 KB, compared against hundreds of megabytes of baseline container RAM required by containerized NER pipelines.
  • Teardown cost awareness: Notice the column difference between sanitize + reveal (0.11–0.15 ms) and full context-manager exit (0.64–0.69 ms). That extra ~0.5 ms comes from explicit RAM overwriting and forced garbage collection at session exit (Theorem 2 zeroization). In high-RPS streaming workers, keeping a request-scoped session open across multiple chunks amortizes teardown cost to once per HTTP stream rather than per token chunk.

Code Pattern: Scoped In-Memory Sanitization in 8 Lines

With the open-source Python reference engine (pip install ztds), sensitive payloads never leave the function frame unmasked:

import ztds

with ztds.session() as guard:
    masked_prompt, minted = guard.sanitize(raw_ticket_text)
    # Only "[EMAIL_1]" and "[IBAN_1]" cross the wire to the LLM API
    llm_reply = call_model(masked_prompt)
    # Bijective restoration occurs strictly inside local RAM
    final_output = guard.reveal(llm_reply)
# Out of scope: ephemeral surrogate map is zeroized in memory
Enter fullscreen mode Exit fullscreen mode

For TypeScript, Node.js microservices, and SSE stream un-scrubbing where teams also need pre-built industry compliance packs across 30 regulated verticals (HIPAA Safe Harbor, PCI-DSS, SWIFT, GDPR), see the commercial SDK comparison and migration guide:


When Presidio Still Wins vs. When In-Memory Wins

No single pattern fits every workload:

  1. Choose Microsoft Presidio + spaCy NER when your inputs are unstructured conversational narratives full of ambiguous proper nouns ("Jordan met Austin in Victoria") inside a batch offline ETL cluster where container memory and 100+ ms latency budgets are acceptable.
  2. Choose In-Memory ZTDS (ztds Python or @privacyscrubber/sdk) when you run low-latency RAG pipelines, serverless Edge functions, coding-agent MCP wrappers, or browser/desktop assistants where Docker overhead breaks cold starts, network proxies violate zero-server privacy mandates, or every external subprocessor triggers a legal vendor review.

Next Steps to Test Locally

  1. Run the open benchmark (ztds==1.1.2) against your own synthetic payloads without network access.
  2. Check your outbound LLM requests in DevTools or proxy logs to confirm only synthetic surrogates ([EMAIL_1], [SECRET_1]) cross the wire.
  3. Review the open protocol draft at https://ztds.ai/ and evaluate production runtime requirements on the PrivacyScrubber vs. Presidio breakdown.

Top comments (0)