Distributed Systems Security and Trust Questions
Securing communication and trust across a distributed system: service-to-service authentication and authorization, mutual TLS, zero-trust boundaries, secrets management, and multi-party protocols and distributed trust models. Covers securing data in transit and at rest across service boundaries and isolating tenant and network zones. The security-architecture concerns specific to distributed designs.
Design an anti-replay mechanism for asynchronous message processing (e.g., Kafka consumers) where retries and duplicates can cause repeated processing. Requirements: idempotent processing, bounded deduplication window, modest coordination cost, and resilience to consumer restarts. Describe the deduplication data structure, sharding strategy, and how to garbage collect old dedup state.
Sample Answer
Approach overview:
- Use a bounded, durable deduplication store per message partition that supports fast check-and-insert with TTL. Consumers check the store before processing; if ID seen, skip. Keep window bounded by time or count, GC old entries regularly. Minimize coordination by sharding dedup state the same way Kafka partitions are assigned to consumers.
Deduplication data structure:
- Per-partition ordered key-value store mapping message-id -> processing-timestamp (or commit-offset). Implement with a local LRU cache for speed + durable backing (Redis hash with TTLs or RocksDB on disk per consumer).
- For heavy write load use a compacted Kafka topic or RocksDB keyed by (partition, message-id) to persist seen IDs.
Sharding and ownership:
- Align dedup shards with Kafka partitions: each consumer instance owns N partitions via the consumer group protocol. That gives implicit ownership and avoids cross-node coordination.
- Within a consumer, keep dedup state partitioned per Kafka partition to allow parallel GC and checkpointing.
Check-and-insert (atomic):
- Use atomic check-and-set in backing store:
- Redis: HSETNX or SET key with NX and expiry (message-id -> timestamp).
- RocksDB + local lock: batch write the new IDs before processing commit.
- Workflow: receive record -> atomic insert-if-not-exists -> if inserted, process -> on success commit Kafka offset and (optionally) mark processed timestamp; if insert failed, skip and commit offset.
Garbage collection:
- Time-based TTL on each entry (e.g., 24 hours) in backing store to bound window.
- Periodic compaction: remove entries older than retention by scanning per-partition store (efficient in RocksDB with time-based keys or Redis with EXPIRE).
- Tie GC to Kafka offsets: once consumer checkpoints that all offsets <= O are committed and the dedup entries for those offsets are older than retention, they can be safely purged.
Resilience to restarts:
- Durable backing (Redis/RocksDB/compacted Kafka) preserves seen IDs across restarts.
- On rebalance, new owner loads partition's dedup state from durable store + warm local cache.
- Use idempotent processing semantics (operations themselves are safe to replay) as a final defense.
Trade-offs and notes:
- Redis with TTL is simple and low-latency but costs memory; RocksDB is disk-efficient for high cardinality.
- Choosing TTL bounds deduplication window; make it larger than expected retry/backoff window.
- Monitor dedup store size, hit/miss rate, skipped-count, and GC lag; alert on growth or slow scans.
This design meets idempotence, bounded window (TTL), modest coordination (partition ownership), and restart resilience (durable backing + reload on rebalance).
Design a distributed policy decision point (PDP) for authorization that must serve 1,000 decisions/sec with 50ms latency SLA across three regions, while preventing privilege escalation during partitions. Describe how to store and replicate policies, caching strategies at local PDPs, decision versioning, how to roll back a bad policy quickly, and operational monitoring and audit logging requirements for compliance.
Sample Answer
Requirements:
- Functional: 1,000 authorization decisions/sec total (≈333/s per region), 50 ms SLA tail per decision, deny-by-default during partitions to prevent privilege escalation.
- Non-functional: cross-region deployment (3 regions), strong audit logging, fast rollback of policy changes, versioning, high availability.
High-level architecture:
- Central Policy Store (master): authoritative, supports transactional policy updates and versioning (e.g., strongly-consistent DB like CockroachDB or etcd cluster with multi-region replication).
- Regional PDPs (local decision instances): serve decisions, maintain local policy cache, handle requests from regional PDP clients or PDP proxies.
- Control Plane API: for policy authoring, validation, canary rollout, and rollback.
- Message Stream: durable changelog (Kafka or Pulsar) distributing policy deltas to regional PDPs.
- Monitoring & Audit pipeline: logs to OLAP store (ClickHouse/Splunk) and SIEM.
Policy storage & replication:
- Store canonical policies in the Central Policy Store with semantic metadata and immutable versions (policy_id, version, hash, author, timestamp, intent-test results).
- On commit, produce a signed delta event to the changelog containing version, delta, and precomputed compiled artifact (e.g., Rego bundle or WASM).
- Regional PDPs subscribe to changelog; they apply deltas in order and verify signatures + hash to ensure integrity.
Local caching strategy:
- PDPs keep an in-memory compiled-policy bundle (hot cache) and an on-disk bundle for restart. Use lock-free reads (read-copy-update) to serve decisions at low latency.
- Per-request: evaluate against compiled artifact. Use LRU request-result cache for idempotent attribute sets (TTL-limited, size-bounded) to reduce load for repetitive queries.
- If policy bundle missing or stale beyond staleness-window, PDP falls back to deny-by-default and returns a specific error code to trigger client retry/backoff.
Decision versioning & canary:
- Every decision includes policy_version, bundle_hash, and evaluation_trace. Policy versions are monotonic; include semantic “safe” metadata from automated test suite (unit, property, integration).
- Canary rollout: push new bundle to a small subset of PDPs and traffic (e.g., 5%). Run shadow/eval mode where decisions are made with new version but not enforced; compare diffs to detect privilege increases.
- Promote to all regions only after automated and manual sign-off.
Fast rollback:
- Because bundles are immutable and versioned, rollback = publish previous stable version as a new delta (or mark previous version as active). Changelog guarantees ordered application; PDPs will apply revert delta within <1s (depends on streaming latency). Enforce immediate local activation flag to skip canary in emergency.
- Also provide “kill-switch” toggle in Control Plane to set nodes to deny-by-default and throttle policy updates if high-risk changes detected.
Preventing privilege escalation during partitions:
- Deny-by-default out-of-band: PDPs must be configured to refuse decisions when they cannot confirm they have a policy bundle ≤ allowed staleness threshold.
- Require attestation: every bundle has cryptographic signature + sequence number; during partition, PDP refuses to accept unsigned or out-of-order updates.
- Shadow testing and differential checks pre-deploy detect expansions of allow sets; require explicit higher-privilege approval.
Operational monitoring & audit:
- Metrics: decisions/sec, p95/p99 latency, cache hit rate, bundle propagation latency, policy apply errors, number of deny-by-default events, canary diffs, rollback events.
- Alerts: p99 latency > SLA, bundle propagation > threshold, unexpected rise in allows vs baseline, audit mismatches.
- Logs: every decision persisted with request attributes (subject, resource, action), policy_version/hash used, decision (allow/deny), evaluation_trace (redact PII), and decision timestamp. Retention per compliance (e.g., 1 year) stored in tamper-evident storage with write-once logs.
- Tracing: distributed trace IDs for request paths to correlate auth latencies with upstream calls.
- Compliance: signed policy commits, immutable version history, RBAC on Control Plane, multi-person approval for sensitive policy changes, periodic attestations.
Trade-offs:
- Strong consistency for policy authoring avoids split-brain but increases commit latency; we mitigate by distributing compiled bundles via async changelog so decision path remains fast.
- Deny-by-default during partition increases availability risk (more denies) but prevents privilege escalation — acceptable for security-sensitive systems.
Capacity & Sizing:
- Each PDP CPU/memory sized to handle regional peak (≥2x 333/s) with headroom for cache and compiled policy. Benchmark compiled-eval latency; expect <5-20 ms per eval for precompiled rules; remaining budget for network and logging.
This design prioritizes safety (deny-by-default, signed bundles), low-latency decisions via local caches and compiled artifacts, fast rollback through immutable versioned bundles and streaming deltas, and operational observability for SRE and compliance needs.
Case study: After rotating verification keys for an auth service, downstream services were not updated and 40% of requests started failing authentication for 4 hours, requiring rolling restarts and manual fixes. Analyze possible root causes in the rotation and deployment design. Provide immediate remediation steps to restore availability, and propose long-term engineering changes (automation, preflight checks, canaries, monitoring, and fail-safe verification behavior) to prevent recurrence.
Sample Answer
Situation/summary (brief): A verification-key rotation for the auth service was deployed, but downstream services continued using old keys; ~40% of requests failed auth for 4 hours and required rolling restarts/manual fixes.
Possible root causes (design + process):
- Coupling of key config to service process memory — services cache keys on startup and don’t refresh.
- No centralized key distribution or discovery; keys delivered via static config/deployment artifacts.
- Deployment ordering flaw: auth rotated keys before pushing key rollout to consumers.
- Lack of atomic rollout/feature flagging and no canary gating for downstream compatibility.
- Insufficient monitoring/alerts for sudden auth failure rate spikes or key-mismatch errors.
- Missing preflight or compatibility checks and no automated rollback path.
- Manual restarts required because services lacked hot-reload for verification keys.
Immediate remediation (restore availability):
- Backfill: Re-deploy auth with previous keys (rollback) to restore compat quickly.
- Or push old key as a secondary/accepted key in auth (if supported) to accept tokens signed by either key.
- For downstream: trigger controlled rolling restarts with automation to reload updated keys; prioritize high-traffic instances.
- Communicate incident and apply temporary traffic routing to healthy instances/canaries.
- Monitor auth success rate closely until stable.
Short-term fixes during incident:
- Add old key to consumers’ config via centralized config service (e.g., SSM/Consul) and hot-reload where possible.
- Increase observability: enable debug logs for verification failures (key id mismatches).
Long-term engineering changes (prevent recurrence):
Automation & distribution
- Centralize key management (KMS + dynamic config service). Publish keys with versioned metadata and key IDs (kid).
- Implement a push/pull mechanism so services fetch keys at runtime and cache with TTL; support JWKS endpoints for auth keys.
Preflight checks & compatibility
- Add automated compatibility test that validates downstream services can verify tokens signed with new key before switching primary.
- Run integration preflight in CI/CD: deploy new key to staging consumers and run synthetic auth flows.
Canaries & rollout strategy
- Canary the key as secondary: auth should accept tokens signed by both old and new keys during a transition window (dual-sign or dual-accept).
- Gradually shift signing to new key only after canary verification is successful and metrics stable.
Monitoring & alerting
- Add SLOs and real-time alerts for auth failure rate, per-service error spikes, and key-id mismatch metric.
- Create dashboards showing key version usage and token verification success per downstream.
Fail-safe verification behavior
- Support JWKS with key-id lookup; on unknown kid, fetch latest keys before rejecting.
- Implement graceful degradation: if verification service unreachable, fail open only for low-risk endpoints or route to reduced-privilege path—otherwise fail closed but with rapid remediation automation.
Operational tooling & runbook
- Build automated rollout/rollback playbook and one-click rollback for key rotations.
- Provide runbook steps: detect, rollback, dual-accept configuration, restart automation, communication template.
- Schedule rehearsals (chaos drills) for key rotation scenarios.
Team/process
- Require coordinated change windows for key rotations with sign-off from owners of top downstream consumers.
- Post-incident: conduct blameless postmortem, document root causes and action items, track to completion.
Why these changes help:
- Decoupling keys from frozen deployment and enabling runtime refresh avoids manual restarts.
- Dual-accept canaries prevent immediate failure when some consumers lag.
- Preflight tests and monitoring ensure problems are detected early and rolled back automatically, reducing blast radius and MTTR.
Design a multi-region TLS termination strategy for a global API gateway with three goals: low latency for users, compliance with region-specific key residency rules, and the ability to provide end-to-end encryption when required. Describe where to terminate TLS (edge vs origin), key management approaches (per-region keys, key-wrapping), and the SRE operational model for certificate issuance, rotation, and emergency key compromise.
Sample Answer
Requirements:
- Low latency: TLS handshake close to users.
- Region key-residency: private keys must remain in-region for certain customers.
- Optional E2E encryption: some customers require that origin sees decrypted payload only in their region.
High-level approach:
- Use edge TLS termination by default (global CDN/edge PoPs) to minimize latency. Support two modes per customer/service:
- Edge-terminate: TLS at PoP for low latency; re-encrypt to origin with service-to-origin TLS.
- Pass-through / regional-origin-terminate: TLS is TCP-proxied to a regional origin that holds residency keys — provides E2E and residency compliance.
Key management:
- Per-region resident keys stored in region-specific KMS/HSM clusters (cloud KMS with regional isolation or on-prem HSM). Customer-managed keys (BYOK) supported.
- For edge termination where residency allows, edges can use regional replicas of certs’ public parts; private keys never leave region unless policy permits.
- Key-wrapping: maintain a region-local private key; edges that must perform TLS but cannot hold private key use a secure key-wrapping proxy: edges forward TLS handshake token to regional HSM via mutually authenticated TLS or use KMS-as-a-service to perform crypto ops (signing) without exporting key material. Cache session tickets/OCSP stapling at edge to reduce round trips.
Architecture components:
- Global CDN/edge layer (terminates TLS when allowed, caches session tickets)
- Control plane KMS/HSM per region
- Regional termination clusters (origin) for pass-through/E2E
- Routing layer that uses customer policy (latency vs residency) to pick edge vs regional origin
- Certificate manager service for issuance/rotation
SRE operational model:
- Certificate issuance: Automated ACME/private CA integration via certificate manager. For resident keys, issuance requests routed to regional CA/KMS; keys generated in-region (no export).
- Rotation: Automated staging (pre-issue overlapping certs), staged deployment to edges and origin, health checks, gradual rollout using canary edges, automated rollback on failures. Rotation frequency per policy (e.g., 90d) with emergency short-lived certs as needed.
- Monitoring & runbooks: Alerts for expiry windows, high TLS handshake errors, latency regressions. Dashboards for cert state and KMS health.
- Emergency compromise: Immediate control-plane revocation: mark key compromised, update routing policy to force pass-through to non-compromised regional origin or require client revalidation; issue emergency certs/keys in-region; push CRL/OCSP revocations and invalidate session tickets at edge. Runbook: isolate affected region, rotate keys in-region, coordinate customer notifications, post-incident review.
- Tests & DR: Regular key-rotation drills, failover tests (edge->origin pass-through), pen tests for HSM/KMS access.
Trade-offs:
- Edge termination gives best latency but increases complexity to honor residency constraints (requires secure remote crypto ops).
- Pass-through preserves residency and E2E but adds latency and load on regional origins.
- Key-wrapping remote ops add network hops — mitigate with session caching.
This design balances latency and compliance by making termination mode policy-driven, keeping private keys in-region when required, and automating SRE flows for issuance, rotation, and emergency response.
You're evaluating integrating a cloud HSM-backed KMS for signing keys used by microservices. Describe an SRE integration plan covering key hierarchy (root/wrapping/data keys), envelope encryption, replication strategy across regions for DR, latency/performance considerations for signing at 10k req/sec, caching and rate limits, fallback modes when HSM is unavailable, and how to satisfy audit and compliance requirements.
Sample Answer
High-level plan (goal: secure, highly-available signing with HSM-backed KMS, meet 10k req/sec, DR, and audit).
Key hierarchy & envelope encryption
- Root/Cloud HSM master key (Customer-managed HSM) stored only in HSM; used to wrap/unwarp (encrypt) intermediate wrapping keys.
- Regional wrapping keys (KMS keys created in HSM per region) wrapped by root; used to encrypt (wrap) service data keys.
- Per-service ephemeral data keys (symmetric) used to sign or MAC payloads locally via envelope encryption: services request a data key (DecryptKey API) that returns encrypted data key and plaintext for short TTL, or services use KMS to generate data keys and cache locally.
- Asymmetric signing keys: keep private signing keys in HSM where possible; otherwise store sealed signing key encrypted by wrapping key and loaded into a trusted signing process.
Replication & DR
- Create HSM-backed KMS keys in each active region; replicate wrapped key material via secure, authenticated backup storage (encrypted by root) to passive DR regions.
- Periodically export wrapped-wrapped backups (not plaintext) to DR, run automated restore drills.
- Use multi-region active-active for services that require low-latency; fallback cross-region KMS calls only for failover.
Latency & performance for 10k req/sec
- Avoid per-request HSM round-trip for symmetric operations: use envelope encryption and local in-memory or sidecar caches of plaintext data keys with short TTL (e.g., 1–5m) and secure memory management.
- For asymmetric signatures where HSM is required per op, batch sign or use a signing pool of HSM-backed instances; measure HSM ops/sec and provision multiple HSM slots/instances to meet throughput.
- Target latency budget (e.g., signing <10ms): benchmark HSM signing latency, size pool to meet 99.9th percentile.
Caching & rate limits
- Cache decrypted data keys in each service instance with LRU + TTL, protect with mlock-like prevention of swap and zeroization on eviction.
- Implement token bucket client-side to smooth bursts; central rate-limits via KMS quotas. Monitor and alert on nearing limits.
- Use local sidecar signer that mediates HSM calls across many app threads to prevent exceeding KMS QPS.
Fallback modes when HSM unavailable
- Graceful degrade: use cached plaintext data keys for TTL window to continue signing; mark metrics and reduce security posture (short window).
- If no cached keys: switch to pre-authorized fallback keys stored in sealed vault (e.g., Vault with auto-unseal using a different KMS) accessible only during declared outage and subject to strict audit/approval.
- Queue signing requests and async-sign once HSM restored for non-real-time transactions; for auth-critical flows, fail closed if required by policy.
- Run automated failover playbooks and health checks to prevent silent use of fallback keys.
Audit & compliance
- Enable immutable KMS/HSM audit logs (cloud audit, tamper-evident) forwarded to SIEM and WORM storage.
- Record every key operation with principal, IP, operation, reason, and request ID; correlate service request ID with signing event.
- Maintain key rotation policy and automatic rotation workflows; retain old wrapped keys for verification and forensic.
- Use HSM attestation (certificate) and periodic re-attestation; capture M of N admin approval for key material exports.
- Periodic pen-tests, KMS configuration reviews, and compliance evidence (SOC2/ISO) automation.
Operational & SRE controls
- Define SLOs: e.g., signing availability 99.99%, p99 latency <X ms; define runbooks, automated remediation (recreate wrapping key, failover).
- Monitoring: HSM health, KMS ops/sec, error rates, cache hit ratio, key usage anomalies (alertable).
- Chaos drills: simulate HSM outage, key compromise, region failover; validate rollback and audit trails.
This plan balances security (HSM root protection, wrapped keys, attestation, audit) with reliability and performance (caching, signing pools, regional replicas, clear fallback policies).
Unlock Full Question Bank
Get access to all 45 Distributed Systems Security and Trust interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.