Error Handling and Defensive Programming Questions
Making code robust against bad input and failure: exceptions versus error returns, input validation, guard clauses, graceful degradation, and designing for the unhappy path. Covers where to handle versus propagate errors and how to fail safely without hiding bugs. A recurring probe of production maturity.
Tail latency has increased because of downstream timeouts. Explain how you would implement timeouts and deadlines at multiple layers to protect the system: client SDK timeouts, HTTP client timeouts, worker thread-pool task deadlines, and a request-level deadline propagated via context or headers across service calls. Include pseudocode showing how you would enforce a global deadline and cancel subtasks when it expires, and walk through an example timeline for a request touching three services.
Design a pragmatic escalation policy and documentation template for residual risks you cannot fully mitigate (a low-probability hardware bug, a remaining single point of failure, a partial-data-loss scenario). Describe the contents of a risk-register entry (risk description, likelihood, impact, mitigation, owner, priority, deadline, escalation path), how to monitor leading indicators, and when to trigger human escalation rather than let a threshold sit silently.
Explain idempotency: what it means for an HTTP API or a distributed-systems operation, and why it matters for safely retrying after a failure. Give concrete examples of idempotent versus non-idempotent operations (for example, a PUT that sets a field to an absolute value versus a POST that increments a counter or charges a payment). Describe at least two simple techniques to achieve idempotency (an idempotency key with a stored response, a unique constraint or dedup at the database level) and their trade-offs (storage cost, key expiration, concurrency).
Design a consistent error RESPONSE schema for an HTTP API used by both external clients and internal services: correlation id, a machine-readable error code, a human-readable message, optional field-level validation details, and a trace id. Map common failure situations (validation error, auth failure, transient downstream failure, rate limit) to HTTP status codes, distinguishing 4xx from 5xx with concrete edge-case examples of each. Provide a JSON example for a validation-failure payload, and explain how you would version the schema and keep it backward compatible without leaking internal implementation details.
Design an SLO-based alerting strategy that minimizes pager/alert fatigue: what metrics feed the SLO, symptom alerts versus cause alerts, using an error budget to gate alerting, minimum sample sizes, and grouping/sampling strategies for a noisy downstream integration that would otherwise drown out real signals. Sketch a PromQL-like expression for 'error ratio exceeds 1% over 5 minutes with at least 1000 requests'.
Unlock Full Question Bank
Get access to all Error Handling and Defensive Programming interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.