Python Tutorial 0/52 lessons ~6 min read Lesson 49

    Python System Design Patterns

    Advanced Python interviews and real projects require system design fluency: choose the right concurrency model, cache strategy, and failure handling.

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Advanced Python interviews and real projects require system design fluency: choose the right concurrency model, cache strategy, and failure handling.

    Understanding the topic

    Core concepts to understand:

    • Pick async for I/O-heavy APIs, processes for CPU-heavy workers.
    • Use idempotent jobs + retry with exponential backoff.
    • Add caching layers (in-memory/Redis) around expensive reads.
    • Design observability: logs, metrics, traces from day one.

    Syntax reference

    Visual flow / code:

    python
    # API + worker architecture (conceptual)
    # Client -> FastAPI -> Postgres
    # -> Redis cache
    # -> Queue -> Worker pool -> External APIs
    #
    # Retry pattern (pseudo):
    for attempt in range(max_retries):
    try:
    return call_remote()
    except TransientError:
    sleep(2 ** attempt)
    raise RuntimeError("failed after retries")

    Execution workflow

    1Python System Design Patterns Workflow
    1 / 4

    Pick async for I/O-heavy APIs,…

    Pick async for I/O-heavy APIs, processes for CPU-heavy workers.

    Real-world use

    Most production Python systems are API gateways + async jobs + Postgres + Redis + observability stack. Knowing these patterns is what distinguishes senior engineers.

    Best practices

    • Define SLOs first.
    • Idempotency keys for retries.
    • Circuit breakers for flaky dependencies.
    • Degrade gracefully when downstream is down.

    Common mistakes

    • No timeout/retry policy.
    • Retry storms causing cascading failures.
    • No instrumentation.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1AdvancedQuestionDesign a Python rate limiter.+

    Answer

    A common choice is a token bucket: each client has a bucket with capacity and a refill rate, and each request consumes a token or is rejected (HTTP 429). For one process, keep timestamps or counters in memory with a lock; for several instances, store state in Redis and update it atomically, for example in a Lua script. Alternatives are fixed or sliding windows, which trade accuracy for memory.

    Follow-up

    How do you avoid race conditions across multiple servers?
    2AdvancedQuestionHow to handle retry storms?+

    Answer

    A retry storm happens when many clients retry a failing service at once and keep it down. Mitigate with exponential backoff plus random jitter, a cap on attempts, retry budgets, and circuit breakers that stop calls to an unhealthy dependency. Retry only idempotent operations or use idempotency keys, and add timeouts and load shedding on the server.

    Follow-up

    Why is jitter needed on top of exponential backoff?
    3AdvancedQuestionAsync vs worker queue in this scenario?+

    Answer

    The question does not give the scenario, so it depends on the work. async handles many concurrent I/O waits inside a request, with low latency but no durability. A worker queue (Celery, RQ, or similar, with a broker) suits long, CPU-heavy, or must-not-lose jobs because it gives retries, persistence, and independent scaling. Use async for short I/O fan-out and a queue for background or heavy work.

    Follow-up

    What delivery guarantees would you want from the queue?

    Summary

    In summary: System design is trade-offs under failure. Reliability patterns are mandatory, not optional.

    Ready to mark this lesson complete?Track your journey across the entire course.