<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Stanislav Lazarenko on Medium]]></title>
        <description><![CDATA[Stories by Stanislav Lazarenko on Medium]]></description>
        <link>https://medium.com/@staskoltsov?source=rss-e15c42fd98e0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*EMIfE4fakJA-uzS3</url>
            <title>Stories by Stanislav Lazarenko on Medium</title>
            <link>https://medium.com/@staskoltsov?source=rss-e15c42fd98e0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 15:50:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@staskoltsov/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Cloud Models & Scaling: Picking the Right Abstraction (Interview Prep)]]></title>
            <link>https://staskoltsov.medium.com/cloud-models-scaling-picking-the-right-abstraction-interview-prep-64886175c10d?source=rss-e15c42fd98e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/64886175c10d</guid>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Stanislav Lazarenko]]></dc:creator>
            <pubDate>Mon, 03 Aug 2026 09:53:53 GMT</pubDate>
            <atom:updated>2026-08-03T09:53:53.513Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Part of the Senior Python Interview Prep series.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WQk8DP3te0WYbxrxkFdWsg.png" /></figure><p>Two cloud questions show up in almost every senior interview, and they’re really the same question asked from two angles:</p><ul><li><em>“Which cloud model would you use for this workload — IaaS, PaaS, FaaS, or SaaS?”</em></li><li><em>“How would you scale this service to 10x its traffic?”</em></li></ul><p>Both are testing whether you can reason about a <strong>spectrum of abstraction and a set of trade-offs</strong>, rather than reaching for whatever you used last. The first is about how much of the stack you let the cloud manage; the second is about how that choice plays out when load grows. This article handles them together, because the answer to one constrains the other.</p><h3>The service models as a spectrum of control vs operational burden</h3><p>Don’t memorize four definitions in isolation — understand them as <strong>one sliding scale.</strong> As you move down, you give up control and gain freedom from operational work:</p><ul><li><strong>IaaS (Infrastructure as a Service)</strong> — raw VMs (EC2, Compute Engine). You manage the OS, patching, scaling, and runtime. Maximum control and flexibility, maximum operational burden. Use when you need specific OS-level control or are lifting-and-shifting existing apps.</li><li><strong>PaaS (Platform as a Service)</strong> — you deploy code, the platform manages the runtime, OS, and scaling (App Engine, Elastic Beanstalk, Heroku). You trade control for speed of delivery. Use when you want to ship application code without babysitting servers.</li><li><strong>FaaS (Function as a Service / serverless)</strong> — you deploy individual functions that run on events; the platform handles everything, including scaling to zero (Lambda, Cloud Functions). Least operational burden, least control. Pay only per invocation.</li><li><strong>SaaS (Software as a Service)</strong> — you consume finished software (Gmail, Salesforce). No development of the thing itself; you just use it.</li></ul><p>The single sentence that frames the whole answer: <strong>“It’s a spectrum from IaaS to SaaS trading control for reduced operational burden — so the right choice depends on how much of the stack I actually need to control versus how much I’d rather not manage.”</strong></p><h3>Picking the model — and the serverless trade-off you must name</h3><p>The senior signal is matching the model to the workload <em>with the caveat</em>:</p><ul><li><strong>FaaS shines for spiky, event-driven, short-lived work</strong> — a webhook handler, image-thumbnail generation on upload, a scheduled job. It scales automatically and costs nothing when idle.</li><li><strong>But FaaS is wrong for long-running or latency-critical workloads</strong>, and you must name <em>why</em>: <strong>cold starts</strong> (the first invocation after idle pays a startup penalty, bad for latency-sensitive paths), <strong>execution time/memory limits</strong>, and the difficulty of holding persistent connections. A steady, high-throughput, latency-sensitive service often belongs on containers or PaaS, not FaaS.</li></ul><p>For <strong>PaaS vs IaaS</strong>: choose PaaS when you want to move fast and don’t need OS-level control; choose IaaS when you need specific control (custom kernel modules, particular networking, compliance requirements) or are migrating something that assumes a full server. The trade-off line: <strong>PaaS buys speed by taking away control — fine until you need the control it took.</strong></p><h3>Now the scaling angle: compute choices under load</h3><p>“Scale this to 10x” forces you to pick a compute substrate and a scaling strategy:</p><ul><li><strong>VMs</strong> — full isolation, heavier, slower to spin up.</li><li><strong>Containers</strong> — lightweight, portable, fast to start and scale; the common default for scalable services (orchestrated by Kubernetes).</li><li><strong>Serverless</strong> — scales automatically, but with the cold-start/limits caveats above.</li></ul><p>And the two ways to scale anything:</p><ul><li><strong>Vertical scaling (scale up)</strong> — a bigger machine (more CPU/RAM). Simple, but it has a ceiling and is a single point of failure.</li><li><strong>Horizontal scaling (scale out)</strong> — more instances behind a load balancer. Effectively unlimited and more resilient, but it requires your application to be designed for it.</li></ul><p>The senior answer for 10x is almost always: <strong>scale out horizontally — multiple stateless instances behind a load balancer with autoscaling</strong> — not just “buy a bigger box.”</p><h3>Why stateless is the linchpin — and the Python/GIL angle</h3><p>This is the highest-value point in the whole answer. <strong>Horizontal scaling only works if your instances are stateless.</strong> If a server stores session data, in-memory caches, or user state locally, then a load balancer sending the next request to a different instance breaks things, and you can’t freely add or remove instances. So you push state <em>out</em> — to a shared store (Redis, a database) — leaving the application instances interchangeable and disposable. Then you can add, remove, or replace them at will, which is the entire point of autoscaling.</p><p>The Python-specific layer that impresses interviewers: <strong>the GIL means a single Python process can’t use multiple CPU cores for CPU-bound work.</strong> So you don’t scale a Python service by relying on threads — you run <strong>multiple worker processes</strong> (gunicorn/uvicorn workers), and you scale <em>out</em> across machines. The GIL essentially pushes Python toward a multi-process, horizontally-scaled, stateless architecture by default. Connecting the GIL to a deployment decision shows depth most candidates don’t reach.</p><h3>How autoscaling actually decides</h3><p>The follow-up: <em>“What triggers autoscaling?”</em> Scaling policies react to metrics — <strong>CPU utilization</strong> (the classic), <strong>memory</strong>, <strong>request count / RPS per instance</strong>, <strong>queue depth</strong> (for worker pools — scale on backlog), or <strong>p95 latency.</strong> You set thresholds (e.g. “add an instance when average CPU &gt; 70% for 5 minutes”) and, crucially, scale <em>down</em> on the way back to control cost. The senior nuance: pick the metric that actually reflects <em>your</em> bottleneck — a queue-backed worker should scale on queue depth, not CPU.</p><h3>Your interview answer, in four beats</h3><ol><li><strong>What it is:</strong> “Cloud models are a control-vs-operational-burden spectrum; scaling is choosing a substrate and a strategy on top of that.”</li><li><strong>The options/trade-offs:</strong> “IaaS→SaaS trades control for less ops; FaaS is great for spiky events but bad for latency (cold starts); scale horizontally with stateless instances behind a load balancer.”</li><li><strong>What I’d pick and why:</strong> “For 10x I scale out stateless containers with autoscaling, push state to Redis/DB, and for Python run multiple processes because the GIL blocks multi-core in one process.”</li><li><strong>From my experience:</strong> “I’ve worked across AWS and GCP with containers, serverless (Lambda), and managed data services, choosing the model to fit the workload.”</li></ol><h3>Follow-up questions they’ll fire next</h3><ul><li><strong>“Downside of serverless?”</strong> → Cold starts hurt latency, execution time/memory limits, hard to hold persistent connections, and harder local debugging — wrong for long-running or latency-critical work.</li><li><strong>“When PaaS over IaaS?”</strong> → When you want to ship code fast without managing servers and don’t need OS-level control; go IaaS when you need that control or are lifting-and-shifting.</li><li><strong>“Why prefer stateless?”</strong> → So instances are interchangeable — any can handle any request and you can add/remove them freely, which is what makes horizontal autoscaling possible.</li><li><strong>“What triggers autoscaling?”</strong> → Metrics like CPU, memory, RPS, queue depth, or latency against thresholds — pick the one that reflects the real bottleneck (queue depth for worker pools).</li></ul><h3>The takeaway</h3><p>Both questions reward the same thinking: <strong>understand the abstraction spectrum, name the trade-off, and pick to fit the workload.</strong> Serverless for spiky events (mind the cold starts), horizontal-stateless-behind-a-load-balancer for scale, and — the detail that marks a Python senior — multiple processes because the GIL won’t let one use all your cores. Tie the model choice to the scaling consequence and you’ve answered both in one coherent breath.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=64886175c10d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[“Monolith or Microservices?” — Answer Like You’ve Run Both (Interview Prep)]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://staskoltsov.medium.com/monolith-or-microservices-answer-like-youve-run-both-interview-prep-3857749f02c9?source=rss-e15c42fd98e0------2"><img src="https://cdn-images-1.medium.com/max/1536/1*5d3h5snUZxMBL9XhP8mOmw.png" width="1536"></a></p><p class="medium-feed-snippet">Part of the Senior Python Interview Prep series.</p><p class="medium-feed-link"><a href="https://staskoltsov.medium.com/monolith-or-microservices-answer-like-youve-run-both-interview-prep-3857749f02c9?source=rss-e15c42fd98e0------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://staskoltsov.medium.com/monolith-or-microservices-answer-like-youve-run-both-interview-prep-3857749f02c9?source=rss-e15c42fd98e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/3857749f02c9</guid>
            <category><![CDATA[interview-questions]]></category>
            <dc:creator><![CDATA[Stanislav Lazarenko]]></dc:creator>
            <pubDate>Mon, 03 Aug 2026 09:53:13 GMT</pubDate>
            <atom:updated>2026-08-03T09:53:13.001Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[OOP, SOLID, Patterns & Dunders: The Senior Python Refresher (Interview Prep)]]></title>
            <link>https://staskoltsov.medium.com/oop-solid-patterns-dunders-the-senior-python-refresher-interview-prep-e4e942b7b99e?source=rss-e15c42fd98e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/e4e942b7b99e</guid>
            <category><![CDATA[interview-preparation]]></category>
            <dc:creator><![CDATA[Stanislav Lazarenko]]></dc:creator>
            <pubDate>Fri, 05 Jun 2026 06:59:00 GMT</pubDate>
            <atom:updated>2026-06-05T06:59:00.443Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Part of the Senior Python Interview Prep series.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9RXc_y-PE7tnwJJrlCtTyw.png" /></figure><p>There’s a cluster of fundamentals that a senior interviewer fires rapid-fire to calibrate you fast: <em>“Explain SOLID. Give me a pattern you’ve used. What’s the difference between </em><em>__new__ and </em><em>__init__? When would you define </em><em>__slots__?&quot;</em> None is hard on its own, but the bar is higher than a textbook definition — they want <strong>the trade-off and a real use</strong>, in two sentences, without rambling. This is the refresher for that round: OOP foundations, SOLID, the patterns worth naming, and the dunder methods that actually come up.</p><h3>Part 1: OOP foundations (the version they actually probe)</h3><p>Everyone can recite “encapsulation, inheritance, polymorphism, abstraction.” Seniors get probed on the Python-specific mechanics underneath:</p><p><strong>The MRO and </strong><strong>super().</strong> Python uses <strong>C3 linearization</strong> to compute the Method Resolution Order — the deterministic order in which base classes are searched. super() doesn&#39;t mean &quot;call my parent&quot;; it means &quot;call the next class in the MRO,&quot; which is why cooperative multiple inheritance works at all. You can inspect it with Class.__mro__. Knowing super() follows the MRO, not a hardcoded parent, is the distinction that separates a real understanding from a memorized one.</p><p><strong>Composition over inheritance.</strong> A senior reflexively prefers composition (an object <em>has-a</em> collaborator) over deep inheritance hierarchies (an object <em>is-a</em>). Inheritance is rigid and couples subclasses to base internals; composition is flexible and testable. “Favor composition over inheritance” is a principle you should be ready to defend with the brittleness of deep hierarchies.</p><p><strong>Abstract base classes.</strong> abc.ABC plus @abstractmethod lets you define an interface that subclasses <em>must</em> implement, failing at instantiation if they don&#39;t. This is how you enforce a contract in a duck-typed language.</p><h3>Part 2: SOLID — with a one-line “why” each</h3><p>The trap is reciting the acronym without saying what each <em>prevents</em>. Pair every letter with the pain it avoids:</p><ul><li><strong>S — Single Responsibility.</strong> A class should have one reason to change. <em>Why:</em> mixing concerns (say, business logic + persistence) means a change to one breaks the other.</li><li><strong>O — Open/Closed.</strong> Open to extension, closed to modification. <em>Why:</em> you add behavior by adding code (a new subclass/strategy), not by editing tested code and risking regressions.</li><li><strong>L — Liskov Substitution.</strong> A subtype must be usable anywhere its base is, without surprises. <em>Why:</em> if a subclass violates the base’s contract (a Square that breaks Rectangle&#39;s width/height assumptions), polymorphism silently breaks.</li><li><strong>I — Interface Segregation.</strong> Many small, focused interfaces beat one fat one. <em>Why:</em> clients shouldn’t be forced to depend on methods they don’t use.</li><li><strong>D — Dependency Inversion.</strong> Depend on abstractions, not concretions; inject dependencies. <em>Why:</em> high-level logic shouldn’t be welded to a specific database or API — inject it so you can swap and test.</li></ul><p>The senior addition: <strong>know when SOLID is over-applied.</strong> Aggressively splitting every class or abstracting prematurely (YAGNI violation) creates more indirection than value. “I apply SOLID where change is likely, not dogmatically” is a mature thing to say. Round it out with <strong>DRY</strong> (don’t repeat knowledge — but beware premature abstraction of things that only <em>look</em> similar) and <strong>KISS</strong>.</p><h3>Part 3: Design patterns worth naming — with where they live</h3><p>Don’t list all 23 GoF patterns. Name a handful you’ve actually used and say <em>where</em>:</p><ul><li><strong>Strategy</strong> — swap an algorithm at runtime behind a common interface. <em>In practice:</em> different handling per data source or per file type — pick the strategy by input. (In Python this is often just passing a function, since functions are first-class.)</li><li><strong>Factory</strong> — centralize and defer object creation. <em>In practice:</em> creating the right parser/client based on a config value, so callers don’t hardcode classes.</li><li><strong>Singleton</strong> — one shared instance. <em>In practice:</em> a config object or a connection pool. <em>Trade-off to name:</em> singletons are effectively global state, which hurts testability — in Python a module is already a singleton, so reach for it sparingly.</li><li><strong>Decorator</strong> — wrap an object/function to add behavior without changing it. <em>In practice:</em> Python’s @decorator syntax <em>is</em> this pattern — logging, caching (lru_cache), auth checks, retries.</li><li><strong>Adapter</strong> — translate one interface into another. <em>In practice:</em> wrapping a third-party library so your code depends on your interface, not theirs.</li><li><strong>Observer</strong> — publish/subscribe; notify dependents of changes. <em>In practice:</em> event systems, signals.</li><li><strong>Repository</strong> — abstract data access behind a collection-like interface. <em>In practice:</em> sits over your ORM so business logic doesn’t know about SQL.</li></ul><p>The senior framing: patterns are a <strong>shared vocabulary</strong>, not a checklist to force into code. Two things to be ready for — <em>“a pattern you overused?”</em> (honest answer: forcing a Singleton or over-abstracting with a Factory where a function would do) and <em>“the most Pythonic pattern?”</em> (decorators, because the language builds the pattern in).</p><h3>Part 4: Magic / dunder methods — the ones that come up</h3><p>“Dunder” = double-underscore. They let your objects integrate with Python’s syntax and protocols. The senior-relevant ones:</p><p><strong>__new__ vs </strong><strong>__init__.</strong> The classic question. __new__ <em>creates and returns</em> the instance (allocation); __init__ <em>initializes</em> the already-created instance (it returns nothing). You override __init__ 99% of the time; you override __new__ only for special cases — immutable types (you can&#39;t change a tuple/str in __init__ because it already exists), singletons, or metaclass-level control. &quot;__new__ constructs, __init__ configures&quot; is the crisp answer.</p><p><strong>__eq__ and </strong><strong>__hash__ — define them together.</strong> If you override __eq__ (value equality), you should define __hash__ too, because objects that compare equal must hash equal to behave correctly in sets and dict keys. Defining __eq__ alone makes the object unhashable by default. And <strong>mutable objects generally shouldn&#39;t be hashable</strong>, because if a key&#39;s hash changes after insertion, you can&#39;t find it again.</p><p><strong>__repr__ vs </strong><strong>__str__.</strong> __repr__ is the unambiguous, developer-facing representation (ideally reconstructs the object); __str__ is the readable, user-facing one. If you only define one, define __repr__ — it&#39;s the fallback for __str__ and what you see in logs and the debugger.</p><p><strong>Context managers — </strong><strong>__enter__ / </strong><strong>__exit__.</strong> Power the with statement for guaranteed resource cleanup (files, locks, connections) even on exceptions. __exit__ receives any exception info and can suppress it. contextlib.contextmanager lets you write one as a generator instead.</p><p><strong>Iteration — </strong><strong>__iter__ / </strong><strong>__next__.</strong> Make an object usable in a for loop. __iter__ returns the iterator; __next__ yields the next value and raises StopIteration when done.</p><p><strong>__call__.</strong> Makes an instance callable like a function — useful for stateful callables, decorators-as-classes, and factories.</p><p><strong>__getattr__ / </strong><strong>__setattr__.</strong> Intercept attribute access — powerful for proxies and lazy loading, but easy to misuse and make code hard to follow.</p><p>The senior point about dunders: they’re how Python achieves <strong>duck typing through protocols</strong> — your object “is” a sequence/iterator/context manager if it implements the right dunders, no inheritance required. That protocol-based design is the heart of Pythonic OOP.</p><h3>Honorable mention: dataclasses</h3><p>Worth a sentence because it ties this together. @dataclass auto-generates __init__, __repr__, and __eq__ from annotated fields, with frozen=True for immutability and field(default_factory=list) to safely default mutable fields. It&#39;s the modern way to write data-holding classes without dunder boilerplate — knowing it signals you write current Python.</p><h3>Your interview answer, in four beats (for any of these)</h3><ol><li><strong>What it is:</strong> crisp definition — “SOLID is five principles for change-tolerant OOP,” “dunders are protocol hooks into Python syntax.”</li><li><strong>The options/trade-offs:</strong> the distinction they’re after — “__new__ allocates, __init__ configures,&quot; &quot;composition over inheritance because deep hierarchies are brittle.&quot;</li><li><strong>What I’d pick and why:</strong> “I apply SOLID where change is likely, not dogmatically; I prefer composition and reach for patterns as vocabulary, not a checklist.”</li><li><strong>From my experience:</strong> a real placement — “Strategy per data source, Repository over the ORM, decorators for cross-cutting concerns like retries and caching.”</li></ol><h3>Follow-up questions they’ll fire next</h3><ul><li><strong>“__new__ vs </strong><strong>__init__?&quot;</strong> → __new__ creates and returns the instance (needed for immutables/singletons); __init__ initializes it and returns nothing.</li><li><strong>“Why define </strong><strong>__hash__ with </strong><strong>__eq__?&quot;</strong> → Equal objects must hash equal to work in sets/dicts; overriding __eq__ alone makes the object unhashable. Mutable objects shouldn&#39;t be hashable.</li><li><strong>“Composition or inheritance?”</strong> → Prefer composition — deep inheritance couples subclasses to base internals and is brittle; compose collaborators for flexibility and testing.</li><li><strong>“A pattern you overused?”</strong> → Honest answer: forcing a Singleton or over-abstracting with a Factory where a plain function would do — patterns are vocabulary, not a goal.</li><li><strong>“What does </strong><strong>super() actually do?&quot;</strong> → Calls the next class in the MRO (C3 linearization), not necessarily the direct parent — which is what makes cooperative multiple inheritance work.</li></ul><h3>The takeaway</h3><p>This round isn’t about knowing the definitions — it’s about pairing each with <strong>the trade-off and a real use, fast.</strong> SOLID with the pain each prevents (and when it’s over-applied); patterns as shared vocabulary placed in real layers; dunders as Python’s protocol system; and the precise distinctions (__new__ vs __init__, __eq__ with __hash__, composition over inheritance) that separate a memorized answer from a lived one.</p><p><em>Next in the series: continue through the design and process topics.</em></p><p><em>Tags: Python, OOP, SOLID, Design Patterns, Interview</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e4e942b7b99e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How Senior Engineers Talk About Code Quality (Interview Prep)]]></title>
            <link>https://staskoltsov.medium.com/how-senior-engineers-talk-about-code-quality-interview-prep-59ed45e4fd87?source=rss-e15c42fd98e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/59ed45e4fd87</guid>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Stanislav Lazarenko]]></dc:creator>
            <pubDate>Wed, 03 Jun 2026 12:33:52 GMT</pubDate>
            <atom:updated>2026-06-03T12:33:52.572Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Part 1 of the Senior Python Interview Prep series.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tL-b-aA-uLNCo8-9w0fHvQ.png" /></figure><p>When a junior engineer is asked, “How do you measure code quality?”, they usually say “write clean code and add tests.” It’s not wrong — it’s just not an answer. It has no metrics, no trade-offs, and nothing the interviewer can dig into.</p><p>At the senior level, this question is a trap with a specific shape. The interviewer isn’t checking whether you <em>value</em> quality. They’re checking whether you can <strong>define it measurably, enforce it across a team, and explain when stricter isn’t better.</strong> Here’s how to answer like someone who has actually owned quality on a real project.</p><h3>What “code quality” actually means</h3><p>Code quality is the degree to which code is correct, maintainable, readable, and safe to change. The important word is <em>measurable</em> — if you can’t attach a number or a gate to it, an interviewer will assume you’re describing a feeling.</p><p>In practice, it breaks into two layers: <strong>automated enforcement</strong> (tools that fail the build) and <strong>human practices</strong> (review, standards, shared understanding). A senior answer names both, because tools catch the mechanical problems and humans catch the design ones.</p><h3>The tools layer</h3><p>This is the easy part to enumerate, so be crisp and move on:</p><ul><li><strong>Formatters</strong> — black, ruff format. Eliminate style debates entirely; the formatter is the style guide.</li><li><strong>Linters</strong> — ruff, flake8, pylint. Catch unused imports, shadowing, complexity violations.</li><li><strong>Type checkers</strong> — mypy, pyright. Catch a whole class of bugs before runtime.</li><li><strong>Pre-commit hooks</strong> — run all of the above before code even leaves the developer’s machine, so CI isn’t the first line of defense.</li></ul><p>The senior framing: these belong in CI as <strong>gates</strong>, and their config lives <strong>committed in the repo</strong>, so the whole team is held to the same bar automatically rather than by reminder.</p><h3>The metrics — and the trap inside them</h3><p>This is where the question is really decided. Name real metrics:</p><ul><li><strong>Test coverage</strong> — % of code exercised by tests.</li><li><strong>Cyclomatic complexity</strong> — number of independent paths through a function; high numbers signal hard-to-test, bug-prone code.</li><li><strong>Code duplication</strong> — copy-paste density.</li><li><strong>Maintainability index</strong> — a composite score some tools produce.</li><li><strong>Defect escape rate</strong> — bugs that reach production vs caught earlier. This is the one that actually correlates with the quality the business feels.</li></ul><p>Now the trap. If you stop at “we aim for high test coverage,” a good interviewer will ask: <em>“Is 100% coverage good?”</em> The senior answer is that <strong>coverage is a vanity metric on its own</strong> — it tells you a line <em>ran</em>, not that it was <em>meaningfully asserted</em>. You can have 100% coverage with zero useful assertions. Pair coverage with mutation testing (does a test actually fail when you break the code?) and treat it as a floor, not a goal. Showing you know the limits of a metric is the single strongest signal in this whole answer.</p><h3>“It depends on context” — and how to prove you mean it</h3><p>The matrix-level skill here is <em>establishing</em> quality practices “in a given context.” So make the context explicit:</p><p>A fast-moving early-stage product and a regulated financial data platform do not deserve the same gates. On a startup MVP, heavy gating slows learning, and the cost of a bug is low — lighter linting, pragmatic coverage. On a hedge-fund data platform where a wrong number is a real-money incident, you justify strict type checking, high coverage on transformation logic, mandatory review, and data-validation tests. <strong>The skill isn’t “maximum strictness everywhere” — it’s matching the gate to the risk.</strong> That sentence alone marks you as senior.</p><h3>Your interview answer, in four beats</h3><p>Rehearse this shape, and you can answer almost any quality question:</p><ol><li><strong>What it is:</strong> “Quality is correctness plus maintainability, and it has to be measurable.”</li><li><strong>The options/trade-offs:</strong> “Automated gates — format, lint, type, coverage — plus human review. Metrics like complexity, duplication, and defect escape rate. Coverage alone is a vanity metric.”</li><li><strong>What I’d pick and why:</strong> “I match gates to risk — lighter on an MVP, strict on financial/regulated code.”</li><li><strong>From my experience:</strong> “On a data platform, I enforced validation tests and type checking on transformation logic because a wrong value was a real incident.”</li></ol><h3>Follow-up questions they’ll fire next</h3><ul><li><strong>“Is 100% coverage good?”</strong> → No — it measures execution, not assertion quality. Pair with mutation testing; treat as a floor.</li><li><strong>“What’s cyclomatic complexity?”</strong> → Count of independent paths through a function; high = hard to test and bug-prone; refactor by extracting branches.</li><li><strong>“How do you enforce standards across a team?”</strong> → Committed linter/formatter config + pre-commit hooks + CI gates, so it’s automatic, not nagging.</li><li><strong>“What’s the most useful single metric?”</strong> → Defect escape rate — it reflects what the business actually feels.</li></ul><h3>The takeaway</h3><p>The difference between a junior and a senior answer to “how do you measure code quality” isn’t more tools — it’s <strong>knowing what each metric does and doesn’t tell you, and matching enforcement to the cost of being wrong.</strong> Walk in with metrics, the coverage caveat, and a context-driven decision, and you’ve already answered the three follow-ups before they’re asked.</p><p><em>Next in the series: Advanced Git — proving you know version control beyond add, commit, push.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=59ed45e4fd87" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deployment Strategies, Cloud Architecture, Rate Limiting, and RAG Optimization]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://staskoltsov.medium.com/deployment-strategies-cloud-architecture-rate-limiting-and-rag-optimization-e8d3c549e9cd?source=rss-e15c42fd98e0------2"><img src="https://cdn-images-1.medium.com/max/1360/1*6KAC2OZFSLBUcOms3XkeKA.jpeg" width="1360"></a></p><p class="medium-feed-snippet">Blue-Green vs. Canary, S3 vs. DynamoDB, Token Bucket vs. Leaky Bucket, API versioning tradeoffs, and why your RAG chunking strategy&#x2026;</p><p class="medium-feed-link"><a href="https://staskoltsov.medium.com/deployment-strategies-cloud-architecture-rate-limiting-and-rag-optimization-e8d3c549e9cd?source=rss-e15c42fd98e0------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://staskoltsov.medium.com/deployment-strategies-cloud-architecture-rate-limiting-and-rag-optimization-e8d3c549e9cd?source=rss-e15c42fd98e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8d3c549e9cd</guid>
            <category><![CDATA[interview-questions]]></category>
            <dc:creator><![CDATA[Stanislav Lazarenko]]></dc:creator>
            <pubDate>Wed, 03 Jun 2026 12:11:34 GMT</pubDate>
            <atom:updated>2026-06-03T12:11:34.917Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Three Microservices Patterns — Circuit Breaker, Idempotency Keys, and Dead Letter Queues]]></title>
            <link>https://staskoltsov.medium.com/three-microservices-patterns-circuit-breaker-idempotency-keys-and-dead-letter-queues-e787ecf2b9df?source=rss-e15c42fd98e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/e787ecf2b9df</guid>
            <category><![CDATA[python-interview-question]]></category>
            <dc:creator><![CDATA[Stanislav Lazarenko]]></dc:creator>
            <pubDate>Thu, 19 Feb 2026 09:58:15 GMT</pubDate>
            <atom:updated>2026-02-19T09:58:15.924Z</atom:updated>
            <content:encoded><![CDATA[<p><em>The distributed systems patterns that interviewers use to separate “I’ve read about microservices” from “I’ve operated microservices at 3 AM.”</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/784/1*nRU6YW8jGccHWsVhCeudSw.jpeg" /></figure><h4>Pattern 1: Circuit Breaker — Stop Calling a Dead Service</h4><h4>The Problem</h4><p>Imagine Service A calls Service B for every request. Service B goes down. Without a circuit breaker, Service A keeps sending requests to a dead service — each one waits for a timeout (say, 5 seconds), then fails. If Service A handles 500 requests per second, that’s 500 threads/connections stuck waiting for something that will never respond. Service A’s connection pool fills up, its latency spikes, and now <em>it</em> looks like it’s down too. Service C, which depends on Service A, experiences the same cascading failure. One dead service just took down three.</p><h4>How Circuit Breaker Works</h4><p>The pattern is borrowed from electrical engineering. A circuit breaker has three states:</p><pre>failure threshold reached<br>    ┌──────────┐ ─────────────────────────────▶ ┌──────────┐<br>    │  CLOSED  │                                │   OPEN   │<br>    │ (normal) │ ◀───────────────────────────── │ (reject) │<br>    └──────────┘    probe succeeds              └──────────┘<br>         │                                            │<br>         │              recovery timeout expires      │<br>         │                                            │<br>         │         ┌─────────────┐                    │<br>         └───────  │  HALF-OPEN  │ ◀──────────────────┘<br>           probe   │  (testing)  │<br>          fails    └─────────────┘</pre><p><strong>CLOSED (normal):</strong> Requests flow through. The breaker tracks failures. If the failure rate exceeds a threshold (e.g., 50% of the last 20 calls), the breaker <strong>trips open</strong>.</p><p><strong>OPEN (rejecting):</strong> All requests are rejected immediately — no waiting for timeouts. This protects Service A’s resources and gives Service B time to recover. After a cooldown period (e.g., 30 seconds), the breaker transitions to half-open.</p><p><strong>HALF-OPEN (probing):</strong> The breaker allows a small number of test requests through. If they succeed, the breaker closes (normal operation resumes). If they fail, the breaker opens again for another cooldown period.</p><h3>Production Implementation</h3><pre>import asyncio<br>import time<br>from enum import Enum<br>from dataclasses import dataclass, field<br>from collections import deque<br>from typing import TypeVar, Callable, Awaitable<br>from functools import wraps<br><br>T = TypeVar(&quot;T&quot;)<br><br>class CircuitState(Enum):<br>    CLOSED = &quot;closed&quot;<br>    OPEN = &quot;open&quot;<br>    HALF_OPEN = &quot;half_open&quot;<br><br>@dataclass<br>class CircuitBreakerConfig:<br>    failure_threshold: float = 0.5       # 50% failure rate trips the breaker<br>    min_calls_before_trip: int = 10      # need at least N calls before evaluating<br>    recovery_timeout: float = 30.0       # seconds before trying half-open<br>    half_open_max_calls: int = 3         # probes allowed in half-open<br>    window_size: int = 20                # sliding window of recent calls<br>    excluded_exceptions: tuple = ()      # exceptions that don&#39;t count as failures<br><br>class CircuitBreakerOpen(Exception):<br>    &quot;&quot;&quot;Raised when the circuit breaker is open and rejecting calls.&quot;&quot;&quot;<br>    def __init__(self, service: str, time_until_retry: float):<br>        self.service = service<br>        self.time_until_retry = time_until_retry<br>        super().__init__(<br>            f&quot;Circuit breaker for &#39;{service}&#39; is OPEN. &quot;<br>            f&quot;Retry in {time_until_retry:.1f}s&quot;<br>        )<br><br>@dataclass<br>class CircuitBreaker:<br>    name: str<br>    config: CircuitBreakerConfig = field(default_factory=CircuitBreakerConfig)<br>    # Internal state<br>    _state: CircuitState = field(init=False, default=CircuitState.CLOSED)<br>    _results: deque = field(init=False, default_factory=lambda: deque(maxlen=20))<br>    _last_failure_time: float = field(init=False, default=0.0)<br>    _half_open_calls: int = field(init=False, default=0)<br>    _lock: asyncio.Lock = field(init=False, default_factory=asyncio.Lock)<br><br>    def __post_init__(self):<br>        self._results = deque(maxlen=self.config.window_size)<br><br>    @property<br>    def state(self) -&gt; CircuitState:<br>        if self._state == CircuitState.OPEN:<br>            elapsed = time.monotonic() - self._last_failure_time<br>            if elapsed &gt;= self.config.recovery_timeout:<br>                self._state = CircuitState.HALF_OPEN<br>                self._half_open_calls = 0<br>        return self._state<br><br>    @property<br>    def failure_rate(self) -&gt; float:<br>        if not self._results:<br>            return 0.0<br>        failures = sum(1 for success in self._results if not success)<br>        return failures / len(self._results)<br><br>    async def call(self, func: Callable[..., Awaitable[T]], *args, **kwargs) -&gt; T:<br>        async with self._lock:<br>            state = self.state<br><br>            if state == CircuitState.OPEN:<br>                remaining = self.config.recovery_timeout - (<br>                    time.monotonic() - self._last_failure_time<br>                )<br>                raise CircuitBreakerOpen(self.name, max(0, remaining))<br><br>            if state == CircuitState.HALF_OPEN:<br>                if self._half_open_calls &gt;= self.config.half_open_max_calls:<br>                    raise CircuitBreakerOpen(self.name, 5.0)<br>                self._half_open_calls += 1<br><br>        # Execute outside the lock so we don&#39;t hold it during I/O<br>        try:<br>            result = await func(*args, **kwargs)<br>        except Exception as exc:<br>            if isinstance(exc, self.config.excluded_exceptions):<br>                raise  # don&#39;t count business logic errors as failures<br>            await self._record_failure()<br>            raise<br>        else:<br>            await self._record_success()<br>            return result<br><br>    async def _record_success(self):<br>        async with self._lock:<br>            self._results.append(True)<br>            if self._state == CircuitState.HALF_OPEN:<br>                # Enough successful probes → close the circuit<br>                recent_successes = sum(1 for s in list(self._results)[-self.config.half_open_max_calls:] if s)<br>                if recent_successes &gt;= self.config.half_open_max_calls:<br>                    self._state = CircuitState.CLOSED<br>                    self._results.clear()<br><br>    async def _record_failure(self):<br>        async with self._lock:<br>            self._results.append(False)<br>            self._last_failure_time = time.monotonic()<br><br>            if self._state == CircuitState.HALF_OPEN:<br>                # Any failure in half-open → back to open<br>                self._state = CircuitState.OPEN<br>                return<br><br>            if (<br>                len(self._results) &gt;= self.config.min_calls_before_trip<br>                and self.failure_rate &gt;= self.config.failure_threshold<br>            ):<br>                self._state = CircuitState.OPEN</pre><h4>Using It in a FastAPI Service</h4><pre>import httpx<br>import logging<br><br>logger = logging.getLogger(__name__)<br># One circuit breaker per downstream service<br>payment_breaker = CircuitBreaker(<br>    name=&quot;payment-service&quot;,<br>    config=CircuitBreakerConfig(<br>        failure_threshold=0.5,<br>        recovery_timeout=30.0,<br>        min_calls_before_trip=5,<br>    ),<br>)<br>inventory_breaker = CircuitBreaker(<br>    name=&quot;inventory-service&quot;,<br>    config=CircuitBreakerConfig(<br>        failure_threshold=0.3,       # inventory is more critical - trip faster<br>        recovery_timeout=60.0,       # give it longer to recover<br>    ),<br>)<br><br>class PaymentClient:<br>    def __init__(self, base_url: str):<br>        self.base_url = base_url<br>        self.client = httpx.AsyncClient(timeout=5.0)<br>    async def charge(self, order_id: str, amount_cents: int) -&gt; dict:<br>        return await payment_breaker.call(<br>            self._do_charge, order_id, amount_cents<br>        )<br>    async def _do_charge(self, order_id: str, amount_cents: int) -&gt; dict:<br>        response = await self.client.post(<br>            f&quot;{self.base_url}/charges&quot;,<br>            json={&quot;order_id&quot;: order_id, &quot;amount&quot;: amount_cents},<br>        )<br>        response.raise_for_status()<br>        return response.json()<br><br># In the endpoint - handle the open circuit gracefully<br>@router.post(&quot;/orders/{order_id}/pay&quot;)<br>async def pay_for_order(order_id: str, service: OrderServiceDep):<br>    try:<br>        result = await service.process_payment(order_id)<br>        return PaymentResponse.model_validate(result)<br>    except CircuitBreakerOpen as exc:<br>        logger.warning(f&quot;Payment circuit open: {exc}&quot;)<br>        raise HTTPException(<br>            status_code=503,<br>            detail={<br>                &quot;type&quot;: &quot;/errors/service-unavailable&quot;,<br>                &quot;title&quot;: &quot;Payment Service Temporarily Unavailable&quot;,<br>                &quot;detail&quot;: f&quot;Please retry in {exc.time_until_retry:.0f} seconds&quot;,<br>                &quot;retry_after&quot;: int(exc.time_until_retry),<br>            },<br>        )</pre><h4>Fallback Strategies</h4><p>A circuit breaker that just returns 503 is only half the pattern. Production systems need <strong>fallbacks</strong> — degraded but functional responses:</p><pre>async def get_product_recommendations(user_id: str) -&gt; list[Product]:<br>    &quot;&quot;&quot;Recommendation service is non-critical — degrade gracefully.&quot;&quot;&quot;<br>    try:<br>        return await recommendation_breaker.call(<br>            recommendation_client.get_personalized, user_id<br>        )<br>    except (CircuitBreakerOpen, httpx.HTTPError):<br>        # Fallback: return cached popular products instead of personalized ones<br>        return await cache.get(&quot;popular_products&quot;, default=DEFAULT_PRODUCTS)</pre><h3>Pattern 2: Idempotency Keys — Make Retries Safe</h3><h4>The Problem</h4><p>A customer clicks “Pay” and the request times out. Did the payment go through? The client retries. Without idempotency protection, you just charged the customer twice. This is not hypothetical — double charges are one of the most common bugs in payment systems, and they destroy user trust instantly.</p><h4>How Idempotency Keys Work</h4><p>The client generates a unique key (usually a UUID) and sends it with the request. The server stores the key along with the result. If the same key arrives again, the server returns the stored result instead of executing the operation again.</p><pre>First request:                       Retry (same key):<br>Client ──▶ Server                    Client ──▶ Server<br>  Idempotency-Key: abc-123             Idempotency-Key: abc-123<br>  POST /payments                       POST /payments<br>  {amount: 5000}                       {amount: 5000}<br>                                     <br>Server:                              Server:<br>  1. Check: &quot;abc-123&quot; exists? No       1. Check: &quot;abc-123&quot; exists? Yes<br>  2. Process payment                   2. Return stored result<br>  3. Store: abc-123 → {result}         3. Skip processing entirely<br>  4. Return result                     4. Return same result</pre><h4>Production Implementation</h4><pre>import hashlib<br>import json<br>from datetime import datetime, timedelta<br>from dataclasses import dataclass<br>from enum import Enum<br><br>from fastapi import Request, HTTPException, Depends<br>from fastapi.responses import JSONResponse<br>from sqlalchemy.ext.asyncio import AsyncSession<br>from sqlalchemy import select, text<br><br>class IdempotencyStatus(str, Enum):<br>    PROCESSING = &quot;processing&quot;    # request is being handled right now<br>    COMPLETED = &quot;completed&quot;      # result is stored and ready<br><br>@dataclass<br>class IdempotencyRecord:<br>    key: str<br>    status: IdempotencyStatus<br>    request_fingerprint: str       # hash of the request body<br>    status_code: int | None<br>    response_body: str | None<br>    created_at: datetime<br>    expires_at: datetime<br><br>class IdempotencyStore:<br>    &quot;&quot;&quot;Database-backed idempotency storage.&quot;&quot;&quot;<br>    def __init__(self, session: AsyncSession):<br>        self.session = session<br><br>    async def get(self, key: str) -&gt; IdempotencyRecord | None:<br>        result = await self.session.execute(<br>            select(IdempotencyModel).where(<br>                IdempotencyModel.key == key,<br>                IdempotencyModel.expires_at &gt; datetime.utcnow(),<br>            )<br>        )<br>        row = result.scalar_one_or_none()<br>        return row.to_record() if row else None<br><br>    async def create_lock(<br>        self,<br>        key: str,<br>        request_fingerprint: str,<br>        ttl: timedelta = timedelta(hours=24),<br>    ) -&gt; bool:<br>        &quot;&quot;&quot;<br>        Atomically create a PROCESSING record.<br>        Returns True if we got the lock, False if the key already exists.<br>        &quot;&quot;&quot;<br>        try:<br>            record = IdempotencyModel(<br>                key=key,<br>                status=IdempotencyStatus.PROCESSING,<br>                request_fingerprint=request_fingerprint,<br>                expires_at=datetime.utcnow() + ttl,<br>            )<br>            self.session.add(record)<br>            await self.session.flush()<br>            return True<br>        except Exception:<br>            # Unique constraint violation - key already exists<br>            await self.session.rollback()<br>            return False<br><br>    async def complete(<br>        self,<br>        key: str,<br>        status_code: int,<br>        response_body: str,<br>    ):<br>        &quot;&quot;&quot;Mark the request as completed with its response.&quot;&quot;&quot;<br>        await self.session.execute(<br>            text(&quot;&quot;&quot;<br>                UPDATE idempotency_keys<br>                SET status = :status,<br>                    status_code = :status_code,<br>                    response_body = :response_body<br>                WHERE key = :key<br>            &quot;&quot;&quot;),<br>            {<br>                &quot;status&quot;: IdempotencyStatus.COMPLETED,<br>                &quot;status_code&quot;: status_code,<br>                &quot;response_body&quot;: response_body,<br>                &quot;key&quot;: key,<br>            },<br>        )<br><br>    async def release(self, key: str):<br>        &quot;&quot;&quot;Remove a PROCESSING lock (on failure, so it can be retried).&quot;&quot;&quot;<br>        await self.session.execute(<br>            text(&quot;DELETE FROM idempotency_keys WHERE key = :key AND status = :status&quot;),<br>            {&quot;key&quot;: key, &quot;status&quot;: IdempotencyStatus.PROCESSING},<br>        )</pre><h4>The Middleware That Ties It Together</h4><pre>def compute_fingerprint(body: bytes, path: str, method: str) -&gt; str:<br>    &quot;&quot;&quot;Hash the request to detect mismatched retries.&quot;&quot;&quot;<br>    content = f&quot;{method}:{path}:{body.decode()}&quot;<br>    return hashlib.sha256(content.encode()).hexdigest()<br><br>async def idempotency_middleware(<br>    request: Request,<br>    call_next,<br>    store: IdempotencyStore,<br>):<br>    # Only applies to state-changing methods<br>    if request.method not in (&quot;POST&quot;, &quot;PUT&quot;, &quot;PATCH&quot;):<br>        return await call_next(request)<br>    # Check for idempotency key header<br>    idempotency_key = request.headers.get(&quot;Idempotency-Key&quot;)<br><br>    if not idempotency_key:<br>        return await call_next(request)<br><br>    body = await request.body()<br>    fingerprint = compute_fingerprint(body, request.url.path, request.method)<br>    # Check if this key was already processed<br>    existing = await store.get(idempotency_key)<br><br>    if existing:<br>        # CASE 1: Same key, different request body → client error<br>        if existing.request_fingerprint != fingerprint:<br>            return JSONResponse(<br>                status_code=422,<br>                content={<br>                    &quot;type&quot;: &quot;/errors/idempotency-mismatch&quot;,<br>                    &quot;title&quot;: &quot;Idempotency Key Reuse&quot;,<br>                    &quot;detail&quot;: (<br>                        &quot;This idempotency key was used with a different request body. &quot;<br>                        &quot;Generate a new key for different requests.&quot;<br>                    ),<br>                },<br>            )<br>        # CASE 2: Still processing from a previous request<br>        if existing.status == IdempotencyStatus.PROCESSING:<br>            return JSONResponse(<br>                status_code=409,<br>                content={<br>                    &quot;type&quot;: &quot;/errors/request-in-progress&quot;,<br>                    &quot;title&quot;: &quot;Request In Progress&quot;,<br>                    &quot;detail&quot;: &quot;This request is already being processed. Please wait.&quot;,<br>                },<br>            )<br>        # CASE 3: Already completed → return stored response (idempotent replay)<br>        return JSONResponse(<br>            status_code=existing.status_code,<br>            content=json.loads(existing.response_body),<br>        )<br>    # New key - acquire the processing lock<br>    locked = await store.create_lock(idempotency_key, fingerprint)<br>    if not locked:<br>        # Race condition: another request grabbed the lock between our check and insert<br>        return JSONResponse(<br>            status_code=409,<br>            content={&quot;detail&quot;: &quot;Request is being processed concurrently&quot;},<br>        )<br>    try:<br>        # Execute the actual request<br>        response = await call_next(request)<br>        # Store the response for future replays<br>        response_body = b&quot;&quot;<br>        async for chunk in response.body_iterator:<br>            response_body += chunk<br>        await store.complete(<br>            idempotency_key,<br>            status_code=response.status_code,<br>            response_body=response_body.decode(),<br>        )<br>        return JSONResponse(<br>            status_code=response.status_code,<br>            content=json.loads(response_body),<br>        )<br>    except Exception:<br>        # Processing failed - release the lock so the client can retry<br>        await store.release(idempotency_key)<br>        raise</pre><h4>Edge Cases Interviewers Love to Ask About</h4><p><strong>“What if the server crashes mid-processing?”</strong></p><p>The PROCESSING lock is left in the database. Options: set a short TTL on PROCESSING records (e.g., 5 minutes) and treat expired PROCESSING as retryable, or run a background cleanup job that releases stale locks.</p><p><strong>“What about the request fingerprint — why hash the body?”</strong></p><p>Without the fingerprint, a client could reuse a key with a <em>different</em> request body — paying $50 with key X, then paying $500 with the same key X. The fingerprint catches this and returns a 422 error.</p><p><strong>“How long do you keep idempotency records?”</strong></p><p>Typically 24–48 hours. Long enough for retry storms to pass, short enough that the table doesn’t grow unbounded. Use a TTL index or a background job to clean expired records.</p><p><strong>“Can you use Redis instead of PostgreSQL?”</strong></p><p>Yes, and it’s often simpler for the locking mechanism. Redis SET key value NX EX 300 gives you atomic lock acquisition with expiration in one command. The tradeoff is that Redis is less durable — a Redis crash could lose idempotency records, potentially allowing a duplicate operation.</p><pre>import redis.asyncio as redis<br><br>class RedisIdempotencyStore:<br>    def __init__(self, client: redis.Redis):<br>        self.client = client<br><br>    async def acquire_lock(self, key: str, fingerprint: str, ttl: int = 86400) -&gt; bool:<br>        &quot;&quot;&quot;Atomic: set only if not exists, with expiration.&quot;&quot;&quot;<br>        value = json.dumps({&quot;fingerprint&quot;: fingerprint, &quot;status&quot;: &quot;processing&quot;})<br>        return await self.client.set(f&quot;idempotency:{key}&quot;, value, nx=True, ex=ttl)<br><br>    async def get(self, key: str) -&gt; dict | None:<br>        data = await self.client.get(f&quot;idempotency:{key}&quot;)<br>        return json.loads(data) if data else None<br><br>    async def complete(self, key: str, status_code: int, body: str):<br>        existing = await self.get(key)<br>        if existing:<br>            existing[&quot;status&quot;] = &quot;completed&quot;<br>            existing[&quot;status_code&quot;] = status_code<br>            existing[&quot;response_body&quot;] = body<br>            await self.client.set(<br>                f&quot;idempotency:{key}&quot;,<br>                json.dumps(existing),<br>                keepttl=True,  # preserve the original expiration<br>            )<br><br>    async def release(self, key: str):<br>        await self.client.delete(f&quot;idempotency:{key}&quot;)</pre><h3>Pattern 3: Dead Letter Queues — Handle What Can’t Be Processed</h3><h4>The Problem</h4><p>In an event-driven system, messages flow through queues: “order created,” “payment received,” “shipment dispatched.” A consumer picks up each message, processes it, and acknowledges it. But what happens when a message can’t be processed? Maybe the payload is malformed, a required downstream service is permanently gone, or there’s a bug in the consumer code.</p><p>Without a DLQ, you have two bad options. <strong>Option A:</strong> the message is retried forever, clogging the queue and wasting resources on something that will never succeed. <strong>Option B:</strong> the message is discarded, and you lose data silently.</p><h4>How Dead Letter Queues Work</h4><p>A DLQ is a separate queue where “poisoned” messages are sent after they’ve failed a configurable number of times. This clears the main queue so healthy messages keep flowing, while preserving the failed messages for investigation and reprocessing.</p><pre>Main Queue                              Dead Letter Queue<br>┌─────────────────────┐                 ┌─────────────────────┐<br>│ msg-1 (OK)          │──▶ Consumer     │ msg-X (failed 5x)   │<br>│ msg-2 (OK)          │──▶ Consumer     │ msg-Y (bad payload) │<br>│ msg-X (fails)       │──▶ retry 1      │ msg-Z (missing ref) │<br>│                     │──▶ retry 2      │                     │<br>│                     │──▶ retry 3      │  ▼                  │<br>│                     │──▶ retry 4      │  Alerting           │<br>│                     │──▶ retry 5      │  Investigation      │<br>│                     │──▶ MOVE TO DLQ  │  Manual reprocessing│<br>└─────────────────────┘                 └─────────────────────┘</pre><h4>Implementation With RabbitMQ</h4><p>RabbitMQ supports DLQ natively through “dead letter exchanges” (DLX):</p><pre>import aio_pika<br>import json<br>import logging<br>from dataclasses import dataclass<br><br>logger = logging.getLogger(__name__)<br><br>async def setup_queues(connection: aio_pika.Connection):<br>    &quot;&quot;&quot;Set up main queue with automatic dead-lettering.&quot;&quot;&quot;<br>    channel = await connection.channel()<br>    # Declare the dead letter exchange and queue<br>    dlx = await channel.declare_exchange(&quot;dlx.orders&quot;, aio_pika.ExchangeType.DIRECT)<br>    dlq = await channel.declare_queue(&quot;orders.dlq&quot;, durable=True)<br>    await dlq.bind(dlx, routing_key=&quot;orders&quot;)<br>    # Declare the main exchange and queue with DLX configuration<br>    main_exchange = await channel.declare_exchange(&quot;orders&quot;, aio_pika.ExchangeType.DIRECT)<br>    main_queue = await channel.declare_queue(<br>        &quot;orders.processing&quot;,<br>        durable=True,<br>        arguments={<br>            &quot;x-dead-letter-exchange&quot;: &quot;dlx.orders&quot;,       # where to send dead letters<br>            &quot;x-dead-letter-routing-key&quot;: &quot;orders&quot;,         # routing key for DLQ<br>            &quot;x-message-ttl&quot;: 300_000,                      # 5 min TTL (optional)<br>        },<br>    )<br>    await main_queue.bind(main_exchange, routing_key=&quot;orders&quot;)<br>    return main_queue, dlq</pre><h4>Consumer With Retry Logic and DLQ Routing</h4><pre>MAX_RETRIES = 5<br>RETRY_DELAYS = [1, 5, 15, 60, 300]  # seconds: progressive backoff<br><br>@dataclass<br>class MessageProcessor:<br>    channel: aio_pika.Channel<br>    async def process_message(self, message: aio_pika.IncomingMessage):<br>        &quot;&quot;&quot;Process a message with retry tracking and DLQ routing.&quot;&quot;&quot;<br>        async with message.process(requeue=False):<br>            # Track retry count in message headers<br>            headers = message.headers or {}<br>            retry_count = headers.get(&quot;x-retry-count&quot;, 0)<br>            original_queue = headers.get(&quot;x-original-queue&quot;, &quot;orders.processing&quot;)<br>            try:<br>                payload = json.loads(message.body.decode())<br>                await self._handle_order_event(payload)<br>                logger.info(f&quot;Processed message {message.message_id}&quot;)<br>            except RecoverableError as exc:<br>                # Transient failures: retry with backoff<br>                if retry_count &lt; MAX_RETRIES:<br>                    delay = RETRY_DELAYS[min(retry_count, len(RETRY_DELAYS) - 1)]<br>                    logger.warning(<br>                        f&quot;Retryable error (attempt {retry_count + 1}/{MAX_RETRIES}): {exc}. &quot;<br>                        f&quot;Retrying in {delay}s&quot;<br>                    )<br>                    await self._schedule_retry(message, retry_count + 1, delay)<br>                else:<br>                    logger.error(<br>                        f&quot;Message exhausted {MAX_RETRIES} retries, sending to DLQ: {exc}&quot;<br>                    )<br>                    await self._send_to_dlq(message, retry_count, str(exc))<br>            except PermanentError as exc:<br>                # Non-transient failures: go to DLQ immediately<br>                logger.error(f&quot;Permanent failure, sending to DLQ immediately: {exc}&quot;)<br>                await self._send_to_dlq(message, retry_count, str(exc))<br>            except Exception as exc:<br>                # Unexpected errors: treat as retryable but log loudly<br>                logger.exception(f&quot;Unexpected error processing message: {exc}&quot;)<br>                if retry_count &lt; MAX_RETRIES:<br>                    await self._schedule_retry(message, retry_count + 1, RETRY_DELAYS[-1])<br>                else:<br>                    await self._send_to_dlq(message, retry_count, f&quot;Unexpected: {exc}&quot;)<br><br>    async def _schedule_retry(<br>        self,<br>        original: aio_pika.IncomingMessage,<br>        retry_count: int,<br>        delay_seconds: int,<br>    ):<br>        &quot;&quot;&quot;Republish with incremented retry count and a delay.&quot;&quot;&quot;<br>        # Use a per-delay retry queue with TTL → messages auto-forward back to main queue<br>        retry_queue_name = f&quot;orders.retry.{delay_seconds}s&quot;<br>        retry_exchange = await self.channel.declare_exchange(<br>            &quot;retry.orders&quot;, aio_pika.ExchangeType.DIRECT<br>        )<br>        await self.channel.declare_queue(<br>            retry_queue_name,<br>            durable=True,<br>            arguments={<br>                &quot;x-dead-letter-exchange&quot;: &quot;orders&quot;,            # forward back to main<br>                &quot;x-dead-letter-routing-key&quot;: &quot;orders&quot;,<br>                &quot;x-message-ttl&quot;: delay_seconds * 1000,         # delay in ms<br>            },<br>        )<br>        await retry_exchange.publish(<br>            aio_pika.Message(<br>                body=original.body,<br>                headers={<br>                    **(original.headers or {}),<br>                    &quot;x-retry-count&quot;: retry_count,<br>                    &quot;x-original-queue&quot;: &quot;orders.processing&quot;,<br>                    &quot;x-last-error-time&quot;: datetime.utcnow().isoformat(),<br>                },<br>                message_id=original.message_id,<br>                content_type=original.content_type,<br>                delivery_mode=aio_pika.DeliveryMode.PERSISTENT,<br>            ),<br>            routing_key=retry_queue_name,<br>        )<br><br>    async def _send_to_dlq(<br>        self,<br>        original: aio_pika.IncomingMessage,<br>        retry_count: int,<br>        error_message: str,<br>    ):<br>        &quot;&quot;&quot;Send to dead letter queue with diagnostic metadata.&quot;&quot;&quot;<br>        dlx = await self.channel.declare_exchange(&quot;dlx.orders&quot;, aio_pika.ExchangeType.DIRECT)<br>        await dlx.publish(<br>            aio_pika.Message(<br>                body=original.body,<br>                headers={<br>                    **(original.headers or {}),<br>                    &quot;x-retry-count&quot;: retry_count,<br>                    &quot;x-final-error&quot;: error_message,<br>                    &quot;x-original-queue&quot;: &quot;orders.processing&quot;,<br>                    &quot;x-died-at&quot;: datetime.utcnow().isoformat(),<br>                    &quot;x-original-message-id&quot;: original.message_id or &quot;&quot;,<br>                },<br>                content_type=original.content_type,<br>                delivery_mode=aio_pika.DeliveryMode.PERSISTENT,<br>            ),<br>            routing_key=&quot;orders&quot;,<br>        )<br><br>    async def _handle_order_event(self, payload: dict):<br>        &quot;&quot;&quot;Actual business logic - separated for testability.&quot;&quot;&quot;<br>        event_type = payload.get(&quot;event_type&quot;)<br>        if event_type == &quot;order_created&quot;:<br>            await self._process_new_order(payload)<br>        elif event_type == &quot;payment_received&quot;:<br>            await self._process_payment(payload)<br>        else:<br>            raise PermanentError(f&quot;Unknown event type: {event_type}&quot;)<br><br>class RecoverableError(Exception):<br>    &quot;&quot;&quot;Transient failure - worth retrying (network timeout, DB lock, etc.).&quot;&quot;&quot;<br>    pass<br><br>class PermanentError(Exception):<br>    &quot;&quot;&quot;Non-transient failure - retrying won&#39;t help (bad data, unknown event, etc.).&quot;&quot;&quot;<br>    pass</pre><h4>Distinguishing Recoverable vs. Permanent Errors</h4><p>This is the design decision interviewers care about most. Not all failures are equal:</p><pre>async def _process_new_order(self, payload: dict):<br>    try:<br>        order_id = payload[&quot;order_id&quot;]   # KeyError → permanent<br>        customer = payload[&quot;customer&quot;]<br>    except KeyError as e:<br>        raise PermanentError(f&quot;Missing required field: {e}&quot;)<br><br>    try:<br>        customer_data = await customer_service.get(customer[&quot;id&quot;])<br>    except httpx.TimeoutException:<br>        raise RecoverableError(&quot;Customer service timeout&quot;)  # retry<br>    except httpx.HTTPStatusError as e:<br>        if e.response.status_code == 404:<br>            raise PermanentError(f&quot;Customer {customer[&#39;id&#39;]} not found&quot;)  # no point retrying<br>        if e.response.status_code &gt;= 500:<br>            raise RecoverableError(f&quot;Customer service error: {e}&quot;)  # retry<br>        raise PermanentError(f&quot;Client error: {e}&quot;)  # 4xx → our fault, don&#39;t retry</pre><p><strong>Permanent failures</strong> (send to DLQ immediately): malformed payloads, missing required fields, referenced entities that don’t exist, schema mismatches, unknown event types.</p><p><strong>Recoverable failures</strong> (retry with backoff): network timeouts, connection refused, 5xx from downstream services, database lock contention, rate limiting.</p><h4>DLQ Monitoring and Reprocessing</h4><p>A DLQ without monitoring is just a graveyard. You need alerts and a reprocessing mechanism.</p><pre>import asyncio<br><br><br>async def monitor_dlq(connection: aio_pika.Connection, alert_threshold: int = 10):<br>    &quot;&quot;&quot;Alert when DLQ depth exceeds threshold.&quot;&quot;&quot;<br>    channel = await connection.channel()<br>    while True:<br>        dlq = await channel.declare_queue(&quot;orders.dlq&quot;, passive=True)<br>        depth = dlq.declaration_result.message_count<br>        if depth &gt; alert_threshold:<br>            logger.critical(<br>                f&quot;DLQ depth is {depth} (threshold: {alert_threshold}). &quot;<br>                f&quot;Investigation required.&quot;<br>            )<br>            await send_pagerduty_alert(<br>                summary=f&quot;DLQ &#39;orders.dlq&#39; has {depth} messages&quot;,<br>                severity=&quot;warning&quot; if depth &lt; 100 else &quot;critical&quot;,<br>            )<br>        await asyncio.sleep(60)  # check every minute<br><br><br>async def reprocess_dlq(<br>    connection: aio_pika.Connection,<br>    max_messages: int = 100,<br>):<br>    &quot;&quot;&quot;<br>    Manually triggered: move DLQ messages back to the main queue.<br>    Call this after fixing the root cause.<br>    &quot;&quot;&quot;<br>    channel = await connection.channel()<br>    dlq = await channel.declare_queue(&quot;orders.dlq&quot;, passive=True)<br>    main_exchange = await channel.declare_exchange(&quot;orders&quot;, passive=True)<br>    reprocessed = 0<br>    async with dlq.iterator() as queue_iter:<br>        async for message in queue_iter:<br>            if reprocessed &gt;= max_messages:<br>                break<br>            # Reset retry count and republish to main queue<br>            await main_exchange.publish(<br>                aio_pika.Message(<br>                    body=message.body,<br>                    headers={<br>                        **(message.headers or {}),<br>                        &quot;x-retry-count&quot;: 0,<br>                        &quot;x-reprocessed-from-dlq&quot;: True,<br>                        &quot;x-reprocessed-at&quot;: datetime.utcnow().isoformat(),<br>                    },<br>                    content_type=message.content_type,<br>                    delivery_mode=aio_pika.DeliveryMode.PERSISTENT,<br>                ),<br>                routing_key=&quot;orders&quot;,<br>            )<br>            await message.ack()<br>            reprocessed += 1<br>    logger.info(f&quot;Reprocessed {reprocessed} messages from DLQ&quot;)<br>    return reprocessed</pre><h3>How the Three Patterns Work Together</h3><p>In a production microservices system, these patterns aren’t isolated — they form layers of resilience:</p><pre>Client sends POST /payments<br>  │<br>  ▼<br>API Gateway<br>  │ Idempotency-Key: &quot;abc-123&quot;<br>  │<br>  ▼<br>Payment Service<br>  │ ┌─ Check idempotency store → key exists? Return stored result<br>  │ └─ New key → process payment<br>  │<br>  ├──▶ Call Stripe API ──▶ Circuit Breaker<br>  │                         │ CLOSED → forward request<br>  │                         │ OPEN → fail fast, return 503<br>  │                         │ HALF-OPEN → probe with one request<br>  │<br>  ├──▶ Publish &quot;payment.completed&quot; to message queue<br>  │                         │<br>  │                         ▼<br>  │                    Order Service (consumer)<br>  │                         │ Process message<br>  │                         │ Fails? Retry with backoff<br>  │                         │ Exhausted retries? → Dead Letter Queue<br>  │                         │<br>  │                         ▼<br>  │                    DLQ → Alert → Investigate → Reprocess<br>  │<br>  └──▶ Store idempotency result</pre><p>A payment retry hits the idempotency layer (no double charge). The Stripe call is wrapped in a circuit breaker (no cascading failure if Stripe is down). The downstream event processing uses DLQ (no lost messages, no poison-pill blocking).</p><h3>The Senior-Level Interview Answers</h3><p><strong>Q: Explain the Circuit Breaker pattern. Why do you need it?</strong></p><blockquote><em>“A circuit breaker prevents cascading failures when a downstream service is unhealthy. It has three states: closed (normal traffic flows), open (requests are rejected immediately without waiting for timeouts), and half-open (a few probe requests test whether the service has recovered). Without it, a single failing dependency can exhaust connection pools and bring down the entire system through timeout accumulation. In practice, I configure one breaker per downstream service, with failure thresholds tuned to criticality — a payment service might trip at 30% failure rate while a recommendation service trips at 50%. Each breaker needs a fallback strategy — cached data, default values, or graceful degradation.”</em></blockquote><p><strong>Q: How do idempotency keys work? How would you implement them for a payment API?</strong></p><blockquote><em>“The client sends a unique key with each request — typically a UUID in an </em><em>Idempotency-Key header. The server atomically acquires a lock on that key before processing. If the key already exists with a completed status, the stored response is returned without re-executing. If it&#39;s in a processing state, we return 409 to indicate the request is in flight. The request body is fingerprinted so reusing a key with different parameters is detected and rejected. For storage, I use PostgreSQL with a unique constraint for durability, or Redis with </em><em>SET NX EX for performance. The key design decisions are the lock release strategy on failure — you must release so the client can retry — and the TTL on completed records, typically 24-48 hours.&quot;</em></blockquote><p><strong>Q: What are Dead Letter Queues? When and how do you use them?</strong></p><blockquote><em>“A DLQ is a holding queue for messages that failed processing after exhausting their retry budget. It solves two problems: it prevents poison-pill messages from blocking the main queue, and it preserves failed messages for investigation instead of discarding them. The critical design decision is distinguishing permanent errors from recoverable ones — a malformed payload goes to DLQ immediately since retrying won’t help, while a network timeout gets exponential backoff retries first. Every DLQ needs monitoring with alerts on queue depth, and a reprocessing mechanism for after you fix the root cause. I enrich DLQ messages with metadata — retry count, error messages, timestamps — so investigation is fast.”</em></blockquote><p><em>This article was born from direct interview feedback telling me to go deeper on distributed systems patterns. If you got similar feedback, start with these three — they come up in almost every senior backend interview, and the depth of your implementation knowledge is what separates a “pass” from a “strong hire.”</em></p><p><em>Follow the series for more. Each post covers a real gap identified through real interviews.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e787ecf2b9df" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[FastAPI + Pydantic Design for High-Performance APIs — A Senior Python Interview Deep Dive]]></title>
            <link>https://staskoltsov.medium.com/fastapi-pydantic-design-for-high-performance-apis-a-senior-python-interview-deep-dive-07ba8ac2f162?source=rss-e15c42fd98e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/07ba8ac2f162</guid>
            <category><![CDATA[python-interview-question]]></category>
            <dc:creator><![CDATA[Stanislav Lazarenko]]></dc:creator>
            <pubDate>Wed, 18 Feb 2026 05:12:33 GMT</pubDate>
            <atom:updated>2026-02-18T05:12:33.716Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Everything you need to explain and implement: validation, dependency injection, clean architecture, error handling, and the senior-level patterns that separate production code from tutorial code.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CUTzLsxU4u9glKBKm4h12Q.png" /></figure><p>If you’re interviewing for a senior Python backend role in 2026, FastAPI questions are almost guaranteed. But the interviewer isn’t going to ask you to build a TODO app. They’ll ask you to <em>design</em> an API — and the design decisions you make in the first five minutes reveal exactly how much production experience you have.</p><p>This guide covers every angle I’ve seen asked in senior interviews: from Pydantic v2 validation specifics to dependency injection, from clean layering to error handling strategy. Each section includes both the “what” and the “why” — because senior interviews are about justifying decisions, not just writing code.</p><h4>1. Request/Response Validation With Pydantic v2</h4><p><em>Why This Gets Asked</em></p><p>Validation is the API’s front door. Bad validation leads to garbage in the database, misleading error messages, and security holes. The interviewer wants to know if you treat validation as a first-class architectural concern or an afterthought.</p><h4>Pydantic v2: What Changed and Why It Matters</h4><p>Pydantic v2 was rewritten with a Rust core (pydantic-core), making it 5-50x faster than v1 depending on the workload. In an interview, you should know the practical differences.</p><pre>from pydantic import BaseModel, Field, field_validator, model_validator, ConfigDict<br>from datetime import datetime<br>from enum import Enum<br><br>class OrderStatus(str, Enum):<br>    PENDING = &quot;pending&quot;<br>    CONFIRMED = &quot;confirmed&quot;<br>    SHIPPED = &quot;shipped&quot;<br>    DELIVERED = &quot;delivered&quot;<br>    CANCELLED = &quot;cancelled&quot;<br><br>class CreateOrderRequest(BaseModel):<br>    &quot;&quot;&quot;Request schema - only what the client sends.&quot;&quot;&quot;<br>    model_config = ConfigDict(<br>        str_strip_whitespace=True,    # auto-strip strings<br>        strict=False,                  # allow coercion (str → int, etc.)<br>    )<br>    customer_id: int = Field(..., gt=0, description=&quot;Must be a positive integer&quot;)<br>    items: list[&quot;OrderItemRequest&quot;] = Field(..., min_length=1, max_length=100)<br>    shipping_address: str = Field(..., min_length=10, max_length=500)<br>    notes: str | None = Field(None, max_length=2000)<br>    <br>    @field_validator(&quot;shipping_address&quot;)<br>    @classmethod<br>    def validate_address(cls, v: str) -&gt; str:<br>        if not any(c.isdigit() for c in v):<br>            raise ValueError(&quot;Address must contain a street number&quot;)<br>        return v<br>    <br>    @model_validator(mode=&quot;after&quot;)<br>    def validate_order(self) -&gt; &quot;CreateOrderRequest&quot;:<br>        &quot;&quot;&quot;Cross-field validation: business rules that span multiple fields.&quot;&quot;&quot;<br>        total_quantity = sum(item.quantity for item in self.items)<br>        if total_quantity &gt; 500:<br>            raise ValueError(<br>                f&quot;Total quantity {total_quantity} exceeds maximum of 500 per order&quot;<br>            )<br>        return self<br><br>class OrderItemRequest(BaseModel):<br>    product_id: int = Field(..., gt=0)<br>    quantity: int = Field(..., gt=0, le=100)<br>    unit_price_cents: int = Field(..., gt=0)  # store money as cents, always<br><br>class OrderResponse(BaseModel):<br>    &quot;&quot;&quot;Response schema - what the client receives. Never expose internal fields.&quot;&quot;&quot;<br>    model_config = ConfigDict(from_attributes=True)  # v2 replacement for orm_mode<br>    id: int<br>    customer_id: int<br>    status: OrderStatus<br>    total_cents: int<br>    items: list[&quot;OrderItemResponse&quot;]<br>    created_at: datetime<br>    updated_at: datetime<br><br>class OrderItemResponse(BaseModel):<br>    model_config = ConfigDict(from_attributes=True)<br>    product_id: int<br>    quantity: int<br>    unit_price_cents: int<br>    line_total_cents: int</pre><h4>Senior-Level Validation Patterns</h4><p><strong>Separate input and output schemas.</strong> Never reuse the same model for request and response. The request contains what the client controls; the response contains what your system produces. Mixing them leaks internal fields and couples your API contract to your database schema.</p><pre># ❌ Lazy — same model for everything<br>class Order(BaseModel):<br>    id: int | None = None      # optional for create, present in response?<br>    customer_id: int<br>    status: str = &quot;pending&quot;    # client can set status? That&#39;s a security hole.<br>    created_at: datetime | None = None  # confusing<br><br># ✅ Explicit - separate schemas per operation<br>class CreateOrderRequest(BaseModel): ...   # client sends this<br>class UpdateOrderRequest(BaseModel): ...   # client sends this (partial)<br>class OrderResponse(BaseModel): ...        # client receives this<br>class OrderListResponse(BaseModel): ...    # paginated list response</pre><p><strong>Use </strong><strong>Annotated types for reusable constraints.</strong></p><pre>from typing import Annotated<br>from pydantic import Field<br><br>PositiveInt = Annotated[int, Field(gt=0)]<br>MoneyInCents = Annotated[int, Field(gt=0, description=&quot;Amount in cents&quot;)]<br>ShortString = Annotated[str, Field(min_length=1, max_length=255)]<br>PageSize = Annotated[int, Field(ge=1, le=100, default=20)]<br>class CreateOrderRequest(BaseModel):<br>    customer_id: PositiveInt<br>    total_cents: MoneyInCents<br>    description: ShortString</pre><p><strong>Computed fields for derived values (v2 feature).</strong></p><pre>from pydantic import computed_field<br><br>class OrderResponse(BaseModel):<br>    model_config = ConfigDict(from_attributes=True)<br>    total_cents: int<br>    items: list[OrderItemResponse]<br>    <br>    @computed_field<br>    @property<br>    def total_formatted(self) -&gt; str:<br>        return f&quot;${self.total_cents / 100:.2f}&quot;<br>    <br>    @computed_field<br>    @property<br>    def item_count(self) -&gt; int:<br>        return len(self.items)</pre><h4>2. Dependency Injection: The Pattern That Makes or Breaks Your Architecture</h4><h4>Why FastAPI’s DI System Is Unique</h4><p>FastAPI’s Depends() system is one of its most powerful features — and one of the most poorly used. In tutorials, you see simple examples. In production, the dependency graph handles authentication, database sessions, feature flags, rate limiting, tracing, and more.</p><h4>The Dependency Hierarchy</h4><pre>from fastapi import Depends, Request, HTTPException, status<br>from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials<br>from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker<br>from typing import Annotated<br>from contextlib import asynccontextmanager<br><br># ─── Layer 1: Infrastructure ───<br>async def get_db_session(request: Request) -&gt; AsyncSession:<br>    &quot;&quot;&quot;Yield a database session per request, with automatic cleanup.&quot;&quot;&quot;<br>    session_factory: async_sessionmaker = request.app.state.session_factory<br>    async with session_factory() as session:<br>        try:<br>            yield session<br>            await session.commit()<br>        except Exception:<br>            await session.rollback()<br>            raise<br><br>DbSession = Annotated[AsyncSession, Depends(get_db_session)]<br><br># ─── Layer 2: Authentication ───<br>security = HTTPBearer()<br><br>async def get_current_user(<br>    credentials: Annotated[HTTPAuthorizationCredentials, Depends(security)],<br>    db: DbSession,<br>) -&gt; &quot;User&quot;:<br>    &quot;&quot;&quot;Validate JWT and return the authenticated user.&quot;&quot;&quot;<br>    token = credentials.credentials<br>    payload = decode_jwt(token)  # your JWT decode logic<br>    if payload is None:<br>        raise HTTPException(<br>            status_code=status.HTTP_401_UNAUTHORIZED,<br>            detail=&quot;Invalid or expired token&quot;,<br>        )<br>    user = await UserRepository(db).get_by_id(payload[&quot;sub&quot;])<br>    if user is None:<br>        raise HTTPException(<br>            status_code=status.HTTP_401_UNAUTHORIZED,<br>            detail=&quot;User not found&quot;,<br>        )<br>    return user<br><br>CurrentUser = Annotated[&quot;User&quot;, Depends(get_current_user)]<br><br># ─── Layer 3: Authorization ───<br>def require_role(*roles: str):<br>    &quot;&quot;&quot;Factory that returns a dependency checking for specific roles.&quot;&quot;&quot;<br>    async def check_role(user: CurrentUser) -&gt; &quot;User&quot;:<br>        if user.role not in roles:<br>            raise HTTPException(<br>                status_code=status.HTTP_403_FORBIDDEN,<br>                detail=f&quot;Requires one of: {&#39;, &#39;.join(roles)}&quot;,<br>            )<br>        return user<br>    return check_role<br><br>AdminUser = Annotated[&quot;User&quot;, Depends(require_role(&quot;admin&quot;, &quot;superadmin&quot;))]<br><br># ─── Layer 4: Service injection ───<br>async def get_order_service(db: DbSession) -&gt; &quot;OrderService&quot;:<br>    &quot;&quot;&quot;Wire up service with its dependencies.&quot;&quot;&quot;<br>    repository = OrderRepository(db)<br>    return OrderService(repository)<br><br>OrderServiceDep = Annotated[&quot;OrderService&quot;, Depends(get_order_service)]</pre><h4>How Dependencies Compose in Endpoints</h4><pre>from fastapi import APIRouter<br><br>router = APIRouter(prefix=&quot;/orders&quot;, tags=[&quot;orders&quot;])<br><br>@router.post(&quot;/&quot;, status_code=201)<br>async def create_order(<br>    body: CreateOrderRequest,<br>    user: CurrentUser,<br>    service: OrderServiceDep,<br>) -&gt; OrderResponse:<br>    &quot;&quot;&quot;<br>    The endpoint is thin: validate → authorize → delegate → respond.<br>    No business logic here.<br>    &quot;&quot;&quot;<br>    order = await service.create_order(<br>        customer_id=user.id,<br>        items=body.items,<br>        shipping_address=body.shipping_address,<br>        notes=body.notes,<br>    )<br>    return OrderResponse.model_validate(order)<br><br>@router.get(&quot;/{order_id}&quot;)<br>async def get_order(<br>    order_id: int,<br>    user: CurrentUser,<br>    service: OrderServiceDep,<br>) -&gt; OrderResponse:<br>    order = await service.get_order(order_id, requesting_user=user)<br>    return OrderResponse.model_validate(order)<br><br>@router.delete(&quot;/{order_id}&quot;, status_code=204)<br>async def cancel_order(<br>    order_id: int,<br>    user: AdminUser,       # only admins can cancel<br>    service: OrderServiceDep,<br>) -&gt; None:<br>    await service.cancel_order(order_id)</pre><p>Notice: the endpoint functions are 3–5 lines each. They validate input (Pydantic does this automatically), resolve dependencies (FastAPI does this), and call a service method. That’s it. This is intentional.</p><h4>3. Clean Layering: Router → Service → Repository</h4><p><em>Why This Gets Asked</em></p><p>The interviewer is testing whether you can explain <em>where</em> code lives and <em>why</em>. In production codebases with 50+ endpoints, the difference between a clean architecture and a tangle of spaghetti is this layering.</p><h3>The Three Layers</h3><pre>┌──────────────────────────────────────────────────────────┐<br>│  ROUTER (Presentation Layer)                             │<br>│  • HTTP concerns: status codes, headers, content types   │<br>│  • Request/response schema mapping                       │<br>│  • Dependency injection wiring                           │<br>│  • No business logic                                     │<br>├──────────────────────────────────────────────────────────┤<br>│  SERVICE (Business Logic Layer)                          │<br>│  • Domain rules and workflows                            │<br>│  • Orchestrates multiple repositories                    │<br>│  • Transaction boundaries                                │<br>│  • Raises domain exceptions (not HTTP exceptions)        │<br>├──────────────────────────────────────────────────────────┤<br>│  REPOSITORY (Data Access Layer)                          │<br>│  • SQL queries / ORM calls                               │<br>│  • One repository per aggregate/entity                   │<br>│  • Returns domain objects, not ORM models                │<br>│  • No business logic, no HTTP awareness                  │<br>└──────────────────────────────────────────────────────────┘</pre><h4>Repository: Pure Data Access</h4><pre>from sqlalchemy import select, func<br>from sqlalchemy.ext.asyncio import AsyncSession<br>from dataclasses import dataclass<br><br>@dataclass<br>class OrderRepository:<br>    session: AsyncSession<br><br>    async def get_by_id(self, order_id: int) -&gt; &quot;Order | None&quot;:<br>        result = await self.session.execute(<br>            select(OrderModel).where(OrderModel.id == order_id)<br>        )<br>        row = result.scalar_one_or_none()<br>        return row.to_domain() if row else None<br><br>    async def get_by_customer(<br>        self,<br>        customer_id: int,<br>        *,<br>        limit: int = 20,<br>        offset: int = 0,<br>    ) -&gt; list[&quot;Order&quot;]:<br>        result = await self.session.execute(<br>            select(OrderModel)<br>            .where(OrderModel.customer_id == customer_id)<br>            .order_by(OrderModel.created_at.desc())<br>            .limit(limit)<br>            .offset(offset)<br>        )<br>        return [row.to_domain() for row in result.scalars()]<br><br>    async def save(self, order: &quot;Order&quot;) -&gt; &quot;Order&quot;:<br>        model = OrderModel.from_domain(order)<br>        self.session.add(model)<br>        await self.session.flush()  # get the generated ID<br>        return model.to_domain()<br><br>    async def count_by_customer(self, customer_id: int) -&gt; int:<br>        result = await self.session.execute(<br>            select(func.count()).where(OrderModel.customer_id == customer_id)<br>        )<br>        return result.scalar_one()</pre><h4>Service: Business Logic Lives Here</h4><pre>from dataclasses import dataclass<br><br>class OrderLimitExceeded(Exception):<br>    def __init__(self, customer_id: int, current: int, limit: int):<br>        self.customer_id = customer_id<br>        self.current = current<br>        self.limit = limit<br>        super().__init__(<br>            f&quot;Customer {customer_id} has {current} orders (limit: {limit})&quot;<br>        )<br><br>class OrderNotFound(Exception):<br>    def __init__(self, order_id: int):<br>        self.order_id = order_id<br>        super().__init__(f&quot;Order {order_id} not found&quot;)<br><br>class OrderAccessDenied(Exception):<br>    pass<br><br>@dataclass<br>class OrderService:<br>    repository: OrderRepository<br>    max_orders_per_customer: int = 50<br><br>    async def create_order(<br>        self,<br>        customer_id: int,<br>        items: list,<br>        shipping_address: str,<br>        notes: str | None = None,<br>    ) -&gt; &quot;Order&quot;:<br>        # Business rule: enforce order limit<br>        current_count = await self.repository.count_by_customer(customer_id)<br>        if current_count &gt;= self.max_orders_per_customer:<br>            raise OrderLimitExceeded(<br>                customer_id, current_count, self.max_orders_per_customer<br>            )<br>        # Business rule: calculate totals<br>        total_cents = sum(item.quantity * item.unit_price_cents for item in items)<br>        order = Order(<br>            customer_id=customer_id,<br>            status=OrderStatus.PENDING,<br>            total_cents=total_cents,<br>            shipping_address=shipping_address,<br>            notes=notes,<br>            items=[<br>                OrderItem(<br>                    product_id=item.product_id,<br>                    quantity=item.quantity,<br>                    unit_price_cents=item.unit_price_cents,<br>                    line_total_cents=item.quantity * item.unit_price_cents,<br>                )<br>                for item in items<br>            ],<br>        )<br>        return await self.repository.save(order)<br><br>    async def get_order(self, order_id: int, requesting_user: &quot;User&quot;) -&gt; &quot;Order&quot;:<br>        order = await self.repository.get_by_id(order_id)<br>        if order is None:<br>            raise OrderNotFound(order_id)<br>        # Business rule: users can only see their own orders (admins see all)<br>        if requesting_user.role != &quot;admin&quot; and order.customer_id != requesting_user.id:<br>            raise OrderAccessDenied()<br>        return order<br><br>    async def cancel_order(self, order_id: int) -&gt; &quot;Order&quot;:<br>        order = await self.repository.get_by_id(order_id)<br>        if order is None:<br>            raise OrderNotFound(order_id)<br>        # Business rule: can only cancel pending or confirmed orders<br>        if order.status not in (OrderStatus.PENDING, OrderStatus.CONFIRMED):<br>            raise ValueError(<br>                f&quot;Cannot cancel order in &#39;{order.status.value}&#39; status&quot;<br>            )<br>        order.status = OrderStatus.CANCELLED<br>        return await self.repository.save(order)</pre><p>The service raises <strong>domain exceptions</strong> (OrderNotFound, OrderLimitExceeded), not HTTP exceptions. It has no idea it&#39;s being called from a web framework. This is the key — this code is testable with a mock repository, no HTTP server needed.</p><h4>Why “Fat Endpoints” Are a Red Flag</h4><p>In interviews, this is the anti-pattern you should explicitly call out:</p><pre># ❌ &quot;Fat endpoint&quot; — everything crammed into the route handler<br>@router.post(&quot;/orders&quot;)<br>async def create_order(body: CreateOrderRequest, db: DbSession, user: CurrentUser):<br>    # Counting orders in the endpoint...<br>    count = await db.execute(<br>        select(func.count()).where(OrderModel.customer_id == user.id)<br>    )<br>    if count.scalar_one() &gt;= 50:<br>        raise HTTPException(400, &quot;Too many orders&quot;)<br><br># Building the model in the endpoint...<br>    total = sum(i.quantity * i.unit_price_cents for i in body.items)<br>    order = OrderModel(customer_id=user.id, total_cents=total, status=&quot;pending&quot;)<br>    db.add(order)<br>    # Adding items in the endpoint...<br>    for item in body.items:<br>        db.add(OrderItemModel(order=order, product_id=item.product_id, ...))<br>    await db.commit()<br>    await db.refresh(order)<br>    return order  # returning ORM model directly? Also bad.</pre><p>This is untestable (requires a real database), unmaintainable (business rules are buried in HTTP handling), and unreusable (can’t call this logic from a CLI command or background job).</p><h4>4. Error Handling: Consistent Problem Details</h4><h4>The Problem With Default Error Responses</h4><p>FastAPI’s default error responses are inconsistent. Validation errors return one format, HTTPException returns another, and unhandled exceptions return a third. Clients can’t parse errors reliably.</p><pre>// Validation error (422)<br>{&quot;detail&quot;: [{&quot;loc&quot;: [&quot;body&quot;, &quot;items&quot;], &quot;msg&quot;: &quot;field required&quot;, &quot;type&quot;: &quot;value_error.missing&quot;}]}<br><br>// HTTPException (404)<br>{&quot;detail&quot;: &quot;Order not found&quot;}<br><br>// Unhandled exception (500)<br>{&quot;detail&quot;: &quot;Internal Server Error&quot;}</pre><h4>The Solution: RFC 9457 Problem Details</h4><p>Adopt a consistent error format based on the RFC 9457 standard (formerly RFC 7807). Every error response has the same structure.</p><pre>from pydantic import BaseModel<br>from fastapi import FastAPI, Request<br>from fastapi.responses import JSONResponse<br>from fastapi.exceptions import RequestValidationError<br><br>class ProblemDetail(BaseModel):<br>    type: str = &quot;about:blank&quot;<br>    title: str<br>    status: int<br>    detail: str<br>    instance: str | None = None<br>    errors: list[dict] | None = None  # for validation errors<br><br># ─── Exception-to-response mapping ───<br>def create_app() -&gt; FastAPI:<br>    app = FastAPI()<br><br>    @app.exception_handler(OrderNotFound)<br>    async def handle_not_found(request: Request, exc: OrderNotFound):<br>        return JSONResponse(<br>            status_code=404,<br>            content=ProblemDetail(<br>                type=&quot;/errors/order-not-found&quot;,<br>                title=&quot;Order Not Found&quot;,<br>                status=404,<br>                detail=str(exc),<br>                instance=str(request.url),<br>            ).model_dump(),<br>        )<br><br>    @app.exception_handler(OrderLimitExceeded)<br>    async def handle_limit_exceeded(request: Request, exc: OrderLimitExceeded):<br>        return JSONResponse(<br>            status_code=409,<br>            content=ProblemDetail(<br>                type=&quot;/errors/order-limit-exceeded&quot;,<br>                title=&quot;Order Limit Exceeded&quot;,<br>                status=409,<br>                detail=str(exc),<br>                instance=str(request.url),<br>            ).model_dump(),<br>        )<br><br>    @app.exception_handler(OrderAccessDenied)<br>    async def handle_access_denied(request: Request, exc: OrderAccessDenied):<br>        return JSONResponse(<br>            status_code=403,<br>            content=ProblemDetail(<br>                type=&quot;/errors/access-denied&quot;,<br>                title=&quot;Access Denied&quot;,<br>                status=403,<br>                detail=&quot;You do not have permission to access this resource&quot;,<br>                instance=str(request.url),<br>            ).model_dump(),<br>        )<br><br>    @app.exception_handler(RequestValidationError)<br>    async def handle_validation_error(request: Request, exc: RequestValidationError):<br>        return JSONResponse(<br>            status_code=422,<br>            content=ProblemDetail(<br>                type=&quot;/errors/validation-error&quot;,<br>                title=&quot;Validation Error&quot;,<br>                status=422,<br>                detail=&quot;Request body failed validation&quot;,<br>                instance=str(request.url),<br>                errors=[<br>                    {<br>                        &quot;field&quot;: &quot; → &quot;.join(str(loc) for loc in err[&quot;loc&quot;]),<br>                        &quot;message&quot;: err[&quot;msg&quot;],<br>                        &quot;type&quot;: err[&quot;type&quot;],<br>                    }<br>                    for err in exc.errors()<br>                ],<br>            ).model_dump(),<br>        )<br><br>    @app.exception_handler(Exception)<br>    async def handle_unhandled(request: Request, exc: Exception):<br>        # Log the full traceback for debugging<br>        import traceback<br>        traceback.print_exc()<br>        return JSONResponse(<br>            status_code=500,<br>            content=ProblemDetail(<br>                type=&quot;/errors/internal-error&quot;,<br>                title=&quot;Internal Server Error&quot;,<br>                status=500,<br>                detail=&quot;An unexpected error occurred&quot;,<br>                instance=str(request.url),<br>            ).model_dump(),<br>        )<br>    return app</pre><p>Now every error response looks the same:</p><pre>{<br>  &quot;type&quot;: &quot;/errors/order-not-found&quot;,<br>  &quot;title&quot;: &quot;Order Not Found&quot;,<br>  &quot;status&quot;: 404,<br>  &quot;detail&quot;: &quot;Order 42 not found&quot;,<br>  &quot;instance&quot;: &quot;/api/v1/orders/42&quot;<br>}</pre><h4>A Scalable Exception Mapping Pattern</h4><p>For larger applications with dozens of domain exceptions, a registry pattern is cleaner than individual handlers:</p><pre>from dataclasses import dataclass<br><br>@dataclass<br>class ErrorMapping:<br>    status_code: int<br>    error_type: str<br>    title: str<br><br># Central registry: domain exception → HTTP error details<br>EXCEPTION_MAP: dict[type[Exception], ErrorMapping] = {<br>    OrderNotFound:       ErrorMapping(404, &quot;/errors/order-not-found&quot;, &quot;Order Not Found&quot;),<br>    OrderLimitExceeded:  ErrorMapping(409, &quot;/errors/order-limit-exceeded&quot;, &quot;Order Limit Exceeded&quot;),<br>    OrderAccessDenied:   ErrorMapping(403, &quot;/errors/access-denied&quot;, &quot;Access Denied&quot;),<br>    CustomerNotFound:    ErrorMapping(404, &quot;/errors/customer-not-found&quot;, &quot;Customer Not Found&quot;),<br>    PaymentFailed:       ErrorMapping(402, &quot;/errors/payment-failed&quot;, &quot;Payment Failed&quot;),<br>    DuplicateOrder:      ErrorMapping(409, &quot;/errors/duplicate-order&quot;, &quot;Duplicate Order&quot;),<br>}<br><br>def register_exception_handlers(app: FastAPI):<br>    for exc_class, mapping in EXCEPTION_MAP.items():<br>        def make_handler(m: ErrorMapping):<br>            async def handler(request: Request, exc: Exception):<br>                return JSONResponse(<br>                    status_code=m.status_code,<br>                    content=ProblemDetail(<br>                        type=m.error_type,<br>                        title=m.title,<br>                        status=m.status_code,<br>                        detail=str(exc),<br>                        instance=str(request.url),<br>                    ).model_dump(),<br>                )<br>            return handler<br>        app.add_exception_handler(exc_class, make_handler(mapping))</pre><h4>5. Schema Versioning and Backward Compatibility</h4><h4>The /v1/ Pattern</h4><pre>from fastapi import FastAPI<br><br>app = FastAPI()<br><br># Version 1<br>from app.api.v1.router import router as v1_router<br>app.include_router(v1_router, prefix=&quot;/api/v1&quot;)<br><br># Version 2 (when needed)<br>from app.api.v2.router import router as v2_router<br>app.include_router(v2_router, prefix=&quot;/api/v2&quot;)</pre><h3>Project Structure for Versioned APIs</h3><pre>app/<br>├── api/<br>│   ├── v1/<br>│   │   ├── router.py          # v1 endpoints<br>│   │   ├── schemas.py         # v1 request/response models<br>│   │   └── dependencies.py    # v1-specific deps (if any)<br>│   └── v2/<br>│       ├── router.py<br>│       └── schemas.py         # v2 schemas (evolved)<br>├── services/                   # shared — business logic doesn&#39;t change per API version<br>│   └── order_service.py<br>├── repositories/               # shared — data access doesn&#39;t change per API version<br>│   └── order_repository.py<br>├── domain/                     # shared — domain models<br>│   └── order.py<br>└── main.py</pre><p>The critical insight: <strong>services and repositories are shared across versions.</strong> Only the presentation layer (routers and schemas) changes. A v2 endpoint might expose a new field or restructure the response, but it calls the same service.</p><h4>Backward-Compatible Schema Evolution</h4><pre># v1 — original schema<br>class OrderResponseV1(BaseModel):<br>    id: int<br>    customer_id: int<br>    total_cents: int<br>    status: str<br>    created_at: datetime<br><br># v2 - adds new fields, restructures money<br>class MoneyResponse(BaseModel):<br>    amount_cents: int<br>    currency: str<br>    @computed_field<br>    @property<br>    def formatted(self) -&gt; str:<br>        return f&quot;{self.amount_cents / 100:.2f} {self.currency}&quot;<br><br>class OrderResponseV2(BaseModel):<br>    id: int<br>    customer_id: int<br>    total: MoneyResponse            # structured money object instead of raw int<br>    status: OrderStatus             # enum instead of string<br>    items: list[OrderItemResponse]  # now included by default<br>    created_at: datetime<br>    updated_at: datetime            # new field</pre><p>Both versions call the same OrderService.get_order() — they just map the domain object to different response schemas.</p><h4>Putting It All Together: Full Project Structure</h4><pre>app/<br>├── main.py                     # App factory, lifespan, middleware<br>├── config.py                   # Settings via pydantic-settings<br>├── domain/<br>│   ├── order.py                # Domain entities (dataclasses)<br>│   └── exceptions.py           # Domain exceptions<br>├── repositories/<br>│   ├── base.py                 # Base repository (optional)<br>│   └── order_repository.py     # SQL queries<br>├── services/<br>│   └── order_service.py        # Business logic<br>├── api/<br>│   ├── dependencies.py         # Shared: DB session, auth, service factories<br>│   ├── errors.py               # Exception → ProblemDetail mapping<br>│   └── v1/<br>│       ├── router.py           # Thin endpoint definitions<br>│       └── schemas.py          # Request/response Pydantic models<br>├── models/<br>│   └── order.py                # SQLAlchemy ORM models<br>└── tests/<br>    ├── test_services/<br>    │   └── test_order_service.py   # Unit tests with mock repos<br>    ├── test_repositories/<br>    │   └── test_order_repo.py      # Integration tests with test DB<br>    └── test_api/<br>        └── test_orders.py          # End-to-end API tests</pre><h3>The Senior-Level Interview Answers</h3><p><strong>Q: How do you handle request/response validation in FastAPI?</strong></p><blockquote><em>“Separate Pydantic models for input and output — never the same model for both. I use </em><em>field_validator for single-field rules, </em><em>model_validator for cross-field business rules, and </em><em>Annotated types for reusable constraints like </em><em>PositiveInt or </em><em>MoneyInCents. In v2, </em><em>ConfigDict(from_attributes=True) replaces </em><em>orm_mode, and </em><em>computed_field handles derived response fields. The key principle is that the request schema represents what the client controls, and the response schema represents what the system produces — they evolve independently.&quot;</em></blockquote><p><strong>Q: Explain FastAPI’s dependency injection and how you use it.</strong></p><blockquote><em>“Dependencies form a layered graph: infrastructure at the bottom (DB sessions, config), authentication in the middle, and service factories at the top. I use </em><em>Annotated type aliases like </em><em>CurrentUser = Annotated[User, Depends(get_current_user)] to keep endpoint signatures clean. Dependencies that need cleanup use </em><em>yield. For authorization, I use factory functions like </em><em>require_role(&#39;admin&#39;) that return dependencies — this composes naturally in endpoint signatures. The DI system replaces the need for global singletons or manual wiring.&quot;</em></blockquote><p><strong>Q: How do you structure a FastAPI project for a real production API?</strong></p><blockquote><em>“Three layers: router, service, repository. Routers handle HTTP concerns and are thin — 3 to 5 lines per endpoint. Services contain all business logic and raise domain exceptions, never HTTP exceptions. Repositories encapsulate data access. The critical principle is that services are framework-agnostic and testable with mock repositories. Fat endpoints are the main anti-pattern — they’re untestable, unreusable, and unmaintainable.”</em></blockquote><p><strong>Q: How do you handle errors consistently?</strong></p><blockquote><em>“I map domain exceptions to RFC 9457 Problem Details responses using a central exception registry. The service layer raises </em><em>OrderNotFound or </em><em>OrderLimitExceeded; the error handling layer translates those to consistent JSON responses with type URIs, status codes, and human-readable details. Validation errors get the same treatment. Every error response has an identical structure, which makes client-side error handling predictable.&quot;</em></blockquote><p><strong>Q: How do you handle API versioning?</strong></p><blockquote><em>“URL-based versioning with </em><em>/api/v1/ prefixes. Only the presentation layer — routers and schemas — is versioned. Services and repositories are shared because business logic doesn&#39;t change when you restructure a response format. When evolving schemas, v2 can add fields, restructure nested objects, or change types, while v1 continues to work unchanged. The key is that adding a version is cheap because the layers are decoupled.&quot;</em></blockquote><p><em>This is the first article in a new interview prep cycle focused on upcoming senior Python interviews. Each post covers one major topic area with implementable code, architectural reasoning, and ready-to-deliver interview answers.</em></p><p><em>Follow along — we’re building the complete senior backend engineer knowledge base, one topic at a time.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=07ba8ac2f162" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Zero-downtime migrations, WHERE vs HAVING, JOIN types, and Many-to-Many — the SQL fundamentals that…]]></title>
            <link>https://staskoltsov.medium.com/zero-downtime-migrations-where-vs-having-join-types-and-many-to-many-the-sql-fundamentals-that-0d4a5bab4ce1?source=rss-e15c42fd98e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/0d4a5bab4ce1</guid>
            <category><![CDATA[sql-interview-questions]]></category>
            <category><![CDATA[python-interview-question]]></category>
            <dc:creator><![CDATA[Stanislav Lazarenko]]></dc:creator>
            <pubDate>Tue, 17 Feb 2026 06:30:29 GMT</pubDate>
            <atom:updated>2026-02-17T06:30:29.437Z</atom:updated>
            <content:encoded><![CDATA[<h3><em>Zero-downtime migrations, WHERE vs HAVING, JOIN types, and Many-to-Many — the SQL fundamentals that senior developers are expected to answer without hesitation.</em></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0S1AO6S39LpSZAxpuFQ3Mg.png" /></figure><h4>Question 1: “Adding a Column With DEFAULT and NOT NULL — What Happens to the Table?”</h4><p>The Problem: ALTER TABLE Can Lock Your Entire Table</p><p>When you write this innocent-looking migration:</p><pre>ALTER TABLE orders<br>ADD COLUMN status VARCHAR(20) NOT NULL DEFAULT &#39;pending&#39;;</pre><p>In PostgreSQL versions before 11, this statement <strong>rewrites the entire table</strong>. Every single row needs to be updated with the default value, and the table is locked with an ACCESS EXCLUSIVE lock for the duration. On a table with 50 million rows, that could mean minutes of downtime — no reads, no writes, nothing.</p><p>Even in PostgreSQL 11+, which handles simple defaults without a rewrite, adding NOT NULL to an existing column with data still requires a full table scan to verify the constraint. And if you&#39;re on MySQL, the behavior varies by version and storage engine, but the risk is real across all major databases.</p><h4>The Safe Migration: Three Steps, Zero Downtime</h4><p>The approach I described in the interview — and have used in production — breaks the dangerous single statement into safe, incremental steps.</p><p><strong>Step 1: Add the column as nullable, no default</strong></p><pre>-- This is nearly instant — just a metadata change<br>ALTER TABLE orders ADD COLUMN status VARCHAR(20);</pre><p>This acquires a brief lock to update the catalog but doesn’t touch any existing rows. On a table of any size, it completes in milliseconds.</p><p><strong>Step 2: Backfill existing rows in batches</strong></p><pre>import asyncpg<br>import asyncio<br><br>async def backfill_status(pool: asyncpg.Pool, batch_size: int = 5000):<br>    &quot;&quot;&quot;Update existing rows in small batches to avoid long locks.&quot;&quot;&quot;<br>    total_updated = 0<br>    while True:<br>        async with pool.acquire() as conn:<br>            # Update a small batch at a time<br>            result = await conn.execute(&quot;&quot;&quot;<br>                UPDATE orders<br>                SET status = &#39;pending&#39;<br>                WHERE id IN (<br>                    SELECT id FROM orders<br>                    WHERE status IS NULL<br>                    LIMIT $1<br>                    FOR UPDATE SKIP LOCKED  -- don&#39;t block concurrent transactions<br>                )<br>            &quot;&quot;&quot;, batch_size)<br>            count = int(result.split()[-1])<br>            total_updated += count<br>            print(f&quot;Updated {total_updated} rows...&quot;)<br>            if count &lt; batch_size:<br>                break<br>        # Small pause between batches to let other queries breathe<br>        await asyncio.sleep(0.1)<br>    print(f&quot;Backfill complete: {total_updated} rows updated&quot;)</pre><p>The key details: LIMIT keeps each transaction small. FOR UPDATE SKIP LOCKED avoids blocking concurrent writes. The sleep between batches prevents the backfill from monopolizing database resources.</p><p><strong>Step 3: Add the constraint after all data is clean</strong></p><pre>-- Set the default for future inserts<br>ALTER TABLE orders ALTER COLUMN status SET DEFAULT &#39;pending&#39;;<br><br>-- Add NOT NULL constraint<br>-- In PostgreSQL 12+, you can validate separately:<br>ALTER TABLE orders ADD CONSTRAINT orders_status_not_null<br>    CHECK (status IS NOT NULL) NOT VALID;<br>-- Validate without holding ACCESS EXCLUSIVE for the full scan<br>ALTER TABLE orders VALIDATE CONSTRAINT orders_status_not_null;<br>-- Or if you&#39;re confident all rows are backfilled:<br>ALTER TABLE orders ALTER COLUMN status SET NOT NULL;</pre><p>The NOT VALID + VALIDATE pattern is a PostgreSQL trick: NOT VALID adds the constraint for new rows immediately (brief lock), then VALIDATE checks existing rows with a weaker SHARE UPDATE EXCLUSIVE lock that doesn&#39;t block writes.</p><h4>Handling Race Conditions With Triggers</h4><p>The interviewer raised a subtle point: what happens if new rows are inserted between Step 1 and Step 3 <em>without</em> the default? You could end up with NULL values sneaking in during the backfill window.</p><p>The solution is a trigger that enforces the default during the transition period:</p><pre>-- Temporary trigger active during migration<br>CREATE OR REPLACE FUNCTION set_default_status()<br>RETURNS TRIGGER AS $$<br>BEGIN<br>    IF NEW.status IS NULL THEN<br>        NEW.status := &#39;pending&#39;;<br>    END IF;<br>    RETURN NEW;<br>END;<br>$$ LANGUAGE plpgsql;<br><br>CREATE TRIGGER trg_default_status<br>    BEFORE INSERT ON orders<br>    FOR EACH ROW<br>    EXECUTE FUNCTION set_default_status();<br>-- After Step 3 is complete, remove the trigger<br>DROP TRIGGER trg_default_status ON orders;<br>DROP FUNCTION set_default_status();</pre><p>This guarantees that no row ever enters the table with a NULL status, even during the migration window. Once the DEFAULT and NOT NULL constraints are in place, the trigger becomes redundant and gets dropped.</p><h4>The Complete Migration Timeline</h4><pre>Time ──────────────────────────────────────────────────────────►</pre><pre>1. ADD COLUMN (nullable)     — instant, brief lock<br>2. CREATE TRIGGER            — catch new inserts during backfill<br>3. BACKFILL in batches       — minutes to hours, no locks<br>4. SET DEFAULT               — instant, metadata only<br>5. ADD NOT NULL constraint   — validated with weak lock<br>6. DROP TRIGGER              — cleanup</pre><pre>Table is fully available for reads and writes throughout.</pre><h4>Migration Framework Integration</h4><p>If you’re using Alembic (the standard for SQLAlchemy projects), you structure this as multiple migration steps:</p><pre>&quot;&quot;&quot;add_status_column_safe&quot;&quot;&quot;<br><br>from alembic import op<br>import sqlalchemy as sa<br><br>def upgrade():<br>    # Step 1: Add nullable column (instant)<br>    op.add_column(&#39;orders&#39;, sa.Column(&#39;status&#39;, sa.String(20), nullable=True))<br>    # Step 2: Set default for new rows<br>    op.alter_column(&#39;orders&#39;, &#39;status&#39;, server_default=&#39;pending&#39;)<br>    # Step 3: Backfill will be run as a separate data migration script<br>    # DO NOT backfill inside the Alembic migration - it holds a transaction open<br>    # Step 4: After backfill completes, run a second migration:<br>    # op.alter_column(&#39;orders&#39;, &#39;status&#39;, nullable=False)<br><br>def downgrade():<br>    op.drop_column(&#39;orders&#39;, &#39;status&#39;)</pre><p>The backfill runs separately, outside Alembic’s transaction, using the batched approach shown above.</p><h4>Question 2: “What’s the Difference Between WHERE and HAVING?”</h4><p>This sounds like a junior question, but the interviewer asked it in the context of debugging a slow aggregation query — which changes the conversation entirely.</p><p><em>The Core Difference</em></p><p>WHERE filters individual rows <strong>before</strong> grouping. HAVING filters groups <strong>after</strong> aggregation.</p><pre>-- WHERE: &quot;Only look at orders from 2025&quot;<br>-- HAVING: &quot;Only show customers who spent more than $1000&quot;<br><br>SELECT<br>    u.name,<br>    COUNT(o.id) AS order_count,<br>    SUM(o.total) AS total_spent<br>FROM users u<br>JOIN orders o ON o.user_id = u.id<br>WHERE o.created_at &gt;= &#39;2025-01-01&#39;     -- filters rows BEFORE grouping<br>GROUP BY u.name<br>HAVING SUM(o.total) &gt; 1000;           -- filters groups AFTER aggregation</pre><h4>The Execution Order</h4><p>Understanding why this matters requires knowing SQL’s logical execution order:</p><pre>1. FROM / JOIN     — assemble the data sources<br>2. WHERE           — filter individual rows<br>3. GROUP BY        — create groups<br>4. Aggregate       — compute COUNT, SUM, AVG, etc.<br>5. HAVING          — filter groups based on aggregates<br>6. SELECT          — choose output columns<br>7. ORDER BY        — sort results<br>8. LIMIT           — restrict row count</pre><h4>The Performance Implication</h4><p>This is why the interviewer asked it in a senior context. Moving a filter from HAVING to WHERE (when possible) can dramatically improve performance:</p><pre>-- ❌ Slow: groups ALL orders, then filters by country<br>SELECT country, SUM(total) AS revenue<br>FROM orders<br>GROUP BY country<br>HAVING country IN (&#39;UA&#39;, &#39;PL&#39;, &#39;DE&#39;);<br><br>-- ✅ Fast: filters rows first, then groups only relevant orders<br>SELECT country, SUM(total) AS revenue<br>FROM orders<br>WHERE country IN (&#39;UA&#39;, &#39;PL&#39;, &#39;DE&#39;)<br>GROUP BY country;</pre><p>The first query aggregates every row in the table, then throws away most groups. The second query filters down to three countries first, then aggregates a fraction of the data. On a table with 100 countries and millions of rows, the difference is enormous.</p><p><strong>Rule of thumb:</strong> if a condition doesn’t involve an aggregate function (COUNT, SUM, AVG, MIN, MAX), it belongs in WHERE, not HAVING.</p><h4>Question 3: “What’s the Difference Between INNER JOIN and LEFT JOIN?”</h4><p>Again — sounds basic. The senior angle is about understanding data integrity implications and when choosing the wrong join silently gives you wrong results.</p><h4>INNER JOIN: Only Matching Rows</h4><pre>SELECT u.name, o.total<br>FROM users u<br>INNER JOIN orders o ON o.user_id = u.id;</pre><p>If a user has no orders, they <strong>disappear from the results entirely</strong>. INNER JOIN acts as an implicit filter.</p><h4>LEFT JOIN: All Left Rows, Matching Right Rows or NULL</h4><pre>SELECT u.name, o.total<br>FROM users u<br>LEFT JOIN orders o ON o.user_id = u.id;</pre><p>Every user appears, even if they have zero orders. For users without orders, o.total is NULL.</p><h4>Where This Actually Matters in Production</h4><p><strong>Reporting bugs:</strong> The most common mistake I’ve seen in production is using INNER JOIN in reports where LEFT JOIN is correct. A customer report that uses INNER JOIN orders silently excludes customers with no orders — making your customer count look lower than reality.</p><pre>-- ❌ Bug: &quot;How many users do we have per country?&quot;<br>-- Users with no orders are invisible<br>SELECT u.country, COUNT(DISTINCT u.id) AS user_count<br>FROM users u<br>INNER JOIN orders o ON o.user_id = u.id<br>GROUP BY u.country;<br><br>-- ✅ Correct: count ALL users, regardless of orders<br>SELECT u.country, COUNT(DISTINCT u.id) AS user_count<br>FROM users u<br>LEFT JOIN orders o ON o.user_id = u.id<br>GROUP BY u.country;</pre><p><strong>Debugging silent data loss:</strong> If your query returns fewer rows than expected, the first thing to check is whether an INNER JOIN is accidentally filtering out rows with no match.</p><p><strong>Conditional aggregation with LEFT JOIN:</strong></p><pre>-- Users with their order count (including 0 for users with no orders)<br>SELECT<br>    u.name,<br>    COUNT(o.id) AS order_count,       -- COUNT(column) ignores NULLs<br>    COALESCE(SUM(o.total), 0) AS revenue<br>FROM users u<br>LEFT JOIN orders o ON o.user_id = u.id<br>GROUP BY u.name;</pre><h4>The Full JOIN Family (For Completeness)</h4><p>JOIN Type Returns INNER JOIN Only rows with matches on both sides LEFT JOIN All left rows + matching right rows (NULL if no match) RIGHT JOIN All right rows + matching left rows (rarely used — just swap table order and use LEFT) FULL OUTER JOIN All rows from both sides (NULLs where no match) CROSS JOIN Cartesian product — every left row paired with every right row</p><h4>Question 4: “How Do You Implement a Many-to-Many Relationship?”</h4><p>The answer is straightforward: a <strong>junction table</strong> (also called a bridge table, link table, or association table). The senior angle is about what else goes on that table and how to handle it in application code.</p><h3>The Schema</h3><pre>-- Students can enroll in many courses; courses have many students<br>CREATE TABLE students (<br>    id SERIAL PRIMARY KEY,<br>    name VARCHAR(100) NOT NULL<br>);<br><br>CREATE TABLE courses (<br>    id SERIAL PRIMARY KEY,<br>    title VARCHAR(200) NOT NULL<br>);<br>-- Junction table<br>CREATE TABLE student_courses (<br>    student_id INTEGER NOT NULL REFERENCES students(id) ON DELETE CASCADE,<br>    course_id  INTEGER NOT NULL REFERENCES courses(id) ON DELETE CASCADE,<br>    enrolled_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),<br>    grade       VARCHAR(2),<br>    PRIMARY KEY (student_id, course_id)<br>);<br>-- Index for reverse lookups (find all students in a course)<br>CREATE INDEX idx_student_courses_course ON student_courses(course_id);</pre><h4>Senior-Level Details That Matter</h4><p><strong>1. The junction table often carries its own data</strong></p><p>Real relationships have metadata. A student-course link has an enrollment date, a grade, a status. An order-product link has quantity, unit price, discount. The junction table isn’t just a pair of foreign keys — it models the relationship itself.</p><pre>CREATE TABLE user_roles (<br>    user_id    INTEGER NOT NULL REFERENCES users(id),<br>    role_id    INTEGER NOT NULL REFERENCES roles(id),<br>    granted_by INTEGER REFERENCES users(id),<br>    granted_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),<br>    expires_at TIMESTAMPTZ,<br>    PRIMARY KEY (user_id, role_id)<br>);</pre><p><strong>2. Composite primary key vs. surrogate key</strong></p><p>The composite PK (student_id, course_id) enforces uniqueness naturally — a student can&#39;t enroll twice in the same course. Some ORMs prefer a surrogate id column, but that requires a separate unique constraint:</p><pre>-- Alternative with surrogate key (some ORMs prefer this)<br>CREATE TABLE student_courses (<br>    id SERIAL PRIMARY KEY,<br>    student_id INTEGER NOT NULL REFERENCES students(id),<br>    course_id  INTEGER NOT NULL REFERENCES courses(id),<br>    UNIQUE (student_id, course_id)   -- still enforce uniqueness<br>);</pre><p><strong>3. SQLAlchemy implementation</strong></p><pre>from sqlalchemy import Table, Column, Integer, ForeignKey, DateTime<br>from sqlalchemy.orm import DeclarativeBase, relationship, Mapped, mapped_column<br>from datetime import datetime<br><br>class Base(DeclarativeBase):<br>    pass<br><br># Option A: Simple link (no extra data on the relationship)<br>student_courses = Table(<br>    &quot;student_courses&quot;,<br>    Base.metadata,<br>    Column(&quot;student_id&quot;, Integer, ForeignKey(&quot;students.id&quot;), primary_key=True),<br>    Column(&quot;course_id&quot;, Integer, ForeignKey(&quot;courses.id&quot;), primary_key=True),<br>)<br><br>class Student(Base):<br>    __tablename__ = &quot;students&quot;<br>    id: Mapped[int] = mapped_column(primary_key=True)<br>    name: Mapped[str]<br>    courses: Mapped[list[&quot;Course&quot;]] = relationship(<br>        secondary=student_courses, back_populates=&quot;students&quot;<br>    )<br><br>class Course(Base):<br>    __tablename__ = &quot;courses&quot;<br>    id: Mapped[int] = mapped_column(primary_key=True)<br>    title: Mapped[str]<br>    students: Mapped[list[&quot;Student&quot;]] = relationship(<br>        secondary=student_courses, back_populates=&quot;courses&quot;<br>    )<br><br># Option B: Association Object (when junction table has extra data)<br>class Enrollment(Base):<br>    __tablename__ = &quot;enrollments&quot;<br>    student_id: Mapped[int] = mapped_column(ForeignKey(&quot;students.id&quot;), primary_key=True)<br>    course_id: Mapped[int] = mapped_column(ForeignKey(&quot;courses.id&quot;), primary_key=True)<br>    enrolled_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)<br>    grade: Mapped[str | None]<br>    student: Mapped[&quot;Student&quot;] = relationship()<br>    course: Mapped[&quot;Course&quot;] = relationship()</pre><p><strong>4. Querying Many-to-Many efficiently</strong></p><pre>-- Find all courses for a student (with enrollment info)<br>SELECT c.title, sc.enrolled_at, sc.grade<br>FROM courses c<br>JOIN student_courses sc ON sc.course_id = c.id<br>WHERE sc.student_id = 42;<br><br>-- Find students enrolled in multiple specific courses<br>SELECT sc.student_id, COUNT(DISTINCT sc.course_id) AS matched<br>FROM student_courses sc<br>WHERE sc.course_id IN (1, 5, 12)<br>GROUP BY sc.student_id<br>HAVING COUNT(DISTINCT sc.course_id) = 3;  -- enrolled in ALL three</pre><h4>Why “Basic” Questions Still Matter at Senior Level</h4><p>These four questions might appear in junior interviews too. The difference is in the expected depth.</p><p>A junior says: “LEFT JOIN returns nulls for unmatched rows.” A senior says: “LEFT JOIN returns nulls for unmatched rows — and using INNER JOIN accidentally in a reporting query is one of the most common sources of silent data bugs I’ve seen in production. I always default to LEFT JOIN for reports and explicitly choose INNER only when I want the filtering behavior.”</p><p>A junior says: “Use a junction table for many-to-many.” A senior says: “Junction table with a composite primary key, proper indexes for reverse lookups, CASCADE deletes on the foreign keys, and I model it as an Association Object in SQLAlchemy when the relationship carries its own data.”</p><p>The syntax is the same. The experience behind it is not.</p><h4>The Senior-Level Answers (Quick Reference)</h4><p><strong>Q: How do you safely add a NOT NULL column with DEFAULT to a large table?</strong></p><blockquote><em>“Never in a single ALTER TABLE on a live system. I add the column as nullable first — instant metadata change. Then I backfill existing rows in batches using </em><em>FOR UPDATE SKIP LOCKED to avoid blocking concurrent traffic. During the backfill window, a temporary trigger ensures new inserts get the default value, preventing race conditions. Once backfill is complete, I add the DEFAULT and NOT NULL constraint — using the </em><em>NOT VALID + </em><em>VALIDATE pattern in PostgreSQL for a non-blocking constraint check. Then drop the trigger.&quot;</em></blockquote><p><strong>Q: WHERE vs HAVING?</strong></p><blockquote><em>“WHERE filters individual rows before GROUP BY executes. HAVING filters aggregated groups after. Any condition that doesn’t involve an aggregate function belongs in WHERE — moving it there is often a significant performance win because it reduces the dataset before aggregation.”</em></blockquote><p><strong>Q: INNER JOIN vs LEFT JOIN?</strong></p><blockquote><em>“INNER JOIN returns only rows that match on both sides — it’s an implicit filter. LEFT JOIN preserves all rows from the left table, filling NULLs where there’s no match. In production, the critical decision is recognizing when INNER JOIN’s filtering behavior is what you want versus when it’s silently dropping data from your results.”</em></blockquote><p><strong>Q: How do you implement Many-to-Many?</strong></p><blockquote><em>“A junction table with foreign keys to both sides, a composite primary key for uniqueness, and an index on the non-leading FK for reverse lookups. In practice, the junction table usually carries relationship metadata — timestamps, status, permissions — which means modeling it as a full entity rather than just a pair of foreign keys.”</em></blockquote><p><em>Fifth in the series. This was the section where years of production debugging actually paid off. The migration question in particular — that’s a lesson you learn exactly once, usually during an incident.</em></p><p><em>Follow along. The series covers every question from a real senior Python interview.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0d4a5bab4ce1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[From slow JOINs to denormalization to partitioning — a complete guide to the DB performance…]]></title>
            <link>https://staskoltsov.medium.com/from-slow-joins-to-denormalization-to-partitioning-a-complete-guide-to-the-db-performance-c0b46290ee36?source=rss-e15c42fd98e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/c0b46290ee36</guid>
            <category><![CDATA[sql-interview-questions]]></category>
            <category><![CDATA[python-interview-question]]></category>
            <dc:creator><![CDATA[Stanislav Lazarenko]]></dc:creator>
            <pubDate>Mon, 16 Feb 2026 15:46:43 GMT</pubDate>
            <atom:updated>2026-02-16T15:46:43.347Z</atom:updated>
            <content:encoded><![CDATA[<h3><em>From slow JOINs to denormalization to partitioning — a complete guide to the DB performance questions senior developers actually get asked.</em></h3><h4>Three Database Performance Questions That Exposed the Gaps in My Senior Python Interview Prep</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZAc7IwKxtnyYUO2pKrYoIA.png" /></figure><p>Database questions in senior Python interviews have a way of humbling you. You can write beautiful async code, architect microservices, and debate type hints all day — but when the interviewer says “your SELECT is slow, where do you look first?”, the conversation shifts to territory where handwaving doesn’t work.</p><p>I got three database performance questions in a row during a recent interview. I had the right instincts on some, needed hints on others, and left knowing I had real gaps to fill. This post is me filling them.</p><h4>Question 1: “We Have a Slow SELECT With JOINs on a Large Dataset. Where Do You Look First?”</h4><p>This is the classic opener. It sounds simple, but the interviewer is mapping out your entire mental model for database performance troubleshooting. Your answer reveals whether you debug systematically or guess randomly.</p><h4>The Systematic Approach: Start With EXPLAIN</h4><p>Before changing anything, you need to <strong>see</strong> what the database is actually doing. Every investigation starts with EXPLAIN ANALYZE.</p><pre>EXPLAIN ANALYZE<br>SELECT u.name, o.total, p.name AS product<br>FROM users u<br>JOIN orders o ON o.user_id = u.id<br>JOIN order_items oi ON oi.order_id = o.id<br>JOIN products p ON p.id = oi.product_id<br>WHERE o.created_at &gt; &#39;2025-01-01&#39;<br>  AND u.country = &#39;UA&#39;;</pre><p>The output tells you everything: which indexes are being used (or not), what join strategy the planner chose, how many rows were scanned versus returned, and where the time is actually spent.</p><h4>The Checklist: What to Look For (In Priority Order)</h4><p><strong>1. Missing Indexes</strong></p><p>This is the number one cause of slow queries. If a column appears in a WHERE, JOIN ON, or ORDER BY clause and doesn&#39;t have an index, the database performs a sequential scan — reading every single row in the table.</p><pre>-- Check existing indexes<br>SELECT indexname, indexdef<br>FROM pg_indexes<br>WHERE tablename = &#39;orders&#39;;<br><br>-- Create indexes on columns used in JOINs and WHERE clauses<br>CREATE INDEX idx_orders_user_id ON orders(user_id);<br>CREATE INDEX idx_orders_created_at ON orders(created_at);<br>CREATE INDEX idx_order_items_order_id ON order_items(order_id);<br><br>-- Composite index for queries that filter on multiple columns together<br>CREATE INDEX idx_orders_user_date ON orders(user_id, created_at);</pre><p><strong>2. Join Strategy</strong></p><p>PostgreSQL chooses between three join algorithms. <strong>Nested Loop</strong> works well for small result sets but is disastrous for large ones — it’s O(n×m). <strong>Hash Join</strong> builds a hash table from one side and probes it with the other — efficient for large unsorted datasets. <strong>Merge Join</strong> works on sorted inputs and is very efficient when both sides are pre-sorted by the join key.</p><p>If EXPLAIN shows a Nested Loop on a million-row table, that&#39;s your problem.</p><p><strong>3. Selecting Too Much Data</strong></p><pre>-- ❌ Pulling everything when you need three columns<br>SELECT * FROM users u<br>JOIN orders o ON o.user_id = u.id;<br><br>-- ✅ Select only what you need<br>SELECT u.name, o.total, o.created_at<br>FROM users u<br>JOIN orders o ON o.user_id = u.id;</pre><p>SELECT * forces the database to read and transmit every column, including large text blobs and fields you never use. With wide tables, this alone can cause major slowdowns.</p><p><strong>4. Filtering Late</strong></p><pre>-- ❌ Join everything, then filter<br>SELECT u.name, o.total<br>FROM users u<br>JOIN orders o ON o.user_id = u.id<br>WHERE o.created_at &gt; &#39;2025-01-01&#39;;<br><br>-- ✅ With proper indexes, the planner filters early<br>-- But sometimes a CTE or subquery makes intent clearer:<br>SELECT u.name, o.total<br>sqJOIN (<br>    SELECT * FROM orders<br>    WHERE created_at &gt; &#39;2025-01-01&#39;<br>) o ON o.user_id = u.id;</pre><p>The optimizer usually handles this, but in complex queries with many joins, pushing filters closer to the source can help the planner make better decisions.</p><p><strong>5. Statistics and Maintenance</strong></p><p>PostgreSQL relies on table statistics to choose execution plans. Stale statistics lead to bad plans.</p><pre>-- Update statistics<br>ANALYZE orders;<br>ANALYZE users;<br><br>-- Check when the last ANALYZE ran<br>SELECT relname, last_analyze, last_autoanalyze<br>FROM pg_stat_user_tables<br>WHERE relname IN (&#39;orders&#39;, &#39;users&#39;);</pre><p>Quick Diagnostic Script</p><pre>import asyncpg<br><br>async def diagnose_slow_query(pool: asyncpg.Pool, query: str):<br>    async with pool.acquire() as conn:<br>        # Get the execution plan<br>        plan = await conn.fetch(f&quot;EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) {query}&quot;)<br>        plan_data = plan[0][&quot;QUERY PLAN&quot;][0]<br>        execution_time = plan_data[&quot;Execution Time&quot;]<br>        planning_time = plan_data[&quot;Planning Time&quot;]<br>        print(f&quot;Planning: {planning_time:.1f}ms | Execution: {execution_time:.1f}ms&quot;)<br>        # Walk the plan tree looking for problems<br>        <br>        def check_node(node, depth=0):<br>            node_type = node.get(&quot;Node Type&quot;, &quot;&quot;)<br>            rows_estimated = node.get(&quot;Plan Rows&quot;, 0)<br>            rows_actual = node.get(&quot;Actual Rows&quot;, 0)<br>            <br>            # Flag sequential scans on large tables<br>            if node_type == &quot;Seq Scan&quot; and rows_actual &gt; 10_000:<br>                table = node.get(&quot;Relation Name&quot;, &quot;unknown&quot;)<br>                print(f&quot;{&#39;  &#39; * depth}⚠️  Seq Scan on &#39;{table}&#39; - {rows_actual:,} rows. Needs index?&quot;)<br><br>            # Flag bad estimates (planner is guessing wrong)<br>            if rows_estimated &gt; 0 and rows_actual &gt; rows_estimated * 10:<br>                print(f&quot;{&#39;  &#39; * depth}⚠️  Bad estimate: planned {rows_estimated:,}, got {rows_actual:,}. Run ANALYZE?&quot;)<br><br>            for child in node.get(&quot;Plans&quot;, []):<br>                check_node(child, depth + 1)<br>        <br>        check_node(plan_data[&quot;Plan&quot;])</pre><h4>Question 2: “If Too Many JOINs Are the Problem, How Do You Optimize?”</h4><p>After I described the diagnostic approach, the interviewer tightened the scenario: <em>“What if the number of JOINs itself is the bottleneck?”</em></p><p>I suggested restructuring queries and using CTEs. The interviewer guided me toward the real answer: <strong>denormalization</strong>.</p><h4>Why JOINs Are Expensive at Scale</h4><p>Joins are the relational model’s superpower — and its Achilles’ heel. Each join multiplies the work the database has to do. With 6–8 joins across tables with millions of rows, even with perfect indexes, the sheer volume of intermediate results becomes the bottleneck.</p><p>The normalized schema is <em>correct</em> but <em>slow</em>:</p><pre>users → orders → order_items → products → categories → suppliers<br>  │                                                        │<br>  └──── addresses ─── cities ─── countries ────────────────┘</pre><p>Every query that needs “user name, product name, supplier country” traverses this entire chain.</p><h4>The Solution: Denormalization</h4><p>Denormalization means intentionally duplicating data to reduce the number of joins needed at query time. You trade storage space and write complexity for read performance.</p><p><strong>Strategy 1: Add Redundant Columns</strong></p><p>Instead of joining to get a user’s country on every order query, store it directly on the order.</p><pre>-- Before: requires JOIN to users → addresses → cities → countries<br>SELECT o.id, o.total, c.name AS country<br>FROM orders o<br>JOIN users u ON u.id = o.user_id<br>JOIN addresses a ON a.id = u.address_id<br>JOIN cities ci ON ci.id = a.city_id<br>JOIN countries c ON c.id = ci.country_id;<br><br>-- After: denormalized - country stored directly on orders<br>ALTER TABLE orders ADD COLUMN user_country VARCHAR(100);<br>-- Now it&#39;s a simple single-table query<br>SELECT id, total, user_country FROM orders;</pre><p><strong>Strategy 2: Materialized Views</strong></p><p>When you can’t (or don’t want to) change the schema, materialized views pre-compute the joined result.</p><pre>CREATE MATERIALIZED VIEW order_summary AS<br>SELECT<br>    o.id AS order_id,<br>    u.name AS user_name,<br>    u.email,<br>    o.total,<br>    o.created_at,<br>    p.name AS product_name,<br>    cat.name AS category,<br>    s.name AS supplier<br>FROM orders o<br>JOIN users u ON u.id = o.user_id<br>JOIN order_items oi ON oi.order_id = o.id<br>JOIN products p ON p.id = oi.product_id<br>JOIN categories cat ON cat.id = p.category_id<br>JOIN suppliers s ON s.id = p.supplier_id;<br><br>-- Create indexes on the materialized view<br>CREATE INDEX idx_order_summary_date ON order_summary(created_at);<br>CREATE INDEX idx_order_summary_user ON order_summary(user_name);<br>-- Refresh when underlying data changes<br>REFRESH MATERIALIZED VIEW CONCURRENTLY order_summary;</pre><p><strong>Strategy 3: Precomputed Aggregation Tables</strong></p><p>For analytics queries that aggregate across many tables, maintain summary tables updated by triggers or batch jobs.</p><pre>async def refresh_daily_summary(pool: asyncpg.Pool, date: str):<br>    async with pool.acquire() as conn:<br>        await conn.execute(&quot;&quot;&quot;<br>            INSERT INTO daily_sales_summary (date, country, category, total_orders, revenue)<br>            SELECT<br>                $1::date,<br>                o.user_country,<br>                p.category_name,    -- denormalized on products<br>                COUNT(*),<br>                SUM(o.total)<br>            FROM orders o<br>            JOIN order_items oi ON oi.order_id = o.id<br>            JOIN products p ON p.id = oi.product_id<br>            WHERE o.created_at::date = $1::date<br>            GROUP BY o.user_country, p.category_name<br>            ON CONFLICT (date, country, category)<br>            DO UPDATE SET total_orders = EXCLUDED.total_orders,<br>                         revenue = EXCLUDED.revenue<br>        &quot;&quot;&quot;, date)</pre><p>The key insight: <strong>denormalization is not a shortcut or a hack. It’s a deliberate architectural decision to optimize read-heavy workloads at the cost of write-time complexity and storage.</strong> Most production systems at scale use some form of denormalization.</p><h4>Question 3: “We Have a Huge Table But Only Query One Month at a Time. How Do You Optimize?”</h4><p>The interviewer set up a scenario: a table with years of data, but queries always filter by a date range — typically one month. My instinct was right (break the data into smaller pieces), but I didn’t use the correct terminology until the interviewer said the word: <strong>partitioning</strong>.</p><h4>What Is Partitioning?</h4><p>Partitioning splits one logical table into multiple physical sub-tables (partitions) based on a column value. The database knows which partition holds which data and can skip irrelevant ones entirely — this is called <strong>partition pruning</strong>.</p><pre>-- Create a partitioned table<br>CREATE TABLE events (<br>    id          BIGSERIAL,<br>    event_type  VARCHAR(50),<br>    payload     JSONB,<br>    created_at  TIMESTAMPTZ NOT NULL,<br>    PRIMARY KEY (id, created_at)   -- partition key must be in PK<br>) PARTITION BY RANGE (created_at);<br><br>-- Create monthly partitions<br>CREATE TABLE events_2025_01 PARTITION OF events<br>    FOR VALUES FROM (&#39;2025-01-01&#39;) TO (&#39;2025-02-01&#39;);<br>CREATE TABLE events_2025_02 PARTITION OF events<br>    FOR VALUES FROM (&#39;2025-02-01&#39;) TO (&#39;2025-03-01&#39;);<br>CREATE TABLE events_2025_03 PARTITION OF events<br>    FOR VALUES FROM (&#39;2025-03-01&#39;) TO (&#39;2025-04-01&#39;);</pre><p>Now when you query one month, PostgreSQL only reads that single partition:</p><pre>-- This ONLY scans events_2025_02, not the entire table<br>EXPLAIN SELECT * FROM events<br>WHERE created_at &gt;= &#39;2025-02-01&#39; AND created_at &lt; &#39;2025-03-01&#39;;<br><br>-- Output shows: &quot;Scan on events_2025_02&quot; - other partitions are pruned</pre><p>If your table has 500 million rows spanning 5 years but you query one month at a time, you just went from scanning 500M rows to roughly 8M. That’s a 60x improvement from schema design alone.</p><h4>Automatic Partition Creation</h4><p>The interviewer then asked: <strong>“How do you automatically create new partitions when a new month begins?”</strong></p><p>This is where many people get stuck. You don’t want an engineer manually running CREATE TABLE on the first of every month. There are several approaches.</p><p><strong>Approach 1: pg_partman (The Standard Answer for PostgreSQL)</strong></p><p>pg_partman is the de facto extension for automated partition management.</p><pre>-- Install and configure pg_partman<br>CREATE EXTENSION pg_partman;<br><br>SELECT create_parent(<br>    p_parent_table := &#39;public.events&#39;,<br>    p_control := &#39;created_at&#39;,<br>    p_type := &#39;range&#39;,<br>    p_interval := &#39;1 month&#39;,<br>    p_premake := 3              -- create 3 months ahead<br>);<br>-- pg_partman maintains partitions via a background worker<br>-- or via a cron job calling:<br>SELECT run_maintenance();</pre><p><strong>Approach 2: Cron Job + DDL Script</strong></p><p>If you can’t install extensions, a scheduled script works.</p><pre>import asyncpg<br>from datetime import datetime, timedelta<br><br>async def ensure_partitions(pool: asyncpg.Pool, months_ahead: int = 3):<br>    &quot;&quot;&quot;Create partitions for the next N months if they don&#39;t exist.&quot;&quot;&quot;<br>    today = datetime.utcnow().replace(day=1, hour=0, minute=0, second=0, microsecond=0)<br>    async with pool.acquire() as conn:<br>        for i in range(months_ahead + 1):<br>            month_start = today + timedelta(days=32 * i)<br>            month_start = month_start.replace(day=1)<br>            next_month = (month_start + timedelta(days=32)).replace(day=1)<br>            partition_name = f&quot;events_{month_start.strftime(&#39;%Y_%m&#39;)}&quot;<br>            # Check if partition already exists<br>            exists = await conn.fetchval(&quot;&quot;&quot;<br>                SELECT EXISTS (<br>                    SELECT 1 FROM pg_class WHERE relname = $1<br>                )<br>            &quot;&quot;&quot;, partition_name)<br>            if not exists:<br>                await conn.execute(f&quot;&quot;&quot;<br>                    CREATE TABLE {partition_name} PARTITION OF events<br>                    FOR VALUES FROM (&#39;{month_start.isoformat()}&#39;)<br>                                 TO (&#39;{next_month.isoformat()}&#39;)<br>                &quot;&quot;&quot;)<br>                print(f&quot;Created partition: {partition_name}&quot;)<br><br># Run this via cron or APScheduler<br># 0 0 1 * * python manage_partitions.py</pre><p><strong>Approach 3: Event-Driven with a Database Trigger</strong></p><p>For cases where you need partitions created on-demand (less common, but clever):</p><pre>CREATE OR REPLACE FUNCTION create_partition_if_needed()<br>RETURNS TRIGGER AS $$<br>DECLARE<br>    partition_name TEXT;<br>    start_date DATE;<br>    end_date DATE;<br>BEGIN<br>    start_date := DATE_TRUNC(&#39;month&#39;, NEW.created_at);<br>    end_date := start_date + INTERVAL &#39;1 month&#39;;<br>    partition_name := &#39;events_&#39; || TO_CHAR(start_date, &#39;YYYY_MM&#39;);<br><br>-- Try to create the partition (ignore if exists)<br>    BEGIN<br>        EXECUTE FORMAT(<br>            &#39;CREATE TABLE IF NOT EXISTS %I PARTITION OF events<br>             FOR VALUES FROM (%L) TO (%L)&#39;,<br>            partition_name, start_date, end_date<br>        );<br>    EXCEPTION WHEN duplicate_table THEN<br>        -- Already exists, that&#39;s fine<br>        NULL;<br>    END;<br>    RETURN NEW;<br>END;<br>$$ LANGUAGE plpgsql;</pre><h4>Partitioning vs. Sharding: Know the Difference</h4><p>The interviewer touched on both concepts, and senior candidates are expected to distinguish them clearly.</p><p><strong>Partitioning</strong> splits data within a single database server. All partitions live on the same machine. The database engine handles routing transparently. This is what you use when the data is large but a single server can still handle the load.</p><p><strong>Sharding</strong> splits data across multiple database servers. Each shard is an independent database instance, possibly on different machines. The application (or a middleware layer) handles routing. You use sharding when a single server can’t handle the load — either the data volume exceeds storage capacity, or the query throughput exceeds CPU/IO capacity.</p><pre>PARTITIONING (single server)          SHARDING (multiple servers)<br>┌─────────────────────────┐           ┌───────────┐  ┌───────────┐  ┌───────────┐<br>│ PostgreSQL              │           │ Server 1  │  │ Server 2  │  │ Server 3  │<br>│ ┌───────┐ ┌───────┐     │           │ Users A-H │  │ Users I-P │  │ Users Q-Z │<br>│ │ Jan   │ │ Feb   │     │           └───────────┘  └───────────┘  └───────────┘<br>│ ├───────┤ ├───────┤     │<br>│ │ Mar   │ │ Apr   │     │           Application must know which<br>│ └───────┘ └───────┘     │           server to query<br>│ DB handles routing      │<br>└─────────────────────────┘</pre><h4>The Senior-Level Answers</h4><p><strong>Q: Slow SELECT with JOINs — where do you look first?</strong></p><blockquote><em>“I start with </em><em>EXPLAIN ANALYZE to see the actual execution plan. The most common culprits in order: missing indexes on JOIN and WHERE columns causing sequential scans, suboptimal join strategies like Nested Loop on large tables, </em><em>SELECT * pulling unnecessary data, stale table statistics causing bad planner decisions, and filter conditions applied too late in the execution. I fix indexes first since that&#39;s usually 80% of the problem, then look at query structure.&quot;</em></blockquote><p><strong>Q: Too many JOINs hurting performance — how to optimize?</strong></p><blockquote><em>“If the number of JOINs is the core issue, the answer is denormalization — intentionally duplicating data to reduce joins at read time. This can mean adding redundant columns to frequently queried tables, creating materialized views that pre-compute the joined result, or maintaining aggregation tables refreshed by batch jobs or triggers. The tradeoff is increased write complexity and potential staleness, but for read-heavy workloads at scale, it’s the standard approach.”</em></blockquote><p><strong>Q: Huge table, always querying by month?</strong></p><blockquote><em>“That’s a textbook case for range partitioning on the date column. PostgreSQL’s declarative partitioning splits the table into monthly sub-tables, and partition pruning ensures queries only scan the relevant month. For automatic partition creation, I’d use </em><em>pg_partman in PostgreSQL, or a cron job that pre-creates partitions a few months ahead. This is different from sharding, which distributes data across multiple servers — partitioning stays on one server but organizes data for more efficient access.&quot;</em></blockquote><h4>Takeaway</h4><p>Database performance questions are where “I’ve read about it” and “I’ve debugged it at 3 AM” become visibly different. The theory is the same, but experienced developers describe things with the specificity that comes from having stared at EXPLAIN output, argued about denormalization tradeoffs in design reviews, and written the partition creation script themselves.</p><p>If you’re prepping for senior interviews, don’t just memorize these concepts. Open a PostgreSQL instance. Create a table with 10 million rows. Write a terrible query. Run EXPLAIN ANALYZE. Fix it. Break it again. That muscle memory is what interviews are really testing for.</p><p><em>Fourth post in my senior Python interview question series. This one hurt a bit — database internals are where I had the biggest gaps. Hopefully this saves you the same scramble.</em></p><p><em>Follow for the next installment. We’re getting through the hard stuff together.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c0b46290ee36" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Handling HTTP 429 (Too Many Requests) on the client side: from exponential backoff to internal…]]></title>
            <link>https://staskoltsov.medium.com/handling-http-429-too-many-requests-on-the-client-side-from-exponential-backoff-to-internal-4401d4345322?source=rss-e15c42fd98e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/4401d4345322</guid>
            <category><![CDATA[python-interview-question]]></category>
            <category><![CDATA[software-architecture]]></category>
            <dc:creator><![CDATA[Stanislav Lazarenko]]></dc:creator>
            <pubDate>Sun, 15 Feb 2026 19:07:19 GMT</pubDate>
            <atom:updated>2026-02-15T19:07:19.136Z</atom:updated>
            <content:encoded><![CDATA[<h3><em>Handling HTTP 429 (Too Many Requests) on the client side: from exponential backoff to internal proxy services and beyond.</em></h3><h4>I Got a Senior Python Interview Question Right — But the Interviewer’s Follow-Up Showed Me How Much Deeper It Goes</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q8yDx_h0BtheL4-Z3Qj8bQ.png" /></figure><h4>First, What Is a 429?</h4><p>HTTP 429 Too Many Requests means you&#39;ve hit a rate limit. The server is telling you: &quot;Slow down. You&#39;re sending more requests than I&#39;m willing to accept right now.&quot;</p><p>This is different from a 500 (server broke) or a 503 (server overloaded). A 429 is intentional, controlled, and usually temporary. The correct response is never to hammer the server harder — it’s to back off intelligently.</p><h4>Level 1: Exponential Backoff with Retry</h4><p>This is the foundational approach, and it’s what every senior developer should know by heart.</p><p>The idea is simple: when you get a 429, wait before retrying. If it fails again, wait longer. Each retry doubles the delay, and you add a small random jitter to prevent a “thundering herd” of clients all retrying at exactly the same moment.</p><pre>import httpx<br>import asyncio<br>import random<br><br>async def request_with_backoff(<br>    client: httpx.AsyncClient,<br>    method: str,<br>    url: str,<br>    *,<br>    max_retries: int = 5,<br>    base_delay: float = 1.0,<br>    max_delay: float = 60.0,<br>    **kwargs,<br>) -&gt; httpx.Response:<br>    for attempt in range(max_retries + 1):<br>        response = await client.request(method, url, **kwargs)<br>        if response.status_code != 429:<br>            return response<br>        if attempt == max_retries:<br>            response.raise_for_status()<br>        # Respect Retry-After header if the server provides one<br>        retry_after = response.headers.get(&quot;Retry-After&quot;)<br>        if retry_after:<br>            delay = float(retry_after)<br>        else:<br>            delay = min(base_delay * (2 ** attempt), max_delay)<br>        # Add jitter: randomize between 50-100% of calculated delay<br>        jitter = delay * (0.5 + random.random() * 0.5)<br>        print(f&quot;429 received. Retry {attempt + 1}/{max_retries} in {jitter:.1f}s&quot;)<br>        await asyncio.sleep(jitter)<br>    # Should never reach here, but just in case<br>    return response</pre><h4>Why Exponential and Not Linear?</h4><p>Linear backoff (wait 1s, 2s, 3s, 4s…) recovers too aggressively. If a rate limit window is 60 seconds, linear retries will keep bumping into it. Exponential backoff (1s, 2s, 4s, 8s, 16s…) quickly spaces out requests, giving the rate limit window time to reset.</p><h4>The Retry-After Header</h4><p>Many well-designed APIs include a Retry-After header in their 429 response. It tells you exactly how long to wait — either as seconds or as an HTTP date. Always check for it before calculating your own delay.</p><pre># Retry-After can be seconds...<br>Retry-After: 30<br># ...or an HTTP date<br>Retry-After: Thu, 01 Jan 2026 00:00:00 GMT</pre><p>Ignoring this header means you’re guessing when you could be precise.</p><h4>Production-Ready: Using tenacity</h4><p>In real projects, you rarely hand-roll retry logic. The tenacity library handles this cleanly:</p><pre>from tenacity import (<br>    retry,<br>    retry_if_result,<br>    wait_exponential,<br>    stop_after_attempt,<br>    before_sleep_log,<br>)<br>import logging<br>import httpx<br><br>logger = logging.getLogger(__name__)<br>def is_rate_limited(response: httpx.Response) -&gt; bool:<br>    return response.status_code == 429<br>@retry(<br>    retry=retry_if_result(is_rate_limited),<br>    wait=wait_exponential(multiplier=1, min=1, max=60),<br>    stop=stop_after_attempt(6),<br>    before_sleep=before_sleep_log(logger, logging.WARNING),<br>)<br>async def fetch(client: httpx.AsyncClient, url: str) -&gt; httpx.Response:<br>    return await client.get(url)</pre><h4>Level 2: The Internal Proxy Service</h4><p>Here’s the scenario the interviewer described: imagine you have 15 microservices, all calling the same third-party API (say, a payment provider or an AI model API). Each service independently implements retry logic. The problem? None of them know what the others are doing.</p><p>Service A burns through 80% of the rate limit. Services B through 0 each think they have a full budget. They all hit 429s simultaneously, all back off, all retry at roughly the same time, and the cycle continues.</p><p>The solution is an <strong>internal proxy service</strong> (sometimes called a rate limit gateway or API gateway) that sits between your services and the external API.</p><pre>┌─────────────┐<br>│ Service A    │──┐<br>├─────────────┤  │    ┌──────────────────┐    ┌─────────────────┐<br>│ Service B    │──┼───▶│  Rate Limit      │───▶│  External API   │<br>├─────────────┤  │    │  Proxy Service   │    │  (e.g. Stripe)  │<br>│ Service C    │──┘    └──────────────────┘    └─────────────────┘<br>└─────────────┘<br>                       - Single point of rate tracking<br>                       - Request queuing<br>                       - Fair scheduling across services</pre><h4>What the Proxy Does</h4><p>The proxy maintains a <strong>global view</strong> of rate limit consumption. It knows how many requests have been sent in the current window, how many remain, and it can queue or throttle requests before they ever hit the external API.</p><pre>import asyncio<br>import time<br>from dataclasses import dataclass, field<br><br>@dataclass<br>class RateLimitProxy:<br>    &quot;&quot;&quot;Simplified token bucket rate limiter for an internal proxy.&quot;&quot;&quot;<br>    max_tokens: int          # e.g., 100 requests per window<br>    refill_rate: float       # tokens per second<br>    _tokens: float = field(init=False)<br>    _last_refill: float = field(init=False)<br>    _lock: asyncio.Lock = field(init=False, default_factory=asyncio.Lock)<br>    <br>    def __post_init__(self):<br>        self._tokens = float(self.max_tokens)<br>        self._last_refill = time.monotonic()<br><br>    async def acquire(self, timeout: float = 30.0) -&gt; bool:<br>        deadline = time.monotonic() + timeout<br>        while True:<br>            async with self._lock:<br>                self._refill()<br>                if self._tokens &gt;= 1:<br>                    self._tokens -= 1<br>                    return True<br>            # Wait a bit and try again<br>            if time.monotonic() &gt;= deadline:<br>                return False<br>            await asyncio.sleep(0.1)<br>    <br>     def _refill(self):<br>         now = time.monotonic()<br>         elapsed = now - self._last_refill<br>         self._tokens = min(<br>             self.max_tokens,<br>             self._tokens + elapsed * self.refill_rate,<br>         )<br>         self._last_refill = now</pre><h4>Why This Is a Senior/Architect-Level Answer</h4><p>This approach solves problems that per-service retry logic simply can’t address. It provides <strong>centralized tracking</strong>: one component knows the global request count, not just a single service’s count. It enables <strong>fair scheduling</strong> so one greedy service can’t starve the others. It offers <strong>proactive throttling</strong> by queuing requests before hitting 429s rather than reacting to them. And it produces <strong>better observability</strong> since you get a single dashboard for rate limit consumption, queue depth, and per-service usage.</p><h4>Level 3: The Complete Strategy — Combining Both</h4><p>In a well-architected system, you don’t choose between backoff and a proxy. You layer them.</p><h4>Layer 1: Client-Side Awareness</h4><p>Each service should still handle 429s gracefully. The proxy might be down, or you might hit rate limits from an API that doesn’t go through the proxy.</p><pre>class ResilientClient:<br>    def __init__(self, proxy_url: str, direct_url: str):<br>        self.proxy_url = proxy_url<br>        self.direct_url = direct_url<br>        self.client = httpx.AsyncClient()<br><br>    async def call_api(self, path: str, **kwargs) -&gt; httpx.Response:<br>            # Try through proxy first<br>            try:<br>                response = await request_with_backoff(<br>                    self.client, &quot;GET&quot;, f&quot;{self.proxy_url}{path}&quot;, <br>                    max_retries=2, **kwargs,<br>                )<br>                return response<br>            except httpx.ConnectError:<br>                # Proxy is down - fall back to direct with more conservative retries<br>                return await request_with_backoff(<br>                    self.client, &quot;GET&quot;, f&quot;{self.direct_url}{path}&quot;,<br>                    max_retries=5, base_delay=2.0, **kwargs,<br>                )</pre><h4>Layer 2: Proxy-Level Rate Management</h4><p>The proxy tracks global consumption, queues requests, and manages the token bucket.</p><h4>Layer 3: Circuit Breaker</h4><p>If you’re consistently hitting 429s even with backoff, something is fundamentally wrong — maybe your rate limit tier is too low for your traffic, or there’s a bug creating runaway requests. A circuit breaker stops the bleeding.</p><pre>from enum import Enum<br><br>class CircuitState(Enum):<br>    CLOSED = &quot;closed&quot;         # Normal operation<br>    OPEN = &quot;open&quot;             # Failing - block requests<br>    HALF_OPEN = &quot;half_open&quot;   # Testing if recovery happened<br><br>class CircuitBreaker:<br>    def __init__(self, failure_threshold: int = 10, recovery_time: float = 60.0):<br>        self.failure_threshold = failure_threshold<br>        self.recovery_time = recovery_time<br>        self.state = CircuitState.CLOSED<br>        self.failure_count = 0<br>        self.last_failure_time = 0.0<br>    <br>    def record_failure(self):<br>        self.failure_count += 1<br>        self.last_failure_time = time.monotonic()<br>        if self.failure_count &gt;= self.failure_threshold:<br>            self.state = CircuitState.OPEN<br><br>    def record_success(self):<br>        self.failure_count = 0<br>        self.state = CircuitState.CLOSED<br><br>    def can_proceed(self) -&gt; bool:<br>        if self.state == CircuitState.CLOSED:<br>            return True<br>        if self.state == CircuitState.OPEN:<br>            if time.monotonic() - self.last_failure_time &gt; self.recovery_time:<br>                self.state = CircuitState.HALF_OPEN<br>                return True<br>            return False<br>        # HALF_OPEN: allow one request to test<br>        return True</pre><h4>What the Interviewer Was Really Testing</h4><p>This question has three layers, and each reveals a different level of experience.</p><p><strong>Junior answer:</strong> “Retry the request.” — Shows basic awareness but no strategy.</p><p><strong>Mid-level answer:</strong> “Retry with exponential backoff and jitter, respect the Retry-After header.&quot; — Shows practical implementation skill.</p><p><strong>Senior answer:</strong> “Exponential backoff with jitter at the client level, plus a centralized rate limit proxy when multiple services share the same API budget, combined with circuit breakers for systemic issues.” — Shows architectural thinking.</p><p>The interviewer wasn’t testing whether I could write a retry loop. They were testing whether I think about <strong>distributed systems</strong> — how multiple services coordinate around a shared constraint.</p><h4>Quick Reference: The 429 Handling Toolkit</h4><p><strong>Client-side essentials</strong> — exponential backoff with jitter, Retry-After header parsing, max retry limits with eventual failure propagation, and libraries like tenacity or stamina for production code.</p><p><strong>Architecture-level tools</strong> — internal rate limit proxy or API gateway, token bucket or sliding window algorithms, per-service quota allocation, and request queuing with priority levels.</p><p><strong>Resilience patterns</strong> — circuit breakers to prevent cascading failures, bulkheads to isolate API call pools, fallback strategies such as cached responses and degraded mode, and observability through rate limit dashboards and alerting.</p><h4>The Senior-Level Answer</h4><blockquote><em>“On the client side, the foundation is exponential backoff with jitter, always respecting the </em><em>Retry-After header when provided. In production I use </em><em>tenacity to avoid hand-rolling retry logic. But at the architecture level, when multiple services share the same external API budget, you need a centralized rate limit proxy — an internal service that tracks global consumption, queues requests, and distributes the rate limit budget fairly across consumers. I&#39;d also layer in a circuit breaker to detect when rate limiting becomes systemic rather than transient. The client retry handles intermittent 429s; the proxy prevents them; the circuit breaker catches the case when neither is enough.&quot;</em></blockquote><p><em>Third post in a series dissecting real senior Python interview questions. Some I bombed, some I nailed, all of them taught me something. Follow along if you’re prepping too.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4401d4345322" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>