Optimizing for the money and resources a given level of performance consumes, not just raw speed. Covers cost-per-request reasoning, right-sizing compute and memory, efficiency of resource utilization, and trading performance against spend. Emphasizes treating cost and resource efficiency as first-class performance objectives.
MediumTechnical
104 practiced
Explain benefits and risks of using spot/preemptible instances for large-scale model training. Describe checkpointing strategies (frequency, incremental checkpoints), job orchestration patterns to tolerate preemptions, and cost-aware retry policies. How would you measure whether the additional complexity is worth the cost savings?
Sample Answer
Using spot/preemptible instances can cut training costs dramatically (often 50–90%) by using excess cloud capacity, but they introduce unpredictable interruptions that affect time-to-solution, reliability, and engineering complexity.Benefits:- Large cost savings per GPU/CPU hour.- Ability to scale horizontally at lower budget—useful for hyperparameter sweeps and large parallel workloads.- Good fit for fault-tolerant, checkpointed workloads.Risks:- Preemptions cause wasted work if not checkpointed frequently.- Higher orchestration complexity (retry logic, state management).- Potential longer wall-clock time due to restarts and queue waits.- Variability in availability can destabilize project timelines and SLAs.Checkpointing strategies:- Frequency: balance overhead vs. rework—checkpoint interval T should satisfy expected mean time to preemption (MTTP). A common rule: set T so expected lost work ≈ checkpoint cost (e.g., T ≈ sqrt(2 * checkpoint_time * MTTP) from optimal restart theory).- Incremental checkpoints: save deltas (optimizer states, recent batches) to reduce I/O and upload only changed tensors. Use atomic uploads and versioned manifests.- Multi-tier: local fast saves (NVMe) every short interval + periodic durable (S3/GCS) snapshots. Keep lightweight metadata so training can resume quickly.- Sharded checkpoints for data-parallel (shard per rank) and use coordinated or asynchronous barrier-free saves for elasticity.Job orchestration patterns:- Elastic, stateless workers + centralized parameter server / checkpoint store. Use a controller that spawns workers and rebalances upon join/leave.- Use fault-tolerant frameworks: PyTorch DDP with elastic launches, Horovod with checkpoint-coordinator, or Ray Train for autoscaling.- Leader election for checkpointing: designate a consistent coordinator or use distributed consensus to avoid duplicate expensive uploads.- For hyperparameter sweeps: treat each trial as independent and run primarily on spot capacity; persist intermediate results centrally.Cost-aware retry policies:- Backoff + cap: on preemption, retry with exponential backoff but cap retries per instance type/region.- Preemption-aware bidding: prefer instance pools or zones with lower historical preemption rates; diversify across zones.- Abort when projected cost of remaining retries + expected lost work > cost of switching to on-demand instances for remaining training.- Budget-based policies: set max spend per job and failover to reserved/on-demand when threshold reached.Measuring whether complexity is worth it:- Metric: cost-per-successful-training (total cloud cost / number of completed models) and wall-clock time to completion.- Run A/B experiments: run identical training sets on on-demand vs. spot with implemented resilience. Measure median/95th percentiles of total cost, time-to-convergence, engineering time to implement/maintain.- Include operational metrics: incident frequency, human intervention time, storage costs from checkpoints, queue/wait times.- Compute ROI: (savings in compute spend − additional engineering & storage costs) / engineering effort in FTE-months. Also evaluate business impact of increased time-to-solution.- Decision rule: adopt spot-heavy strategy when cost-per-successful-training decreases materially (e.g., >30%) without unacceptable increase in SLA violations or engineering maintenance burden.In practice: start with low-risk workloads (sweeps, pretraining) to build tooling, use hybrid pools (mix spot + a small on-demand baseline), and continuously monitor preemption statistics to adapt checkpoint frequency and retry policies.
HardTechnical
100 practiced
During an A/B experiment, variant B shows 2x p99 latency compared to control. Outline a thorough root-cause analysis plan spanning code, model, infrastructure, and data. Include steps to collect evidence (traces, logs, flame graphs), run targeted canary rollbacks or shadowing, compare request characteristics between variants, and what instrumentation you'd add to isolate the issue.
Sample Answer
High-level goal: quickly narrow root cause (code/model/infrastructure/data) and gather reproducible evidence so we can fix or mitigate regressions without harming users.Immediate triage (first 30–60 min)- Stop widening the experiment; freeze traffic split.- Capture full metrics for B vs control: p50/p90/p99, error rates, CPU/GPU, memory, IO, queue lengths, service concurrency.- Run a quick canary rollback of B for a small slice if safe to confirm latency reverts (fast verification).Evidence collection (logs, traces, profiles)- Enable/collect distributed traces (Jaeger/Zipkin): sample p99 traces from both variants. Compare spans, service durations, retry loops.- Collect application logs (structured) tied to request IDs, model version, input hash, and serving node.- Produce flame graphs / profiler output (asyncCPU/asyncGPU) for p99 requests from B and for normal requests.- Capture system metrics on nodes (top, iostat, nvtop) and container metrics (cAdvisor).Compare request characteristics- Stratify requests by user, feature flags, request size, input distribution (e.g., sequence length), model route, and upstream latency.- Compute delta in request attribute distributions between variants (chi-square/KL divergence). Look for skewed buckets (e.g., B receiving larger inputs or rare features).- Check whether sampling/traffic routing inadvertently correlated with customer segments.Targeted experiments (canary, rollback, shadowing)- Canary rollback: rollback B for a tiny proportion and observe latency trend to confirm causality.- Shadowing: run control and B in shadow mode on identical inputs to compare end-to-end latency and resource use without affecting responses.- A/B with feature toggles: turn off model-specific features (quantization, caching, batching) in B to isolate the change.Code & model-level checks- Diff model code & serving code: look for changed batching logic, synchronous I/O, serialization/deserialization, logging, or instrumentation that blocks hot path.- Verify model artifacts: check file sizes, loading time, lazy init, memory copies (CPU<->GPU), and changed dependencies (new PyTorch ops, fallback to slow kernels).- Re-run offline deterministic microbenchmarks for single-inference latency and throughput (same hardware, same inputs).Infra & orchestration checks- Verify pod/container resource requests/limits, autoscaler configs, node affinity, and GPU assignment. Look for CPU throttling, OOM kills, or garbage collection.- Check networking: service mesh sidecar versions, protocol changes, connection pool exhaustion, or new TLS configs causing latency.- Confirm launch-time noise (co-located noisy neighbors) by correlating pod start times and latency spikes.Instrumentation to add (short-term)- Add fine-grained timing: deserialize, preprocess, model forward, postprocess, serialize, network send — per request span tags.- Add sampling of raw inputs (redact PII) for slow requests to reproduce offline.- Add resource-usage tagging (GPU index, memory usage) into traces.- Add counters for batching size and queue wait time.Hypothesis-driven iterations- Form 2–3 top hypotheses (e.g., larger inputs to B, fallback to slow kernel, added synchronous logging). For each: design minimal experiment to falsify (toggle, rollback, shadow).- Perform controlled microbenchmarks and replay captured slow inputs against both builds.Validation & rollout plan- Only resume/expand B once p99 parity achieved in shadow/canary and metrics are stable.- Post-mortem: document root cause, fix, test additions, and rollout checklist to prevent recurrence (pre-deploy perf regression tests, better canary gating).Communication- Keep stakeholders informed with short updates: status, evidence, hypothesis, mitigation path, and estimated time to resolution.
MediumTechnical
98 practiced
Design a monitoring and alerting system to detect and prevent ML performance regressions in production. Include which metrics to collect (latency percentiles, error rates, resource utilization, prediction-distribution drift), alert thresholds, canary deployments, and automated rollback mechanisms. Explain how you would correlate model-specific signals (e.g., prediction distribution shifts) with infrastructure metrics.
Sample Answer
Situation & goal: Detect and prevent ML performance regressions in production by combining infra, service, and model-specific signals, and automate safe rollouts and rollback.Metrics to collect- Latency: p50/p95/p99, tail latencies per model version and endpoint- Error rates: 4xx/5xx, model exceptions, failed feature lookups- Resource utilization: CPU/GPU, memory, queue length, pod restart counts- Throughput: requests/sec, batching stats- Model signals: prediction distribution (histograms per key feature and output), confidence/calibration, PSI/KS, KL-divergence over sliding windows, label/feedback latency, business KPIs (CTR, conversion)- Traces & logs: request traces and sampled inputs/outputsAlert thresholds (examples)- p95 latency > 2x baseline OR > 500ms for >5m- Error rate increase > 0.5 percentage points and >2x baseline for 5m- Resource saturation: CPU/GPU >85% for 5m- PSI > 0.2 or KL > 0.1 sustained over N requests or 30m- Drop in business KPI >5% relative to baseline with statistical significanceCanary deployments- Deploy candidate model to small traffic slice (e.g., 1-5%) with mirrored logging of inputs/outputs.- Collect same metrics for canary and control; run statistical tests (e.g., bootstrap on KPI, confidence intervals on prediction distribution, two-sample KS) over a fixed evaluation window.- Require passing gating criteria: no infra regressions, latency within X%, no significant drift (PSI/KL below threshold), no KPI degradation.Automated rollback/promotion- CI/CD pipeline (ArgoCD/Jenkins) orchestrates rollout; use Kubernetes + feature flags or traffic router (Istio/Envoy).- Automated policies: if any critical alert fires for canary (latency/error/resource/detectable drift/KPI drop), automatically shift traffic back to baseline and mark deployment failed; notify on-call and create incident with sampled traces and diffs.- Controlled promotion after N successful intervals.Correlating model signals with infra metrics- Time-series alignment: compute cross-correlation between prediction-shift metrics (e.g., KL over time) and infra/time-based metrics to find lagged relationships.- Joint anomaly detection: build multivariate anomaly detector (e.g., isolation forest, streaming PCA) across model and infra features to surface joint failures.- Causal clues: examine request-level traces—if prediction shifts coincide with increased feature nulls or feature cardinality spikes, that points to input/data issues; if shifts coincide with GC spikes or degraded GPU throughput, investigate infra contention.- Tooling: dashboards that show canary vs baseline splits, heatmaps of feature distribution differences, and linked traces/logs for fast RCA.Operational practices- Sample and store inputs/outputs for audit (privacy-safe), keep shadow labels for periodic re-eval.- Runbooks defining thresholds, investigation steps, and recovery actions.- Periodic drift retraining pipelines and automated retraining triggers guarded by validation tests.This design balances fast detection, statistical rigor in canaries, and safe automated rollback to minimize user impact while surfacing root causes that span data, model, and infra.
MediumSystem Design
82 practiced
Design a caching layer for serving dense embeddings used by a similarity search pipeline. Requirements: handle 100k queries/sec, support TTL-based invalidation, operate under memory constraints, provide high hit rate for popular embeddings, and handle consistency when embeddings are updated. Discuss cache placement (edge vs central), sharding strategy, eviction policy, and a cost vs latency trade-off analysis.
Sample Answer
Requirements (clarified)- 100k QPS, TTL-based invalidation, memory-constrained, high hit rate for popular embeddings, handle updates/consistency, low latency (ms).High-level architecture- Client -> API Gateway / Edge Cache (read-heavy) -> Central Sharded Cache Cluster -> Vector DB / Embedding Store (fallback).- Edge caches (per-region) store hottest embeddings; central cache holds a larger working set; persistent store is authoritative.Cache placement- Edge: low-latency reads for hot items, reduces central load and network cost. Use small in-memory LRU+TTL caches on inference nodes or CDN-like layer.- Central: larger sharded cluster (e.g., Redis Cluster, Memcached, or custom in-memory store optimized for vectors) to serve less-hot items and coordinate invalidations.Sharding strategy- Hash-based consistent hashing on embedding id (or user+item shard key) to evenly distribute memory and QPS; use virtual nodes to rebalance without large moves.- Per-shard vector storage uses compressed float16 or product-quantized representations to meet memory constraints.- Routing: client or gateway computes shard to avoid extra hop.Eviction & TTL- TTL for automatic staleness control (per-item TTL configurable by freshness need).- Eviction: combination of TTL + LFU (or TinyLFU) to favor frequently-accessed embeddings; for memory constraints, use segmented LRU/LFU hybrid.- Keep separate tiers: hot bucket pinned (small, rarely evicted), warm (LFU), cold (evict aggressively).Consistency & updates- Source-of-truth updates write to persistent store and publish update events on a pub/sub (Kafka).- Central and edge caches subscribe: on update, invalidate or push new embedding to caches. Use versioned keys (id:version) to avoid read-your-write anomalies.- For strong consistency on critical paths, route write-through or read-after-write to central shard; otherwise accept eventual consistency with short TTLs.Performance & memory optimizations- Compress embeddings (float16, quantization); store residuals for frequent items.- Batch fetches for misses; prefetch popular neighbors after a miss.- Use pinned cache for top-K popular embeddings (maintained by an access-count background job).Cost vs latency trade-offs- Edge-heavy: lowest latency, higher memory footprint (more replicas) and operational cost. Best when tail latency is critical.- Central-heavy: cheaper (fewer replicas), slightly higher latency and network cost, but easier to manage and consistent.- Hybrid: edge for top 1–5% hot items, central for rest — good compromise.- Compression and PQ reduce memory and cost at small accuracy/latency trade-offs (dequantize on fetch adds CPU).Metrics & monitoring- Track hit-rate (edge/central), miss latency, refresh/invalidation latency, memory per shard, QPS per shard.- Autoscale shards by load; monitor skew and rebalance virtual nodes.Example numbers (rough)- If average embedding 512-d float16 ~ 512*2B = ~1KB. For 1M cached vectors = ~1GB plus overhead. Plan shards accordingly to hit 100k QPS with per-shard QPS limits (e.g., 5–10k QPS/shard) and keep headroom.This design balances low-latency serving for hot vectors with memory-efficient, scalable central caching and robust invalidation/consistency via pub/sub and versioned keys.
EasyTechnical
105 practiced
What tools and techniques would you use to profile an end-to-end ML inference request in production? Cover client-side timing, distributed tracing (e.g., OpenTelemetry), server CPU/GPU profiling (cProfile, perf, nvidia-smi, Nsight), and infrastructure metrics. Describe a basic low-overhead workflow for capturing and correlating data across stages (client, network, preprocess, model, postprocess).
Sample Answer
Start with the principle: add a single correlated identifier (trace_id/request_id) that travels from client → gateway → model server so you can join data sources with low overhead.Tools & techniques- Client-side timing: instrument SDK to capture timestamps for request start, serialize, send, receive, deserialize. Minimal: record high-resolution clocks and attach trace_id in headers. Example: performance.now() or time.monotonic().- Distributed tracing: OpenTelemetry to create lightweight spans (client, network, preprocess, model_inference, postprocess). Use sampling (~1-5%) in steady state; increase to 100% for targeted investigations.- Server CPU/GPU profiling: use cProfile or py-spy for Python CPU stacks (low overhead via sampling), Linux perf for kernel/user hotspots, nvidia-smi for coarse GPU utilization and memory, Nsight Systems / Nsight Compute for deep kernel timing when needed.- Infrastructure metrics: Prometheus for CPU/memory, GPU exporter (DCGM) for GPU metrics, network latency metrics from LB/ingress, and logs to ELK/Cloud Logging.Low-overhead workflow (basic)1. Inject trace_id at client and emit client timing (start, send, receive).2. Auto-instrument server entry with OpenTelemetry to create root span; propagate trace_id via headers.3. Create child spans: deserialize, preprocess, model.execute (short span), postprocess, serialize. Make spans minimal (timestamp + metadata like model_version, batch_size).4. Export traces to a tracing backend (Jaeger/Tempo) with sampling policy.5. Continuously scrape infra metrics (Prometheus) and GPU metrics (DCGM); tag metrics with instance_id and time-range.6. Correlate: join traces (trace_id) with logs and Prometheus series to get resource usage during span windows. For deep parts, trigger on-demand profiler (py-spy / Nsight) using the trace_id to limit duration.Best practices- Prefer sampling and short spans to keep overhead low.- Emit sparse high-cardinality metadata only when troubleshooting.- Automate on-demand “full” profiling using the trace_id to collect heavier artifacts for a short period.- Monitor derived SLOs: p95 end-to-end latency, model-only latency, queue/wait time. Use alerts to capture high-latency traces for deeper analysis.
Unlock Full Question Bank
Get access to hundreds of Performance Cost Optimization & Resource Efficiency interview questions and detailed answers.