Practice Hub /
Core InfrastructureHard~40 min interview roundSentryPostHogDatadog

Web Analytics & Error Monitoring SDK (Sentry / PostHog)

Design a lightweight client telemetry SDK capturing unhandled errors, Web Vitals, and clickstreams with ring-buffered queues, PII scrubbing, sampling, and non-blocking `sendBeacon` batching.

1. Interactive System Architecture Diagram

Click any architecture component below to inspect its exact responsibilities and trace connected data flows.

Non-Blocking Init Scrub & Sample Batch Queue sendBeacon / Fetch Host App & Listeners Global Error & Vitals Sampling & Scrubber PII Guard + Rate Cap Event Queue Buffer Batching + Beacon Ingestion Endpoint Fast 202 Ack & SourceMaps Async Script Loader < 8KB Snippet + Replay Split
Selected ComponentHost App & Listeners — Global Error & Vitals
  • Hooks `window.onerror`, `unhandledrejection`, `PerformanceObserver` (LCP/INP/CLS).
  • Wraps `console` / click breadcrumbs cleanly inside a fixed 30-item ring buffer.
  • Safely wraps all SDK internals in `try/catch` so telemetry NEVER crashes the host app!

2. Interactive Trade-Off Simulator

Toggle between real architectural options to compare Pros, Cons, and exactly when to pick each choice in interviews.

Page-Exit Flush Mechanism

How do you guarantee the final analytics/crash batch arrives when the user closes the tab?

Pros

  • Browser schedules `sendBeacon` / `fetch({ keepalive: true })` asynchronously AFTER tab closes without delaying navigation!
  • Avoids deprecated `unload` event that breaks Back/Forward Cache (`bfcache`).

Cons & Pitfalls

  • `sendBeacon` payload limit ~64KB per call — solved cleanly by capping batch size.
Interview Rule of Thumb: Pick `visibilitychange` + `sendBeacon` / `keepalive` for every telemetry SDK.
Sensitive Data & PII Safety

Where should passwords, authorization headers, and email strings be redacted?

Pros

  • Sensitive PII never even crosses the public internet wire or hits raw edge logs.
  • Combines regex scrubbers + `[data-mask]` DOM rules before batch serialization.

Cons & Pitfalls

  • Tiny CPU cost (~0.2ms) per captured error event before enqueueing.
Interview Rule of Thumb: Always scrub PII on the client BEFORE transport, backed by server checks.

3. RADIO Framework Breakdown & Interview Checklist

Review each RADIO pillar cleanly and check off sections as you master them.

0 / 5 Sections Reviewed
R

Requirements & Scope

  • Functional: Unhandled exceptions, promise rejections, Web Vitals, breadcrumbs, custom track events.
  • Non-Functional: < 10KB gzipped core, zero INP main-thread blocking, zero host-app crashes, strict PII privacy.
A

Architecture Components

  • Pre-buffered `init()` stub -> Global Collectors -> Ring Buffer Breadcrumbs -> Sampling/Scrubber -> Batch Transport.
  • Queue flushes asynchronously on idle windows & `visibilitychange` page hide.
D

Event Schema

  • `TelemetryEvent: { eventId, type: "error"|"vital"|"track", ts, release, url, stack?, breadcrumbs[] }`
  • Includes immutable build `release` hash so backend resolves the exact `.map` source file.
I

Interface & API Contracts

  • `SDK.init({ dsn, release, sampleRate, beforeSend })`, `SDK.captureException(err)`, `SDK.track(name, props)`.
  • `POST /api/v1/ingest/batch` receives compressed batch via `sendBeacon` returning `202 Accepted`.
O

Performance & Safeguards

  • Guard recursive error loops if SDK’s own ingestion call fails (`ignore telemetry URLs`).
  • Never upload `.map` SourceMaps to public CDN (`hidden-source-map` in CI directly to Sentry/SDK backend).
  • Flush batches under 64KB using `requestIdleCallback` + `sendBeacon` preserving `bfcache`.