<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anas Issath</title>
    <description>The latest articles on DEV Community by Anas Issath (@anas_issath).</description>
    <link>https://dev.to/anas_issath</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F907893%2F57da9c4e-064f-47b3-a5ca-5a75138b4d51.png</url>
      <title>DEV Community: Anas Issath</title>
      <link>https://dev.to/anas_issath</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anas_issath"/>
    <language>en</language>
    <item>
      <title>Microservices Won't Fix Your Architecture. They Might Expose It.</title>
      <dc:creator>Anas Issath</dc:creator>
      <pubDate>Sun, 29 Nov 2026 11:01:31 +0000</pubDate>
      <link>https://dev.to/anas_issath/microservices-wont-fix-your-architecture-they-might-expose-it-5e11</link>
      <guid>https://dev.to/anas_issath/microservices-wont-fix-your-architecture-they-might-expose-it-5e11</guid>
      <description>&lt;p&gt;&lt;em&gt;The hidden costs of distributed systems.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A monolith becomes difficult to maintain.&lt;/p&gt;

&lt;p&gt;Deployments take longer. Teams start stepping on each other's code. A small change requires running thousands of tests.&lt;/p&gt;

&lt;p&gt;Someone suggests microservices.&lt;/p&gt;

&lt;p&gt;Break the application into smaller services. Give each team ownership. Deploy independently. Scale only what needs scaling.&lt;/p&gt;

&lt;p&gt;It sounds like the natural next step.&lt;/p&gt;

&lt;p&gt;Six months later, the company has 20 services, multiple databases, several message queues, and a deployment pipeline nobody wants to touch on Friday.&lt;/p&gt;

&lt;p&gt;The original problems haven't disappeared. Some have simply moved across the network.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Microservices don't automatically reduce complexity. They change where that complexity lives.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And that's something engineering teams need to understand before choosing a distributed architecture.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. The Problem Was Never Just the Monolith
&lt;/h2&gt;

&lt;p&gt;When developers complain about monoliths, they're often complaining about something else:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unclear module boundaries&lt;/li&gt;
&lt;li&gt;Poorly organized business logic&lt;/li&gt;
&lt;li&gt;Too many dependencies between features&lt;/li&gt;
&lt;li&gt;Slow testing and deployment pipelines&lt;/li&gt;
&lt;li&gt;Teams that can't make changes without coordinating with everyone else&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are legitimate engineering problems. &lt;strong&gt;But moving code into separate services doesn't automatically solve them.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine an e-commerce application with three modules: &lt;strong&gt;Orders&lt;/strong&gt;, &lt;strong&gt;Payments&lt;/strong&gt;, and &lt;strong&gt;Inventory&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Inside a monolith, creating an order might involve calling three internal functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;OrderService
    |
    +--&amp;gt; PaymentService
    |
    +--&amp;gt; InventoryService
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now split those modules into independently deployed services.&lt;/p&gt;

&lt;p&gt;The business workflow hasn't changed. But &lt;strong&gt;internal function calls have become network requests.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You now need to think about timeouts, authentication between services, partial failures, retries, deployment compatibility, and distributed tracing.&lt;/p&gt;

&lt;p&gt;A failed function call is usually straightforward to investigate. A failed distributed workflow might involve three services, different logs, and a message that was delivered twice.&lt;/p&gt;

&lt;p&gt;The architecture has gained deployment flexibility. It has also gained new failure modes. &lt;strong&gt;That's the trade-off.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Start with boundaries, not deployments
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;modular monolith&lt;/strong&gt; can often provide many of the organizational benefits teams want without introducing network boundaries.&lt;/p&gt;

&lt;p&gt;You can establish separate modules, ownership rules, internal contracts, and dependency restrictions while keeping a single deployable application.&lt;/p&gt;

&lt;p&gt;That doesn't make a modular monolith universally better. It means &lt;strong&gt;independent deployment should solve an actual problem&lt;/strong&gt;, rather than become an architectural goal on its own.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Independent Deployment Is the Real Test
&lt;/h2&gt;

&lt;p&gt;Microservices are often described as small, independently deployable services.&lt;/p&gt;

&lt;p&gt;But the important word isn't &lt;em&gt;small&lt;/em&gt;. &lt;strong&gt;It's &lt;em&gt;independently&lt;/em&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose a team wants to release a change to the Orders service. Before deploying, they must coordinate with Payments, Inventory, and Notifications because all four services depend on the same database schema.&lt;/p&gt;

&lt;p&gt;Can Orders really be deployed independently? Not in any meaningful sense.&lt;/p&gt;

&lt;p&gt;The application has been physically separated, but it remains tightly coupled. This is how teams end up with a &lt;strong&gt;distributed monolith&lt;/strong&gt;: the operational overhead of microservices without the flexibility that justified adopting them.&lt;/p&gt;

&lt;h3&gt;
  
  
  A useful architecture review question
&lt;/h3&gt;

&lt;p&gt;Ask your team:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Can we deploy this service without requiring another team to deploy theirs at the same time?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If the answer is consistently no, investigate the boundaries. Look for shared database tables, incompatible API changes, synchronized releases, and business logic spread across multiple services.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sometimes the solution is improving the contracts.&lt;/li&gt;
&lt;li&gt;Sometimes it's moving responsibilities.&lt;/li&gt;
&lt;li&gt;And sometimes &lt;strong&gt;two services should simply become one.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Microsoft's Azure Architecture Center makes a similar recommendation: when services communicate excessively and depend heavily on one another, reconsider their boundaries or merge them.&lt;/p&gt;

&lt;p&gt;The number of services isn't a useful measure of architectural maturity. &lt;strong&gt;The ability to change them safely is.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Shared Databases Quietly Destroy Service Ownership
&lt;/h2&gt;

&lt;p&gt;One of the easiest mistakes during a microservices migration is keeping a shared database.&lt;/p&gt;

&lt;p&gt;It feels practical. The data already exists. Splitting it would take significant effort. Services can simply query the tables they need.&lt;/p&gt;

&lt;p&gt;Consider two services:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Orders Service ------+
                     |
                     v
                Shared Database
                     ^
                     |
Payments Service ----+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both services access the same tables. Initially, this reduces migration effort.&lt;/p&gt;

&lt;p&gt;But imagine the Payments team needs to change the schema. That change might break Orders. Now both teams must coordinate database migrations, deployment order, and rollback procedures.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The service boundaries provide little protection.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Ownership should include data
&lt;/h3&gt;

&lt;p&gt;A stronger design gives each service authority over its own data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Orders Service          Payments Service
      |                        |
      v                        v
Orders Database         Payments Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Orders can access payment information through a defined API or consume published events.&lt;/p&gt;

&lt;p&gt;This introduces new challenges, particularly around synchronization and consistency. But it also establishes clearer ownership.&lt;/p&gt;

&lt;p&gt;The principle isn't that every service must run a separate physical database server. Multiple services can use the same database infrastructure while owning separate schemas and tables.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The important rule is avoiding direct access to another service's private data.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Microsoft's official microservices data guidance emphasizes this distinction. A service that can't control its own data model isn't fully independent.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Distributed Transactions Change Everything
&lt;/h2&gt;

&lt;p&gt;A monolith often relies on database transactions to maintain consistency. Imagine creating an order and reducing inventory inside a single database transaction.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;status&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="mi"&gt;101&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'CREATED'&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;inventory&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;available&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;available&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;product_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;available&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Commit only if the inventory update&lt;/span&gt;
&lt;span class="c1"&gt;-- affected the expected row.&lt;/span&gt;

&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The example is simplified, but the principle is clear. &lt;strong&gt;Both changes can succeed together or be rolled back together.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now imagine Orders and Inventory use separate databases. A local transaction in Orders cannot automatically guarantee a successful update in Inventory.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What happens when the order is created but the inventory request fails?&lt;/li&gt;
&lt;li&gt;What happens when the inventory update succeeds but its response never arrives?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These questions introduce a fundamental distributed systems concern: &lt;strong&gt;consistency across independently failing components.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Consider the Saga pattern
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;Saga&lt;/strong&gt; coordinates a business operation through multiple local transactions. For example:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create an order in a pending state.&lt;/li&gt;
&lt;li&gt;Reserve inventory.&lt;/li&gt;
&lt;li&gt;Authorize payment.&lt;/li&gt;
&lt;li&gt;Confirm the order.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If payment fails, the workflow can release the inventory reservation and cancel the order. These are &lt;strong&gt;compensating actions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But compensation is not always equivalent to rolling back a database transaction:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An email cannot be unsent.&lt;/li&gt;
&lt;li&gt;An external payment operation may require a separate refund.&lt;/li&gt;
&lt;li&gt;Other systems may observe intermediate states before the workflow completes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's why distributed workflows need explicit state management, failure handling, and clear business rules.&lt;/p&gt;

&lt;p&gt;Not every workflow requires strong consistency. But engineers should know &lt;strong&gt;which operations can tolerate eventual consistency and which cannot&lt;/strong&gt;. This is especially important in payments, inventory management, financial systems, and other domains where partial completion has real consequences.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Asynchronous Messaging Isn't Free Decoupling
&lt;/h2&gt;

&lt;p&gt;When synchronous communication becomes difficult, teams often introduce a message broker. Instead of directly calling another service, one service publishes an event.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"event_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"evt_1024"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"event_type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"order.created"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"order_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"ord_782"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"occurred_at"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"2026-11-11T08:00:00Z"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another service consumes the event and performs its own work.&lt;/p&gt;

&lt;p&gt;This can reduce direct dependencies. The Orders service doesn't need to wait for every downstream operation before responding.&lt;/p&gt;

&lt;p&gt;But messaging introduces another set of problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What if the consumer processes the same event &lt;strong&gt;twice&lt;/strong&gt;?&lt;/li&gt;
&lt;li&gt;What if messages arrive &lt;strong&gt;out of order&lt;/strong&gt;?&lt;/li&gt;
&lt;li&gt;What if the producer updates its database but &lt;strong&gt;crashes before publishing&lt;/strong&gt; the event?&lt;/li&gt;
&lt;li&gt;What happens when a consumer &lt;strong&gt;repeatedly fails&lt;/strong&gt; to process a message?&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Design for at-least-once delivery
&lt;/h3&gt;

&lt;p&gt;Many practical messaging setups can deliver a message more than once. Consumers therefore need &lt;strong&gt;idempotent processing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;One simple approach is recording processed event IDs in a database table with a unique constraint.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;processed_events&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'evt_1024'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;CONFLICT&lt;/span&gt; &lt;span class="k"&gt;DO&lt;/span&gt; &lt;span class="k"&gt;NOTHING&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Continue with business changes only&lt;/span&gt;
&lt;span class="c1"&gt;-- if the event ID was newly inserted.&lt;/span&gt;

&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The insertion and corresponding business changes should participate in the same database transaction when they must succeed together. The exact SQL and implementation will depend on the database and processing framework.&lt;/p&gt;

&lt;p&gt;For reliable publishing, the &lt;strong&gt;transactional outbox pattern&lt;/strong&gt; is also worth considering. It stores the business change and an outgoing event in the same local transaction. A separate publisher then delivers the event to the broker.&lt;/p&gt;

&lt;p&gt;This avoids relying on two unrelated operations (updating a database and publishing a message) to succeed at exactly the same moment.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Messaging can make a distributed system more resilient. But it doesn't eliminate complexity. It exchanges immediate coordination for eventual coordination.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  6. More Services Mean More Ways to Fail
&lt;/h2&gt;

&lt;p&gt;Consider a request that passes through five services.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;API Gateway
     |
     v
Orders
     |
     v
Customers
     |
     v
Pricing
     |
     v
Inventory
     |
     v
Payments
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each service is healthy most of the time.&lt;/p&gt;

&lt;p&gt;Suppose each of the five downstream operations independently succeeds 99.9% of the time. The probability that all five succeed is approximately:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;0.999⁵ ≈ 0.995, or about 99.5%&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's a simplified mathematical example, not a measured production availability figure. Real systems have correlated failures, retries, fallback behavior, and different dependency characteristics.&lt;/p&gt;

&lt;p&gt;But it illustrates an important point: &lt;strong&gt;adding dependencies can reduce the reliability of the complete request path.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Timeouts and retries need deliberate design
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Every network call should have an appropriate &lt;strong&gt;timeout&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retries&lt;/strong&gt; should be bounded and should target failures that are genuinely recoverable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exponential backoff with jitter&lt;/strong&gt; can reduce the chance that many clients retry simultaneously.&lt;/li&gt;
&lt;li&gt;High-impact write operations need &lt;strong&gt;idempotency safeguards&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Otherwise, retry logic can make an outage worse. Imagine five services independently retrying a failing dependency three times. The resulting request amplification may place even more pressure on a system that's already struggling.&lt;/p&gt;

&lt;p&gt;Circuit breakers, concurrency limits, load shedding, and graceful degradation can help contain failures. But they must match the business operation.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If recommendations fail, perhaps the application can continue without recommendations.&lt;/li&gt;
&lt;li&gt;If payment authorization fails, silently continuing is probably unacceptable.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Resilience isn't about making every request succeed. It's about making failure predictable and limiting its impact.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  7. Observability Becomes Part of the Architecture
&lt;/h2&gt;

&lt;p&gt;In a monolith, investigating an error might involve examining logs from one application. In microservices, a single user request can travel through multiple services, queues, and databases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Without correlation, debugging becomes guesswork.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose a customer reports that an order took eight seconds to complete. Which service was responsible? Was it a slow database query? A network timeout? A retry? Or a queue backlog?&lt;/p&gt;

&lt;p&gt;Service-level CPU and memory metrics won't necessarily answer those questions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use distributed tracing
&lt;/h3&gt;

&lt;p&gt;A distributed trace can show the path of a request across services.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Trace: abc-123

POST /orders                 8200 ms
  |
  +-- Customer lookup         120 ms
  |
  +-- Inventory reservation   240 ms
  |
  +-- Payment authorization  7600 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the bottleneck is much easier to investigate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OpenTelemetry&lt;/strong&gt; provides standardized conventions for collecting and correlating traces, metrics, and logs across different technologies.&lt;/p&gt;

&lt;p&gt;In practice, I would want at least three things before operating a growing microservices platform:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Consistent request and trace identifiers&lt;/strong&gt; across service boundaries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Metrics&lt;/strong&gt; for latency, error rates, throughput, and dependency health.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Structured logs&lt;/strong&gt; with enough context to investigate failures without exposing sensitive data.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Telemetry also needs careful control of cardinality, retention, and collection costs. Collecting everything indefinitely isn't a sustainable observability strategy. The goal is to answer operational questions quickly.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;If your team can't trace a request across services, it doesn't fully understand how the system behaves in production.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  8. Kubernetes Doesn't Solve Architectural Complexity
&lt;/h2&gt;

&lt;p&gt;A common assumption is that adopting Kubernetes makes an application cloud-native or production-ready.&lt;/p&gt;

&lt;p&gt;Kubernetes can manage workloads, scheduling, deployments, service discovery, and many other infrastructure concerns. Those capabilities are valuable.&lt;/p&gt;

&lt;p&gt;But Kubernetes cannot decide whether your service boundaries are correct:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It can't repair a distributed transaction that leaves an order in an invalid state.&lt;/li&gt;
&lt;li&gt;It can't make a tightly coupled deployment independent.&lt;/li&gt;
&lt;li&gt;It doesn't automatically make business operations resilient.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The orchestration platform manages infrastructure behavior. The application still needs good engineering decisions.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For smaller teams, fully managed container services or serverless platforms may offer a more suitable operational model. For larger organizations with mature platform engineering capabilities, Kubernetes may be appropriate. The choice should depend on workload requirements, team expertise, operational ownership, and costs.&lt;/p&gt;

&lt;p&gt;Google Cloud's Well-Architected guidance continues to emphasize simplicity, managed services where practical, and avoiding unnecessary complexity when building new systems.&lt;/p&gt;

&lt;p&gt;The lesson is straightforward: &lt;strong&gt;don't introduce infrastructure your team isn't prepared to operate just to follow an architectural trend.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  9. AI Doesn't Remove Distributed Systems Problems
&lt;/h2&gt;

&lt;p&gt;In 2026, AI-powered development tools can help engineers generate service scaffolding, infrastructure definitions, API clients, and integration code. That can reduce implementation effort.&lt;/p&gt;

&lt;p&gt;But &lt;strong&gt;writing a service is only one part of owning it.&lt;/strong&gt; The harder questions remain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Who owns its data?&lt;/li&gt;
&lt;li&gt;What happens when a dependency is unavailable?&lt;/li&gt;
&lt;li&gt;How do events recover after failure?&lt;/li&gt;
&lt;li&gt;Can the service be deployed independently?&lt;/li&gt;
&lt;li&gt;How will engineers investigate production incidents?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AI-assisted development can also make it easier to create more services than a team genuinely needs. Generating five deployable applications may be straightforward. Maintaining five applications, their infrastructure, dependencies, security policies, and operational lifecycle is a different responsibility.&lt;/p&gt;

&lt;p&gt;The same caution applies to AI-driven agents that call multiple backend services. They still operate over networks with latency, permissions, rate limits, retries, and partial failures.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Automation changes how software is built and used. It doesn't remove the fundamental constraints of distributed computing.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  10. When Microservices Actually Make Sense
&lt;/h2&gt;

&lt;p&gt;After discussing the drawbacks, it's important to recognize where microservices provide real value. They can be an excellent choice when the organization has problems that independent services genuinely solve.&lt;/p&gt;

&lt;p&gt;Consider a large product with several engineering teams:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Payments&lt;/strong&gt; has strict reliability requirements.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Search&lt;/strong&gt; has different scaling patterns.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Media processing&lt;/strong&gt; involves expensive background workloads.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Customer-facing features&lt;/strong&gt; need to ship frequently without waiting for unrelated deployments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In that environment, separating certain capabilities can provide meaningful benefits. Teams gain ownership. Services can scale independently. Failures can sometimes be isolated. And deployments can become more flexible.&lt;/p&gt;

&lt;p&gt;But those benefits depend on how the system and organization are structured. Before extracting a service, I would ask:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does this component have a &lt;strong&gt;clear business responsibility&lt;/strong&gt;?&lt;/li&gt;
&lt;li&gt;Does it need an &lt;strong&gt;independent release lifecycle&lt;/strong&gt;?&lt;/li&gt;
&lt;li&gt;Can it &lt;strong&gt;own its data and contracts&lt;/strong&gt;?&lt;/li&gt;
&lt;li&gt;Will separating it &lt;strong&gt;reduce coordination&lt;/strong&gt; between teams?&lt;/li&gt;
&lt;li&gt;Do we have the &lt;strong&gt;operational capability&lt;/strong&gt; to support it?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the answers are unclear, keeping the component inside a well-structured monolith may be the more responsible decision.&lt;/p&gt;

&lt;p&gt;AWS's guidance on decomposing monoliths recommends evaluating existing reliability, performance, coupling, business requirements, and dependencies before deciding what to extract. Its documented approaches include decomposition by business capability, decomposition by subdomain, and the &lt;strong&gt;Strangler Fig pattern&lt;/strong&gt; for gradual migration.&lt;/p&gt;

&lt;p&gt;The important point is that &lt;strong&gt;migration should be driven by a specific outcome&lt;/strong&gt;, not by the assumption that smaller deployable units are automatically better.&lt;/p&gt;




&lt;h2&gt;
  
  
  11. The Engineering Decision That Matters Most
&lt;/h2&gt;

&lt;p&gt;Software architecture isn't about choosing the most advanced technology available. It's about accepting the right trade-offs for the problem you're solving.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;monolith&lt;/strong&gt; centralizes deployment and runtime dependencies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Microservices&lt;/strong&gt; distribute ownership, deployment, and failure boundaries.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Neither architecture guarantees maintainability. A poorly designed monolith can be painful. A poorly designed microservices system can be even more painful, because its problems now involve networks, infrastructure, and multiple data stores.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Role&lt;/th&gt;
&lt;th&gt;Priority&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Backend engineers&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Clean boundaries, reliable operations, and understandable dependencies&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Senior engineers &amp;amp; architects&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Recognizing when distribution creates genuine value&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Tech leads&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Aligning technical boundaries with the teams responsible for them&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Engineering managers&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Accounting for the ongoing cost of operating the architecture, not just building it&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A good architectural decision doesn't have to look impressive on a diagram. It has to help the organization deliver and maintain software reliably.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Microservices are powerful. But they're not a shortcut to good system design.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If your modules are tightly coupled, splitting them across services won't automatically make them independent.&lt;/li&gt;
&lt;li&gt;If your data ownership is unclear, separate databases won't resolve the underlying domain confusion.&lt;/li&gt;
&lt;li&gt;If your team struggles to understand production behavior today, adding more network boundaries can make that problem significantly harder.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Start with clear responsibilities.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Make dependencies visible.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Design failure handling before production forces you to.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Build observability into the system.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Introduce independent services when the benefits justify their operational cost.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There is nothing outdated about a well-designed monolith. There is nothing inherently modern about having 30 microservices.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The best architecture isn't the one with the most services. It's the one that lets your team change software safely without making the whole system harder to understand.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Over to You
&lt;/h2&gt;

&lt;p&gt;Has your team ever merged services back together, or found a distributed monolith hiding in your architecture? Tell me in the comments 👇&lt;/p&gt;




&lt;h2&gt;
  
  
  References and Further Reading
&lt;/h2&gt;

&lt;p&gt;The architectural recommendations in this article are informed by official engineering documentation and established distributed systems patterns, reviewed in November 2026.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Microsoft Azure Architecture Center: Microservices Architecture.&lt;/strong&gt; Service boundaries, domain-driven design, independent deployment, and coupling.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Microsoft: Data Considerations for Microservices.&lt;/strong&gt; Data ownership, eventual consistency, distributed transactions, and event-driven communication.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AWS Prescriptive Guidance: Decomposing Monoliths into Microservices.&lt;/strong&gt; Migration strategies, business capability decomposition, and gradual modernization.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OpenTelemetry: Semantic Conventions.&lt;/strong&gt; Standardized observability across distributed applications.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Google Cloud Well-Architected Framework.&lt;/strong&gt; Architectural simplicity, operational excellence, and appropriate use of managed services.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;For backend engineers, senior software engineers, software architects, technical leads, and platform engineering teams.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;If this was useful, follow me here for more stories from the backend trenches, and subscribe to &lt;a href="https://thebackendtechlead.substack.com/" rel="noopener noreferrer"&gt;The Backend Tech Lead&lt;/a&gt; on Substack for weekly blueprints.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>microservices</category>
      <category>backend</category>
      <category>distributedsystems</category>
    </item>
    <item>
      <title>Your API Works Today. Will It Survive Tomorrow?</title>
      <dc:creator>Anas Issath</dc:creator>
      <pubDate>Sun, 15 Nov 2026 11:45:09 +0000</pubDate>
      <link>https://dev.to/anas_issath/your-api-works-today-will-it-survive-tomorrow-27f1</link>
      <guid>https://dev.to/anas_issath/your-api-works-today-will-it-survive-tomorrow-27f1</guid>
      <description>&lt;p&gt;&lt;em&gt;The engineering decisions behind reliable APIs.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Most APIs look good when they first go live.&lt;/p&gt;

&lt;p&gt;The endpoints respond correctly. The documentation looks complete. The frontend gets the data it needs. The tests pass, and everyone moves on to the next feature.&lt;/p&gt;

&lt;p&gt;Then, six months later, someone asks for a small change.&lt;/p&gt;

&lt;p&gt;A field needs to be renamed. A response structure needs to change. A database query that worked with 500 records now struggles with 5 million.&lt;/p&gt;

&lt;p&gt;Suddenly, a simple API becomes difficult to maintain.&lt;/p&gt;

&lt;p&gt;The problem usually isn't the endpoint itself. It's the decisions made when nobody was thinking about what would happen after other systems started depending on it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;An API is easy to publish. The difficult part is keeping it reliable while everything around it changes.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's the part of API design I think deserves more attention.&lt;/p&gt;

&lt;p&gt;And in 2026, with APIs connecting web applications, mobile clients, cloud services, internal platforms, and increasingly autonomous software, those decisions matter more than ever.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. A Good API Should Be Predictable, Not Clever
&lt;/h2&gt;

&lt;p&gt;Engineers naturally enjoy building elegant abstractions.&lt;/p&gt;

&lt;p&gt;We introduce flexible request structures, generic resource handlers, configurable endpoints, and layers of abstraction to avoid repetition.&lt;/p&gt;

&lt;p&gt;Sometimes those decisions are justified.&lt;/p&gt;

&lt;p&gt;But API consumers rarely care how clever the implementation is. &lt;strong&gt;They care about getting their work done.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Compare these two endpoints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="no"&gt;POST&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;execute&lt;/span&gt;
&lt;span class="nc"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;

&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="s2"&gt;"action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"get_user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"parameters"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"user_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"123"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="no"&gt;GET&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both can return the same user. But the second communicates its purpose immediately.&lt;/p&gt;

&lt;p&gt;A developer doesn't need to understand a custom command structure or learn another convention. That familiarity reduces onboarding time, documentation requirements, and integration mistakes.&lt;/p&gt;

&lt;p&gt;It also makes debugging easier. When something fails, familiar HTTP semantics provide a starting point.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A good API shouldn't force developers to understand your architecture before they can use your product.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  What this means for engineering teams
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;For backend engineers&lt;/strong&gt;, it means choosing clear resources, consistent naming, and familiar HTTP behavior.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;For tech leads&lt;/strong&gt;, it means establishing API conventions before different teams invent their own.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;For platform engineers&lt;/strong&gt;, it means reducing the amount of specialized knowledge required to integrate with a service.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Flexibility is useful, but it should solve a real problem rather than create another one.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Backward Compatibility Is an Engineering Responsibility
&lt;/h2&gt;

&lt;p&gt;One of the most expensive API changes can look completely harmless in a code review.&lt;/p&gt;

&lt;p&gt;Suppose an endpoint originally returns:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"123"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Alex"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"email"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"alex@example.com"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Later, a developer decides the response would be better organized like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"123"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"profile"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Alex"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"email"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"alex@example.com"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From an internal design perspective, the new structure might look cleaner.&lt;/p&gt;

&lt;p&gt;But what happens to every application accessing &lt;code&gt;user.name&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It breaks.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The backend deploys successfully, yet downstream applications start failing.&lt;/p&gt;

&lt;p&gt;The impact becomes even more serious when the API is consumed by third-party developers, mobile applications with older installed versions, or customers running scheduled integrations. &lt;strong&gt;You don't control when those consumers update their software.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's why I consider backward compatibility part of the API contract, not simply a development preference.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prefer additive changes
&lt;/h3&gt;

&lt;p&gt;Instead of moving an existing field immediately, introduce the new structure while preserving the original one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"123"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Alex"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"email"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"alex@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"profile"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Alex"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"email"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"alex@example.com"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Existing clients continue working, while new clients can adopt the new representation.&lt;/p&gt;

&lt;p&gt;This approach still needs discipline. Consumers should tolerate unknown response fields, and API owners should document which fields are stable or deprecated. Even apparently harmless changes can affect clients with strict schema validation.&lt;/p&gt;

&lt;h3&gt;
  
  
  When versioning becomes necessary
&lt;/h3&gt;

&lt;p&gt;Sometimes a breaking change is unavoidable. A major redesign, an incorrect original contract, or a fundamental change in domain behavior may justify a new API version.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="no"&gt;GET&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;
&lt;span class="no"&gt;GET&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;v2&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But versioning comes with a cost. Now the engineering team must support multiple contracts, test compatibility, maintain documentation, and manage migration timelines. The implementation can share business logic, but old behavior still needs to be preserved.&lt;/p&gt;

&lt;p&gt;Stripe provides a useful real-world example. Its API versioning model distinguishes backward-compatible monthly releases from major releases that can introduce breaking changes.&lt;/p&gt;

&lt;p&gt;The lesson isn't that every company should copy Stripe's versioning strategy. &lt;strong&gt;It's that compatibility needs an explicit policy before breaking changes become necessary.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For senior engineers and architects, this is one of the most important decisions to make early.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Retries Are Normal. Duplicate Operations Shouldn't Be.
&lt;/h2&gt;

&lt;p&gt;Consider a payment API.&lt;/p&gt;

&lt;p&gt;A client sends a request to create a payment. The backend processes it successfully, but the network connection drops before the response reaches the client.&lt;/p&gt;

&lt;p&gt;The client sees a timeout.&lt;/p&gt;

&lt;p&gt;Did the payment succeed? Should the client try again?&lt;/p&gt;

&lt;p&gt;Without additional safeguards, retrying might create a &lt;strong&gt;second payment&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This isn't a rare networking mystery. It's a fundamental problem in distributed systems.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A timeout does not prove that an operation failed.&lt;/strong&gt; The client can lose the response even when the server has completed the work.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Design operations to be safely retried
&lt;/h3&gt;

&lt;p&gt;A common solution is an &lt;strong&gt;idempotency key&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="no"&gt;POST&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;payments&lt;/span&gt;
&lt;span class="nc"&gt;Idempotency&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;payment&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8f21&lt;/span&gt;
&lt;span class="nc"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;

&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="s2"&gt;"amount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"currency"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"USD"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the client retries the same operation, it sends the same key. The server uses the key to recognize an earlier attempt and prevent the payment from being created twice.&lt;/p&gt;

&lt;p&gt;The important detail is how this works internally. A naive implementation might:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check whether the key exists.&lt;/li&gt;
&lt;li&gt;Create the payment.&lt;/li&gt;
&lt;li&gt;Save the key.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That looks reasonable, but &lt;strong&gt;concurrent requests can both pass the first check before either saves the key.&lt;/strong&gt; The result could still be two payments.&lt;/p&gt;

&lt;p&gt;For operations where duplication is unacceptable, idempotency enforcement needs &lt;strong&gt;atomic coordination&lt;/strong&gt; with the business operation, often through database transactions, unique constraints, or a carefully designed processing state machine.&lt;/p&gt;

&lt;p&gt;The key should also be associated with the caller and original request parameters. Reusing it with different parameters should produce a clear error rather than silently return an unrelated result.&lt;/p&gt;

&lt;h3&gt;
  
  
  HTTP methods already provide some guarantees
&lt;/h3&gt;

&lt;p&gt;According to RFC 9110, &lt;code&gt;GET&lt;/code&gt;, &lt;code&gt;PUT&lt;/code&gt;, and &lt;code&gt;DELETE&lt;/code&gt; are defined as idempotent under their intended HTTP semantics. &lt;code&gt;POST&lt;/code&gt; does not have that guarantee by default.&lt;/p&gt;

&lt;p&gt;But selecting &lt;code&gt;PUT&lt;/code&gt; instead of &lt;code&gt;POST&lt;/code&gt; doesn't automatically fix an incorrectly implemented operation. &lt;strong&gt;The server must actually honor the expected behavior.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;For backend engineers&lt;/strong&gt;, idempotency is an implementation requirement.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;For architects&lt;/strong&gt;, it's a reliability decision that influences data modeling and transaction boundaries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;For engineering managers&lt;/strong&gt;, it's the difference between an occasional network error and a costly customer incident.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. Your API Will Eventually Be Used Differently Than You Expected
&lt;/h2&gt;

&lt;p&gt;An endpoint built for a user interface may receive only a handful of requests per minute during initial development.&lt;/p&gt;

&lt;p&gt;Then someone builds an integration.&lt;/p&gt;

&lt;p&gt;Instead of a human clicking a button, a script starts calling the endpoint hundreds of times per second.&lt;/p&gt;

&lt;p&gt;The operation hasn't changed. &lt;strong&gt;The usage pattern has.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine an endpoint that generates a report:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="no"&gt;POST&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;reports&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;generate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One report might involve several database queries, aggregations, and file operations. Now imagine a customer accidentally triggering that endpoint inside an aggressive polling loop.&lt;/p&gt;

&lt;p&gt;Even without malicious intent, the integration can overwhelm backend resources.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;This is why rate limiting isn't something to add only after an API becomes popular. It's a production safety mechanism.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Rate limits should reflect actual resource costs
&lt;/h3&gt;

&lt;p&gt;Not every endpoint deserves the same limit. Fetching a cached user profile is different from generating a report across millions of records.&lt;/p&gt;

&lt;p&gt;An API might allow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;High request volumes for inexpensive read operations.&lt;/li&gt;
&lt;li&gt;Lower limits for expensive reporting or search operations.&lt;/li&gt;
&lt;li&gt;Separate concurrency limits for long-running tasks.&lt;/li&gt;
&lt;li&gt;Different quotas for customers with different usage requirements.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Clients also need to know when to slow down. A rate-limited response might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="no"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;1.1&lt;/span&gt; &lt;span class="mi"&gt;429&lt;/span&gt; &lt;span class="nc"&gt;Too&lt;/span&gt; &lt;span class="nc"&gt;Many&lt;/span&gt; &lt;span class="nc"&gt;Requests&lt;/span&gt;
&lt;span class="nc"&gt;Retry&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;After&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;
&lt;span class="nc"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;problem&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of repeatedly retrying immediately, the client can respect the delay.&lt;/p&gt;

&lt;p&gt;GitHub's REST API documentation provides a practical reference for responding to primary and secondary rate limits, including backing off and avoiding repeated failed requests.&lt;/p&gt;

&lt;p&gt;In production, I would also consider per-customer usage metrics, request cost tracking, circuit breakers, and the ability to temporarily restrict abusive integrations.&lt;/p&gt;

&lt;p&gt;Because &lt;strong&gt;one customer's integration shouldn't be able to bring down a service for everyone else.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Pagination Is a Data Architecture Decision
&lt;/h2&gt;

&lt;p&gt;Pagination often begins as a frontend requirement. The UI needs 20 records per page, so the backend introduces an offset.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="k"&gt;OFFSET&lt;/span&gt; &lt;span class="mi"&gt;100000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works, but large offsets can become expensive. The database may need to traverse many entries before returning the requested records. As datasets grow, later pages can become increasingly slow.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cursor-based pagination&lt;/strong&gt; offers a more scalable approach for many sequential access patterns.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100000&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With an appropriate index, the database can locate the cursor position efficiently.&lt;/p&gt;

&lt;p&gt;The corresponding API might return:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100001&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nl"&gt;"total"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2500&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"next_cursor"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"eyJpZCI6MTAwMDAxfQ"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"has_more"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The cursor is treated as an &lt;strong&gt;opaque token&lt;/strong&gt; by the client. In a real implementation, it should encode or reference enough information to maintain consistent ordering and filtering. It should not require consumers to understand internal database IDs.&lt;/p&gt;

&lt;p&gt;Cursor pagination isn't automatically better for every application. Offset pagination remains useful for small datasets or interfaces requiring direct navigation to numbered pages.&lt;/p&gt;

&lt;p&gt;But if you're building an API where collections can grow into millions of records, deciding pagination strategy early can prevent a painful migration later.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Data access patterns should influence API contracts before scale makes the decision for you.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  6. Security Cannot End at Authentication
&lt;/h2&gt;

&lt;p&gt;There is a common design mistake that deserves more attention.&lt;/p&gt;

&lt;p&gt;A developer implements authentication, confirms the request contains a valid token, and considers the endpoint secure.&lt;/p&gt;

&lt;p&gt;But &lt;strong&gt;authentication only establishes &lt;em&gt;who&lt;/em&gt; is making the request. It doesn't establish &lt;em&gt;what&lt;/em&gt; that caller is allowed to access.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Consider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="no"&gt;GET&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;456&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;invoices&lt;/span&gt;
&lt;span class="nc"&gt;Authorization&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bearer&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The token might be valid. But does the authenticated user actually have permission to access account 456?&lt;/p&gt;

&lt;p&gt;If the backend only checks token validity, an attacker may be able to access another customer's invoices by changing the account identifier.&lt;/p&gt;

&lt;p&gt;This is a form of &lt;strong&gt;broken object-level authorization&lt;/strong&gt;. It's also the &lt;strong&gt;first risk listed in OWASP's API Security Top 10:2023.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Authorization belongs at the resource level
&lt;/h3&gt;

&lt;p&gt;Every operation accessing customer-owned data should enforce the relevant access rules. A secure implementation must verify the relationship between the authenticated identity, the requested resource, and the action.&lt;/p&gt;

&lt;p&gt;For example, the database query should be constrained by tenant or ownership information rather than trusting a resource ID supplied by the client.&lt;/p&gt;

&lt;p&gt;Authentication design also requires some balance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;API keys&lt;/strong&gt; are convenient for server-to-server integrations and small scripts, but they should be protected with scopes, expiration or rotation policies, secure storage, and revocation capabilities.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OAuth or short-lived credentials&lt;/strong&gt; may be more appropriate when an application acts on behalf of a user or operates across organizational trust boundaries.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The correct authentication mechanism depends on the threat model. Simplicity is valuable, but not at the cost of exposing customer data.&lt;/p&gt;

&lt;p&gt;For platform engineers and security-conscious backend teams, this should be part of the initial API design, not a final security review.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. Error Responses Are Part of Developer Experience
&lt;/h2&gt;

&lt;p&gt;A successful API response usually receives more design attention than a failed one. But developers spend a significant amount of integration time investigating errors.&lt;/p&gt;

&lt;p&gt;Consider this response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"error"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Something went wrong"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It tells the consumer almost nothing. Was the request invalid? Was the user unauthorized? Did the backend fail? Should the request be retried?&lt;/p&gt;

&lt;p&gt;A better error response provides structured information that applications can interpret. &lt;strong&gt;RFC 9457&lt;/strong&gt; defines a standardized format called &lt;em&gt;Problem Details for HTTP APIs&lt;/em&gt;. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="no"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;1.1&lt;/span&gt; &lt;span class="mi"&gt;422&lt;/span&gt; &lt;span class="nc"&gt;Unprocessable&lt;/span&gt; &lt;span class="nc"&gt;Content&lt;/span&gt;
&lt;span class="nc"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;problem&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;

&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="s2"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"https://api.example.com/problems/invalid-amount"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Invalid payment amount"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;422&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"detail"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Amount must be greater than zero."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"instance"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"/requests/req_72a"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The client can distinguish the &lt;strong&gt;error category&lt;/strong&gt; from the &lt;strong&gt;human-readable explanation&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Support teams can use the &lt;strong&gt;occurrence identifier&lt;/strong&gt; to investigate the request.&lt;/li&gt;
&lt;li&gt;API consumers can build &lt;strong&gt;predictable error handling&lt;/strong&gt; rather than parsing arbitrary error messages.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Not every API needs to adopt RFC 9457 immediately, particularly if it already has a stable error contract. But consistency matters.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A useful API error should help the consumer decide what to do next.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  8. AI Integrations Make Existing API Problems More Visible
&lt;/h2&gt;

&lt;p&gt;One development worth considering in 2026 is how APIs are increasingly consumed by software that &lt;em&gt;initiates actions&lt;/em&gt; rather than simply displaying data.&lt;/p&gt;

&lt;p&gt;An AI-powered application may query records, create support tickets, update resources, or trigger business workflows through tools and integrations.&lt;/p&gt;

&lt;p&gt;The underlying API principles haven't changed. But &lt;strong&gt;automation can expose weaknesses quickly&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A poorly documented endpoint might be misunderstood.&lt;/li&gt;
&lt;li&gt;An operation without idempotency controls might create duplicate records when retried.&lt;/li&gt;
&lt;li&gt;Overly broad credentials might give an integration more access than it needs.&lt;/li&gt;
&lt;li&gt;Missing rate limits can turn an automated workflow into an expensive incident.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The solution isn't to design an entirely separate class of API for AI. It's to apply established engineering principles consistently.&lt;/p&gt;

&lt;p&gt;An API intended for automated consumers should have clear contracts, precise descriptions of side effects, scoped permissions, structured errors, safe retry behavior, and observable execution. Destructive or high-impact operations may also require explicit approval at the application level.&lt;/p&gt;

&lt;p&gt;The important shift is recognizing that the caller might not always be a human writing a carefully controlled integration.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The more autonomous the consumer, the more important predictable API behavior becomes.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  9. Internal APIs Need Discipline Too
&lt;/h2&gt;

&lt;p&gt;Public APIs receive a lot of attention because breaking changes affect external customers.&lt;/p&gt;

&lt;p&gt;Internal APIs can feel safer. The consumers work at the same company. The teams communicate directly. If something changes, surely everyone can update their code.&lt;/p&gt;

&lt;p&gt;Sometimes that's true.&lt;/p&gt;

&lt;p&gt;But an internal endpoint can quietly become a dependency for several services, scheduled jobs, dashboards, and data pipelines. &lt;strong&gt;A breaking change may affect systems that the API owner doesn't even know exist.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Internal APIs can evolve faster because teams have more control over deployments. That doesn't mean they should ignore contracts.&lt;/p&gt;

&lt;p&gt;For growing engineering organizations, I would establish a few basic practices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Maintain an &lt;strong&gt;OpenAPI specification&lt;/strong&gt; for important HTTP interfaces.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;contract tests&lt;/strong&gt; to catch accidental response changes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Identify consuming services&lt;/strong&gt; before removing endpoints or fields.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Record deprecation decisions&lt;/strong&gt; and migration expectations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Track API errors, latency, and usage by consumer.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The goal isn't to turn every internal endpoint into a formally governed public product. &lt;strong&gt;It's to avoid making dependencies invisible.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A small amount of discipline early can save substantial coordination work later.&lt;/p&gt;




&lt;h2&gt;
  
  
  10. Good API Design Starts With Better Questions
&lt;/h2&gt;

&lt;p&gt;Before exposing a new endpoint, I think engineers should ask more than whether the handler works. They should ask whether the API will remain understandable, safe, and maintainable when the number of consumers grows.&lt;/p&gt;

&lt;p&gt;Some questions are especially useful during design reviews:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Role&lt;/th&gt;
&lt;th&gt;Questions to ask&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Backend engineers&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;What happens if the request is retried? What happens when the dataset becomes much larger? Are the authorization checks tied to the actual resource?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Senior engineers &amp;amp; architects&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Which parts of the contract are likely to evolve? Can the design accommodate additional fields without breaking existing consumers? Are internal implementation details leaking into the public interface?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Platform teams&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Can developers integrate without specialized knowledge? Are errors actionable? Do consumers have enough information to respect rate limits and recover from failures?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Tech leads &amp;amp; engineering managers&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Who owns compatibility decisions? How are breaking changes communicated? Can the team see which consumers still rely on older behavior?&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;These aren't questions that require a large architecture committee. They're questions worth asking &lt;strong&gt;before an API becomes difficult to change.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;It's tempting to judge API quality by how clean the endpoints look or how closely they follow a particular architectural style. Those things have value, but they don't tell the whole story.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An API with elegant resource names can still fail under retries.&lt;/li&gt;
&lt;li&gt;A well-documented interface can still expose another customer's data.&lt;/li&gt;
&lt;li&gt;A fast endpoint can still become unusable when the dataset grows.&lt;/li&gt;
&lt;li&gt;A carefully designed contract can still create years of maintenance problems if backward compatibility is treated casually.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The strongest API designs balance three things:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;They are easy to understand, difficult to misuse, and safe to evolve.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;For &lt;strong&gt;backend engineers&lt;/strong&gt;, that means thinking beyond request handlers.&lt;/li&gt;
&lt;li&gt;For &lt;strong&gt;senior engineers and architects&lt;/strong&gt;, it means treating API contracts as long-term technical commitments.&lt;/li&gt;
&lt;li&gt;For &lt;strong&gt;tech leads and platform teams&lt;/strong&gt;, it means making reliability and compatibility part of the engineering culture.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Good API design isn't about predicting every future requirement. It's about making decisions today that leave tomorrow's engineers, and tomorrow's consumers, with fewer problems to solve.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;An API isn't truly well designed when it first works. It's well designed when it keeps working as the system evolves.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Over to You
&lt;/h2&gt;

&lt;p&gt;Which of these ten bit your team hardest: a breaking change, a duplicate write, a runaway integration, or something else? Tell me in the comments 👇&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Written for backend engineers, senior software engineers, technical leads, software architects, and platform engineering teams.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;If this was useful, follow me here for more stories from the backend trenches, and subscribe to &lt;a href="https://thebackendtechlead.substack.com/" rel="noopener noreferrer"&gt;The Backend Tech Lead&lt;/a&gt; on Substack for weekly blueprints.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>api</category>
      <category>backend</category>
      <category>architecture</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Python Just Got a New Kind of String. It Could End a Whole Class of Security Bugs.</title>
      <dc:creator>Anas Issath</dc:creator>
      <pubDate>Wed, 11 Nov 2026 18:25:11 +0000</pubDate>
      <link>https://dev.to/anas_issath/python-just-got-a-new-kind-of-string-it-could-end-a-whole-class-of-security-bugs-247h</link>
      <guid>https://dev.to/anas_issath/python-just-got-a-new-kind-of-string-it-could-end-a-whole-class-of-security-bugs-247h</guid>
      <description>&lt;p&gt;&lt;em&gt;t-strings (PEP 750) are the biggest change to Python string formatting since f-strings, and they exist for one reason: f-strings cannot be made safe. Here is what they are and why they matter.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Every SQL injection, every cross-site scripting hole, every shell-injection bug has the same shape at its heart. Somebody took a piece of untrusted input and glued it directly into a string that was then treated as code, as a query, as markup. &lt;strong&gt;The input became structure instead of data.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"SELECT * FROM users WHERE name = '{name}'"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;name&lt;/code&gt; is &lt;code&gt;O'Brien&lt;/code&gt;, the apostrophe closes the quoted value early and the query breaks. If &lt;code&gt;name&lt;/code&gt; is crafted by an attacker, the same mechanism lets them add their own commands to your query.&lt;/p&gt;

&lt;p&gt;The f-string did exactly what it was designed to do: take the value and put it in the string. Immediately. Finished. And that immediacy is precisely the problem. By the time you have the string, the dangerous value is already welded into it, indistinguishable from the safe parts. &lt;strong&gt;There is no seam left to escape.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python 3.14 introduced a new kind of string literal, standardized in &lt;strong&gt;PEP 750&lt;/strong&gt;, built specifically to fix this. They are called &lt;strong&gt;t-strings&lt;/strong&gt; (template strings), and as the Python 3.15 world settles in late 2026, they are the most consequential change to string handling since f-strings arrived.&lt;/p&gt;

&lt;p&gt;Here is what they are, how they differ from f-strings in one crucial way, and why that difference can close an entire category of vulnerabilities.&lt;/p&gt;




&lt;h2&gt;
  
  
  The One Difference That Changes Everything
&lt;/h2&gt;

&lt;p&gt;A t-string looks almost exactly like an f-string. Same braces, same interpolation syntax. The only visible change is the prefix: &lt;code&gt;t&lt;/code&gt; instead of &lt;code&gt;f&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Pythonista"&lt;/span&gt;
&lt;span class="n"&gt;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="s2"&gt;"Hello, {name}!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But the result is completely different in the way that matters. &lt;strong&gt;An f-string evaluates to a finished &lt;code&gt;str&lt;/code&gt;. A t-string evaluates to a &lt;code&gt;Template&lt;/code&gt; object&lt;/strong&gt;, from the new &lt;code&gt;string.templatelib&lt;/code&gt; module. Not a string. A structured object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="s2"&gt;"Hello, {name}!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;lt;class 'string.templatelib.Template'&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here is the heart of it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;An f-string does the interpolation eagerly.&lt;/strong&gt; It grabs the value of &lt;code&gt;name&lt;/code&gt;, drops it into the text, and hands you a done string with no memory of which parts were literal and which came from a variable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A t-string does not interpolate into a final string at all.&lt;/strong&gt; Instead, it keeps the pieces separate: the static text parts on one side, the interpolated values on the other, handed to you as a structured object you can inspect and process before anything becomes a finished string.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The f-string gives you the result. The t-string gives you the recipe&lt;/strong&gt;, with the dangerous ingredients still in their own labeled containers, so a library can treat them differently from the trusted parts before combining them.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  What a &lt;code&gt;Template&lt;/code&gt; Actually Holds
&lt;/h2&gt;

&lt;p&gt;Because a &lt;code&gt;Template&lt;/code&gt; keeps the pieces apart, you can look at them. It exposes its structure directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Pythonista"&lt;/span&gt;
&lt;span class="n"&gt;site&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"example.dev"&lt;/span&gt;
&lt;span class="n"&gt;template&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="s2"&gt;"Hello, {name}! Welcome to {site}!"&lt;/span&gt;

&lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;strings&lt;/span&gt;
&lt;span class="c1"&gt;# ('Hello, ', '! Welcome to ', '!')      &amp;lt;- the static text parts&lt;/span&gt;

&lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt;
&lt;span class="c1"&gt;# ('Pythonista', 'example.dev')           &amp;lt;- the interpolated values&lt;/span&gt;

&lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;interpolations&lt;/span&gt;
&lt;span class="c1"&gt;# (Interpolation('Pythonista', 'name', ...), Interpolation('example.dev', 'site', ...))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three things to notice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.strings&lt;/code&gt;&lt;/strong&gt; holds the literal text fragments you typed, the parts that are always safe because you wrote them. There is always exactly &lt;strong&gt;one more static string than there are interpolations&lt;/strong&gt; (the text before, between, and after the holes).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.interpolations&lt;/code&gt;&lt;/strong&gt; holds the dynamic parts, each as an &lt;code&gt;Interpolation&lt;/code&gt; object. Each &lt;code&gt;Interpolation&lt;/code&gt; carries not just the &lt;strong&gt;&lt;code&gt;.value&lt;/code&gt;&lt;/strong&gt; (the actual runtime value, &lt;code&gt;'Pythonista'&lt;/code&gt;) but also the &lt;strong&gt;&lt;code&gt;.expression&lt;/code&gt;&lt;/strong&gt; (the source text you wrote in the braces, &lt;code&gt;'name'&lt;/code&gt;), plus the conversion and format spec.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The value and the literal text are kept in completely separate buckets.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can also just iterate the template, and it alternates between static strings and &lt;code&gt;Interpolation&lt;/code&gt; objects in order:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;templatelib&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Interpolation&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;part&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;part&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Interpolation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;  &lt;span class="c1"&gt;# a dynamic value, treat it as untrusted&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;  &lt;span class="c1"&gt;# a static string, trusted, you wrote it&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That loop is the key. It lets code walk through the template and do one thing to the parts you wrote and a different thing to the parts that came from outside. &lt;strong&gt;That is exactly the distinction every injection bug fails to make.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The Safety Payoff, Concretely
&lt;/h2&gt;

&lt;p&gt;Here is why this matters, shown with HTML and the classic cross-site scripting problem. To keep the example simple, imagine a user submits markup where plain text was expected.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;With an f-string&lt;/strong&gt;, untrusted input goes straight into the page, raw:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;user_input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;b&amp;gt;injected markup&amp;lt;/b&amp;gt;"&lt;/span&gt;
&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;p&amp;gt;{user_input}&amp;lt;/p&amp;gt;"&lt;/span&gt;
&lt;span class="c1"&gt;# "&amp;lt;p&amp;gt;&amp;lt;b&amp;gt;injected markup&amp;lt;/b&amp;gt;&amp;lt;/p&amp;gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The f-string is already a finished string, and the user's tags are now live markup, exactly as if you had written them yourself. It is too late to escape; you cannot tell anymore which angle brackets you wrote and which came from the user. In a real attack, those tags would carry a script instead of bold text.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;With a t-string&lt;/strong&gt;, you hand the &lt;code&gt;Template&lt;/code&gt; to a function that walks it and escapes &lt;strong&gt;only the interpolated values&lt;/strong&gt;, leaving your static markup untouched:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;html&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;escape&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;templatelib&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Template&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Interpolation&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;to_safe_html&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tpl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Template&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;part&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;tpl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;part&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Interpolation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;escape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;part&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;quote&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# untrusted: escape it&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;part&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                                   &lt;span class="c1"&gt;# trusted: your own text&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;bad&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;script&amp;gt;alert('x')&amp;lt;/script&amp;gt;"&lt;/span&gt;
&lt;span class="nf"&gt;to_safe_html&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;p&amp;gt;{bad}&amp;lt;/p&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# '&amp;lt;p&amp;gt;&amp;amp;lt;script&amp;amp;gt;alert(&amp;amp;#x27;x&amp;amp;#x27;)&amp;amp;lt;/script&amp;amp;gt;&amp;lt;/p&amp;gt;'   &amp;lt;- neutralized&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The interpolated value is treated as untrusted and escaped, while the static text is your own and passes through unchanged. The user's tags are now harmless visible text, while your &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tags pass through intact.&lt;/p&gt;

&lt;p&gt;One writer put the principle perfectly: the escaper &lt;em&gt;"sees user input as user input, not as a string fragment."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now generalize. The same pattern applies everywhere injection lives:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;SQL library&lt;/strong&gt; can take a t-string and turn every interpolation into a bound parameter instead of concatenated text, which removes the main route for injection.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;shell library&lt;/strong&gt; can quote every interpolated argument.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;logging library&lt;/strong&gt; can redact.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In each case the library gets to decide how to handle the dynamic parts &lt;strong&gt;because they arrived separately&lt;/strong&gt;, which is the one thing f-strings never allow.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Deliberate Inconvenience: A Template Is Not a String
&lt;/h2&gt;

&lt;p&gt;There is a design choice in t-strings that looks like a limitation and is actually the point. &lt;strong&gt;A &lt;code&gt;Template&lt;/code&gt; does not render itself into a string by default.&lt;/strong&gt; You cannot just use it where a string is expected.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Printing one shows its &lt;code&gt;repr&lt;/code&gt;, not interpolated text.&lt;/li&gt;
&lt;li&gt;You cannot add a plain string to a &lt;code&gt;Template&lt;/code&gt; with &lt;code&gt;+&lt;/code&gt; (that raises &lt;code&gt;TypeError&lt;/code&gt;; only &lt;code&gt;Template + Template&lt;/code&gt; is allowed).&lt;/li&gt;
&lt;li&gt;It has no &lt;code&gt;len()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It genuinely is not a string, and it refuses to pretend to be one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="s2"&gt;"Hello, {name}!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# Template(strings=('Hello, ', '!'), interpolations=(Interpolation('Pythonista', 'name', None, ''),))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is the structured object, not the text &lt;code&gt;"Hello, Pythonista!"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This feels annoying until you see why. If a &lt;code&gt;Template&lt;/code&gt; quietly turned itself into a string the moment you used it carelessly, it would reintroduce the exact danger it exists to prevent: an un-escaped interpolation sliding into output because someone forgot to process it.&lt;/p&gt;

&lt;p&gt;By requiring that every template be run through a processing function before it becomes text, the design makes "forgot to escape" much harder. You cannot accidentally use the raw version, because the raw version is not usable as a string.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The inconvenience is a guardrail.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So t-strings do not escape anything on their own. &lt;strong&gt;They are the mechanism that lets a library escape safely.&lt;/strong&gt; The t-string carries the pieces, apart and inspectable; the library does the right thing with them. What Python standardized is the &lt;em&gt;shape&lt;/em&gt;, so every library can speak it.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Rule I Take Away
&lt;/h2&gt;

&lt;p&gt;Compressed to one line:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;An f-string interpolates eagerly into a finished &lt;code&gt;str&lt;/code&gt;, fusing trusted text and untrusted values together with no seam left to escape&lt;/strong&gt;, which is why it is unsafe for SQL, HTML, and shell. &lt;strong&gt;A t-string produces a &lt;code&gt;Template&lt;/code&gt; that keeps the static parts (&lt;code&gt;.strings&lt;/code&gt;) and the interpolated values (&lt;code&gt;.interpolations&lt;/code&gt;, each with &lt;code&gt;.value&lt;/code&gt;) separate&lt;/strong&gt;, so a library can escape or parameterize the untrusted parts before building the final string.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The deeper lesson, the one that outlasts this specific feature, is that &lt;strong&gt;safety against injection comes from preserving the boundary between structure and data for as long as possible.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Every injection vulnerability is a place where that boundary was erased too early, where a value became part of the sentence before anyone could treat it as a value. F-strings erase the boundary instantly, by design, because they were built for convenience, not safety. T-strings keep the boundary intact and hand it to you, which is what finally lets Python libraries offer "safe by construction" interpolation the way other ecosystems have with tagged templates.&lt;/p&gt;

&lt;p&gt;And the practical reality as of today: t-strings are new (3.14, carried into the 3.15 world), and the libraries that consume them are still growing. You will not rewrite everything overnight. But the direction is set, and when a SQL, HTML, or shell library offers a t-string API, reaching for it instead of an f-string is one of the cheapest security upgrades you can make.&lt;/p&gt;




&lt;h2&gt;
  
  
  What to Actually Do With This
&lt;/h2&gt;

&lt;p&gt;Concrete guidance for where t-strings are right now:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Situation&lt;/th&gt;
&lt;th&gt;Use&lt;/th&gt;
&lt;th&gt;Why&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Logging a number, debug messages, formatting output you control&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;f-strings&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Perfect there and not going anywhere. The problem is only ever untrusted input crossing into code, query, or markup.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SQL queries, HTML output, shell commands with values you didn't write&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;t-strings + t-string-aware libraries&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;That is the boundary t-strings were built to protect.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;And two standing rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Never build SQL, HTML, or shell commands with f-strings and raw interpolation.&lt;/strong&gt; This was always true; t-strings now give you a first-class, standard alternative instead of only "remember to use parameterized queries / escape manually."&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Watch for t-string support in your libraries.&lt;/strong&gt; As database drivers, template engines, and shell helpers add t-string APIs, prefer them. The whole value of PEP 750 is a single standard shape that every library can accept, so the ecosystem can make the safe path the easy path.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Python did not just add a nicer way to format strings. It added a way to hand libraries the raw ingredients of a string with the dangerous ones still labeled, so they can be made safe before serving. That is a small syntax change pointed at one of the oldest and most damaging categories of bug in software.&lt;/p&gt;




&lt;h2&gt;
  
  
  The 30-Second Takeaway
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;t-strings&lt;/strong&gt; (PEP 750, Python 3.14+) look like f-strings but use a &lt;code&gt;t&lt;/code&gt; prefix and evaluate to a &lt;strong&gt;&lt;code&gt;Template&lt;/code&gt; object&lt;/strong&gt; (&lt;code&gt;string.templatelib&lt;/code&gt;), not a &lt;code&gt;str&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The key difference:&lt;/strong&gt; an f-string interpolates eagerly into a finished string, fusing your text and the values together with no way left to escape them. A t-string keeps the pieces separate for you to process first.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A &lt;code&gt;Template&lt;/code&gt; exposes its structure:&lt;/strong&gt; &lt;code&gt;.strings&lt;/code&gt; (the static text you wrote, always one more than the values), &lt;code&gt;.interpolations&lt;/code&gt; (each an &lt;code&gt;Interpolation&lt;/code&gt; with &lt;code&gt;.value&lt;/code&gt; the runtime value and &lt;code&gt;.expression&lt;/code&gt; the source text), and &lt;code&gt;.values&lt;/code&gt;. Iterating it alternates static strings and interpolations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The security payoff:&lt;/strong&gt; a library can walk the template and escape or parameterize &lt;strong&gt;only the interpolated (untrusted) values&lt;/strong&gt;, leaving your static markup or query intact, before building the string. This removes the main route for SQL injection, XSS, and shell injection.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A &lt;code&gt;Template&lt;/code&gt; is deliberately not a string:&lt;/strong&gt; no default render, adding a plain string with &lt;code&gt;+&lt;/code&gt; raises &lt;code&gt;TypeError&lt;/code&gt;, no &lt;code&gt;len()&lt;/code&gt;. This forces you to process it, so you cannot accidentally emit un-escaped output. The inconvenience is a guardrail.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;t-strings do not escape by themselves;&lt;/strong&gt; they are the standard mechanism that lets libraries escape safely. Use f-strings for safe internal formatting; use t-strings (and t-string-aware libraries) wherever untrusted input meets SQL, HTML, or shell.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Your Turn
&lt;/h2&gt;

&lt;p&gt;Find one place in your code where you build SQL, HTML, or a shell command with an f-string and an interpolated value you did not write. &lt;strong&gt;That is an injection risk today.&lt;/strong&gt; As your libraries add t-string support, that exact spot is where the &lt;code&gt;t&lt;/code&gt; prefix becomes the cheapest security fix you will ever ship.&lt;/p&gt;

&lt;p&gt;Have you tried t-strings yet? What's the first place you'd use them? Tell me in the comments 👇&lt;/p&gt;




&lt;p&gt;&lt;em&gt;If this was useful, follow me here for more stories from the backend trenches, and subscribe to &lt;a href="https://thebackendtechlead.substack.com/" rel="noopener noreferrer"&gt;The Backend Tech Lead&lt;/a&gt; on Substack for weekly blueprints.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>security</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Type 257 Characters of Anything. You're In.</title>
      <dc:creator>Anas Issath</dc:creator>
      <pubDate>Wed, 11 Nov 2026 06:55:58 +0000</pubDate>
      <link>https://dev.to/anas_issath/type-257-characters-of-anything-youre-in-13ic</link>
      <guid>https://dev.to/anas_issath/type-257-characters-of-anything-youre-in-13ic</guid>
      <description>&lt;p&gt;&lt;em&gt;How two sensible decisions, made four years apart in two different projects, turned into a password bypass on Alpine Linux. And why the same bug is probably hiding in your code.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;On November 4, 2026, a researcher named Özgün Kültekin reported a bug to the OpenSSH project.&lt;/p&gt;

&lt;p&gt;The next day, OpenSSH shipped an emergency release.&lt;/p&gt;

&lt;p&gt;That almost never happens. OpenSSH is one of the most carefully reviewed pieces of software on the internet. It runs on nearly every Linux server you have ever touched. Its maintainers do not rush.&lt;/p&gt;

&lt;p&gt;But this time, Damien Miller, one of OpenSSH's lead developers, told anyone running the affected setup to &lt;strong&gt;turn off password logins or disable SSH immediately.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The bug now has a name, &lt;strong&gt;*sshole&lt;/strong&gt;, and a number, &lt;strong&gt;CVE-2026-123158&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here is what it does, in one sentence:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;On an affected Alpine Linux server, an account that has no password at all will accept &lt;em&gt;any&lt;/em&gt; password between 257 and 1024 characters long.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Not a correct password. Any password.&lt;/p&gt;

&lt;p&gt;A string of 300 letter A's works.&lt;/p&gt;




&lt;h2&gt;
  
  
  Wait, How Is That Even Possible?
&lt;/h2&gt;

&lt;p&gt;The honest answer is the most interesting part of this story.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Nobody wrote a bad line of code.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;No one was careless. No one skipped a review. Every piece involved made a reasonable decision for a good reason.&lt;/p&gt;

&lt;p&gt;The bug only exists where those decisions meet.&lt;/p&gt;

&lt;p&gt;To understand it, you need three small pieces of history. They happened in 2012, 2012 again, and 2016.&lt;/p&gt;




&lt;h2&gt;
  
  
  Decision One: musl Makes Failure Safe (2012)
&lt;/h2&gt;

&lt;p&gt;Most Linux systems use a C library called &lt;strong&gt;glibc&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Alpine Linux uses a smaller one called &lt;strong&gt;musl&lt;/strong&gt;. That is part of why Alpine images are so tiny, and why so many Docker images and cloud VMs are built on it.&lt;/p&gt;

&lt;p&gt;Both libraries provide a function called &lt;code&gt;crypt()&lt;/code&gt;. It takes a password and a salt, and returns the hashed form that gets stored in &lt;code&gt;/etc/shadow&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In August 2012, musl changed what &lt;code&gt;crypt()&lt;/code&gt; does when it fails.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Instead of returning &lt;code&gt;NULL&lt;/code&gt;, it started returning the string &lt;code&gt;"*"&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That was a sensible safety choice. Plenty of old code calls &lt;code&gt;crypt()&lt;/code&gt; and never checks for errors. If it got &lt;code&gt;NULL&lt;/code&gt;, that code could crash. If it gets &lt;code&gt;"*"&lt;/code&gt;, it simply sees a hash that will never match a real password. Login fails. Nothing breaks.&lt;/p&gt;

&lt;p&gt;A few weeks later, musl added a second protection. Its SHA-256 and SHA-512 code started &lt;strong&gt;rejecting passwords longer than 256 bytes&lt;/strong&gt;. Without that limit, an attacker could send a giant password and make the server burn CPU hashing it.&lt;/p&gt;

&lt;p&gt;Also sensible.&lt;/p&gt;

&lt;p&gt;So from late 2012, on musl:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A password longer than 256 bytes makes &lt;code&gt;crypt()&lt;/code&gt; fail. And a failed &lt;code&gt;crypt()&lt;/code&gt; returns &lt;code&gt;"*"&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hold on to that.&lt;/p&gt;




&lt;h2&gt;
  
  
  Decision Two: Linux Marks "No Password" With the Same Character
&lt;/h2&gt;

&lt;p&gt;On Linux, &lt;code&gt;/etc/shadow&lt;/code&gt; stores one line per account.&lt;/p&gt;

&lt;p&gt;When an account has no password, the password field often holds a single character:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;alpine:*:20000:0:99999:7:::
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That &lt;code&gt;*&lt;/code&gt; means "nothing will ever match this." It is how you create a user who can only log in with an SSH key.&lt;/p&gt;

&lt;p&gt;On Alpine, &lt;code&gt;root&lt;/code&gt; itself ships as &lt;code&gt;root:*&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can probably see where this is going.&lt;/p&gt;




&lt;h2&gt;
  
  
  Decision Three: OpenSSH Fixes a Timing Leak (2016)
&lt;/h2&gt;

&lt;p&gt;In 2016, OpenSSH fixed a different security problem, &lt;strong&gt;CVE-2016-6210&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Attackers had noticed that &lt;code&gt;sshd&lt;/code&gt; answered faster for accounts with no real password hash. By timing the response, they could tell which usernames existed on a server.&lt;/p&gt;

&lt;p&gt;The fix was smart. When the target account has no real hash, &lt;code&gt;sshd&lt;/code&gt; now hashes the attempt anyway, &lt;strong&gt;borrowing the salt from another account on the system&lt;/strong&gt;. Every login takes about the same time. The timing leak disappears.&lt;/p&gt;

&lt;p&gt;Again, a reasonable decision.&lt;/p&gt;




&lt;h2&gt;
  
  
  Now Put Them Together
&lt;/h2&gt;

&lt;p&gt;Here is a simplified version of what &lt;code&gt;sshd&lt;/code&gt; does when it checks a password without PAM. This is an illustration of the logic, not the real OpenSSH source:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* Simplified illustration. Not the actual OpenSSH source. */&lt;/span&gt;

&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;stored&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;shadow_entry&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="cm"&gt;/* "*" for a key-only account      */&lt;/span&gt;
&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;attempt_hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;crypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="cm"&gt;/* salt borrowed from another user */&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;strcmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attempt_hash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stored&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;AUTH_SUCCESS&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;AUTH_FAILURE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now send a 300-character password to a key-only account on Alpine:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The account's stored field is &lt;code&gt;"*"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sshd&lt;/code&gt; borrows a real salt from another account, so &lt;code&gt;crypt()&lt;/code&gt; runs a SHA-512 hash.&lt;/li&gt;
&lt;li&gt;musl's SHA-512 code sees a password over 256 bytes and refuses it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;crypt()&lt;/code&gt; fails, and musl returns &lt;code&gt;"*"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sshd&lt;/code&gt; compares &lt;code&gt;"*"&lt;/code&gt; with &lt;code&gt;"*"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;They match. Login succeeds.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The error value and the "no password" marker were the same string.&lt;/strong&gt; Nobody planned that. It just happened, across two projects and four years.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The upper limit of 1024 comes from &lt;code&gt;sshd&lt;/code&gt; itself, which caps password length at that size.&lt;/p&gt;




&lt;h2&gt;
  
  
  Who Is Actually Exposed
&lt;/h2&gt;

&lt;p&gt;This is where the story gets calmer, and where a lot of headlines got it wrong.&lt;/p&gt;

&lt;p&gt;According to Alpine's advisory, a host is exploitable &lt;strong&gt;only when all four of these are true&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;sshd&lt;/code&gt; checks passwords without PAM.&lt;/strong&gt; Alpine's default &lt;code&gt;openssh-server&lt;/code&gt; package is built this way. The &lt;code&gt;openssh-server-pam&lt;/code&gt; package is affected only if you set &lt;code&gt;UsePAM no&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;PasswordAuthentication&lt;/code&gt; is &lt;code&gt;yes&lt;/code&gt;.&lt;/strong&gt; That is OpenSSH's default.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;An account that can log in has &lt;code&gt;*&lt;/code&gt; as its password field.&lt;/strong&gt; For example, a key-only user, or the &lt;code&gt;alpine&lt;/code&gt; user on official cloud images.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;At least one other account has a real &lt;code&gt;$5$&lt;/code&gt; or &lt;code&gt;$6$&lt;/code&gt; password hash.&lt;/strong&gt; &lt;code&gt;sshd&lt;/code&gt; needs that salt to borrow. One such account is enough to expose every passwordless account on the host.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, what does that mean for a typical backend team?&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Environment&lt;/th&gt;
&lt;th&gt;Status&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Ubuntu, Debian, RHEL, Fedora, macOS&lt;/td&gt;
&lt;td&gt;✅ Not affected (glibc, PAM, or both)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Alpine Docker containers that do not run &lt;code&gt;sshd&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;✅ Not affected, per Alpine. Your &lt;code&gt;FROM alpine&lt;/code&gt; API service is fine.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Alpine VMs and servers running &lt;code&gt;sshd&lt;/code&gt; with password logins on&lt;/td&gt;
&lt;td&gt;⚠️ &lt;strong&gt;Affected. Patch today.&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Official Alpine tiny-cloud images, 3.18.11 through 3.24.3&lt;/td&gt;
&lt;td&gt;⚠️ &lt;strong&gt;Special attention&lt;/strong&gt; (see below)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Embedded devices on musl with OpenSSH (Buildroot, Yocto, OpenWrt)&lt;/td&gt;
&lt;td&gt;⚠️ May be affected. OpenWrt's default SSH server, Dropbear, is not.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;About those tiny-cloud images:&lt;/strong&gt; they create an &lt;code&gt;alpine&lt;/code&gt; user with no password and passwordless &lt;code&gt;doas&lt;/code&gt;. They are safe on first boot, because no account has a password hash yet. But &lt;strong&gt;the moment anyone sets a password on any account, an attacker can log in as &lt;code&gt;alpine&lt;/code&gt; and become root.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What about root directly?&lt;/strong&gt; OpenSSH's default &lt;code&gt;PermitRootLogin prohibit-password&lt;/code&gt; blocks password logins to root, even though root has &lt;code&gt;*&lt;/code&gt; on Alpine. That helps, until the attacker lands on another account that can become root.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Part That Should Change How You Think About Patching
&lt;/h2&gt;

&lt;p&gt;Kültekin did not find this by reading code line by line.&lt;/p&gt;

&lt;p&gt;According to his write-up, he used &lt;strong&gt;AI agents to run about 17,600 unusual login attempts&lt;/strong&gt; against &lt;code&gt;sshd&lt;/code&gt; builds for glibc and musl, with and without PAM, using malformed hashes and oversized passwords.&lt;/p&gt;

&lt;p&gt;The bug had been sitting there since 2016.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A decade of human review did not catch it. A systematic, machine-driven sweep of weird inputs did.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is not a one-off. When OpenSSH released version 10.6 in October, the announcement said the team had been receiving a large number of security reports from AI models or made with AI help. It also said that bugs found by AI tools were later being found again, independently, by different researchers.&lt;/p&gt;

&lt;p&gt;The team's conclusion was blunt. If friendly researchers can find these bugs with AI, attackers who never report anything probably can too. So OpenSSH would release fixes faster instead of batching them.&lt;/p&gt;

&lt;p&gt;That is the real headline for backend engineers.&lt;/p&gt;

&lt;p&gt;The time between "a bug exists" and "someone can find it" is shrinking. Long-lived assumptions like &lt;em&gt;"nobody would ever try a 300-character password"&lt;/em&gt; are exactly what automated testing is good at breaking.&lt;/p&gt;




&lt;h2&gt;
  
  
  What to Do on Monday
&lt;/h2&gt;

&lt;p&gt;These steps come from Alpine's advisory and the published guidance. Run them as root on each host, or inside any container that runs &lt;code&gt;sshd&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Find out if a host meets the conditions
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Is sshd running on musl?&lt;/span&gt;
cat /etc/alpine-release 2&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null &lt;span class="o"&gt;||&lt;/span&gt; ldd &lt;span class="s2"&gt;"$(command -v sshd)"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; grep -i musl

&lt;span class="c"&gt;# Which openssh-server package is installed?&lt;/span&gt;
apk info -v 2&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null &lt;span class="o"&gt;|&lt;/span&gt; grep &lt;span class="s1"&gt;'^openssh-server'&lt;/span&gt;

&lt;span class="c"&gt;# What is sshd actually using right now?&lt;/span&gt;
sshd -T &lt;span class="o"&gt;|&lt;/span&gt; grep -Ei &lt;span class="s1"&gt;'^(usepam|passwordauthentication|permitrootlogin) '&lt;/span&gt;

&lt;span class="c"&gt;# Accounts with no password&lt;/span&gt;
awk -F: &lt;span class="s1"&gt;'$2=="*" {print $1}'&lt;/span&gt; /etc/shadow

&lt;span class="c"&gt;# Accounts with a real $5$ or $6$ hash&lt;/span&gt;
awk -F: &lt;span class="s1"&gt;'$2 ~ /^\$[56]\$/ {print $1}'&lt;/span&gt; /etc/shadow
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How to read the output:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the first command prints nothing, &lt;code&gt;sshd&lt;/code&gt; is not on musl. You are not affected.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;sshd -T&lt;/code&gt; shows &lt;code&gt;passwordauthentication yes&lt;/code&gt; and either &lt;code&gt;usepam no&lt;/code&gt; or no &lt;code&gt;usepam&lt;/code&gt; line at all, the host is exposed until it runs the fixed package.&lt;/li&gt;
&lt;li&gt;If both &lt;code&gt;awk&lt;/code&gt; commands print names, the accounts from the first list are the ones an attacker could log in as.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;One catch:&lt;/strong&gt; &lt;code&gt;sshd -T&lt;/code&gt; shows global settings. A &lt;code&gt;Match&lt;/code&gt; block in your config can turn password logins back on for some users or addresses. Read the config file too.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Step 2: Patch, and actually restart &lt;code&gt;sshd&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;apk upgrade -U -a
apk info -v &lt;span class="o"&gt;|&lt;/span&gt; grep &lt;span class="s1"&gt;'^openssh'&lt;/span&gt;
rc-service sshd restart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That last line matters more than it looks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Upgrading the package does not replace the &lt;code&gt;sshd&lt;/code&gt; process that is already running.&lt;/strong&gt; Until you restart it, the old, vulnerable code is still the one answering on port 22.&lt;/p&gt;

&lt;p&gt;Alpine backported the fix to every branch from 3.16 to edge. Stable branches keep their older OpenSSH version number and carry a patch, so do not go looking for "10.6" on 3.22. Compare your package version against Alpine's advisory table instead.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: If you cannot patch today, turn off password logins
&lt;/h3&gt;

&lt;p&gt;Before you do this, confirm that SSH key login works, and &lt;strong&gt;keep your current session open until a new login succeeds.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mkdir -p /etc/ssh/sshd_config.d
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'PasswordAuthentication no'&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /etc/ssh/sshd_config.d/00-no-passwords.conf
sshd -t &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; rc-service sshd restart
sshd -T &lt;span class="o"&gt;|&lt;/span&gt; grep -i passwordauthentication
&lt;span class="c"&gt;# expected output: passwordauthentication no&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alpine reads drop-in files from 3.19 onward. On older releases, change the setting in &lt;code&gt;/etc/ssh/sshd_config&lt;/code&gt; directly, including inside any &lt;code&gt;Match&lt;/code&gt; blocks.&lt;/p&gt;

&lt;p&gt;Honestly, if nobody on your team needs password logins over SSH, leave them off after you patch. &lt;strong&gt;Key-only SSH would have made this entire bug irrelevant.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Check whether anyone already got in
&lt;/h3&gt;

&lt;p&gt;A successful attack leaves a normal-looking log line. The giveaway is &lt;em&gt;who&lt;/em&gt; it names.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;grep &lt;span class="s1"&gt;'sshd.*Accepted password for'&lt;/span&gt; /var/log/messages*
awk -F: &lt;span class="s1"&gt;'$2=="*" {print $1}'&lt;/span&gt; /etc/shadow
grep doas /var/log/messages*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An &lt;code&gt;Accepted password for&lt;/code&gt; line naming &lt;code&gt;alpine&lt;/code&gt;, &lt;code&gt;root&lt;/code&gt;, or any key-only user &lt;strong&gt;cannot happen legitimately&lt;/strong&gt;. Those accounts have no password.&lt;/p&gt;

&lt;p&gt;Check your central log store too, because local logs can be wiped after a break-in.&lt;/p&gt;

&lt;p&gt;As of November 5, no attacks had been reported, and the CVE was not in CISA's Known Exploited Vulnerabilities catalog. But the bug has existed since 2016. &lt;strong&gt;"No reports" is not the same as "nobody used it."&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you find a suspicious login you cannot explain, Alpine's advice is to treat the machine as compromised. Rebuild it rather than cleaning it, and rotate any secrets it held.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5: Stop it from coming back in your images
&lt;/h3&gt;

&lt;p&gt;If any of your Dockerfiles install an SSH server, you want to know about it. A quick check you can add to CI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/usr/bin/env bash&lt;/span&gt;
&lt;span class="c"&gt;# Flag any Dockerfile that installs an OpenSSH server.&lt;/span&gt;
&lt;span class="c"&gt;# Most application containers should never run sshd.&lt;/span&gt;

&lt;span class="nv"&gt;matches&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;grep -rniE &lt;span class="s1"&gt;'apk add.*openssh(-server)?|apt(-get)? install.*openssh-server'&lt;/span&gt; \
  --include=&lt;span class="s1"&gt;'Dockerfile*'&lt;/span&gt; . &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; -n &lt;span class="s2"&gt;"$matches"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Found images that install an SSH server:"&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"$matches"&lt;/span&gt;
  &lt;span class="nb"&gt;exit&lt;/span&gt; 1
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If a container really needs remote access, there are usually better options than &lt;code&gt;sshd&lt;/code&gt; inside the image, such as &lt;code&gt;kubectl exec&lt;/code&gt; or your platform's own session tooling. If an image truly must run &lt;code&gt;sshd&lt;/code&gt;, rebuild it on the fixed package and set &lt;code&gt;PasswordAuthentication no&lt;/code&gt; in the image itself.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Bug That Is Probably in Your Own Code
&lt;/h2&gt;

&lt;p&gt;It is easy to read this as "an OpenSSH problem" and move on.&lt;/p&gt;

&lt;p&gt;Please don't.&lt;/p&gt;

&lt;p&gt;The pattern behind *sshole is one of the most common mistakes in backend code: &lt;strong&gt;an error value that looks like real data.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here is a version of it that could easily exist in an API service today. This is a made-up example, written to show the pattern:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;hashlib&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;hash_api_key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw_key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Guard against huge inputs.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw_key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;256&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;                     &lt;span class="c1"&gt;# failure looks like a normal value&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;hashlib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw_key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hexdigest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;is_valid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw_key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stored_hash&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# stored_hash is "" for revoked keys&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;hash_api_key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw_key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;stored_hash&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every piece looks reasonable on its own.&lt;/p&gt;

&lt;p&gt;Limiting input length is good. Marking revoked keys with an empty string feels tidy.&lt;/p&gt;

&lt;p&gt;But send a 300-character key against a revoked entry, and &lt;code&gt;"" == ""&lt;/code&gt; returns &lt;code&gt;True&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Same shape as *sshole.&lt;/strong&gt; Two sensible choices, one shared value, one open door.&lt;/p&gt;

&lt;p&gt;Here is a safer version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;hashlib&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;hmac&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;KeyTooLong&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;hash_api_key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw_key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw_key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;256&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;KeyTooLong&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"API key exceeds 256 characters"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# fail loudly&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;hashlib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw_key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hexdigest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;is_valid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw_key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stored_hash&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# A missing or malformed stored value can never be a match.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;stored_hash&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stored_hash&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;candidate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;hash_api_key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw_key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;KeyTooLong&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;hmac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compare_digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;candidate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stored_hash&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three habits made the difference:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Errors are exceptions, not values.&lt;/strong&gt; A failure should never be something that can be compared to stored data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check the stored side too.&lt;/strong&gt; A valid hash has a known shape. Anything else is an automatic "no." This is essentially what the OpenSSH fix does: &lt;code&gt;sshd&lt;/code&gt; now refuses a match when the stored field is shorter than four characters.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compare secrets with a constant-time function.&lt;/strong&gt; &lt;code&gt;hmac.compare_digest&lt;/code&gt; avoids leaking information through timing, which was the very problem OpenSSH was solving back in 2016.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you want a quick exercise for your team this week, search your codebase for every place that compares a computed hash, token, or signature against a stored value. For each one, ask a single question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"What does this function return when it fails, and could that ever equal what is stored?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Whose Bug Was It, Anyway?
&lt;/h2&gt;

&lt;p&gt;The projects do not fully agree, and that is worth noticing.&lt;/p&gt;

&lt;p&gt;OpenSSH's release notes describe the fix as a workaround for a root cause outside &lt;code&gt;sshd&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;musl's maintainer, Rich Felker, says it is not a musl bug but an erroneous use of &lt;code&gt;crypt()&lt;/code&gt; by the caller. He also said he expects musl to change the behaviour in its next release cycle anyway.&lt;/p&gt;

&lt;p&gt;Both positions are defensible. &lt;strong&gt;That is exactly the point.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When a bug lives in the seam between two components, each side can reasonably say the other one owns it. Meanwhile, the door stays open for whoever finds it first.&lt;/p&gt;

&lt;p&gt;As a backend engineer or tech lead, you will rarely be the person writing the C library or the SSH daemon. But you are almost always the person who decides &lt;strong&gt;how they fit together&lt;/strong&gt;. Which base image. Which defaults. Which accounts exist. Whether password logins are even on.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;That seam is your job.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  The Takeaway
&lt;/h2&gt;

&lt;p&gt;*sshole was not a failure of skill. It was a failure of assumptions.&lt;/p&gt;

&lt;p&gt;Nobody assumed anyone would send a 300-character password. Nobody assumed an error string and a shadow file entry would ever be compared. Nobody assumed a 2016 timing fix would meet a 2012 safety choice.&lt;/p&gt;

&lt;p&gt;AI-driven testing is now very good at exactly one thing: &lt;strong&gt;trying the inputs nobody assumed.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So the question for your team is not &lt;em&gt;"are we running Alpine?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It is this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Where in our system do two sensible decisions meet, and has anyone ever tested what happens at the edge?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Go find that seam before someone else does.&lt;/p&gt;

&lt;p&gt;What's the scariest "two sensible decisions" seam you've found in your own stack? Tell me in the comments 👇&lt;/p&gt;




&lt;p&gt;&lt;em&gt;If this was useful, follow me here for more stories from the backend trenches, and subscribe to &lt;a href="https://thebackendtechlead.substack.com/" rel="noopener noreferrer"&gt;The Backend Tech Lead&lt;/a&gt; on Substack for weekly blueprints.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>security</category>
      <category>linux</category>
      <category>devops</category>
      <category>backend</category>
    </item>
    <item>
      <title>Database Transactions Are Lying to You: What Isolation Levels Actually Guarantee</title>
      <dc:creator>Anas Issath</dc:creator>
      <pubDate>Mon, 09 Nov 2026 06:44:02 +0000</pubDate>
      <link>https://dev.to/anas_issath/database-transactions-are-lying-to-you-what-isolation-levels-actually-guarantee-1m00</link>
      <guid>https://dev.to/anas_issath/database-transactions-are-lying-to-you-what-isolation-levels-actually-guarantee-1m00</guid>
      <description>&lt;p&gt;&lt;em&gt;Two requests hit your database at the same time. One overwrites the other's work, and neither one errors. Here's why, and how to stop it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Here's a scenario I've seen dozens of times.&lt;/p&gt;

&lt;p&gt;A "buy now" button works perfectly in every manual test. One person clicks it, stock decrements by one, order gets created. Ships to production.&lt;/p&gt;

&lt;p&gt;A few weeks later, a flash sale runs, and support tickets start coming in: &lt;strong&gt;customers charged for items that were actually out of stock.&lt;/strong&gt; The inventory count went negative. Nobody's code has a bug you can point to. Every individual request did exactly what it was told.&lt;/p&gt;

&lt;p&gt;The problem wasn't one request. It was &lt;strong&gt;two requests, reading the same stock count at the same instant&lt;/strong&gt;, both deciding there was enough left, both writing "success."&lt;/p&gt;

&lt;p&gt;This is what transaction isolation is supposed to prevent, and most developers wrap code in &lt;code&gt;BEGIN&lt;/code&gt;/&lt;code&gt;COMMIT&lt;/code&gt; assuming it fully does, without knowing which guarantees their isolation level actually makes.&lt;/p&gt;

&lt;p&gt;This guide explains what a transaction really promises, the four standard isolation levels, and the concurrency bugs that slip through the cracks depending on which one you're using.&lt;/p&gt;

&lt;p&gt;Let's make your writes actually safe under load.&lt;/p&gt;




&lt;h2&gt;
  
  
  What a Transaction Actually Guarantees
&lt;/h2&gt;

&lt;p&gt;A transaction groups multiple operations so they succeed or fail as one unit. That's the "atomic" in ACID.&lt;/p&gt;

&lt;p&gt;What it does &lt;strong&gt;not&lt;/strong&gt; automatically guarantee is that your transaction is isolated from every other transaction running at the same time. That part is configurable, and the default varies by database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;inventory&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;product_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This looks safe. It isn't, by itself. It only guarantees that &lt;em&gt;within this transaction&lt;/em&gt;, both statements happen together. It says nothing about what a second, concurrent transaction running the exact same code is allowed to see or overwrite while this one is still in flight.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;That's what isolation levels control.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The Core Problem: Two Transactions, One Row
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Time    Transaction A                Transaction B
----    -------------------------    -------------------------
t1      BEGIN
t2      SELECT stock FROM inventory
        WHERE id=42;  → stock = 1
t3                                   BEGIN
t4                                   SELECT stock FROM inventory
                                      WHERE id=42;  → stock = 1
t5      UPDATE stock = 0
t6      COMMIT
t7                                   UPDATE stock = 0
t8                                   COMMIT
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both transactions read &lt;code&gt;stock = 1&lt;/code&gt;. Both decided it was safe to sell. Both committed. The final stock is 0, but &lt;strong&gt;two orders were created for one unit of inventory.&lt;/strong&gt; Nothing errored.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is a &lt;strong&gt;lost update&lt;/strong&gt;, and it's the textbook example of what isolation levels exist to prevent.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  The Four Standard Isolation Levels
&lt;/h2&gt;

&lt;p&gt;The SQL standard defines four levels, each permitting fewer anomalies than the last, at the cost of more locking, more blocking, or more retries.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Read Uncommitted
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;TRANSACTION&lt;/span&gt; &lt;span class="k"&gt;ISOLATION&lt;/span&gt; &lt;span class="k"&gt;LEVEL&lt;/span&gt; &lt;span class="k"&gt;READ&lt;/span&gt; &lt;span class="k"&gt;UNCOMMITTED&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The weakest level. A transaction can read data another transaction has written but &lt;strong&gt;not yet committed&lt;/strong&gt;, including data that later gets rolled back. This is called a &lt;strong&gt;dirty read&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Transaction A: UPDATE balance SET amount = 1000 WHERE id = 1;  (not committed yet)
Transaction B: SELECT amount FROM balance WHERE id = 1;  → sees 1000
Transaction A: ROLLBACK;
-- Transaction B made a decision based on data that never existed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Almost nobody should use this in production. PostgreSQL doesn't even implement it as distinct from Read Committed. It's there for standards compliance only.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Read Committed
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;TRANSACTION&lt;/span&gt; &lt;span class="k"&gt;ISOLATION&lt;/span&gt; &lt;span class="k"&gt;LEVEL&lt;/span&gt; &lt;span class="k"&gt;READ&lt;/span&gt; &lt;span class="k"&gt;COMMITTED&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The default in PostgreSQL, Oracle, and SQL Server.&lt;/strong&gt; A transaction only ever sees data that has been committed: no dirty reads.&lt;/p&gt;

&lt;p&gt;But it doesn't protect you from a &lt;strong&gt;non-repeatable read&lt;/strong&gt;: if you query the same row twice in one transaction, another transaction's commit in between can change what you see the second time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Transaction A: SELECT stock FROM inventory WHERE id=42;  → 1
Transaction B: UPDATE inventory SET stock=0 WHERE id=42; COMMIT;
Transaction A: SELECT stock FROM inventory WHERE id=42;  → 0
-- Same query, same transaction, different answer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Repeatable Read
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;TRANSACTION&lt;/span&gt; &lt;span class="k"&gt;ISOLATION&lt;/span&gt; &lt;span class="k"&gt;LEVEL&lt;/span&gt; &lt;span class="k"&gt;REPEATABLE&lt;/span&gt; &lt;span class="k"&gt;READ&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The default in MySQL/InnoDB.&lt;/strong&gt; Once a transaction reads a row, it will see the same data for that row for the rest of the transaction, even if another transaction commits a change.&lt;/p&gt;

&lt;p&gt;This fixes non-repeatable reads, but the standard still permits a &lt;strong&gt;phantom read&lt;/strong&gt;: a query returning a &lt;em&gt;set&lt;/em&gt; of rows can see new rows appear on a second run, even though individual rows you already read stay stable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Transaction A: SELECT * FROM orders WHERE status='pending';  → 5 rows
Transaction B: INSERT INTO orders (status) VALUES ('pending'); COMMIT;
Transaction A: SELECT * FROM orders WHERE status='pending';  → 6 rows
-- A new row "phantom" appeared in the result set
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(In practice, PostgreSQL's implementation of Repeatable Read is stricter than the SQL standard requires and also blocks phantom reads. But don't assume that behavior is portable to every database.)&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Serializable
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;TRANSACTION&lt;/span&gt; &lt;span class="k"&gt;ISOLATION&lt;/span&gt; &lt;span class="k"&gt;LEVEL&lt;/span&gt; &lt;span class="k"&gt;SERIALIZABLE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The strongest level. Guarantees that the result of running transactions concurrently is &lt;strong&gt;identical to running them one after another&lt;/strong&gt; in some order. No dirty reads, no non-repeatable reads, no phantoms, no lost updates.&lt;/p&gt;

&lt;p&gt;The cost: the database has to detect conflicts and &lt;strong&gt;abort&lt;/strong&gt; one of the transactions when it can't guarantee that ordering.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- ... transaction logic ...&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- Sometimes returns:&lt;/span&gt;
&lt;span class="c1"&gt;-- ERROR: could not serialize access due to concurrent update&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your application must be ready to catch this and retry.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Serializable isn't "safe and free." It's "safe, and sometimes it tells you to try again."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Quick Reference: What Each Level Prevents
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The takeaway:&lt;/strong&gt; your database's default isolation level is Read Committed in most systems, and &lt;strong&gt;Read Committed does not prevent the lost-update bug&lt;/strong&gt; from the flash sale example. If you never explicitly thought about isolation, you're running at the level that permits it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Isolation Problems
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Problem 1: Assuming &lt;code&gt;BEGIN&lt;/code&gt;/&lt;code&gt;COMMIT&lt;/code&gt; alone prevents race conditions
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Looks safe. Isn't, under Read Committed.
&lt;/span&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;atomic&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two concurrent requests can both fetch &lt;code&gt;stock = 1&lt;/code&gt;, both pass the &lt;code&gt;if&lt;/code&gt;, both decrement to 0, and both succeed. Wrapping code in a transaction guarantees atomicity of the &lt;em&gt;write&lt;/em&gt;. It does not, by itself, guarantee the read-then-write is protected from another transaction doing the same thing in between.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; use a row-level lock (&lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt;) to force serialization at the row.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;atomic&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select_for_update&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- What select_for_update does under the hood:&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt; &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- Second transaction's identical query now BLOCKS until this one commits&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Problem 2: Using a lock when an atomic update would do
&lt;/h3&gt;

&lt;p&gt;For simple counters, you often don't need a lock at all. You need to stop reading the value into application code in the first place.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Race-prone: read, modify in Python, write
&lt;/span&gt;&lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Race-free: the decrement happens entirely inside the database&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second version pushes the read-modify-write into a &lt;strong&gt;single atomic statement&lt;/strong&gt; the database executes as one operation. There's no window where two transactions can both see the same stale value. This is usually cheaper and simpler than a lock for pure counter updates.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem 3: Deadlocks from inconsistent lock ordering
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Transaction A: locks row 1, then tries to lock row 2
Transaction B: locks row 2, then tries to lock row 1
-- Both wait for each other forever
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Transaction A&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Transaction B, running concurrently&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If A locks row 1 first and B locks row 2 first, each is now waiting on a row the other holds. The database detects this and kills one transaction with a deadlock error, but only after both have been stalled.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; always acquire locks on multiple rows in a &lt;strong&gt;consistent order&lt;/strong&gt;, e.g. always lock the lower ID first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Both transactions now always touch id=1 before id=2&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;amt&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;LEAST&lt;/span&gt;&lt;span class="p"&gt;(:&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;amt&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;GREATEST&lt;/span&gt;&lt;span class="p"&gt;(:&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Problem 4: Long transactions holding locks under load
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;atomic&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select_for_update&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;send_confirmation_email&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# slow network call, 2–3 seconds
&lt;/span&gt;    &lt;span class="nf"&gt;charge_payment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;            &lt;span class="c1"&gt;# slow network call, 2–5 seconds
&lt;/span&gt;    &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;confirmed&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
    &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The row lock from &lt;code&gt;select_for_update&lt;/code&gt; is held for the &lt;strong&gt;entire transaction&lt;/strong&gt;, including both slow external calls. Every other request trying to touch that order queues up behind it for as long as those network calls take.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; keep locks held only as long as the database work itself takes. Do slow, unrelated I/O outside the transaction.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;send_confirmation_email_async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# queued, not inline
&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;charge_payment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;            &lt;span class="c1"&gt;# done before opening the lock
&lt;/span&gt;
&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;atomic&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select_for_update&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;confirmed&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;success&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;failed&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
    &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Problem 5: Picking Serializable and not handling retries
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Set isolation to Serializable, then treat it like it can't fail
&lt;/span&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;atomic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isolation_level&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;SERIALIZABLE&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;transfer_funds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;from_account&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to_account&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Serializable transactions can and will be aborted by the database whenever it can't guarantee a consistent ordering. &lt;strong&gt;This is expected, normal behavior, not an edge case.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; wrap serializable transactions in a retry loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;atomic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isolation_level&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;SERIALIZABLE&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="nf"&gt;transfer_funds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;from_account&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to_account&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;SerializationError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt;
        &lt;span class="k"&gt;continue&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Choosing a Level for Your Use Case
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Approach&lt;/th&gt;
&lt;th&gt;Use it for&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;Read Committed&lt;/strong&gt; (the default, mostly)&lt;/td&gt;
&lt;td&gt;The majority of reads, and writes that don't depend on a prior read of the same row within the transaction. Most CRUD operations.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;Row locks&lt;/strong&gt; (&lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt;) at Read Committed&lt;/td&gt;
&lt;td&gt;The standard fix for read-then-write races on a specific row: inventory, balances, seat reservations. Cheaper and more predictable than jumping to Serializable.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;Atomic SQL updates&lt;/strong&gt; (&lt;code&gt;SET x = x - 1 WHERE x &amp;gt; 0&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;The simplest, fastest option whenever the operation can be expressed as pure SQL without application logic between the read and the write.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Serializable&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Logic too complex for a single atomic statement or a simple row lock: multi-row invariants, complex financial transfers, anything where "occasionally retried" is an acceptable tradeoff for full correctness.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Checking What's Actually Happening
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- PostgreSQL: see current isolation level&lt;/span&gt;
&lt;span class="k"&gt;SHOW&lt;/span&gt; &lt;span class="n"&gt;TRANSACTION&lt;/span&gt; &lt;span class="k"&gt;ISOLATION&lt;/span&gt; &lt;span class="k"&gt;LEVEL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- PostgreSQL: see locks currently held and what's blocked on them&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;blocked_locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;blocked_pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;blocking_locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;blocking_pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;blocked_activity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;blocked_query&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_catalog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pg_locks&lt;/span&gt; &lt;span class="n"&gt;blocked_locks&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;pg_catalog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pg_locks&lt;/span&gt; &lt;span class="n"&gt;blocking_locks&lt;/span&gt;
  &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;blocking_locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;locktype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;blocked_locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;locktype&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;blocking_locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;database&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;blocked_locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;database&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;blocking_locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;relation&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;blocked_locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;relation&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;blocking_locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;blocked_locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;pg_catalog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pg_stat_activity&lt;/span&gt; &lt;span class="n"&gt;blocked_activity&lt;/span&gt;
  &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;blocked_activity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;blocked_locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="n"&gt;blocked_locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;granted&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're seeing mysterious slow requests under concurrent load with no slow query in sight, &lt;strong&gt;checking for blocked locks is often more revealing than checking query plans.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The Bottom Line
&lt;/h2&gt;

&lt;p&gt;Wrapping code in a transaction feels like it should make concurrency problems disappear. It doesn't. It only guarantees the specific things ACID promises, and by default, most databases run at a level that still allows two requests to read the same stale value and both act on it.&lt;/p&gt;

&lt;p&gt;The flash-sale overselling bug isn't a bug in anyone's code; it's the predictable result of Read Committed doing exactly what it's documented to do.&lt;/p&gt;

&lt;p&gt;Remember:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;BEGIN&lt;/code&gt;/&lt;code&gt;COMMIT&lt;/code&gt; guarantees atomicity, not isolation from concurrent reads.&lt;/strong&gt; Those are separate guarantees.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read Committed (most databases' default) still allows lost updates.&lt;/strong&gt; Know your default, don't assume.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt;&lt;/strong&gt; is usually the right fix for read-then-write races on a single row.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Atomic SQL updates&lt;/strong&gt; (&lt;code&gt;SET x = x - 1 WHERE x &amp;gt; 0&lt;/code&gt;) beat locks when the logic is simple enough to express in one statement.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Serializable transactions can fail on purpose.&lt;/strong&gt; Your code needs a retry loop, not just a &lt;code&gt;try/except&lt;/code&gt; that gives up.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The bug that only shows up during your highest-traffic moment (the flash sale, the ticket drop, the Black Friday spike) is usually a concurrency bug, not a capacity bug.&lt;/strong&gt; Understanding isolation levels is how you catch it before that moment finds it for you.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Your Turn
&lt;/h2&gt;

&lt;p&gt;Have you ever shipped a race condition that only showed up under real traffic? What gave it away? Tell me in the comments 👇&lt;/p&gt;




&lt;p&gt;&lt;em&gt;If this was useful, follow me here for more stories from the backend trenches, and subscribe to &lt;a href="https://thebackendtechlead.substack.com/" rel="noopener noreferrer"&gt;The Backend Tech Lead&lt;/a&gt; on Substack for weekly blueprints.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>postgres</category>
      <category>django</category>
      <category>backend</category>
    </item>
    <item>
      <title>asyncpg vs SQLAlchemy ORM - The 9x Slowdown Nobody Warns You About</title>
      <dc:creator>Anas Issath</dc:creator>
      <pubDate>Sun, 08 Nov 2026 14:19:10 +0000</pubDate>
      <link>https://dev.to/anas_issath/asyncpg-vs-sqlalchemy-orm-the-9x-slowdown-nobody-warns-you-about-2j99</link>
      <guid>https://dev.to/anas_issath/asyncpg-vs-sqlalchemy-orm-the-9x-slowdown-nobody-warns-you-about-2j99</guid>
      <description>&lt;p&gt;&lt;em&gt;Async doesn't mean fast. Your ORM might be quietly throwing away everything asyncpg gives you.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We migrated a FastAPI service from sync SQLAlchemy to async SQLAlchemy, expecting a performance win. Async is supposed to be faster, right? Non-blocking I/O, more concurrent requests per worker, better resource utilization.&lt;/p&gt;

&lt;p&gt;Our load test showed the opposite. &lt;strong&gt;The async version was slower per-request than the sync version had been.&lt;/strong&gt; Not by a little: a specific endpoint that fetched 500 rows and mapped them to ORM objects went from &lt;strong&gt;340ms to 890ms&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I assumed we'd misconfigured something. Wrong connection pool size, maybe, or a missing index. I profiled the endpoint with &lt;code&gt;cProfile&lt;/code&gt; and found the answer: &lt;strong&gt;the async ORM layer itself was the bottleneck.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Object instantiation, identity map tracking, and attribute loading (the same overhead that exists in sync SQLAlchemy) was still there in async SQLAlchemy. We'd added the complexity of async without eliminating the cost of the ORM.&lt;/p&gt;

&lt;p&gt;This isn't a reason to avoid async or avoid SQLAlchemy. It's a reason to understand exactly what you're paying for at each layer, so you can make an informed choice instead of assuming "async" automatically means "fast."&lt;/p&gt;




&lt;h2&gt;
  
  
  The Three Layers and What Each One Costs
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fiqedoqz9y74tf5o22pbz.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fiqedoqz9y74tf5o22pbz.webp" alt=" " width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you fetch data from PostgreSQL in a modern async Python app, there are three layers between your code and the database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Your code
  → SQLAlchemy ORM (optional)     ← object mapping, identity map, relationships
  → SQLAlchemy Core / Driver       ← query compilation, connection management
  → asyncpg or psycopg              ← the actual wire protocol to PostgreSQL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each layer adds capability and each layer adds overhead. Understanding &lt;em&gt;where&lt;/em&gt; the overhead comes from tells you when it's worth paying and when it isn't.&lt;/p&gt;

&lt;h3&gt;
  
  
  Layer 1: The Driver (asyncpg vs psycopg 3)
&lt;/h3&gt;

&lt;p&gt;At the bottom, you have the actual PostgreSQL wire protocol implementation. Two real choices in 2026:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;asyncpg&lt;/strong&gt;: built from scratch for asyncio. Speaks PostgreSQL's binary protocol directly. No general-purpose abstraction layer. This is the fastest way to talk to PostgreSQL from Python, full stop.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;psycopg 3 (async mode)&lt;/strong&gt;: the modern rewrite of the classic psycopg2, with native async support. Slightly slower than asyncpg in raw benchmarks, but shares an API with sync psycopg, which matters if you're maintaining both sync and async code paths.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Raw asyncpg - as fast as Python gets talking to PostgreSQL
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;asyncpg&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncpg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT id, total, status FROM orders WHERE user_id = $1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# psycopg 3 async - nearly as fast, more familiar API
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;psycopg&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;psycopg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AsyncConnection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT id, total, status FROM orders WHERE user_id = %s&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchall&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both are dramatically faster than going through an ORM for simple queries. The difference between them is small enough that it rarely decides an architecture. asyncpg wins benchmarks by roughly 2x for raw throughput, but in a real application with network latency and business logic around the query, that gap shrinks to something you won't notice.&lt;/p&gt;

&lt;h3&gt;
  
  
  Layer 2: SQLAlchemy Core (No ORM)
&lt;/h3&gt;

&lt;p&gt;SQLAlchemy Core gives you a query builder and connection management &lt;strong&gt;without object mapping&lt;/strong&gt;. You write something close to SQL, but Python-native and composable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sqlalchemy&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sqlalchemy.ext.asyncio&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;create_async_engine&lt;/span&gt;

&lt;span class="n"&gt;engine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_async_engine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;postgresql+asyncpg://user:pass@localhost/mydb&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;orders&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;total&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchall&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This adds a small amount of overhead over raw asyncpg (query compilation, parameter binding, connection pool management), but it's a fraction of what the full ORM costs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Layer 3: The ORM (SQLAlchemy's Declarative Models)
&lt;/h3&gt;

&lt;p&gt;The ORM adds object identity tracking, relationship loading, change tracking (dirty attribute detection), and Python object instantiation &lt;strong&gt;for every row&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sqlalchemy&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;select&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sqlalchemy.orm&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;declarative_base&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Mapped&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mapped_column&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sqlalchemy.ext.asyncio&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;AsyncSession&lt;/span&gt;

&lt;span class="n"&gt;Base&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;declarative_base&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Base&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;__tablename__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;orders&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Mapped&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mapped_column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;primary_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Mapped&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Mapped&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Mapped&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;AsyncSession&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scalars&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same query. Same data. But now every row becomes a full Python object with SQLAlchemy's instrumentation attached:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tracking whether each attribute has changed&lt;/li&gt;
&lt;li&gt;Registering the object in the session's identity map, so &lt;code&gt;session.get(Order, id)&lt;/code&gt; returns the same instance&lt;/li&gt;
&lt;li&gt;Setting up lazy-loading machinery for any relationships&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The Benchmark: What Each Layer Actually Costs
&lt;/h2&gt;

&lt;p&gt;I ran this on a table with 100,000 rows, fetching 500 rows matching a filter, on a local PostgreSQL instance to eliminate network variance:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The jump from Core to ORM is the one that matters.&lt;/strong&gt; Going from raw asyncpg to SQLAlchemy Core costs you a small, reasonable overhead for a massive gain in composability and safety. Going from Core to full ORM costs you almost &lt;strong&gt;3x more&lt;/strong&gt; on top of that, for object mapping you may not even need.&lt;/p&gt;

&lt;p&gt;This matches what other benchmarks in the Python community have found: SQLAlchemy's ORM layer with async drivers can be an order of magnitude slower than raw driver access for simple read-heavy queries, primarily because of &lt;strong&gt;Python-level object construction overhead&lt;/strong&gt;, not the database round trip itself.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why the ORM Overhead Is So High With Async
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1qx758vvkwbv18myomye.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1qx758vvkwbv18myomye.webp" alt=" " width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's the part that surprised me most. You'd expect async to at least &lt;em&gt;match&lt;/em&gt; sync ORM performance, since the actual database call is non-blocking either way. But in some benchmarks, &lt;strong&gt;async ORM queries are actually slower than sync ORM queries&lt;/strong&gt; for CPU-bound work like object instantiation.&lt;/p&gt;

&lt;p&gt;The reason: async adds its own overhead (event loop scheduling, coroutine creation, awaiting each step of the driver protocol) on top of the ORM's existing object-mapping cost.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For &lt;strong&gt;I/O-bound work&lt;/strong&gt; (waiting on the network), async wins because the event loop can do other things while waiting.&lt;/li&gt;
&lt;li&gt;But &lt;strong&gt;object instantiation isn't I/O-bound.&lt;/strong&gt; It's pure CPU work happening on the same thread, and async adds bookkeeping around it without adding parallelism, because Python's GIL means you're not getting extra CPU time from asyncio. You're just avoiding blocking waits.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;So the ORM's object-mapping tax is paid in full, and the async machinery adds a smaller additional tax on top&lt;/strong&gt;, for operations that were never going to benefit from concurrency in the first place.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  When Each Layer Is the Right Choice
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Use raw asyncpg when...
&lt;/h3&gt;

&lt;p&gt;You're building a narrow, performance-critical service: a data pipeline, an analytics endpoint, a service that streams large result sets. You control the SQL directly and don't need object mapping, relationships, or migrations tied to this code path.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# High-throughput analytics endpoint
&lt;/span&gt;&lt;span class="nd"&gt;@app.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/api/analytics/daily-revenue&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;daily_revenue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;asyncpg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Pool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Depends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get_pool&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;acquire&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
            SELECT date_trunc(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;day&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;, created_at) as day, SUM(total) as revenue
            FROM orders
            WHERE created_at &amp;gt;= NOW() - INTERVAL &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;30 days&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;
            GROUP BY day
            ORDER BY day
        &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Use SQLAlchemy Core when...
&lt;/h3&gt;

&lt;p&gt;You want composable, injection-safe query building without the ORM's object overhead. This is the &lt;strong&gt;sweet spot for most read-heavy API endpoints&lt;/strong&gt; (list views, search, reporting) where you need dynamic filters but don't need to mutate and save objects back.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;list_orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;AsyncSession&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;OrderFilters&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;stmt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;orders_table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;orders_table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;orders_table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orders_table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_active&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;stmt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orders_table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;min_total&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;stmt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orders_table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;min_total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mappings&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# Returns dicts, not ORM objects
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;result.mappings().all()&lt;/code&gt; is the key detail.&lt;/strong&gt; It returns plain dictionaries instead of ORM entity objects, skipping the identity map and change-tracking machinery entirely while still using SQLAlchemy's query builder and connection pooling.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Use the full ORM when...
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You're doing &lt;strong&gt;writes with relationships&lt;/strong&gt; (creating an order with items in one transaction)&lt;/li&gt;
&lt;li&gt;You need the &lt;strong&gt;identity map&lt;/strong&gt; to prevent duplicate object instances across a request&lt;/li&gt;
&lt;li&gt;You're using SQLAlchemy's &lt;strong&gt;relationship loading&lt;/strong&gt; (&lt;code&gt;selectinload&lt;/code&gt;, &lt;code&gt;joinedload&lt;/code&gt;) to manage complex object graphs&lt;/li&gt;
&lt;li&gt;Your team's &lt;strong&gt;productivity gain&lt;/strong&gt; from the ORM's ergonomics outweighs the performance cost for that specific endpoint
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_order_with_items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;AsyncSession&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;]):&lt;/span&gt;
    &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pending&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OrderItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;product_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
            &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;quantity&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;refresh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is exactly where the ORM earns its overhead: cascading inserts, relationship management, and object lifecycle tracking that would take significantly more raw SQL and manual bookkeeping to replicate correctly.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Layer&lt;/th&gt;
&lt;th&gt;Best for&lt;/th&gt;
&lt;th&gt;Returns&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Raw asyncpg&lt;/td&gt;
&lt;td&gt;Narrow, performance-critical paths, analytics, streaming&lt;/td&gt;
&lt;td&gt;Records / dicts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SQLAlchemy Core&lt;/td&gt;
&lt;td&gt;Read-heavy list, search and reporting endpoints&lt;/td&gt;
&lt;td&gt;Dicts via &lt;code&gt;.mappings()&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Full ORM&lt;/td&gt;
&lt;td&gt;Writes with relationships, complex object graphs&lt;/td&gt;
&lt;td&gt;Tracked ORM objects&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  The Hybrid Pattern I Actually Use
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcocz1zh9xn60kp7hqtme.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcocz1zh9xn60kp7hqtme.webp" alt=" " width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Most production FastAPI services benefit from using &lt;strong&gt;different layers for different endpoints&lt;/strong&gt;, all sharing the same async engine and connection pool:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app/database.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sqlalchemy.ext.asyncio&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;create_async_engine&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;async_sessionmaker&lt;/span&gt;

&lt;span class="n"&gt;engine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_async_engine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;postgresql+asyncpg://user:pass@localhost/mydb&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;pool_size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;max_overflow&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;pool_pre_ping&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;AsyncSessionLocal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;async_sessionmaker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;expire_on_commit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_db&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nc"&gt;AsyncSessionLocal&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sqlalchemy&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;

&lt;span class="c1"&gt;# Writes and relationship-heavy reads: full ORM
&lt;/span&gt;&lt;span class="nd"&gt;@app.post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/api/orders&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;OrderCreate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;AsyncSession&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Depends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get_db&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
    &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;order_service&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Uses ORM
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;

&lt;span class="c1"&gt;# High-volume list endpoints: SQLAlchemy Core, dicts not objects
&lt;/span&gt;&lt;span class="nd"&gt;@app.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/api/orders&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;list_orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;OrderFilters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Depends&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;AsyncSession&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Depends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get_db&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;stmt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;build_order_query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Core query, no ORM mapping
&lt;/span&gt;    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mappings&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# Reporting and analytics: raw SQL via the same session
&lt;/span&gt;&lt;span class="nd"&gt;@app.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/api/reports/summary&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;report_summary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;AsyncSession&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Depends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get_db&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
        SELECT status, COUNT(*) as count, SUM(total) as revenue
        FROM orders GROUP BY status
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mappings&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All three share the same &lt;code&gt;AsyncSession&lt;/code&gt; and connection pool. You're not managing separate drivers or separate pools. &lt;strong&gt;You're choosing, per endpoint, how much of the ORM's machinery you actually need.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The One Setting That Changes Everything: &lt;code&gt;expire_on_commit&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;If you do use the ORM, this default setting quietly causes extra queries you didn't ask for:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Default behavior - expires all objects after commit
&lt;/span&gt;&lt;span class="n"&gt;async_session&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;async_sessionmaker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;create_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Triggers a NEW query to refresh the expired object!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, SQLAlchemy marks every object's attributes as "expired" after a commit, so the next time you access them, it silently re-fetches from the database to guarantee freshness.&lt;/p&gt;

&lt;p&gt;In a request-response API, where the session ends right after the commit, this is almost always wasted work. You're about to serialize the object and return it, not keep using it across a long-lived session.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Disable expiration - trust the in-memory data after commit
&lt;/span&gt;&lt;span class="n"&gt;async_session&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;async_sessionmaker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;expire_on_commit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;expire_on_commit=False&lt;/code&gt; is the single highest-leverage SQLAlchemy async setting for API workloads.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I set it on every FastAPI project now. Without it, a simple "create and return" endpoint fires a &lt;code&gt;COMMIT&lt;/code&gt; and then a silent &lt;code&gt;SELECT&lt;/code&gt; to re-fetch data you already had in memory.&lt;/p&gt;




&lt;h2&gt;
  
  
  Bottom Line
&lt;/h2&gt;

&lt;p&gt;Async doesn't erase the cost of the layers above the database driver. It just changes what kind of cost you're paying.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Raw asyncpg&lt;/strong&gt; is fast because it does almost nothing beyond speaking PostgreSQL's protocol.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SQLAlchemy Core&lt;/strong&gt; adds a thin, worthwhile layer of safety and composability.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The full ORM&lt;/strong&gt; adds real, measurable overhead in exchange for object mapping, relationship management, and identity tracking: features that are essential for some endpoints and pure cost for others.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The mistake isn't choosing SQLAlchemy's ORM. &lt;strong&gt;It's using it uniformly across every endpoint without asking whether that endpoint needs what it provides.&lt;/strong&gt; A list endpoint returning 500 rows of read-only data doesn't need an identity map. A checkout endpoint creating an order with five related line items absolutely does.&lt;/p&gt;

&lt;p&gt;That endpoint that went from 340ms to 890ms after our sync-to-async migration? We didn't revert to sync. We kept async, dropped the ORM for that specific read path, and switched to &lt;code&gt;result.mappings().all()&lt;/code&gt; with SQLAlchemy Core.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It came back down to 95ms&lt;/strong&gt;: faster than the original sync version, using the same connection pool, the same database, and about fifteen lines of changed code.&lt;/p&gt;




&lt;h2&gt;
  
  
  Over to You
&lt;/h2&gt;

&lt;p&gt;What's your database access pattern in async Python? Full ORM everywhere, raw SQL everywhere, or a hybrid like this? And has anyone measured the async-vs-sync ORM overhead in their own benchmarks? I'd love to see numbers from a different workload than mine. Share in the comments 👇&lt;/p&gt;




&lt;p&gt;&lt;em&gt;If this was useful, follow me here for more stories from the backend trenches, and subscribe to &lt;a href="https://thebackendtechlead.substack.com/" rel="noopener noreferrer"&gt;The Backend Tech Lead&lt;/a&gt; on Substack for weekly blueprints.&lt;/em&gt;---&lt;br&gt;
title: asyncpg vs SQLAlchemy ORM - The 9x Slowdown Nobody Warns You About&lt;br&gt;
description: Async doesn't mean fast. Your ORM might be quietly throwing away everything asyncpg gives you.&lt;/p&gt;
&lt;h2&gt;
  
  
  tags: python, fastapi, postgres, performance
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Async doesn't mean fast. Your ORM might be quietly throwing away everything asyncpg gives you.&lt;/em&gt;&lt;/p&gt;



&lt;p&gt;We migrated a FastAPI service from sync SQLAlchemy to async SQLAlchemy, expecting a performance win. Async is supposed to be faster, right? Non-blocking I/O, more concurrent requests per worker, better resource utilization.&lt;/p&gt;

&lt;p&gt;Our load test showed the opposite. &lt;strong&gt;The async version was slower per-request than the sync version had been.&lt;/strong&gt; Not by a little: a specific endpoint that fetched 500 rows and mapped them to ORM objects went from &lt;strong&gt;340ms to 890ms&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I assumed we'd misconfigured something. Wrong connection pool size, maybe, or a missing index. I profiled the endpoint with &lt;code&gt;cProfile&lt;/code&gt; and found the answer: &lt;strong&gt;the async ORM layer itself was the bottleneck.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Object instantiation, identity map tracking, and attribute loading (the same overhead that exists in sync SQLAlchemy) was still there in async SQLAlchemy. We'd added the complexity of async without eliminating the cost of the ORM.&lt;/p&gt;

&lt;p&gt;This isn't a reason to avoid async or avoid SQLAlchemy. It's a reason to understand exactly what you're paying for at each layer, so you can make an informed choice instead of assuming "async" automatically means "fast."&lt;/p&gt;


&lt;h2&gt;
  
  
  The Three Layers and What Each One Costs
&lt;/h2&gt;



&lt;p&gt;When you fetch data from PostgreSQL in a modern async Python app, there are three layers between your code and the database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Your code
  → SQLAlchemy ORM (optional)     ← object mapping, identity map, relationships
  → SQLAlchemy Core / Driver       ← query compilation, connection management
  → asyncpg or psycopg              ← the actual wire protocol to PostgreSQL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each layer adds capability and each layer adds overhead. Understanding &lt;em&gt;where&lt;/em&gt; the overhead comes from tells you when it's worth paying and when it isn't.&lt;/p&gt;

&lt;h3&gt;
  
  
  Layer 1: The Driver (asyncpg vs psycopg 3)
&lt;/h3&gt;

&lt;p&gt;At the bottom, you have the actual PostgreSQL wire protocol implementation. Two real choices in 2026:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;asyncpg&lt;/strong&gt;: built from scratch for asyncio. Speaks PostgreSQL's binary protocol directly. No general-purpose abstraction layer. This is the fastest way to talk to PostgreSQL from Python, full stop.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;psycopg 3 (async mode)&lt;/strong&gt;: the modern rewrite of the classic psycopg2, with native async support. Slightly slower than asyncpg in raw benchmarks, but shares an API with sync psycopg, which matters if you're maintaining both sync and async code paths.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Raw asyncpg - as fast as Python gets talking to PostgreSQL
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;asyncpg&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncpg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT id, total, status FROM orders WHERE user_id = $1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# psycopg 3 async - nearly as fast, more familiar API
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;psycopg&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;psycopg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AsyncConnection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT id, total, status FROM orders WHERE user_id = %s&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchall&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both are dramatically faster than going through an ORM for simple queries. The difference between them is small enough that it rarely decides an architecture. asyncpg wins benchmarks by roughly 2x for raw throughput, but in a real application with network latency and business logic around the query, that gap shrinks to something you won't notice.&lt;/p&gt;

&lt;h3&gt;
  
  
  Layer 2: SQLAlchemy Core (No ORM)
&lt;/h3&gt;

&lt;p&gt;SQLAlchemy Core gives you a query builder and connection management &lt;strong&gt;without object mapping&lt;/strong&gt;. You write something close to SQL, but Python-native and composable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sqlalchemy&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sqlalchemy.ext.asyncio&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;create_async_engine&lt;/span&gt;

&lt;span class="n"&gt;engine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_async_engine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;postgresql+asyncpg://user:pass@localhost/mydb&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;orders&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;total&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchall&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This adds a small amount of overhead over raw asyncpg (query compilation, parameter binding, connection pool management), but it's a fraction of what the full ORM costs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Layer 3: The ORM (SQLAlchemy's Declarative Models)
&lt;/h3&gt;

&lt;p&gt;The ORM adds object identity tracking, relationship loading, change tracking (dirty attribute detection), and Python object instantiation &lt;strong&gt;for every row&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sqlalchemy&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;select&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sqlalchemy.orm&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;declarative_base&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Mapped&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mapped_column&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sqlalchemy.ext.asyncio&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;AsyncSession&lt;/span&gt;

&lt;span class="n"&gt;Base&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;declarative_base&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Base&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;__tablename__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;orders&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Mapped&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mapped_column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;primary_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Mapped&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Mapped&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Mapped&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;AsyncSession&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scalars&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same query. Same data. But now every row becomes a full Python object with SQLAlchemy's instrumentation attached:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tracking whether each attribute has changed&lt;/li&gt;
&lt;li&gt;Registering the object in the session's identity map, so &lt;code&gt;session.get(Order, id)&lt;/code&gt; returns the same instance&lt;/li&gt;
&lt;li&gt;Setting up lazy-loading machinery for any relationships&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The Benchmark: What Each Layer Actually Costs
&lt;/h2&gt;

&lt;p&gt;I ran this on a table with 100,000 rows, fetching 500 rows matching a filter, on a local PostgreSQL instance to eliminate network variance:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The jump from Core to ORM is the one that matters.&lt;/strong&gt; Going from raw asyncpg to SQLAlchemy Core costs you a small, reasonable overhead for a massive gain in composability and safety. Going from Core to full ORM costs you almost &lt;strong&gt;3x more&lt;/strong&gt; on top of that, for object mapping you may not even need.&lt;/p&gt;

&lt;p&gt;This matches what other benchmarks in the Python community have found: SQLAlchemy's ORM layer with async drivers can be an order of magnitude slower than raw driver access for simple read-heavy queries, primarily because of &lt;strong&gt;Python-level object construction overhead&lt;/strong&gt;, not the database round trip itself.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why the ORM Overhead Is So High With Async
&lt;/h2&gt;

&lt;p&gt;Here's the part that surprised me most. You'd expect async to at least &lt;em&gt;match&lt;/em&gt; sync ORM performance, since the actual database call is non-blocking either way. But in some benchmarks, &lt;strong&gt;async ORM queries are actually slower than sync ORM queries&lt;/strong&gt; for CPU-bound work like object instantiation.&lt;/p&gt;

&lt;p&gt;The reason: async adds its own overhead (event loop scheduling, coroutine creation, awaiting each step of the driver protocol) on top of the ORM's existing object-mapping cost.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For &lt;strong&gt;I/O-bound work&lt;/strong&gt; (waiting on the network), async wins because the event loop can do other things while waiting.&lt;/li&gt;
&lt;li&gt;But &lt;strong&gt;object instantiation isn't I/O-bound.&lt;/strong&gt; It's pure CPU work happening on the same thread, and async adds bookkeeping around it without adding parallelism, because Python's GIL means you're not getting extra CPU time from asyncio. You're just avoiding blocking waits.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;So the ORM's object-mapping tax is paid in full, and the async machinery adds a smaller additional tax on top&lt;/strong&gt;, for operations that were never going to benefit from concurrency in the first place.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  When Each Layer Is the Right Choice
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Use raw asyncpg when...
&lt;/h3&gt;

&lt;p&gt;You're building a narrow, performance-critical service: a data pipeline, an analytics endpoint, a service that streams large result sets. You control the SQL directly and don't need object mapping, relationships, or migrations tied to this code path.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# High-throughput analytics endpoint
&lt;/span&gt;&lt;span class="nd"&gt;@app.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/api/analytics/daily-revenue&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;daily_revenue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;asyncpg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Pool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Depends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get_pool&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;acquire&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
            SELECT date_trunc(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;day&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;, created_at) as day, SUM(total) as revenue
            FROM orders
            WHERE created_at &amp;gt;= NOW() - INTERVAL &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;30 days&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;
            GROUP BY day
            ORDER BY day
        &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Use SQLAlchemy Core when...
&lt;/h3&gt;

&lt;p&gt;You want composable, injection-safe query building without the ORM's object overhead. This is the &lt;strong&gt;sweet spot for most read-heavy API endpoints&lt;/strong&gt; (list views, search, reporting) where you need dynamic filters but don't need to mutate and save objects back.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;list_orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;AsyncSession&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;OrderFilters&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;stmt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;orders_table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;orders_table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;orders_table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orders_table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_active&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;stmt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orders_table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;min_total&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;stmt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orders_table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;min_total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mappings&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# Returns dicts, not ORM objects
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;result.mappings().all()&lt;/code&gt; is the key detail.&lt;/strong&gt; It returns plain dictionaries instead of ORM entity objects, skipping the identity map and change-tracking machinery entirely while still using SQLAlchemy's query builder and connection pooling.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Use the full ORM when...
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You're doing &lt;strong&gt;writes with relationships&lt;/strong&gt; (creating an order with items in one transaction)&lt;/li&gt;
&lt;li&gt;You need the &lt;strong&gt;identity map&lt;/strong&gt; to prevent duplicate object instances across a request&lt;/li&gt;
&lt;li&gt;You're using SQLAlchemy's &lt;strong&gt;relationship loading&lt;/strong&gt; (&lt;code&gt;selectinload&lt;/code&gt;, &lt;code&gt;joinedload&lt;/code&gt;) to manage complex object graphs&lt;/li&gt;
&lt;li&gt;Your team's &lt;strong&gt;productivity gain&lt;/strong&gt; from the ORM's ergonomics outweighs the performance cost for that specific endpoint
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_order_with_items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;AsyncSession&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;]):&lt;/span&gt;
    &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pending&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OrderItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;product_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
            &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;quantity&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;refresh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is exactly where the ORM earns its overhead: cascading inserts, relationship management, and object lifecycle tracking that would take significantly more raw SQL and manual bookkeeping to replicate correctly.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Layer&lt;/th&gt;
&lt;th&gt;Best for&lt;/th&gt;
&lt;th&gt;Returns&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Raw asyncpg&lt;/td&gt;
&lt;td&gt;Narrow, performance-critical paths, analytics, streaming&lt;/td&gt;
&lt;td&gt;Records / dicts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SQLAlchemy Core&lt;/td&gt;
&lt;td&gt;Read-heavy list, search and reporting endpoints&lt;/td&gt;
&lt;td&gt;Dicts via &lt;code&gt;.mappings()&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Full ORM&lt;/td&gt;
&lt;td&gt;Writes with relationships, complex object graphs&lt;/td&gt;
&lt;td&gt;Tracked ORM objects&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  The Hybrid Pattern I Actually Use
&lt;/h2&gt;

&lt;p&gt;Most production FastAPI services benefit from using &lt;strong&gt;different layers for different endpoints&lt;/strong&gt;, all sharing the same async engine and connection pool:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app/database.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sqlalchemy.ext.asyncio&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;create_async_engine&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;async_sessionmaker&lt;/span&gt;

&lt;span class="n"&gt;engine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_async_engine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;postgresql+asyncpg://user:pass@localhost/mydb&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;pool_size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;max_overflow&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;pool_pre_ping&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;AsyncSessionLocal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;async_sessionmaker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;expire_on_commit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_db&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nc"&gt;AsyncSessionLocal&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sqlalchemy&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;

&lt;span class="c1"&gt;# Writes and relationship-heavy reads: full ORM
&lt;/span&gt;&lt;span class="nd"&gt;@app.post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/api/orders&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;OrderCreate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;AsyncSession&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Depends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get_db&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
    &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;order_service&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Uses ORM
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;

&lt;span class="c1"&gt;# High-volume list endpoints: SQLAlchemy Core, dicts not objects
&lt;/span&gt;&lt;span class="nd"&gt;@app.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/api/orders&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;list_orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;OrderFilters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Depends&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;AsyncSession&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Depends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get_db&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;stmt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;build_order_query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Core query, no ORM mapping
&lt;/span&gt;    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mappings&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# Reporting and analytics: raw SQL via the same session
&lt;/span&gt;&lt;span class="nd"&gt;@app.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/api/reports/summary&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;report_summary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;AsyncSession&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Depends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get_db&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
        SELECT status, COUNT(*) as count, SUM(total) as revenue
        FROM orders GROUP BY status
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mappings&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All three share the same &lt;code&gt;AsyncSession&lt;/code&gt; and connection pool. You're not managing separate drivers or separate pools. &lt;strong&gt;You're choosing, per endpoint, how much of the ORM's machinery you actually need.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The One Setting That Changes Everything: &lt;code&gt;expire_on_commit&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;If you do use the ORM, this default setting quietly causes extra queries you didn't ask for:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Default behavior - expires all objects after commit
&lt;/span&gt;&lt;span class="n"&gt;async_session&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;async_sessionmaker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;create_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Triggers a NEW query to refresh the expired object!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, SQLAlchemy marks every object's attributes as "expired" after a commit, so the next time you access them, it silently re-fetches from the database to guarantee freshness.&lt;/p&gt;

&lt;p&gt;In a request-response API, where the session ends right after the commit, this is almost always wasted work. You're about to serialize the object and return it, not keep using it across a long-lived session.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Disable expiration - trust the in-memory data after commit
&lt;/span&gt;&lt;span class="n"&gt;async_session&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;async_sessionmaker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;expire_on_commit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;expire_on_commit=False&lt;/code&gt; is the single highest-leverage SQLAlchemy async setting for API workloads.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I set it on every FastAPI project now. Without it, a simple "create and return" endpoint fires a &lt;code&gt;COMMIT&lt;/code&gt; and then a silent &lt;code&gt;SELECT&lt;/code&gt; to re-fetch data you already had in memory.&lt;/p&gt;




&lt;h2&gt;
  
  
  Bottom Line
&lt;/h2&gt;

&lt;p&gt;Async doesn't erase the cost of the layers above the database driver. It just changes what kind of cost you're paying.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Raw asyncpg&lt;/strong&gt; is fast because it does almost nothing beyond speaking PostgreSQL's protocol.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SQLAlchemy Core&lt;/strong&gt; adds a thin, worthwhile layer of safety and composability.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The full ORM&lt;/strong&gt; adds real, measurable overhead in exchange for object mapping, relationship management, and identity tracking: features that are essential for some endpoints and pure cost for others.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The mistake isn't choosing SQLAlchemy's ORM. &lt;strong&gt;It's using it uniformly across every endpoint without asking whether that endpoint needs what it provides.&lt;/strong&gt; A list endpoint returning 500 rows of read-only data doesn't need an identity map. A checkout endpoint creating an order with five related line items absolutely does.&lt;/p&gt;

&lt;p&gt;That endpoint that went from 340ms to 890ms after our sync-to-async migration? We didn't revert to sync. We kept async, dropped the ORM for that specific read path, and switched to &lt;code&gt;result.mappings().all()&lt;/code&gt; with SQLAlchemy Core.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It came back down to 95ms&lt;/strong&gt;: faster than the original sync version, using the same connection pool, the same database, and about fifteen lines of changed code.&lt;/p&gt;




&lt;h2&gt;
  
  
  Over to You
&lt;/h2&gt;

&lt;p&gt;What's your database access pattern in async Python? Full ORM everywhere, raw SQL everywhere, or a hybrid like this? And has anyone measured the async-vs-sync ORM overhead in their own benchmarks? I'd love to see numbers from a different workload than mine. Share in the comments 👇&lt;/p&gt;




&lt;p&gt;&lt;em&gt;If this was useful, follow me here for more stories from the backend trenches, and subscribe to &lt;a href="https://thebackendtechlead.substack.com/" rel="noopener noreferrer"&gt;The Backend Tech Lead&lt;/a&gt; on Substack for weekly blueprints.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>fastapi</category>
      <category>postgres</category>
      <category>performance</category>
    </item>
    <item>
      <title>The Life of a Django Request</title>
      <dc:creator>Anas Issath</dc:creator>
      <pubDate>Sat, 07 Nov 2026 06:08:11 +0000</pubDate>
      <link>https://dev.to/anas_issath/the-life-of-a-django-request-3j8n</link>
      <guid>https://dev.to/anas_issath/the-life-of-a-django-request-3j8n</guid>
      <description>&lt;p&gt;&lt;em&gt;A guided tour of everything that happens between someone clicking a link and your view function running. No traps, no gotchas, just how it actually works.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Most articles about Django are about what goes wrong. This one isn't. &lt;strong&gt;This is a tour.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Every time someone loads a page in your Django app, a small, elegant machine springs into motion. It unwraps the request, threads it through a stack of layers, finds the right view, runs your code, and packages the answer back up, usually in well under a tenth of a second.&lt;/p&gt;

&lt;p&gt;Most of us write views for years without ever watching that machine run. We know our view function gets called with a &lt;code&gt;request&lt;/code&gt; object, and we know we return a response, and the space between is a comfortable fog.&lt;/p&gt;

&lt;p&gt;Let's clear the fog. We're going to follow a single request from the instant it leaves a browser to the instant the response arrives back, and at each stop I'll show you what Django is actually doing and why.&lt;/p&gt;

&lt;p&gt;By the end, &lt;code&gt;request&lt;/code&gt; and &lt;code&gt;response&lt;/code&gt; won't be magic. They'll be things you understand from the inside. That understanding is quietly one of the most useful things you can have as a Django developer, because once you can see the machine, you know exactly where to reach when something needs explaining, debugging, or extending.&lt;/p&gt;

&lt;p&gt;Here's the whole journey at a glance, and then we'll walk it step by step:&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Browser → Nginx → Gunicorn (WSGI) → Django handler
      → Middleware (in) → URL resolver → Your view (ORM + templates)
      → Middleware (out) → Gunicorn → Nginx → Browser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Stop 1: The Browser and the Web Server
&lt;/h2&gt;

&lt;p&gt;It starts with a browser making an HTTP request (&lt;code&gt;GET /articles/42/&lt;/code&gt;) and sending it across the internet to your server. The first thing it hits isn't Django at all. It's a &lt;strong&gt;web server&lt;/strong&gt;, typically &lt;strong&gt;Nginx&lt;/strong&gt;, sitting at the front door.&lt;/p&gt;

&lt;p&gt;Nginx handles the things Django shouldn't be bothered with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Serving static files (CSS, images) directly&lt;/li&gt;
&lt;li&gt;Terminating SSL&lt;/li&gt;
&lt;li&gt;Load-balancing across multiple app processes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For anything dynamic (anything that needs your code), Nginx hands the request off to an &lt;strong&gt;application server&lt;/strong&gt; like &lt;strong&gt;Gunicorn&lt;/strong&gt; or &lt;strong&gt;uWSGI&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Think of Nginx as the receptionist who deals with routine visitors themselves and only forwards the real business inward.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So already there are two programs involved before Django wakes up. This is worth knowing, because when people say "Django is slow to serve static files" or debug a deployment, the answer usually lives right here in the handoff between the web server and the app server.&lt;/p&gt;




&lt;h2&gt;
  
  
  Stop 2: WSGI, the Universal Handshake
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvdmtiij0ak5tvhb75t92.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvdmtiij0ak5tvhb75t92.webp" alt=" " width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Gunicorn now has the request, but Gunicorn and Django are separate projects written by different people. How do they talk? Through a contract called &lt;strong&gt;WSGI&lt;/strong&gt; (Web Server Gateway Interface), and understanding this contract demystifies a huge amount.&lt;/p&gt;

&lt;p&gt;WSGI is dead simple. The server calls your application as a plain function with two arguments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;environ&lt;/code&gt;&lt;/strong&gt;: a dictionary holding everything about the request (the path, headers, method, all of it)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;start_response&lt;/code&gt;&lt;/strong&gt;: a callback for setting the status and headers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Your application does its work and returns the response body as bytes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;That's the entire contract.&lt;/strong&gt; And it's why you can run Django under Gunicorn or uWSGI or others interchangeably. They all speak WSGI, so any of them can talk to any WSGI framework.&lt;/p&gt;

&lt;p&gt;Django's end of this handshake lives in a file you've seen and probably never opened: &lt;strong&gt;&lt;code&gt;wsgi.py&lt;/code&gt;&lt;/strong&gt;. That file exposes a &lt;code&gt;WSGIHandler&lt;/code&gt;, the object Gunicorn calls. This is the true front door of Django itself.&lt;/p&gt;

&lt;p&gt;(There's a newer async version of this contract called &lt;strong&gt;ASGI&lt;/strong&gt;, in &lt;code&gt;asgi.py&lt;/code&gt;, which is what makes websockets and async views possible. Same idea, built for concurrency. But the classic synchronous path is WSGI, and that's the one we're walking.)&lt;/p&gt;




&lt;h2&gt;
  
  
  Stop 3: Django Wakes Up and Builds the Request Object
&lt;/h2&gt;

&lt;p&gt;Now Django's &lt;code&gt;WSGIHandler&lt;/code&gt; takes over, and the first meaningful thing it does is fire a signal: &lt;strong&gt;&lt;code&gt;request_started&lt;/code&gt;&lt;/strong&gt;. This is genuinely your earliest possible hook into the lifecycle. Code can subscribe to that signal to run the moment any request begins, before anything else happens.&lt;/p&gt;

&lt;p&gt;Then Django takes that raw &lt;code&gt;environ&lt;/code&gt; dictionary and wraps it into the object you know and love: the &lt;strong&gt;&lt;code&gt;HttpRequest&lt;/code&gt;&lt;/strong&gt;. This is the &lt;code&gt;request&lt;/code&gt; that gets passed to every view you've ever written: &lt;code&gt;request.method&lt;/code&gt;, &lt;code&gt;request.path&lt;/code&gt;, &lt;code&gt;request.GET&lt;/code&gt;, &lt;code&gt;request.user&lt;/code&gt;. It's not magic; it's a friendly Python object built from the raw WSGI environment.&lt;/p&gt;

&lt;p&gt;Here's a lovely detail most people never learn: &lt;strong&gt;parts of the request are lazy.&lt;/strong&gt; &lt;code&gt;request.body&lt;/code&gt;, &lt;code&gt;request.POST&lt;/code&gt;, and &lt;code&gt;request.FILES&lt;/code&gt; don't actually read the incoming data from the network socket until you access them.&lt;/p&gt;

&lt;p&gt;Django creates the request object instantly without reading the (potentially huge) body, and only pulls it in when your code asks. It's an elegant efficiency: why read a 10MB upload into memory if this particular view never looks at it?&lt;/p&gt;

&lt;p&gt;(One consequence: once you've read &lt;code&gt;request.body&lt;/code&gt;, you generally can't read it again. The stream's been consumed.)&lt;/p&gt;




&lt;h2&gt;
  
  
  Stop 4: The Middleware Onion (Going In)
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftf5xcmzo84hyws0am1xj.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftf5xcmzo84hyws0am1xj.webp" alt=" " width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the request begins its journey inward through the &lt;strong&gt;middleware stack&lt;/strong&gt;, and the onion is the perfect mental image. The request travels from the outside layer toward the center, and later the response travels back out through the same layers in reverse.&lt;/p&gt;

&lt;p&gt;Middleware is just a list in your &lt;code&gt;settings.py&lt;/code&gt;, and Django processes it &lt;strong&gt;top to bottom on the way in&lt;/strong&gt;. Each layer is a chance to inspect, modify, or even stop the request before it reaches your view. Walk the default stack and you'll recognize old friends, each doing one job:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Middleware&lt;/th&gt;
&lt;th&gt;What it does on the way in&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;SecurityMiddleware&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Enforces HTTPS and security-related rules&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;SessionMiddleware&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Loads the user's session from the session store and attaches it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;CommonMiddleware&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Handles URL normalization and a few housekeeping rules&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;CsrfViewMiddleware&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Checks the CSRF token on form submissions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;AuthenticationMiddleware&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Gives you &lt;code&gt;request.user&lt;/code&gt;&lt;/strong&gt;, by reading the session and looking up who's logged in&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That last one explains something you've used a thousand times: &lt;strong&gt;&lt;code&gt;request.user&lt;/code&gt; isn't conjured from nowhere.&lt;/strong&gt; A specific middleware put it there, on the way in, before your view ran. (And it's lazy too: &lt;code&gt;request.user&lt;/code&gt; is only actually fetched from the database the first time you touch it.)&lt;/p&gt;

&lt;p&gt;The power of the onion is that &lt;strong&gt;any layer can short-circuit the whole thing.&lt;/strong&gt; If &lt;code&gt;CsrfViewMiddleware&lt;/code&gt; sees a bad token, it returns a 403 right there. Your view never runs, and the response turns around and heads back out.&lt;/p&gt;

&lt;p&gt;Middleware is where cross-cutting concerns (security, sessions, auth) live so your views don't have to repeat them.&lt;/p&gt;




&lt;h2&gt;
  
  
  Stop 5: The URL Resolver Finds Your View
&lt;/h2&gt;

&lt;p&gt;The request has made it through middleware. Now Django needs to answer: &lt;em&gt;which view handles &lt;code&gt;/articles/42/&lt;/code&gt;?&lt;/em&gt; That's the job of the &lt;strong&gt;URL resolver&lt;/strong&gt; (the URL dispatcher), driven by your &lt;code&gt;urls.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Django takes the request path and tests it against your URL patterns, &lt;strong&gt;in order, top to bottom&lt;/strong&gt;, until one matches. When &lt;code&gt;path("articles/&amp;lt;int:article_id&amp;gt;/", views.article_detail)&lt;/code&gt; matches, two things come out of it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The view function to call&lt;/strong&gt; (&lt;code&gt;article_detail&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The captured arguments from the URL&lt;/strong&gt; (&lt;code&gt;article_id=42&lt;/code&gt;, already converted to an &lt;code&gt;int&lt;/code&gt; by the &lt;code&gt;&amp;lt;int:&amp;gt;&lt;/code&gt; converter)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A great under-the-hood detail: &lt;strong&gt;the resolver is cached.&lt;/strong&gt; Django builds the URL-pattern structure once and keeps it in memory rather than rebuilding it on every request. That's an important efficiency, since every single request needs it.&lt;/p&gt;

&lt;p&gt;This is also the quiet reason that &lt;strong&gt;changing your &lt;code&gt;urls.py&lt;/code&gt; requires a server restart in production&lt;/strong&gt;: the resolved URL configuration is cached at startup, so new patterns aren't picked up until the process reloads. If you've ever wondered why a URL change "didn't take" until you restarted, now you know exactly why.&lt;/p&gt;




&lt;h2&gt;
  
  
  Stop 6: Your View Runs (the Part You Already Know)
&lt;/h2&gt;

&lt;p&gt;Here we are at the center of the onion: your code. Django calls your view with the request object and the captured URL arguments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;article_detail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;article_id&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;article&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Article&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;article_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# ORM → SQL → database
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;article.html&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;article&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the part you write every day, so I'll just point out the two systems it usually reaches for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The ORM.&lt;/strong&gt; &lt;code&gt;Article.objects.get(...)&lt;/code&gt; doesn't talk to the database directly. It builds a description of a query that the ORM translates into SQL, sends to the database, and turns the result rows back into Python &lt;code&gt;Article&lt;/code&gt; objects. (Lazy, as ever: the query runs when the data is actually needed.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The template engine.&lt;/strong&gt; &lt;code&gt;render(...)&lt;/code&gt; takes your template and the context dictionary, and the template engine produces the final HTML string, which gets wrapped in an &lt;code&gt;HttpResponse&lt;/code&gt; object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One detail worth carrying with you, especially for class-based views: &lt;strong&gt;Django creates a new view instance for every request.&lt;/strong&gt; That's deliberate. It keeps requests isolated from each other.&lt;/p&gt;

&lt;p&gt;(It's also why a &lt;em&gt;mutable class attribute&lt;/em&gt; on a view is shared across all requests while &lt;em&gt;instance attributes&lt;/em&gt; aren't: a direct consequence of this one-instance-per-request design.)&lt;/p&gt;

&lt;p&gt;Your view's job ends the moment it returns a response object. Now that response begins its journey back out.&lt;/p&gt;




&lt;h2&gt;
  
  
  Stop 7: Back Out Through the Middleware (in Reverse)
&lt;/h2&gt;

&lt;p&gt;The response now travels back out through the same middleware stack, but &lt;strong&gt;in reverse order, bottom to top.&lt;/strong&gt; This symmetry is the elegant heart of the onion: each middleware that got a look at the request on the way in gets a look at the response on the way out.&lt;/p&gt;

&lt;p&gt;And they use it. On the way out:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;SessionMiddleware&lt;/code&gt;&lt;/strong&gt; saves any changes you made to the session back to the session store.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;SecurityMiddleware&lt;/code&gt;&lt;/strong&gt; and others add security headers to the outgoing response (things like &lt;code&gt;X-Content-Type-Options&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Other middleware&lt;/strong&gt; can compress the body, set cookies, or add their own headers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the response that leaves is richer than the one your view returned. Middleware decorated it on the way out with sessions, headers, and cookies.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The same layers, in mirror order: request going in, response coming out. That mirror is the whole design.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Stop 8: The Response Goes Home
&lt;/h2&gt;

&lt;p&gt;The finished &lt;code&gt;HttpResponse&lt;/code&gt; (status code, headers, and body) is handed back to the WSGI server (Gunicorn), which converts it into raw HTTP bytes and passes it to Nginx, which sends it across the internet to the browser, which renders the page. The machine goes quiet, waiting for the next request.&lt;/p&gt;

&lt;p&gt;The whole round trip happened in a fraction of a second:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Browser to Nginx to Gunicorn to Django's handler&lt;/li&gt;
&lt;li&gt;In through middleware&lt;/li&gt;
&lt;li&gt;Resolve the URL&lt;/li&gt;
&lt;li&gt;Run the view, query the database, render the template&lt;/li&gt;
&lt;li&gt;Back out through middleware&lt;/li&gt;
&lt;li&gt;And home again&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And now you can see every stop.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Knowing This Makes You Better at Django
&lt;/h2&gt;

&lt;p&gt;Here's the payoff, and it's real. Almost every Django concept that feels like "magic" is actually just &lt;strong&gt;one specific stop on this tour&lt;/strong&gt;, and once you can place it, it stops being mysterious:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;The question&lt;/th&gt;
&lt;th&gt;Where it lives&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Where does &lt;code&gt;request.user&lt;/code&gt; come from?&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;AuthenticationMiddleware&lt;/code&gt;, on the way in (Stop 4)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Why do my security headers appear even though my view didn't add them?&lt;/td&gt;
&lt;td&gt;Middleware, on the way out (Stop 7)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Why did my URL change not work until I restarted?&lt;/td&gt;
&lt;td&gt;The cached URL resolver (Stop 5)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Where should I put code that runs on every request?&lt;/td&gt;
&lt;td&gt;Middleware, or the &lt;code&gt;request_started&lt;/code&gt; signal (Stops 3–4)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Why is a class attribute on my view shared between users?&lt;/td&gt;
&lt;td&gt;One view instance per request (Stop 6)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;How can I run Django under Gunicorn or uWSGI?&lt;/td&gt;
&lt;td&gt;They both speak WSGI (Stop 2)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;None of these need memorizing once you hold the map. They all fall out of understanding the flow. &lt;strong&gt;That's the difference between using a framework and understanding it&lt;/strong&gt;, and it's the difference between guessing where a problem lives and knowing.&lt;/p&gt;

&lt;p&gt;Django's request/response cycle is, honestly, a beautiful piece of design. A raw network request enters, gets progressively wrapped and enriched into a friendly Python object, passes through orderly layers that each do one job, reaches your code at the calm center, and the answer flows back out through the same layers in mirror image.&lt;/p&gt;

&lt;p&gt;It's an onion, and now you've seen every ring of it. The next time you write &lt;code&gt;def my_view(request):&lt;/code&gt;, you'll know exactly what that request went through to reach you, and exactly where it's going next.&lt;/p&gt;




&lt;h2&gt;
  
  
  The 30-Second Takeaway
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A request hits a web server (Nginx) first.&lt;/strong&gt; It serves static files and forwards dynamic requests to an app server (Gunicorn/uWSGI).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gunicorn and Django talk through WSGI&lt;/strong&gt;, a tiny contract (&lt;code&gt;environ&lt;/code&gt; dict in, response bytes out), which is why any WSGI server can run any WSGI framework. Django's entry point is &lt;code&gt;wsgi.py&lt;/code&gt; (async apps use &lt;code&gt;asgi.py&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Django fires the &lt;code&gt;request_started&lt;/code&gt; signal&lt;/strong&gt;, then wraps the raw &lt;code&gt;environ&lt;/code&gt; into the &lt;code&gt;HttpRequest&lt;/code&gt; you know, with &lt;code&gt;body&lt;/code&gt;/&lt;code&gt;POST&lt;/code&gt;/&lt;code&gt;FILES&lt;/code&gt; read lazily, only when accessed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The request travels inward through middleware (top→bottom):&lt;/strong&gt; &lt;code&gt;SecurityMiddleware&lt;/code&gt;, &lt;code&gt;SessionMiddleware&lt;/code&gt;, &lt;code&gt;AuthenticationMiddleware&lt;/code&gt; (this is what sets &lt;code&gt;request.user&lt;/code&gt;), etc. Any layer can short-circuit and return early.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The URL resolver matches the path against &lt;code&gt;urls.py&lt;/code&gt; (top→bottom)&lt;/strong&gt; and extracts view + arguments. It's cached, which is why URL changes need a restart.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Your view runs&lt;/strong&gt; (a new instance per request for CBVs), uses the ORM and template engine, and returns an &lt;code&gt;HttpResponse&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The response travels back out through middleware in reverse (bottom→top)&lt;/strong&gt;, saving the session and adding security headers, then back through WSGI → Nginx → browser.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nearly every "magic" Django behavior is just one stop on this tour.&lt;/strong&gt; Knowing the map turns mysteries into locations.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Your Turn
&lt;/h2&gt;

&lt;p&gt;Enjoyed the tour? The best way to make it stick is to open your own project's &lt;code&gt;settings.py&lt;/code&gt;, read your &lt;code&gt;MIDDLEWARE&lt;/code&gt; list top to bottom, and trace one real URL from &lt;code&gt;urls.py&lt;/code&gt; to its view. You'll never see &lt;code&gt;def my_view(request):&lt;/code&gt; the same way again.&lt;/p&gt;

&lt;p&gt;Which stop on the tour surprised you most? Tell me in the comments 👇&lt;/p&gt;




&lt;p&gt;&lt;em&gt;If this was useful, follow me here for more stories from the backend trenches, and subscribe to &lt;a href="https://thebackendtechlead.substack.com/" rel="noopener noreferrer"&gt;The Backend Tech Lead&lt;/a&gt; on Substack for weekly blueprints.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>I Built a Tiny Django QuerySet to Finally Understand How .filter() Works</title>
      <dc:creator>Anas Issath</dc:creator>
      <pubDate>Fri, 06 Nov 2026 17:30:06 +0000</pubDate>
      <link>https://dev.to/anas_issath/i-built-a-tiny-django-queryset-to-finally-understand-how-filter-works-d8f</link>
      <guid>https://dev.to/anas_issath/i-built-a-tiny-django-queryset-to-finally-understand-how-filter-works-d8f</guid>
      <description>&lt;p&gt;&lt;em&gt;Django's QuerySet is "lazy" and "chainable" and everyone repeats those words, so let's build a working one in about 50 lines and watch the magic disappear.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You've written &lt;code&gt;Book.objects.filter(published=True).order_by("-created_at")[:10]&lt;/code&gt; a thousand times. You know it's "lazy." You know it's "chainable." You may have read that "nothing hits the database until you iterate."&lt;/p&gt;

&lt;p&gt;But if someone asked you to explain &lt;em&gt;how&lt;/em&gt; that actually works (how chaining &lt;code&gt;.filter()&lt;/code&gt; five times still produces one query, how the thing knows to wait), could you?&lt;/p&gt;

&lt;p&gt;Most of us couldn't, and that's fine, because you don't need to in order to use it. But there's a special kind of confidence that comes from understanding a tool from the inside. The fastest way to get there isn't to read the Django source (it's thousands of lines of edge cases). It's to &lt;strong&gt;build a tiny version yourself.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So that's what we're going to do: a working, chainable, lazy QuerySet in about 50 lines of plain Python. No Django, no database, just the ideas. By the end, the "magic" will just be code you wrote.&lt;/p&gt;

&lt;p&gt;We'll build it in four small steps, each solving the problem the last one revealed.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1: The Naive Version (and Why It's Wrong)
&lt;/h2&gt;

&lt;p&gt;Let's start with the most obvious way to write a "filter my data" object, so we can feel exactly what's missing. Imagine our "database" is just a list of dicts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;BOOKS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;title&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Dune&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;published&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;year&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1965&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;title&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Drafty&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;published&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;year&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2020&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;title&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Foundation&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;published&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;year&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1951&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NaiveQuery&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;conditions&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# immediately loop and keep matching rows
&lt;/span&gt;        &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;conditions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nc"&gt;NaiveQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BOOKS&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;published&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# → [{'title': 'Dune', ...}, {'title': 'Foundation', ...}]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works! But look at what it forces.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;It can't chain.&lt;/strong&gt; Because &lt;code&gt;filter()&lt;/code&gt; immediately returns a plain list, &lt;code&gt;NaiveQuery(BOOKS).filter(published=True).filter(year=1965)&lt;/code&gt; blows up, because a list has no &lt;code&gt;.filter()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It's eager.&lt;/strong&gt; It does the full scan the instant you call &lt;code&gt;filter&lt;/code&gt;, even if you only wanted to know "are there any?" or "how many?". If this were a real database, every &lt;code&gt;.filter()&lt;/code&gt; would be a separate expensive query.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those two problems, &lt;strong&gt;can't chain&lt;/strong&gt; and &lt;strong&gt;runs immediately&lt;/strong&gt;, are exactly the two things Django's design solves. Let's solve them too.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2: Make It Chainable (Return a New Object, Don't Execute)
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffmp1iw5pgno4jgma6zvu.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffmp1iw5pgno4jgma6zvu.webp" alt=" " width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's the key insight that unlocks chaining, and it's the same one Django uses:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;filter()&lt;/code&gt; shouldn't return results. It should return a new query object that remembers the filter and hasn't run anything yet.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So instead of storing the data and filtering it, our query object stores the &lt;strong&gt;list of conditions&lt;/strong&gt; to eventually apply:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;filters&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;      &lt;span class="c1"&gt;# a list of conditions, not results
&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;conditions&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# DON'T run anything. Return a NEW Query with this filter added on.
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filters&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;conditions&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look at what changed. &lt;code&gt;filter()&lt;/code&gt; now returns &lt;code&gt;Query(...)&lt;/code&gt; (another &lt;code&gt;Query&lt;/code&gt; object) with the new condition appended to the list. It touches no data. And because it returns a &lt;code&gt;Query&lt;/code&gt;, that returned object &lt;em&gt;also&lt;/em&gt; has a &lt;code&gt;.filter()&lt;/code&gt; method, so you can call it again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BOOKS&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;published&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1965&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# q is a Query holding filters=[{'published': True}, {'year': 1965}]
# ...and still NOTHING has been computed.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is exactly Django's real behavior, and the real source looks almost identical. Every &lt;code&gt;.filter()&lt;/code&gt; calls &lt;code&gt;_clone()&lt;/code&gt; to make a new QuerySet, adds the condition to it, and returns it.&lt;/p&gt;

&lt;p&gt;Django calls this being &lt;strong&gt;immutable through cloning&lt;/strong&gt;: your original query is never modified; each method hands back a fresh copy with one more instruction. That's why you can safely build a base queryset and branch off it in different directions without them interfering.&lt;/p&gt;

&lt;p&gt;We can chain now. But we still haven't solved &lt;em&gt;"when does it actually run?"&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 3: Make It Lazy (Run Only When Something Asks for Results)
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fu10242medkmxuj60dg95.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fu10242medkmxuj60dg95.webp" alt=" " width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's the heart of it. Our &lt;code&gt;Query&lt;/code&gt; is now a &lt;strong&gt;recipe&lt;/strong&gt;: a stored list of instructions that hasn't been executed. We want it to execute only when someone actually tries to &lt;em&gt;read&lt;/em&gt; the results: loops over it, lists it, counts it.&lt;/p&gt;

&lt;p&gt;In Python, "someone loops over this object" has a specific hook: the &lt;strong&gt;&lt;code&gt;__iter__&lt;/code&gt;&lt;/strong&gt; method. That's our trigger.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;filters&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;          &lt;span class="c1"&gt;# results live here AFTER we run
&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;conditions&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filters&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;conditions&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;_execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# apply every stored filter, in order - this is the "expensive" part
&lt;/span&gt;        &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
                &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
            &lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__iter__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# THE TRIGGER: only now do we actually run the query
&lt;/span&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_cache&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;          &lt;span class="c1"&gt;# ...and only once
&lt;/span&gt;            &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_execute&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;iter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_cache&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now watch the behavior fall into place:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BOOKS&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;published&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# nothing runs - just a recipe
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;built the query, DB untouched&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# prints; still nothing ran
&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;book&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;                            &lt;span class="c1"&gt;# NOW __iter__ fires → _execute() runs
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;book&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;title&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;                  &lt;span class="c1"&gt;# Dune, Foundation
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Building the query ran nothing. The work happened only when we looped. &lt;strong&gt;That is lazy evaluation&lt;/strong&gt;, and it's the entire reason chaining five &lt;code&gt;.filter()&lt;/code&gt; calls doesn't mean five database trips. None of them execute; they just stack instructions onto the recipe, and the one execution happens at the end when you consume it.&lt;/p&gt;

&lt;p&gt;And notice &lt;code&gt;self._cache&lt;/code&gt;. The first time you iterate, we run &lt;code&gt;_execute()&lt;/code&gt; and store the results. The second time you iterate the &lt;em&gt;same object&lt;/em&gt;, &lt;code&gt;self._cache&lt;/code&gt; is already filled, so we skip execution and reuse it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;This is Django's result cache&lt;/strong&gt;, the exact reason that iterating the same queryset variable twice only queries once. We just built it, in three lines.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6l9u5f4lihen57guw8tw.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6l9u5f4lihen57guw8tw.webp" alt=" " width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Django's real &lt;code&gt;__iter__&lt;/code&gt; is remarkably close to ours. It calls &lt;code&gt;_fetch_all()&lt;/code&gt;, which checks &lt;em&gt;"is &lt;code&gt;_result_cache&lt;/code&gt; None? If so, run the query and fill it,"&lt;/em&gt; then hands back the cached list. Same idea, same shape.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 4: Add the Terminal Methods (&lt;code&gt;count&lt;/code&gt;, &lt;code&gt;exists&lt;/code&gt;, &lt;code&gt;first&lt;/code&gt;)
&lt;/h2&gt;

&lt;p&gt;The last piece: the convenience methods that &lt;em&gt;consume&lt;/em&gt; the query. Now that iteration is our trigger, these are almost trivial to write. They just use the results:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;    &lt;span class="c1"&gt;# inside class Query:
&lt;/span&gt;    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;       &lt;span class="c1"&gt;# list(self) triggers __iter__
&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each of these forces evaluation by consuming the query, then answers its narrow question.&lt;/p&gt;

&lt;p&gt;And here's a subtlety worth pausing on that mirrors real Django. In our simple version, &lt;code&gt;list(self)&lt;/code&gt; fills the cache, so calling &lt;code&gt;.count()&lt;/code&gt; and then looping is efficient. In real Django, &lt;code&gt;.count()&lt;/code&gt; and &lt;code&gt;.exists()&lt;/code&gt; are actually smarter: they send &lt;code&gt;SELECT COUNT(*)&lt;/code&gt; and &lt;code&gt;SELECT 1&lt;/code&gt; to the database instead of loading every row.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;That's why you should prefer &lt;strong&gt;&lt;code&gt;.exists()&lt;/code&gt;&lt;/strong&gt; over &lt;code&gt;if queryset:&lt;/code&gt; when you only need a yes/no.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But the shape is the same: &lt;strong&gt;terminal methods are the things that break the laziness and make the query finally run.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The Whole Thing, ~50 Lines
&lt;/h2&gt;

&lt;p&gt;Here's our complete mini-QuerySet. Read it top to bottom. There's no magic left in it, because you built every line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;filters&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;conditions&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# chainable + lazy: return a NEW query, run nothing
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filters&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;conditions&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;_execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
                &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
            &lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__iter__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_cache&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;        &lt;span class="c1"&gt;# run once...
&lt;/span&gt;            &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_execute&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;iter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_cache&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;# ...then reuse the cache
&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;

&lt;span class="c1"&gt;# our "manager":
&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Manager&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;conditions&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;conditions&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;books&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Manager&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BOOKS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And it behaves like the real thing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;published&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# lazy - nothing ran
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;nothing executed yet&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;                        &lt;span class="c1"&gt;# runs once, caches
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;title&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;              &lt;span class="c1"&gt;# Dune, Foundation
&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;                   &lt;span class="c1"&gt;# reuses the cache - no re-run
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1965&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;title&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;   &lt;span class="c1"&gt;# Dune
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That &lt;code&gt;Manager&lt;/code&gt; at the bottom is the last piece of the puzzle. It's why you write &lt;code&gt;Book.objects.filter(...)&lt;/code&gt; and not &lt;code&gt;Query(Book).filter(...)&lt;/code&gt;. In Django, &lt;code&gt;.objects&lt;/code&gt; is a Manager, and its job is exactly what ours does: hand you a fresh QuerySet to start chaining from. Every &lt;code&gt;Book.objects.filter(...)&lt;/code&gt; starts a brand-new recipe.&lt;/p&gt;




&lt;h2&gt;
  
  
  What You Can Now See That You Couldn't Before
&lt;/h2&gt;

&lt;p&gt;You just built, in miniature, the real architecture of Django's ORM. And a bunch of things that were "just how it works" now have obvious mechanical explanations:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Question&lt;/th&gt;
&lt;th&gt;Answer&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Why doesn't &lt;code&gt;Book.objects.filter(...)&lt;/code&gt; hit the database?&lt;/td&gt;
&lt;td&gt;Like our &lt;code&gt;filter()&lt;/code&gt;, it returns a new object holding instructions and runs nothing.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Why can I chain &lt;code&gt;.filter().exclude().order_by()&lt;/code&gt; endlessly with one query?&lt;/td&gt;
&lt;td&gt;Each one just appends to the recipe and returns a new query; execution happens once, at the end.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Why does iterating the same queryset twice only query once?&lt;/td&gt;
&lt;td&gt;The result cache: our &lt;code&gt;self._cache&lt;/code&gt;, Django's &lt;code&gt;_result_cache&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Why is a fresh &lt;code&gt;Model.objects.filter(...)&lt;/code&gt; a separate query from another one?&lt;/td&gt;
&lt;td&gt;Different Query objects, different caches, exactly like calling our &lt;code&gt;Manager.filter()&lt;/code&gt; twice.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Why does &lt;code&gt;.exists()&lt;/code&gt; exist when I could just do &lt;code&gt;if qs:&lt;/code&gt;?&lt;/td&gt;
&lt;td&gt;A purpose-built terminal method can ask the database a cheaper question than "load everything."&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;None of this is memorization anymore. It falls out of the shape:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A query object stores instructions, stays lazy by returning new copies of itself, and only runs (once, then cached) when something consumes it.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That one sentence is the whole ORM, and now you've felt it from the inside.&lt;/p&gt;

&lt;p&gt;The real Django QuerySet is, of course, vastly more. It compiles these instructions into actual SQL across different database backends, handles joins and annotations and prefetching, and manages a hundred edge cases we happily ignored. But the soul of it (lazy, chainable, cloned-not-mutated, cached-after-execution) is exactly the fifty lines you just wrote.&lt;/p&gt;

&lt;p&gt;The next time you chain a queryset, you won't see magic. You'll see the recipe being built, waiting patiently for you to ask for the meal.&lt;/p&gt;




&lt;h2&gt;
  
  
  The 30-Second Takeaway
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fniqrxzjnjziuqm6b1ioq.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fniqrxzjnjziuqm6b1ioq.webp" alt=" " width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Django's QuerySet feels magical because it's lazy&lt;/strong&gt; (doesn't run until consumed) &lt;strong&gt;and chainable&lt;/strong&gt; (&lt;code&gt;.filter().exclude()...&lt;/code&gt;), but both are simple mechanics you can build yourself.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chainable:&lt;/strong&gt; &lt;code&gt;filter()&lt;/code&gt; returns a &lt;em&gt;new&lt;/em&gt; query object holding the accumulated conditions and runs nothing. Django does the same via &lt;code&gt;_clone()&lt;/code&gt;. QuerySets are "immutable through cloning," which is why you can branch a base query safely.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lazy:&lt;/strong&gt; execution is triggered by &lt;em&gt;consumption&lt;/em&gt;, Python's &lt;code&gt;__iter__&lt;/code&gt; hook. Building the query runs nothing; looping/listing/counting is what finally executes it. That's why five chained &lt;code&gt;.filter()&lt;/code&gt;s = one query, not five.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Result cache:&lt;/strong&gt; the first execution stores its results (&lt;code&gt;self._cache&lt;/code&gt; in ours, &lt;code&gt;_result_cache&lt;/code&gt; in Django), so iterating the same queryset again reuses them instead of re-querying.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Terminal methods&lt;/strong&gt; (&lt;code&gt;count&lt;/code&gt;, &lt;code&gt;exists&lt;/code&gt;, &lt;code&gt;first&lt;/code&gt;) are the things that consume the query and break the laziness; real Django's &lt;code&gt;.count()&lt;/code&gt;/&lt;code&gt;.exists()&lt;/code&gt; send cheaper SQL than loading every row.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.objects&lt;/code&gt; is a Manager.&lt;/strong&gt; Its only job is to hand you a fresh QuerySet to start chaining from.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build the ~50-line version once&lt;/strong&gt; and the real ORM's behavior stops being magic. It's just "store instructions, clone to stay lazy, execute once when consumed."&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Your Turn
&lt;/h2&gt;

&lt;p&gt;The best way to make this stick: paste the 50-line version into a file, &lt;strong&gt;add an &lt;code&gt;order_by&lt;/code&gt; method yourself&lt;/strong&gt; (hint: it also just appends to the recipe), and watch it slot right in. Understanding beats memorizing, and building beats reading.&lt;/p&gt;

&lt;p&gt;Got your &lt;code&gt;order_by&lt;/code&gt; working? Share your version in the comments 👇&lt;/p&gt;




&lt;p&gt;&lt;em&gt;If this was useful, follow me here for more stories from the backend trenches, and subscribe to &lt;a href="https://thebackendtechlead.substack.com/" rel="noopener noreferrer"&gt;The Backend Tech Lead&lt;/a&gt; on Substack for weekly blueprints.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>tutorial</category>
      <category>orm</category>
    </item>
    <item>
      <title>Six Things Django Developers Say That Aren't True in 2026</title>
      <dc:creator>Anas Issath</dc:creator>
      <pubDate>Thu, 05 Nov 2026 11:40:53 +0000</pubDate>
      <link>https://dev.to/anas_issath/six-things-django-developers-say-that-arent-true-in-2026-oan</link>
      <guid>https://dev.to/anas_issath/six-things-django-developers-say-that-arent-true-in-2026-oan</guid>
      <description>&lt;p&gt;&lt;em&gt;The recycled criticisms that sound smart, get repeated everywhere, and quietly stopped being accurate years ago.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Every few months, someone declares Django dead.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"It's too slow." "It's not async." "It doesn't scale." "Everyone's on FastAPI now."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;These takes get repeated in Hacker News threads, Discord servers, and hallway conversations until they feel like common knowledge: the kind of thing you nod along to without checking.&lt;/p&gt;

&lt;p&gt;Here's the problem: &lt;strong&gt;most of them stopped being true years ago, and a few were never true to begin with.&lt;/strong&gt; Meanwhile, in 2026, Django quietly powers Instagram, Spotify, and Pinterest and serves millions of users for companies that never got the memo that it "doesn't scale."&lt;/p&gt;

&lt;p&gt;This isn't a "Django is perfect" article. It has real, honest limitations, and I'll point to them. It's a &lt;strong&gt;"let's check the claims against reality"&lt;/strong&gt; article. Because repeating an outdated criticism doesn't make you sound experienced; it makes you sound like you learned Django in 2015 and stopped paying attention.&lt;/p&gt;

&lt;p&gt;Let's go through the six most common ones, concede what's true in each, and correct what isn't.&lt;/p&gt;




&lt;h2&gt;
  
  
  Myth 1: "Django Doesn't Scale"
&lt;/h2&gt;

&lt;p&gt;This is the big one, and it's the easiest to debunk because the counterexamples are enormous and public. Instagram runs on Django at hundreds of millions of users. Disqus scaled Django to serve billions of comments. Spotify and Pinterest are on it. If Django "didn't scale," these companies would have been the first to find out, at the worst possible time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The grain of truth:&lt;/strong&gt; a naive Django app &lt;em&gt;does&lt;/em&gt; fall over under load, but not because of Django. It falls over because of N+1 queries, missing database indexes, blocking calls in the request cycle, and no caching. Those are architecture and code problems, and they'd sink an app in any framework.&lt;/p&gt;

&lt;p&gt;As one 2026 scaling write-up put it bluntly: scaling without measurement is superstition, and the real bottleneck is almost never the framework. It's poor query design, missing indexes, and unclear ownership.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The reality:&lt;/strong&gt; Django is &lt;strong&gt;stateless by design&lt;/strong&gt;, which is exactly what makes it scale horizontally:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add more Gunicorn workers or more instances behind a load balancer&lt;/li&gt;
&lt;li&gt;Cache expensive queries in Redis&lt;/li&gt;
&lt;li&gt;Push slow work to Celery&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do that, and it scales to the moon. The "doesn't scale" argument is from 2015, and it was wrong then too. &lt;strong&gt;The framework was never the ceiling.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Myth 2: "Django Isn't Async, So It's Slow"
&lt;/h2&gt;

&lt;p&gt;This one sounds technical, which is why it spreads. And it contains a real fact wrapped around a wrong conclusion.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The grain of truth:&lt;/strong&gt; Django came to async late (async views in 3.1, ASGI support, and async ORM operations since 4.1), and its async story is genuinely still maturing. The ORM has sync roots, and there are sharp edges. If you're building something truly async-first with heavy concurrent I/O, that friction is real. (I've written about it. It's a legitimate pain point.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where the logic breaks:&lt;/strong&gt; "not async" does not mean "slow." Here's the fact the benchmarks don't advertise: &lt;strong&gt;most web applications are I/O-bound, not CPU-bound&lt;/strong&gt;, and the bottleneck isn't the framework. It's your database queries, third-party API calls, and unoptimized code.&lt;/p&gt;

&lt;p&gt;Async helps when you're I/O-bound &lt;em&gt;and&lt;/em&gt; juggling many concurrent slow operations (external APIs, websockets, streaming). It does not magically make a normal CRUD endpoint faster. A well-architected Django app with proper caching, indexing, and query optimization will outperform a poorly written async app every single time.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Async is a tool for a specific shape of problem, not a global speed setting&lt;/strong&gt;, and most apps don't have that shape.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Myth 3: "Django Can't Do APIs. Use FastAPI."
&lt;/h2&gt;

&lt;p&gt;Django "can't do APIs" would be news to the thousands of production APIs running on &lt;strong&gt;Django REST Framework&lt;/strong&gt;, which has been a mature, capable API toolkit for over a decade.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The grain of truth:&lt;/strong&gt; for a pure, async-first, high-concurrency API microservice, FastAPI is often the better fit in 2026. It's async from the ground up, its Pydantic-based validation is lovely, and it benchmarks faster than DRF on async workloads. That's a real and fair distinction, and if that's your exact use case, reach for it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The reality:&lt;/strong&gt; &lt;em&gt;"Django can't do APIs"&lt;/em&gt; and &lt;em&gt;"FastAPI is better for some API workloads"&lt;/em&gt; are very different claims. Django does APIs extremely well, and in 2026 you're not even limited to DRF. &lt;strong&gt;Django Ninja&lt;/strong&gt; brings FastAPI's exact ergonomics (type hints, Pydantic v2, auto-generated docs, native async) into Django, so you can have the modern API feel without giving up the ORM, admin, auth, and migrations.&lt;/p&gt;

&lt;p&gt;The honest framing the community has settled on:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Django for full products and admin-heavy SaaS, FastAPI for API-first microservices.&lt;/strong&gt; Both are correct choices, not a winner and a loser.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;"Can't" is just wrong.&lt;/p&gt;




&lt;h2&gt;
  
  
  Myth 4: "Django Can't Do Microservices. It's a Monolith."
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The grain of truth:&lt;/strong&gt; Django encourages a monolith, and its batteries-included design is optimized for building one cohesive application. That's a real default.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why the conclusion is wrong:&lt;/strong&gt; "encourages a monolith" is not "can't do microservices." You can absolutely build decoupled Django services that talk over APIs and message queues, with DRF, Docker, and Celery. Django is not the obstacle; the architecture is.&lt;/p&gt;

&lt;p&gt;And here's the part that actually matters in 2026: &lt;strong&gt;most applications don't need microservices in the first place.&lt;/strong&gt; Splitting prematurely buys you network complexity, debugging pain, and operational overhead in exchange for benefits you may never use.&lt;/p&gt;

&lt;p&gt;The guidance from people running Django at scale is to &lt;strong&gt;start as a modular monolith and stay one&lt;/strong&gt; until you have a concrete reason to split:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Teams blocked by codebase coupling&lt;/li&gt;
&lt;li&gt;Deployment bottlenecks&lt;/li&gt;
&lt;li&gt;A domain that needs radically different scaling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A well-designed modular monolith survives astonishing scale. &lt;em&gt;"Django is a monolith"&lt;/em&gt; is a description, not an insult, and often it's describing the right choice.&lt;/p&gt;




&lt;h2&gt;
  
  
  Myth 5: "Batteries-Included Makes Django Bloated and Slow"
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The grain of truth:&lt;/strong&gt; Django ships a lot, and it does carry two decades of history, including some dated modules you'll never use. If your project is genuinely tiny (a single-endpoint service), Django is more than you need, and a microframework fits better.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why it's mostly wrong:&lt;/strong&gt; people call Django "too heavy" until they spend three days wiring up authentication, permissions, migrations, an admin interface, and CSRF protection in a lighter framework. Django gives you all of that on day one, tested and integrated.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"Batteries included is a feature, not a liability"&lt;/em&gt; is the take that holds up. And &lt;strong&gt;bloat doesn't equal slow: the modules you don't use don't run.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Django's security posture alone earns its weight:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PBKDF2/bcrypt password hashing&lt;/li&gt;
&lt;li&gt;CSRF and XSS protections&lt;/li&gt;
&lt;li&gt;SQL-injection-safe ORM queries&lt;/li&gt;
&lt;li&gt;A framework that actively refuses to start with a dangerously weak config&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's not bloat; that's a mountain of correct decisions you didn't have to make or maintain yourself.&lt;/p&gt;




&lt;h2&gt;
  
  
  Myth 6: "Real Developers Hand-Write SQL. The ORM Is for Beginners."
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The grain of truth:&lt;/strong&gt; the ORM &lt;em&gt;can&lt;/em&gt; generate inefficient SQL, it hides the database in ways that cause N+1 problems, and for genuinely complex analytical queries, raw SQL is sometimes the right and clearer tool. A good Django developer can read the SQL the ORM produces and drops to raw SQL when it's warranted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why the "for beginners" framing is backwards:&lt;/strong&gt; the ORM isn't training wheels you outgrow. It's a productivity and safety tool that experts use deliberately. It gives you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Parameterized queries that prevent SQL injection by default&lt;/li&gt;
&lt;li&gt;Database-agnostic code&lt;/li&gt;
&lt;li&gt;Migrations&lt;/li&gt;
&lt;li&gt;Readable query composition&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The mark of an experienced Django developer isn't refusing the ORM. It's &lt;strong&gt;using it fluently for the 95% of queries where it's clearer and safer, and knowing exactly when to reach past it for the other 5%.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Treating &lt;em&gt;"I hand-write all my SQL"&lt;/em&gt; as a badge of seniority usually signals the opposite: someone who hasn't learned to use the ORM well, or who's reintroducing injection risks and database lock-in for no benefit. Use the right tool; the ORM is right far more often than the myth admits.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Pattern Underneath All Six
&lt;/h2&gt;

&lt;p&gt;Step back and notice something: nearly every one of these myths collapses into the same underlying truth.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The bottleneck is almost never Django. It's the architecture, the queries, and the code.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;The myth says...&lt;/th&gt;
&lt;th&gt;What's actually happening&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;"Doesn't scale"&lt;/td&gt;
&lt;td&gt;Your N+1 queries don't scale&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;"Too slow"&lt;/td&gt;
&lt;td&gt;Your missing indexes are slow&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;"Can't do APIs/microservices"&lt;/td&gt;
&lt;td&gt;Your design choices, not the framework&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Over and over, people blame the tool for what is, honestly, a design or discipline problem, because blaming the framework is easier than auditing your own queries. Django is a mirror here: a well-architected Django app is fast and scales enormously; a poorly-architected one falls over, exactly as a poorly-architected app in any framework would.&lt;/p&gt;

&lt;p&gt;None of this means Django is the right choice for everything. It genuinely isn't. If you're building a high-concurrency, async-first, real-time API service, FastAPI or an async-native stack is often the better call, and Django's async maturity is a real, honest limitation. That's a legitimate reason to pick something else.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"Django is slow / dead / doesn't scale / can't do APIs"&lt;/em&gt; is not a legitimate reason. It's a 2015 opinion that reality stopped supporting years ago.&lt;/p&gt;

&lt;p&gt;So the useful move in 2026 isn't to defend Django or bury it. It's to &lt;strong&gt;stop repeating criticisms you haven't checked&lt;/strong&gt;, evaluate the actual workload in front of you, and pick the tool that fits it, knowing that for a huge range of real products, the boring, batteries-included, "doesn't scale" framework is quietly running some of the largest applications on the internet.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Repeating the myth is easy. Checking it is what separates an engineer from a parrot.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  The 30-Second Takeaway
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;"Doesn't scale":&lt;/strong&gt; Instagram, Disqus, Pinterest, Spotify run on Django at massive scale. It's stateless and scales horizontally; what fails is N+1 queries and missing indexes, not the framework.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"Not async, so slow":&lt;/strong&gt; most apps are I/O-bound; async only helps with many concurrent slow I/O operations, and a well-optimized sync app beats a badly-written async one. (Django's async maturity &lt;em&gt;is&lt;/em&gt; a real limitation for async-first workloads, though.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"Can't do APIs":&lt;/strong&gt; DRF has done APIs well for a decade, and Django Ninja now offers FastAPI-style typed, async, auto-documented APIs inside Django. FastAPI is better for pure async microservices; that's a fit question, not a "can't."&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"Can't do microservices / it's a monolith":&lt;/strong&gt; you &lt;em&gt;can&lt;/em&gt; build Django services, but more importantly most apps &lt;em&gt;shouldn't&lt;/em&gt;. A modular monolith survives huge scale, and premature splitting adds cost for no benefit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"Batteries-included = bloated/slow":&lt;/strong&gt; unused modules don't run; the built-in auth, admin, migrations, and security (PBKDF2, CSRF, injection-safe ORM) are a feature that saves you weeks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"The ORM is for beginners":&lt;/strong&gt; experts use it deliberately for safety (parameterized queries) and speed of development, and drop to raw SQL only for the rare query that needs it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The through-line:&lt;/strong&gt; the bottleneck is almost never Django. It's your architecture and your queries.&lt;/p&gt;




&lt;h2&gt;
  
  
  Your Turn
&lt;/h2&gt;

&lt;p&gt;Pick your tools by fit, not by folklore. Django isn't right for everything, but &lt;em&gt;"it doesn't scale"&lt;/em&gt; hasn't been a real reason since before half the people repeating it started coding.&lt;/p&gt;

&lt;p&gt;Which Django myth do you hear most often? Or is there one on this list you think is actually still true? Tell me in the comments 👇&lt;/p&gt;




&lt;p&gt;&lt;em&gt;If this was useful, follow me here for more stories from the backend trenches, and subscribe to &lt;a href="https://thebackendtechlead.substack.com/" rel="noopener noreferrer"&gt;The Backend Tech Lead&lt;/a&gt; on Substack for weekly blueprints.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>webdev</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Adding a Database Server Doubled Capacity. One Index Multiplied It by 2,600.</title>
      <dc:creator>Anas Issath</dc:creator>
      <pubDate>Wed, 04 Nov 2026 19:20:38 +0000</pubDate>
      <link>https://dev.to/anas_issath/adding-a-database-server-doubled-capacity-one-index-multiplied-it-by-2600-437g</link>
      <guid>https://dev.to/anas_issath/adding-a-database-server-doubled-capacity-one-index-multiplied-it-by-2600-437g</guid>
      <description>&lt;p&gt;&lt;em&gt;Measure first, then climb the ladder.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The API is slow.&lt;/p&gt;

&lt;p&gt;The dashboard takes two seconds to load. Someone opens the database graph and sees CPU at 100%.&lt;/p&gt;

&lt;p&gt;Then the meeting starts.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"We need a read replica."&lt;br&gt;
"Should we add Redis?"&lt;br&gt;
"Maybe it's time to shard."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;All three sound like serious engineering. All three cost money, add moving parts, and stay with you for years.&lt;/p&gt;

&lt;p&gt;But nobody in that meeting has asked the first question: &lt;strong&gt;what is the database actually doing?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Nikki Siapno's recent Level Up Coding piece on scaling databases lays out a sensible order: measure the bottleneck, reduce the work, optimize queries, then add replicas, and shard last.&lt;/p&gt;

&lt;p&gt;So this article puts that order to the test. One table, one slow query, one load test, and real measurements at each step.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Test
&lt;/h2&gt;

&lt;p&gt;The setup is a small PostgreSQL lab that you can run yourself. The full script is at the end of this article.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PostgreSQL 16.15 on a 2-core Linux cloud machine&lt;/li&gt;
&lt;li&gt;An &lt;code&gt;orders&lt;/code&gt; table with &lt;strong&gt;5 million rows&lt;/strong&gt; and 100,000 customers&lt;/li&gt;
&lt;li&gt;One common query: the 20 latest orders for a customer&lt;/li&gt;
&lt;li&gt;8 clients sending that query as fast as they can for 30 seconds&lt;/li&gt;
&lt;li&gt;Each step repeated 3 times, reporting the median&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is the query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4242&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It looks harmless. You probably have one like it in your code right now.&lt;/p&gt;

&lt;p&gt;To keep the comparison fair, the primary database was pinned to one CPU core. The second core ran the load generator, or the replica in the replica test.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Results
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fy02blq4fa1diohx6h0z8.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fy02blq4fa1diohx6h0z8.webp" alt=" " width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Read that table twice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The replica worked.&lt;/strong&gt; Capacity went up about 2.2 times, which is close to what a second core should give.&lt;/p&gt;

&lt;p&gt;But &lt;strong&gt;one &lt;code&gt;CREATE INDEX&lt;/code&gt; on the original server handled about 2,600 times the traffic.&lt;/strong&gt; It beat the two-server setup by about 1,180 times.&lt;/p&gt;

&lt;p&gt;The index took 4.8 seconds to build and used 150 MB of disk.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1: Measure the Bottleneck
&lt;/h2&gt;

&lt;p&gt;Before touching anything, ask PostgreSQL where the time goes.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;pg_stat_statements&lt;/code&gt; extension records every query shape, how often it runs, and how long it takes. It must be added to &lt;code&gt;shared_preload_libraries&lt;/code&gt;, which needs a restart.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;calls&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mean_exec_time&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;numeric&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;avg_ms&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total_exec_time&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;numeric&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total_seconds&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="k"&gt;left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_stat_statements&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;total_exec_time&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Sort by total time, not average time.&lt;/strong&gt; A 5 ms query that runs a million times hurts more than a 2 second report that runs once a day.&lt;/p&gt;

&lt;p&gt;Next, look at the plan of your worst query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4242&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the real output from the test, shortened:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Limit (actual time=224.516..224.520 rows=20)
  Buffers: shared hit=66670
  -&amp;gt; Sort
       -&amp;gt; Seq Scan on orders
            Filter: (customer_id = 4242)
            Rows Removed by Filter: 4999944
Execution Time: 245.630 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the whole problem in one line: &lt;strong&gt;&lt;code&gt;Rows Removed by Filter: 4999944&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To find 56 rows for one customer, PostgreSQL read all 5 million. Think of a librarian who walks past every book in the building each time someone asks for one author.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;No amount of extra hardware changes what this query is doing.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Different symptoms point to different problems:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What you see&lt;/th&gt;
&lt;th&gt;Look at first&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;High CPU, slow queries&lt;/td&gt;
&lt;td&gt;Query plans, missing indexes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Many rows read, few returned&lt;/td&gt;
&lt;td&gt;Indexes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Same query running repeatedly&lt;/td&gt;
&lt;td&gt;N+1 queries, caching&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Too many connections&lt;/td&gt;
&lt;td&gt;Connection pooling&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Slow only at peak, plans are ok&lt;/td&gt;
&lt;td&gt;Hardware or replicas&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fast database, slow API&lt;/td&gt;
&lt;td&gt;The app or another service&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Slow writes, heavy disk use&lt;/td&gt;
&lt;td&gt;Write volume, too many indexes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;These two queries help with the first pass:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Tables being read the hard way&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;relname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;seq_scan&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;seq_tup_read&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;idx_scan&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;seq_tup_read&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="k"&gt;NULLIF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seq_scan&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;rows_per_scan&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_stat_user_tables&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;seq_tup_read&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Connection pressure&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'active'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;active&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="k"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'idle in transaction'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;idle_in_txn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="k"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;current_setting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'max_connections'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;max&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_stat_activity&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;backend_type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'client backend'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 2: The Replica Added Capacity, Not Speed
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fehzdp1yhh62p6tu752m1.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fehzdp1yhh62p6tu752m1.webp" alt=" " width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the step many teams take first, so it deserves a fair look.&lt;/p&gt;

&lt;p&gt;With a replica, the same 8 clients were split between two servers. Throughput doubled and average latency fell from 2,020 ms to 926 ms.&lt;/p&gt;

&lt;p&gt;That looks like a fix. &lt;strong&gt;It isn't.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With no load, the query took 246 ms on the primary and 304 ms on the replica. The plan on the replica was identical: same sequential scan, same 4,999,944 rows thrown away.&lt;/p&gt;

&lt;p&gt;So where did the latency gain come from?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Queueing.&lt;/strong&gt; With 8 clients on one core, each request spent most of its time waiting behind other requests. A second server shortened the line. The checkout was just as slow.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A replica shortens the queue. It does not make the work smaller.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The replica also brings bills of its own: another server to pay for and monitor, and one more problem that the next section measures.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 3: The Replica Problem Nobody Sees in Testing
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvh5df38vc4mh5gnrbb3i.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvh5df38vc4mh5gnrbb3i.webp" alt=" " width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A read replica copies changes from the primary a moment after they commit. That moment is called &lt;strong&gt;replication lag&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here is the test. Insert a row on the primary, wait for the commit, then read it from the replica straight away. Do that 2,000 times.&lt;/p&gt;

&lt;p&gt;This was the best possible case: both servers on the same machine, no network between them, and almost no load.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Idle&lt;/th&gt;
&lt;th&gt;Under write load&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Writes tested&lt;/td&gt;
&lt;td&gt;2,000&lt;/td&gt;
&lt;td&gt;2,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Row missing on first read&lt;/td&gt;
&lt;td&gt;1,973&lt;/td&gt;
&lt;td&gt;1,473&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Median catch-up&lt;/td&gt;
&lt;td&gt;0.33 ms&lt;/td&gt;
&lt;td&gt;0.53 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Worst catch-up&lt;/td&gt;
&lt;td&gt;18.44 ms&lt;/td&gt;
&lt;td&gt;41.12 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;98.65% of immediate reads missed the row on an idle system.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The replica was only 0.33 ms behind. That was still slower than the next read.&lt;/p&gt;

&lt;p&gt;Don't read too much into the idle and load numbers being in that order. The point is that &lt;em&gt;"very low lag"&lt;/em&gt; and &lt;em&gt;"no stale reads"&lt;/em&gt; are different things. Across a real network, lag will be higher.&lt;/p&gt;

&lt;p&gt;Your users meet this as a strange bug:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A user updates their profile.&lt;/li&gt;
&lt;li&gt;The app redirects to the profile page.&lt;/li&gt;
&lt;li&gt;The page shows the old name.&lt;/li&gt;
&lt;li&gt;The user refreshes, and now it's correct.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The fix is to &lt;strong&gt;read from the primary right after a write&lt;/strong&gt;. Here is a simple Django router that does this for the rest of a request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;threading&lt;/span&gt;

&lt;span class="n"&gt;_state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;threading&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;local&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PrimaryAfterWriteRouter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;db_for_read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;hints&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;default&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;getattr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;wrote&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;replica&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;db_for_write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;hints&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wrote&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;default&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;allow_relation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;obj1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;obj2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;hints&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;allow_migrate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;app_label&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;hints&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;default&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;primary_after_write_middleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get_response&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;middleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wrote&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;get_response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wrote&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;middleware&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the class to &lt;code&gt;DATABASE_ROUTERS&lt;/code&gt; and the middleware to &lt;code&gt;MIDDLEWARE&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This covers one request. In the profile example, the redirect is a &lt;em&gt;second&lt;/em&gt; request, so you also need something that carries over, such as a short-lived cookie or session flag that keeps that user on the primary for a few seconds.&lt;/p&gt;

&lt;p&gt;Also keep money reads on the primary: &lt;strong&gt;balances, stock counts, permissions, and anything a decision depends on.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The Failure This Lab Hit by Accident
&lt;/h2&gt;

&lt;p&gt;The first run of this test never finished.&lt;/p&gt;

&lt;p&gt;During the write-heavy stage, the replica fell behind. PostgreSQL's primary keeps only a limited amount of old WAL (its change log), and it recycled the segment the replica still needed. The replica could never catch up, and the primary's log filled with this line every few seconds:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ERROR: requested WAL segment 00000001000000000000004C has already been removed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing crashed. The replica stayed up and kept answering queries, while its data froze in the past. The test sat waiting for a row that would never arrive until it was stopped.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;This is the scariest kind of failure: a healthy-looking server serving old data.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The fix was a &lt;strong&gt;replication slot&lt;/strong&gt;, which makes the primary keep WAL until the replica has received it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pg_basebackup &lt;span class="nt"&gt;-h&lt;/span&gt; primary &lt;span class="nt"&gt;-D&lt;/span&gt; /data/replica &lt;span class="nt"&gt;-R&lt;/span&gt; &lt;span class="nt"&gt;-X&lt;/span&gt; stream &lt;span class="nt"&gt;-c&lt;/span&gt; fast &lt;span class="nt"&gt;-C&lt;/span&gt; &lt;span class="nt"&gt;-S&lt;/span&gt; lab_slot
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Slots have their own danger. If a replica dies and the slot stays, the primary keeps WAL forever and can fill its disk. Set &lt;code&gt;max_slot_wal_keep_size&lt;/code&gt; as a limit, and watch both of these:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;slot_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;active&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;pg_size_pretty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pg_wal_lsn_diff&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pg_current_wal_lsn&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;restart_lsn&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;wal_held&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_replication_slots&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;application_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;pg_size_pretty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pg_wal_lsn_diff&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pg_current_wal_lsn&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;replay_lsn&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;behind&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;replay_lag&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_stat_replication&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If a replica is missing from the second query, it has stopped replicating. &lt;strong&gt;Alert on that.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 4: Cache Only What Repeats
&lt;/h2&gt;

&lt;p&gt;Redis is the next suggestion in most of these meetings, so the lab tested it on the slow, unindexed query.&lt;/p&gt;

&lt;p&gt;The code is the standard cache-aside pattern: check Redis, and on a miss, query PostgreSQL and store the result for 5 minutes. One client ran for 60 seconds with three traffic patterns:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Traffic pattern&lt;/th&gt;
&lt;th&gt;Hit rate&lt;/th&gt;
&lt;th&gt;Average&lt;/th&gt;
&lt;th&gt;Median&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;All customers equally&lt;/td&gt;
&lt;td&gt;0.4%&lt;/td&gt;
&lt;td&gt;218 ms&lt;/td&gt;
&lt;td&gt;210 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;90% from 1,000 customers&lt;/td&gt;
&lt;td&gt;9.8%&lt;/td&gt;
&lt;td&gt;197 ms&lt;/td&gt;
&lt;td&gt;209 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;90% from 100 customers&lt;/td&gt;
&lt;td&gt;84.7%&lt;/td&gt;
&lt;td&gt;33 ms&lt;/td&gt;
&lt;td&gt;0.08 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The third row is what people imagine when they say &lt;em&gt;"add a cache."&lt;/em&gt; The first two rows are what can happen instead.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The cache did not fail. The traffic did not repeat enough.&lt;/strong&gt; With 100,000 customers and about 5 slow queries a second, most keys were requested once and never seen again before the test ended.&lt;/p&gt;

&lt;p&gt;Even in the best case, the misses stayed painful. The 95th percentile was still 219 ms, because every miss ran the same full table scan.&lt;/p&gt;

&lt;p&gt;Compare that with the plain index: under 1 ms on average, even with 8 clients at full speed, with no hit rate to worry about and no invalidation to get wrong.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A cache makes a good query cheaper. It is a poor way to hide a bad one.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So before you add one, ask these questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do the same keys get read again and again?&lt;/li&gt;
&lt;li&gt;How stale can this data be?&lt;/li&gt;
&lt;li&gt;Who invalidates it when the data changes?&lt;/li&gt;
&lt;li&gt;What happens to the database if Redis restarts and every request misses?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That last one is a real risk. OpenAI's write-up, covered below, describes using cache locking and leasing so that only one reader fetches a missed key from PostgreSQL.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 5: The Index, and Its Bill
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjxqb3ip27khm9s5gic1w.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjxqb3ip27khm9s5gic1w.webp" alt=" " width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the plan after one index:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;CONCURRENTLY&lt;/span&gt; &lt;span class="n"&gt;orders_customer_created&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Limit (actual time=0.034..0.205 rows=20)
  Buffers: shared hit=23
  -&amp;gt; Index Scan using orders_customer_created on orders
       Index Cond: (customer_id = 4242)
Execution Time: 0.228 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;From 66,670 buffers to 23. From 245.6 ms to 0.23 ms.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The column order matters. &lt;code&gt;customer_id&lt;/code&gt; narrows down to one customer, and &lt;code&gt;created_at DESC&lt;/code&gt; means the rows are already sorted, so PostgreSQL can stop after 20. The sort step disappears.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;CONCURRENTLY&lt;/code&gt; on a live table. A plain &lt;code&gt;CREATE INDEX&lt;/code&gt; blocks writes while it builds. In Django, the safe version looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.contrib.postgres.operations&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;AddIndexConcurrently&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.db&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;migrations&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Migration&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;migrations&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Migration&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;atomic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;

    &lt;span class="n"&gt;dependencies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;orders&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;0041_previous&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;

    &lt;span class="n"&gt;operations&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nc"&gt;AddIndexConcurrently&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;model_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;order&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;customer_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-created_at&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
                &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;orders_customer_created&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If a concurrent build fails, it leaves an invalid index behind. Drop it and try again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Indexes are not free, though.&lt;/strong&gt; The lab measured insert speed on a 1 million row table with no indexes, 1 index, and 5 indexes:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Indexes&lt;/th&gt;
&lt;th&gt;Inserts/sec&lt;/th&gt;
&lt;th&gt;Index size&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;6,524&lt;/td&gt;
&lt;td&gt;0 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;5,879&lt;/td&gt;
&lt;td&gt;17 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;5,178&lt;/td&gt;
&lt;td&gt;131 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Five indexes cost about 21% of write speed on this test. These runs were noisy, and the ranges for 0 and 1 index overlapped, so treat the trend as the finding, not the exact percentage.&lt;/p&gt;

&lt;p&gt;The cost grows with every index you never use. This query finds the candidates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;relname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexrelname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;idx_scan&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;pg_size_pretty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pg_relation_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexrelid&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;size&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_stat_user_indexes&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;pg_index&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexrelid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexrelid&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;idx_scan&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indisunique&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indisprimary&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;pg_relation_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexrelid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before you drop one, remember these counts are &lt;strong&gt;per server&lt;/strong&gt;. An index with no scans on the primary may be busy on a replica.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 6: Shard Last, and Mean It
&lt;/h2&gt;

&lt;p&gt;This lab did not test sharding. It would need several machines to say anything honest. So here are two real teams that lived at the top of the ladder.&lt;/p&gt;

&lt;h3&gt;
  
  
  Case study: Figma
&lt;/h3&gt;

&lt;p&gt;Figma's database team describes a stack that grew almost 100x since 2020.&lt;/p&gt;

&lt;p&gt;They climbed in order. In 2020 they ran a single Postgres database on AWS's largest physical instance. By the end of 2022 they had caching, read replicas, and about a dozen vertically partitioned databases, with related tables split into their own groups.&lt;/p&gt;

&lt;p&gt;Only then did they hit real limits. Some tables reached several terabytes and billions of rows, and the highest-write tables neared the IOPS limit on RDS.&lt;/p&gt;

&lt;p&gt;Horizontal sharding started in late 2022. The first table took roughly nine months and shipped in September 2023. Figma called the work "an order of magnitude more complex" than their earlier scaling, and listed what it gave up: cross-shard transactions, foreign keys, and globally unique indexes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Case study: OpenAI
&lt;/h3&gt;

&lt;p&gt;OpenAI's January 2026 post describes serving 800 million ChatGPT users on PostgreSQL, with one primary handling writes and nearly 50 read replicas.&lt;/p&gt;

&lt;p&gt;They did not shard their PostgreSQL deployment. Their reasons: sharding would mean changing hundreds of endpoints and could take months or years, and the workload is mainly reads.&lt;/p&gt;

&lt;p&gt;But they were not doing nothing. They optimized queries, reviewed ORM-generated SQL, used PgBouncer, put a cache in front, and moved shardable, write-heavy workloads to systems such as Azure Cosmos DB.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note what both stories share.&lt;/strong&gt; Neither team reached for the biggest tool first, and both moved based on a specific limit they had measured.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Signs you might really need sharding
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A write bottleneck on the primary that remains after query and index work&lt;/li&gt;
&lt;li&gt;A single table so large that maintenance like vacuum causes reliability problems&lt;/li&gt;
&lt;li&gt;Storage or IOPS limits on the largest machine you can buy&lt;/li&gt;
&lt;li&gt;Isolating noisy tables into their own database has already been done&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Replicas will not help with any of these writes, because every replica has to apply every write too.&lt;/p&gt;

&lt;p&gt;And there is a smaller step that people skip. &lt;strong&gt;PostgreSQL partitioning&lt;/strong&gt; splits a big table into pieces on the same server, which helps with time-based data and deleting old rows, with no routing layer at all.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Ladder
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fflh0rtxc4r6bihukh4d5.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fflh0rtxc4r6bihukh4d5.webp" alt=" " width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the order, with the question that earns each step:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Step&lt;/th&gt;
&lt;th&gt;Question that earns it&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1. Measure&lt;/td&gt;
&lt;td&gt;What exactly is slow?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2. Do less&lt;/td&gt;
&lt;td&gt;Do we need this query at all?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3. Fix queries&lt;/td&gt;
&lt;td&gt;Is it reading more rows than it returns?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4. Pool&lt;/td&gt;
&lt;td&gt;Are connections the limit?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5. Cache&lt;/td&gt;
&lt;td&gt;Do the same reads repeat, and can they be stale?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6. Scale up&lt;/td&gt;
&lt;td&gt;Is a healthy query simply short of hardware?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7. Replicas&lt;/td&gt;
&lt;td&gt;Are reads the limit, and can they lag?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8. Partition&lt;/td&gt;
&lt;td&gt;Is one table too big to manage?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9. Shard&lt;/td&gt;
&lt;td&gt;Are writes or data size beyond one primary?&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;You can stop at any step. Most systems should.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  What This Test Does Not Prove
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;2,600x is not a promise.&lt;/strong&gt; This query was chosen because it had no index on a big table. It is a best case for indexing. If your query already uses a good index, you will see nothing like this.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The replica test was a clean case.&lt;/strong&gt; One extra core, no network, and only reads. Real replicas also have to replay writes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One query is not a workload.&lt;/strong&gt; Real systems have many query shapes competing for the same resources.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The numbers depend on the machine.&lt;/strong&gt; This was a small shared cloud machine, and baseline runs ranged from 3.7 to 5.5 requests per second. Trust the size of the gaps, not the decimals.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cold caches matter.&lt;/strong&gt; The replica's 304 ms included reading pages into memory for the first time, while the primary's data was already cached.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What To Do on Monday
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Turn on &lt;code&gt;pg_stat_statements&lt;/code&gt;&lt;/strong&gt; if it's off, and find your top 10 queries by total time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Run &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt;&lt;/strong&gt; on each one. Look for a high &lt;code&gt;Rows Removed by Filter&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check for N+1 queries.&lt;/strong&gt; In Django, &lt;code&gt;select_related&lt;/code&gt; and &lt;code&gt;prefetch_related&lt;/code&gt; can remove whole groups of queries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Add indexes from real query patterns&lt;/strong&gt;, using &lt;code&gt;CONCURRENTLY&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Drop indexes nobody uses&lt;/strong&gt;, after checking every replica.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Before any new infrastructure, write down the number it should change.&lt;/strong&gt; &lt;em&gt;"Replica will take p95 from X to Y."&lt;/em&gt; If nobody can fill in that sentence, the measuring isn't finished.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If you already have replicas&lt;/strong&gt;, alert on lag and on a replica going missing, and list which reads must stay on the primary.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Common Questions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Should I add an index for every slow query?&lt;/strong&gt;&lt;br&gt;
No. Start with queries that run often. An index for a report that runs once a month may cost more in write overhead than it saves.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is high CPU always a reason to scale up?&lt;/strong&gt;&lt;br&gt;
No. High CPU tells you the database is working hard. The query plan tells you whether that work is necessary. In this test, a missing index was creating almost all of it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can a replica ever beat an index?&lt;/strong&gt;&lt;br&gt;
Yes, when queries are already efficient and you simply have too many of them. That is exactly when replicas earn their cost.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When should I cache instead of optimizing?&lt;/strong&gt;&lt;br&gt;
When a query is already good but runs constantly on data that rarely changes. A homepage product list is a classic example.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do managed databases solve this?&lt;/strong&gt;&lt;br&gt;
They help with backups, replicas and failover. They will not fix a sequential scan on 5 million rows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Are ORMs the problem?&lt;/strong&gt;&lt;br&gt;
Not by themselves. But they make it easy to run queries you never see. OpenAI's post specifically calls out reviewing ORM-generated SQL.&lt;/p&gt;


&lt;h2&gt;
  
  
  Run It Yourself
&lt;/h2&gt;

&lt;p&gt;Run this on a throwaway machine or container with at least 2 cores, and allow about 15 minutes. It creates and deletes its own data in &lt;code&gt;/tmp/scaling-lab&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-it&lt;/span&gt; &lt;span class="nt"&gt;--rm&lt;/span&gt; &lt;span class="nt"&gt;--cpus&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;2 ubuntu:24.04 bash

&lt;span class="c"&gt;# inside the container&lt;/span&gt;
apt-get update
apt-get &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; postgresql redis-server python3-psycopg2 python3-redis util-linux
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save the script below as &lt;code&gt;/tmp/scaling_lab.py&lt;/code&gt;, then run it as the &lt;code&gt;postgres&lt;/code&gt; user:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;chmod &lt;/span&gt;a+r /tmp/scaling_lab.py
su postgres &lt;span class="nt"&gt;-s&lt;/span&gt; /bin/bash &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="s1"&gt;'cd /tmp &amp;amp;&amp;amp; PATH=/usr/lib/postgresql/16/bin:$PATH python3 /tmp/scaling_lab.py'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The results are saved to &lt;code&gt;lab_results.json&lt;/code&gt;. Change one thing at a time: try 20 million rows, a hotter or colder cache pattern, or a real replica on a second machine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Click to expand the full lab script (scaling_lab.py)&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/usr/bin/env python3
&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Database scaling lab: test each step of the scaling ladder on one workload.

Stages: measure, read replica, cache, index, index write cost, stale reads.
Needs PostgreSQL 16 with contrib, Redis, psycopg2, redis-py and 2 CPU cores.
Run as a non-root user with the PostgreSQL bin directory on PATH.
Use a throwaway machine. It creates and deletes its own data in BASE.
&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;shutil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;statistics&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;redis&lt;/span&gt;

&lt;span class="n"&gt;BASE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/tmp/scaling-lab&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;P&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;R&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;RP&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;55432&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;55433&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;56379&lt;/span&gt;
&lt;span class="n"&gt;ROWS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;CUSTOMERS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;RUN_S&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;REPEATS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5_000_000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100_000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="n"&gt;READ&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT id, status, amount, created_at FROM orders &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;WHERE customer_id = %s ORDER BY created_at DESC LIMIT 20&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;core&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;core&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;cmd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;taskset -c &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;core&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;shell&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;capture_output&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;returncode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cmd&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;P&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dbname&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;postgres&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;autocommit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;P&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchall&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;description&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;plan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;P&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;EXPLAIN (ANALYZE, BUFFERS) &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;READ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;%s&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;4242&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;pgbench&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;clients&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;core&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;seconds&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;RUN_S&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;cmd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pgbench -n -h &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; -p &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; -f &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; -c &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;clients&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; -j 1 &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
           &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-T &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;seconds&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; postgres&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Popen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;taskset -c &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;core&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;shell&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PIPE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;STDOUT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;tps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;tps = ([\d.]+)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;group&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;lat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;latency average = ([\d.]+) ms&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;group&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;tps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lat&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;shutil&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rmtree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ignore_errors&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;makedirs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;sh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;initdb -D &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/p -A trust&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/p/postgresql.conf&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;port=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;P&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;unix_socket_directories=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;listen_addresses=&lt;/span&gt;&lt;span class="sh"&gt;''&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;shared_buffers=1GB&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;max_parallel_workers_per_gather=0&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;shared_preload_libraries=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;pg_stat_statements&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;wal_level=replica&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;max_wal_senders=5&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;hot_standby=on&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;max_connections=50&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/p/pg_hba.conf&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;local replication all trust&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;sh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pg_ctl -D &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/p -l &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/p.log -w start&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;core&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CREATE EXTENSION pg_stat_statements&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;CREATE TABLE orders(id bigserial PRIMARY KEY, customer_id int NOT NULL,
          status text NOT NULL, amount int NOT NULL,
          created_at timestamptz NOT NULL, note text NOT NULL)&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;INSERT INTO orders(customer_id, status, amount, created_at, note)
          SELECT (random()*&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;CUSTOMERS&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;)::int + 1,
                 (ARRAY[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;new&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;paid&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;shipped&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;done&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;])[1+(random()*3)::int],
                 (random()*50000)::int,
                 now() - random() * interval &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;365 days&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;, repeat(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;x&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;, 40)
          FROM generate_series(1, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;ROWS&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ANALYZE orders&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/read.sql&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;w&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;set cid random(1, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;CUSTOMERS&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;READ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;%s&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;:cid&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;;&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/write.sql&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;w&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;set cid random(1, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;CUSTOMERS&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="se"&gt;\n\\&lt;/span&gt;&lt;span class="s"&gt;set amt random(1, 50000)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INSERT INTO orders_w(customer_id,status,amount,created_at,note) &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;VALUES (:cid,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;new&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;,:amt,now(),repeat(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;x&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;,40));&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/load.sql&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;w&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;set cid random(1, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;CUSTOMERS&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="se"&gt;\n\\&lt;/span&gt;&lt;span class="s"&gt;set amt random(1, 50000)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INSERT INTO orders(customer_id,status,amount,created_at,note) &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;VALUES (:cid,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;new&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;,:amt,now(),repeat(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;x&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;,40));&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT count(*) FROM orders&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;table_mb&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT pg_total_relation_size(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;orders&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;)/1048576&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;start_replica&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;sh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pg_basebackup -h &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; -p &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;P&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; -D &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/r -R -X stream -c fast -C -S lab_slot&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/r/postgresql.conf&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;port=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;R&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;max_connections=50&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;sh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pg_ctl -D &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/r -l &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/r.log -w start&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;core&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT count(*) FROM pg_stat_replication&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;
        &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT count(*) FROM orders&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;R&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;stage_baseline&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT pg_stat_statements_reset()&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;REPEATS&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;pgbench&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;P&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/read.sql&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;communicate&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;baseline&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;top_query&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;SELECT calls, round(mean_exec_time::numeric,1),
        round(total_exec_time::numeric/1000,1), shared_blks_hit+shared_blks_read,
        left(query,70) FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 1&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;READ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;%s&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;4242&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;plan_noindex&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;plan&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;stage_replica&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;REPEATS&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;pgbench&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;P&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/read.sql&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;pgbench&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;R&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/read.sql&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;ra&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;communicate&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt; &lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;communicate&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
        &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;ra&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;rb&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ra&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;rb&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;replica&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;plan_replica&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;plan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;R&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;stage_cache&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sched_setaffinity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Redis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unix_socket_path&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/redis.sock&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;pg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cid&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;orders:&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;cid&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="n"&gt;hit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;hit&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
        &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;READ&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cid&lt;/span&gt;&lt;span class="p"&gt;,))&lt;/span&gt;
        &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchall&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pick&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;seconds&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flushall&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;times&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hits&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;monotonic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;seconds&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;monotonic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;lt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;perf_counter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;hits&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nf"&gt;get_orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;pick&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
            &lt;span class="n"&gt;times&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;perf_counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;times&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;times&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;hit_rate&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hits&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;times&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                    &lt;span class="n"&gt;mean_ms&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;times&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;p50_ms&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;times&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;times&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                    &lt;span class="n"&gt;p95_ms&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;times&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;times&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;95&lt;/span&gt;&lt;span class="p"&gt;)],&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cache_uniform&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;CUSTOMERS&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;lt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;
                        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;CUSTOMERS&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cache_hot_1000&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cache_hot_100&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;pg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sched_setaffinity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;stage_index&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;monotonic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CREATE INDEX orders_customer_created ON orders (customer_id, created_at DESC)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;index_build_s&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;monotonic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;index_mb&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT pg_relation_size(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;orders_customer_created&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;)/1048576&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;READ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;%s&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;4242&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;plan_index&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;plan&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;REPEATS&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;pgbench&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;P&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/read.sql&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;communicate&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;indexed&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;stage_writes&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;extra&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;customer_id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;created_at&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;amount&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;(customer_id, created_at DESC)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;DROP TABLE IF EXISTS orders_w&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CREATE TABLE orders_w (LIKE orders INCLUDING DEFAULTS)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ALTER TABLE orders_w ALTER id SET DEFAULT nextval(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;orders_id_seq&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INSERT INTO orders_w SELECT * FROM orders LIMIT 1000000&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;col&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;extra&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]):&lt;/span&gt;
            &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CREATE INDEX w&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; ON orders_w (&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;col&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;()&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CHECKPOINT&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;tps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;REPEATS&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;pgbench&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;P&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/write.sql&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;tps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;communicate&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
        &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tps&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;tps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index_mb&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT pg_indexes_size(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;orders_w&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;)/1048576&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;writes&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;
    &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;DROP TABLE orders_w&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;stale&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;limit_s&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;give_up_s&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;wr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;P&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;R&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;wr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;rd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;miss&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lag&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gave_up&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;monotonic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;limit_s&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;monotonic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;gt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;
        &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INSERT INTO orders(customer_id,status,amount,created_at,note) &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                  &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;VALUES (1,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;new&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;,1,now(),&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;x&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;) RETURNING id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;perf_counter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SELECT 1 FROM orders WHERE id = %s&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,))&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;miss&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;perf_counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;gt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;give_up_s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;gave_up&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;
        &lt;span class="n"&gt;lag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;perf_counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;wr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;rd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;lag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reads&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lag&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;stale_first_read&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;miss&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gave_up&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;gave_up&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;p50_ms&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lag&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lag&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;p99_ms&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lag&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lag&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;99&lt;/span&gt;&lt;span class="p"&gt;)],&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                &lt;span class="n"&gt;max_ms&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lag&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;stage_stale&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;stale_idle&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;stale&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;pgbench&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;P&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/load.sql&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;stale_load&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;stale&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;lag_under_load&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;q&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;SELECT pg_size_pretty(pg_wal_lsn_diff(
        pg_current_wal_lsn(), replay_lsn)), replay_lag::text
        FROM pg_stat_replication&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;postgres --version&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;flush&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;sh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;redis-server --port 0 --unixsocket &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/redis.sock --save &lt;/span&gt;&lt;span class="sh"&gt;''&lt;/span&gt;&lt;span class="s"&gt; --daemonize yes --dir &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fn&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;baseline&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stage_baseline&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;replica_start&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start_replica&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                     &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;replica&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stage_replica&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cache&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stage_cache&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                     &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;index&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stage_index&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;writes&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stage_writes&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;stale&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stage_stale&lt;/span&gt;&lt;span class="p"&gt;)]:&lt;/span&gt;
        &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;monotonic&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;done in&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;monotonic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;s&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flush&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;lab_results.json&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;w&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;indent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;sh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pg_ctl -D &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/r -m fast -w stop&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;sh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pg_ctl -D &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/p -m fast -w stop&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;sh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;redis-cli -s &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;BASE&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/redis.sock shutdown nosave&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Back to That Meeting
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;"We need a read replica."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Maybe you do. In this test, the replica did what it promised: it doubled capacity.&lt;/p&gt;

&lt;p&gt;But it also cost a second server, introduced stale reads, and left a 5-million-row scan behind every request. One line of SQL did far more.&lt;/p&gt;

&lt;p&gt;Here is the short version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Measure first.&lt;/strong&gt; One &lt;code&gt;EXPLAIN&lt;/code&gt; showed 4,999,944 wasted rows.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Replicas add capacity.&lt;/strong&gt; They don't make a bad query good.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Replicas lag.&lt;/strong&gt; Even at 0.33 ms, 98.65% of immediate reads missed the row.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caches need repetition.&lt;/strong&gt; Hit rate ranged from 0.4% to 84.7% on the same code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Indexes have a bill.&lt;/strong&gt; Five of them cost about 21% of insert speed here.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sharding is real work.&lt;/strong&gt; Teams like Figma did it only after hitting measured limits.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the next time someone says &lt;em&gt;"we need to scale,"&lt;/em&gt; ask one question first:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Which number is the bottleneck, and how do we know?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Over to You
&lt;/h2&gt;

&lt;p&gt;What was the most expensive scaling step your team took that one index could have avoided? Tell me in the comments 👇&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Test details: PostgreSQL 16.15 and Redis on a 2-core Linux cloud machine. 5 million rows, 8 clients, 3 repeats per step with medians reported. Numbers will vary on other machines. The scaling order in this piece follows Nikki Siapno's article on database scaling in the Level Up Coding System Design Newsletter (October 2026).&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;If this was useful, follow me here for more stories from the backend trenches, and subscribe to &lt;a href="https://thebackendtechlead.substack.com/" rel="noopener noreferrer"&gt;The Backend Tech Lead&lt;/a&gt; on Substack for weekly blueprints.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>performance</category>
      <category>django</category>
    </item>
    <item>
      <title>x is 257 Was True on Monday and False on Tuesday</title>
      <dc:creator>Anas Issath</dc:creator>
      <pubDate>Tue, 03 Nov 2026 12:06:50 +0000</pubDate>
      <link>https://dev.to/anas_issath/x-is-257-was-true-on-monday-and-false-on-tuesday-26mm</link>
      <guid>https://dev.to/anas_issath/x-is-257-was-true-on-monday-and-false-on-tuesday-26mm</guid>
      <description>&lt;p&gt;&lt;em&gt;Why Python's &lt;code&gt;is&lt;/code&gt; and &lt;code&gt;==&lt;/code&gt; are not the same thing, how one of them lies about numbers, and the single rule that keeps you safe.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I had a check that compared two numbers. Something close to this, simplified:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;parsed_id&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;expected_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;grant_access&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It worked. Every test passed. Then one day, with slightly different data, the same comparison started returning &lt;code&gt;False&lt;/code&gt; for two numbers that were obviously equal. Both were &lt;code&gt;257&lt;/code&gt;. I printed them. They were identical. And &lt;code&gt;parsed_id is expected_id&lt;/code&gt; was &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So I changed the test data, and it went back to &lt;code&gt;True&lt;/code&gt;. Smaller numbers worked. Bigger numbers did not. &lt;strong&gt;The comparison's correctness depended on how large the number was&lt;/strong&gt;, which is not a sentence that should ever make sense.&lt;/p&gt;

&lt;p&gt;The culprit was a single two-letter word I had used where I meant something else: &lt;code&gt;is&lt;/code&gt;. In Python, &lt;code&gt;is&lt;/code&gt; and &lt;code&gt;==&lt;/code&gt; look interchangeable and are not. One asks &lt;em&gt;"are these equal?"&lt;/em&gt; The other asks a completely different question, and the answer to that question happens to be unpredictable for numbers.&lt;/p&gt;

&lt;p&gt;Here is what &lt;code&gt;is&lt;/code&gt; actually checks, why it lied to me, and the one rule that makes this disappear.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Two Questions Are Not the Same Question
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;==&lt;/code&gt; and &lt;code&gt;is&lt;/code&gt; ask different things, and the whole bug lives in the gap between them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;==&lt;/code&gt; asks: are these two values &lt;em&gt;equal&lt;/em&gt;?&lt;/strong&gt; Do they represent the same thing? &lt;code&gt;5 == 5&lt;/code&gt; is &lt;code&gt;True&lt;/code&gt; because five equals five. This is almost always the question you actually want.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;is&lt;/code&gt; asks: are these two the &lt;em&gt;exact same object&lt;/em&gt; in memory?&lt;/strong&gt; Not "equal," but "literally the same object, the same identity."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two different lists that happen to hold the same items are equal but are not the same object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;      &lt;span class="c1"&gt;# True  -&amp;gt; same contents
&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;      &lt;span class="c1"&gt;# False -&amp;gt; two different list objects
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are &lt;em&gt;equal&lt;/em&gt;. They are not &lt;em&gt;identical&lt;/em&gt;. They are two separate lists that look alike. &lt;code&gt;==&lt;/code&gt; sees them as equal; &lt;code&gt;is&lt;/code&gt; sees them as distinct, because they are distinct objects sitting at different places in memory.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;That is the core confusion:&lt;/strong&gt; &lt;code&gt;is&lt;/code&gt; is not a stricter &lt;code&gt;==&lt;/code&gt;. It is a different operator answering a different question.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And for most values you compare, you care about equality, not identity. You want &lt;code&gt;==&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why &lt;code&gt;is&lt;/code&gt; Lied About My Numbers
&lt;/h2&gt;

&lt;p&gt;So why did &lt;code&gt;parsed_id is expected_id&lt;/code&gt; ever return &lt;code&gt;True&lt;/code&gt; for 257, let alone sometimes?&lt;/p&gt;

&lt;p&gt;Because of an optimization most people never learn about. For performance, &lt;strong&gt;CPython keeps a cache of small integer objects, roughly -5 through 256&lt;/strong&gt;, created once at startup and reused everywhere. Every time you use the number &lt;code&gt;5&lt;/code&gt;, you get back the same cached &lt;code&gt;5&lt;/code&gt; object. So for small numbers, two equal values really are the same object, and &lt;code&gt;is&lt;/code&gt; returns &lt;code&gt;True&lt;/code&gt; by luck.&lt;/p&gt;

&lt;p&gt;Step outside that cached range and the luck runs out. Numbers above 256 are not cached, so two equal large numbers can be two different objects, and &lt;code&gt;is&lt;/code&gt; returns &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here is the trap in its purest form, which I ran on Python 3.13:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;256&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;256&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;# True
&lt;/span&gt;&lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;257&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;257&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;# False
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read that again. &lt;strong&gt;The exact same code.&lt;/strong&gt; The only difference is 256 versus 257. One is &lt;code&gt;True&lt;/code&gt;, the other is &lt;code&gt;False&lt;/code&gt;, purely because 256 is in the small-integer cache and 257 is not.&lt;/p&gt;

&lt;p&gt;That is why my check worked for small IDs and broke for large ones. &lt;code&gt;is&lt;/code&gt; was never checking equality. It was checking identity, and identity for numbers depends on an internal caching optimization that you are not supposed to rely on and that can change between Python versions and even between how the values were produced. &lt;strong&gt;I had built a correctness check on top of an implementation detail.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A note, because it makes the danger worse: with plain literals written in your source (&lt;code&gt;257 is 257&lt;/code&gt;), Python's compiler often folds them into one shared constant, so it returns &lt;code&gt;True&lt;/code&gt; and looks fine. The failure only appears when the numbers are &lt;strong&gt;computed at runtime&lt;/strong&gt;, as mine were, from parsing. So it can look correct in a quick REPL test with literals and then fail on real, computed data. Exactly the data you did not test with.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Python Itself Now Warns You
&lt;/h2&gt;

&lt;p&gt;This mistake is common enough that recent versions of Python will warn you about one form of it. Compare a value to a literal with &lt;code&gt;is&lt;/code&gt; and the interpreter says:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SyntaxWarning: "is" with 'str' literal. Did you mean "=="?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The language is telling you, out loud, that &lt;code&gt;is&lt;/code&gt; is almost certainly not what you meant when comparing to a literal string or number. &lt;strong&gt;If you see that warning, the fix is virtually always to change &lt;code&gt;is&lt;/code&gt; to &lt;code&gt;==&lt;/code&gt;.&lt;/strong&gt; Python is trying to save you from the exact bug I shipped.&lt;/p&gt;




&lt;h2&gt;
  
  
  The One Place &lt;code&gt;is&lt;/code&gt; Is Not Only Fine but Correct: &lt;code&gt;None&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Here is where it flips. There is a specific, important case where &lt;code&gt;is&lt;/code&gt; is the &lt;em&gt;right&lt;/em&gt; operator and &lt;code&gt;==&lt;/code&gt; is subtly &lt;em&gt;wrong&lt;/em&gt;: comparing to &lt;code&gt;None&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The official Python style guide, PEP 8, states it directly:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Comparisons to singletons like None should always be done with is or is not, never the equality operators.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Always &lt;code&gt;x is None&lt;/code&gt;, never &lt;code&gt;x == None&lt;/code&gt;. Why? Two reasons.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First, &lt;code&gt;None&lt;/code&gt; is a singleton.&lt;/strong&gt; There is exactly one &lt;code&gt;None&lt;/code&gt; object in the entire program, so "is this the same object as &lt;code&gt;None&lt;/code&gt;" is precisely the right question, and &lt;code&gt;is&lt;/code&gt; answers it directly and fast.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Second, and more important, &lt;code&gt;==&lt;/code&gt; can be overridden.&lt;/strong&gt; Any object can define its own &lt;code&gt;__eq__&lt;/code&gt; method and decide what it considers equal to. A badly behaved or cleverly designed object can make &lt;code&gt;x == None&lt;/code&gt; return &lt;code&gt;True&lt;/code&gt; when &lt;code&gt;x&lt;/code&gt; is not actually &lt;code&gt;None&lt;/code&gt;. I confirmed exactly this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Weird&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__eq__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;

&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Weird&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;      &lt;span class="c1"&gt;# True   &amp;lt;- __eq__ hijacked the comparison
&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;      &lt;span class="c1"&gt;# False  &amp;lt;- correct: w is genuinely not None
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;w == None&lt;/code&gt; is &lt;code&gt;True&lt;/code&gt;, which is a lie. &lt;code&gt;w is None&lt;/code&gt; is &lt;code&gt;False&lt;/code&gt;, which is the truth. &lt;strong&gt;&lt;code&gt;is&lt;/code&gt; cannot be fooled this way&lt;/strong&gt;, because identity is not something an object can redefine. That is why checking for &lt;code&gt;None&lt;/code&gt; is the one case where you reach for &lt;code&gt;is&lt;/code&gt; on purpose.&lt;/p&gt;

&lt;p&gt;So the operators split cleanly by use case:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Comparing to...&lt;/th&gt;
&lt;th&gt;Use&lt;/th&gt;
&lt;th&gt;Why&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;None&lt;/code&gt; (and other singletons like &lt;code&gt;True&lt;/code&gt;/&lt;code&gt;False&lt;/code&gt; when you truly mean the object)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;is&lt;/code&gt; / &lt;code&gt;is not&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;One object exists; &lt;code&gt;==&lt;/code&gt; can be overridden and lie&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Numbers, strings, lists, any value&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;==&lt;/code&gt; / &lt;code&gt;!=&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;You care about equality, not identity&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  The Rule I Take Away
&lt;/h2&gt;

&lt;p&gt;Compressed to one line:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;==&lt;/code&gt; asks "are these equal?" &lt;code&gt;is&lt;/code&gt; asks "are these the exact same object?"&lt;/strong&gt; Use &lt;code&gt;==&lt;/code&gt; for comparing values. Use &lt;code&gt;is&lt;/code&gt; only for &lt;code&gt;None&lt;/code&gt; and other singletons. Never use &lt;code&gt;is&lt;/code&gt; to compare numbers or strings, because its answer depends on caching and interning you must not rely on.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The deeper lesson, the one that outlasts this specific trap, is that &lt;strong&gt;identity and equality are different concepts, and the two-letter word &lt;code&gt;is&lt;/code&gt; means identity, not "equals."&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;English makes &lt;code&gt;is&lt;/code&gt; feel like a synonym for equality, which is exactly why people reach for it when they mean &lt;code&gt;==&lt;/code&gt;. But Python's &lt;code&gt;is&lt;/code&gt; is a precise, narrow operator about object identity, and for values like numbers that identity is governed by internal optimizations that are not part of the language's promises. Build a comparison on &lt;code&gt;is&lt;/code&gt; for a value, and you have built it on sand that shifts between numbers, between runs, and between Python versions.&lt;/p&gt;

&lt;p&gt;And the usual sting applies. This hides in testing because small numbers and literal values make &lt;code&gt;is&lt;/code&gt; accidentally return &lt;code&gt;True&lt;/code&gt;, so it looks correct right up until real, computed, larger data flows through and the identity you were never entitled to quietly stops holding.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to Catch It Before It Catches You
&lt;/h2&gt;

&lt;p&gt;Keep these four habits:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Use &lt;code&gt;==&lt;/code&gt; to compare values, always.&lt;/strong&gt; Numbers, strings, anything where you care about the value being equal. If the question is "do these represent the same thing," that is &lt;code&gt;==&lt;/code&gt;, every time. Reaching for &lt;code&gt;is&lt;/code&gt; on a value is the bug.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use &lt;code&gt;is&lt;/code&gt; only for &lt;code&gt;None&lt;/code&gt; and singletons.&lt;/strong&gt; &lt;code&gt;if x is None&lt;/code&gt;, &lt;code&gt;if x is not None&lt;/code&gt;. This is both correct and the officially recommended style (PEP 8). The same applies if you genuinely mean the one &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt; object, but for &lt;code&gt;None&lt;/code&gt; it is essential, because &lt;code&gt;==&lt;/code&gt; can be overridden and lie.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Heed the &lt;code&gt;SyntaxWarning&lt;/code&gt;.&lt;/strong&gt; If Python warns &lt;em&gt;&lt;code&gt;"is" with a literal. Did you mean "=="?&lt;/code&gt;&lt;/em&gt;, the answer is yes, you did. Change it to &lt;code&gt;==&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Never test identity-based comparisons only with small numbers or literals.&lt;/strong&gt; &lt;code&gt;is&lt;/code&gt; gives false confidence with cached small ints and folded literals. If you must reason about &lt;code&gt;is&lt;/code&gt;, test with large, runtime-computed values, where the caching no longer rescues you.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I changed one &lt;code&gt;is&lt;/code&gt; to &lt;code&gt;==&lt;/code&gt; in that access check, and the comparison started telling the truth for every number, large or small. The two values had been equal the whole time. I had just been asking Python the wrong question, and for small numbers it had been politely giving me the right answer anyway, right up until it stopped.&lt;/p&gt;




&lt;h2&gt;
  
  
  The 30-Second Takeaway
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;==&lt;/code&gt; and &lt;code&gt;is&lt;/code&gt; ask different questions.&lt;/strong&gt; &lt;code&gt;==&lt;/code&gt; asks "are these values equal?" &lt;code&gt;is&lt;/code&gt; asks "are these the exact same object in memory?" They are not interchangeable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Two equal objects can fail an &lt;code&gt;is&lt;/code&gt; check:&lt;/strong&gt; &lt;code&gt;[1,2,3] == [1,2,3]&lt;/code&gt; is &lt;code&gt;True&lt;/code&gt;, but &lt;code&gt;[1,2,3] is [1,2,3]&lt;/code&gt; is &lt;code&gt;False&lt;/code&gt;, because they are two distinct objects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;is&lt;/code&gt; lies about numbers because of small-integer caching.&lt;/strong&gt; On Python 3.13: &lt;code&gt;int('256') is int('256')&lt;/code&gt; is &lt;code&gt;True&lt;/code&gt; (256 is cached), but &lt;code&gt;int('257') is int('257')&lt;/code&gt; is &lt;code&gt;False&lt;/code&gt; (257 is not). Same code, different answer by size. This is an implementation detail you must never rely on.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It hides in testing:&lt;/strong&gt; small numbers and source literals make &lt;code&gt;is&lt;/code&gt; accidentally return &lt;code&gt;True&lt;/code&gt; (caching and compiler constant-folding). It breaks only on large, runtime-computed values.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The one correct use of &lt;code&gt;is&lt;/code&gt;: &lt;code&gt;None&lt;/code&gt;.&lt;/strong&gt; PEP 8 says comparisons to singletons like &lt;code&gt;None&lt;/code&gt; should always use &lt;code&gt;is&lt;/code&gt; or &lt;code&gt;is not&lt;/code&gt;. &lt;code&gt;None&lt;/code&gt; is a singleton, and &lt;code&gt;==&lt;/code&gt; can be overridden to lie (&lt;code&gt;w == None&lt;/code&gt; can be &lt;code&gt;True&lt;/code&gt; while &lt;code&gt;w is None&lt;/code&gt; is &lt;code&gt;False&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The rule:&lt;/strong&gt; &lt;code&gt;==&lt;/code&gt; for comparing values; &lt;code&gt;is&lt;/code&gt; only for &lt;code&gt;None&lt;/code&gt; and singletons; never &lt;code&gt;is&lt;/code&gt; for numbers or strings. And if Python warns &lt;code&gt;"is" with a literal&lt;/code&gt;, change it to &lt;code&gt;==&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Your Turn
&lt;/h2&gt;

&lt;p&gt;Search your code for &lt;code&gt;is&lt;/code&gt; and &lt;code&gt;is not&lt;/code&gt; and check each one: is the right-hand side &lt;code&gt;None&lt;/code&gt; (good) or a number, string, or other value (bug)? Every &lt;code&gt;is&lt;/code&gt; comparing a value should be &lt;code&gt;==&lt;/code&gt;. The ones comparing to &lt;code&gt;None&lt;/code&gt; are exactly right.&lt;/p&gt;

&lt;p&gt;Did &lt;code&gt;is&lt;/code&gt; vs &lt;code&gt;==&lt;/code&gt; ever bite you in production? Tell me in the comments 👇&lt;/p&gt;




&lt;p&gt;&lt;em&gt;If this was useful, follow me here for more stories from the backend trenches, and subscribe to &lt;a href="https://thebackendtechlead.substack.com/" rel="noopener noreferrer"&gt;The Backend Tech Lead&lt;/a&gt; on Substack for weekly blueprints.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>programming</category>
      <category>debugging</category>
    </item>
    <item>
      <title>My Page Ran One Query in Development and 5,000 in Production</title>
      <dc:creator>Anas Issath</dc:creator>
      <pubDate>Mon, 02 Nov 2026 08:45:48 +0000</pubDate>
      <link>https://dev.to/anas_issath/my-page-ran-one-query-in-development-and-5000-in-production-29pk</link>
      <guid>https://dev.to/anas_issath/my-page-ran-one-query-in-development-and-5000-in-production-29pk</guid>
      <description>&lt;p&gt;&lt;em&gt;The N+1 problem: how a harmless-looking loop quietly fires one database query per row, and the two methods that collapse it back to one.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The page was a simple list. Show every book, with its author's name next to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;books&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jinja"&gt;&lt;code&gt;&lt;span class="cp"&gt;{%&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nv"&gt;book&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nv"&gt;books&lt;/span&gt; &lt;span class="cp"&gt;%}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;{{&lt;/span&gt; &lt;span class="nv"&gt;book.title&lt;/span&gt; &lt;span class="cp"&gt;}}&lt;/span&gt; by &lt;span class="cp"&gt;{{&lt;/span&gt; &lt;span class="nv"&gt;book.author.name&lt;/span&gt; &lt;span class="cp"&gt;}}&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;{%&lt;/span&gt; &lt;span class="k"&gt;endfor&lt;/span&gt; &lt;span class="cp"&gt;%}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On my laptop, with twelve books in the database, it loaded instantly. It stayed fast through code review. It shipped.&lt;/p&gt;

&lt;p&gt;Then production, with a real catalogue, got slow. Not broken. &lt;strong&gt;Slow.&lt;/strong&gt; The page that rendered in 30 milliseconds locally was taking four seconds live, and the database was lighting up while it did.&lt;/p&gt;

&lt;p&gt;I turned on query logging and felt my stomach drop. That one page, the simple list, was running &lt;strong&gt;over five thousand database queries&lt;/strong&gt; to render. One to get the books. And then one more for every single book, to go fetch its author.&lt;/p&gt;

&lt;p&gt;I had written what looked like one query. I had actually written one, plus one per row. This is the &lt;strong&gt;N+1 problem&lt;/strong&gt;, and it is probably the single most common performance bug in Django. Here is exactly how the loop betrays you, and the two methods that fix it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Where the Thousands of Queries Come From
&lt;/h2&gt;

&lt;p&gt;The trap hides in a line that does not look like a query at all.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;books&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;      &lt;span class="c1"&gt;# looks like the only database call
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That line fetches the books. One query. So far so good.&lt;/p&gt;

&lt;p&gt;The damage happens later, in the template, on this piece:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jinja"&gt;&lt;code&gt;&lt;span class="cp"&gt;{{&lt;/span&gt; &lt;span class="nv"&gt;book.author.name&lt;/span&gt; &lt;span class="cp"&gt;}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;book.author&lt;/code&gt; is a foreign key. When you first access it, the author is not in memory yet, so &lt;strong&gt;Django quietly runs a query to go get that author from the database.&lt;/strong&gt; One query, right there, triggered just by reading the attribute.&lt;/p&gt;

&lt;p&gt;Now put that inside a loop over every book. For each book, the template reads &lt;code&gt;book.author&lt;/code&gt;, and each read fires its own query. One hundred books means one hundred author queries. Five thousand books means five thousand author queries.&lt;/p&gt;

&lt;p&gt;So the real cost is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;1 query&lt;/strong&gt; to fetch the books, &lt;strong&gt;plus N queries&lt;/strong&gt;, one per book, to fetch each author.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is the "N+1." One query for the parents, N queries for the related objects, and the N is however many rows you have.&lt;/p&gt;

&lt;p&gt;The line that triggers it, &lt;code&gt;book.author&lt;/code&gt;, does not look like database access. It looks like reading a field on an object you already have. That is exactly why it is so easy to write and so hard to spot.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why It Is Invisible Until It Is Catastrophic
&lt;/h2&gt;

&lt;p&gt;This bug has the signature of every dangerous one. &lt;strong&gt;It scales with your data, and your development database has almost none.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;On my laptop I had twelve books. Twelve books means thirteen queries, which run in a couple of milliseconds. Nothing to notice. The page was fast, the tests passed, nobody saw a problem.&lt;/p&gt;

&lt;p&gt;Production had thousands of rows. The exact same code now meant thousands of queries, each a separate round trip to the database, and the page crawled.&lt;/p&gt;

&lt;p&gt;That is the cruel part: &lt;strong&gt;the code did not change between fast and slow. The row count did.&lt;/strong&gt; N+1 is invisible precisely when you are testing, because you test with a handful of rows, and it only becomes catastrophic when real data arrives. The bug was always there. It just needed enough rows to show its teeth, and you never have enough rows on your laptop.&lt;/p&gt;

&lt;p&gt;This is also why it survives review. A reviewer reads &lt;code&gt;book.author.name&lt;/code&gt; and sees a field access, not a query. There is nothing visibly wrong with the code. The wrongness is in how it multiplies, and &lt;strong&gt;multiplication does not show up in a diff.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The Fix for Foreign Keys: &lt;code&gt;select_related&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;When the related object is reached through a forward foreign key or a one-to-one (each book has exactly &lt;strong&gt;one&lt;/strong&gt; author), the fix is &lt;code&gt;select_related&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;books&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select_related&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;author&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells Django to fetch the books &lt;em&gt;and&lt;/em&gt; their authors together, in a single query, using a SQL &lt;strong&gt;JOIN&lt;/strong&gt;. Instead of fetching books and then running off to get each author one at a time, the database returns each book with its author's columns already attached.&lt;/p&gt;

&lt;p&gt;The result:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;N+1 queries collapse into one.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now the template's &lt;code&gt;book.author.name&lt;/code&gt; touches data that is already in memory, loaded by that single joined query. No per-row database trip. The page that ran five thousand queries runs one.&lt;/p&gt;

&lt;p&gt;The way to read it: &lt;code&gt;select_related&lt;/code&gt; does the joining &lt;strong&gt;in the database, in one query&lt;/strong&gt;, and it is for &lt;strong&gt;single-valued relationships&lt;/strong&gt;, where each row points to exactly one related object (&lt;code&gt;ForeignKey&lt;/code&gt;, &lt;code&gt;OneToOne&lt;/code&gt;). That "one related object per row" is what lets it ride along in a single JOIN.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Fix for the Many Side: &lt;code&gt;prefetch_related&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;A JOIN works when each book has one author. But what about the other direction, where one object has &lt;strong&gt;many&lt;/strong&gt; related rows? An author with many books, a post with many tags, an order with many line items. You cannot pull "many" into a single joined row cleanly, so Django gives you a different tool: &lt;code&gt;prefetch_related&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;authors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;prefetch_related&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;books&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This runs a &lt;strong&gt;separate second query&lt;/strong&gt; to fetch all the related objects at once, then joins them to their parents &lt;strong&gt;in Python, in memory&lt;/strong&gt;. So fetching a list of authors and all their books costs:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;2 queries total.&lt;/strong&gt; One for the authors, one for all their books.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Not one query per author. Two queries for the whole thing, regardless of how many authors you have.&lt;/p&gt;

&lt;p&gt;It nests too: &lt;code&gt;prefetch_related("books", "books__publisher")&lt;/code&gt; adds one more query for the publishers, and so on: &lt;strong&gt;one additional query per relationship level, not per row.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The way to read it: &lt;code&gt;prefetch_related&lt;/code&gt; does the joining &lt;strong&gt;in Python across a small number of queries&lt;/strong&gt;, and it is for &lt;strong&gt;multi-valued relationships&lt;/strong&gt;: the many-to-many and reverse-foreign-key cases where a single JOIN would not work.&lt;/p&gt;




&lt;h2&gt;
  
  
  Which One to Reach For
&lt;/h2&gt;

&lt;p&gt;The choice comes down to one question: &lt;strong&gt;how many related objects does each row point to?&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Each row points to...&lt;/th&gt;
&lt;th&gt;Relationship types&lt;/th&gt;
&lt;th&gt;Use&lt;/th&gt;
&lt;th&gt;How it works&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;One&lt;/strong&gt; related object&lt;/td&gt;
&lt;td&gt;Forward &lt;code&gt;ForeignKey&lt;/code&gt;, &lt;code&gt;OneToOne&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;select_related&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;One query, a SQL JOIN&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;Many&lt;/strong&gt; related objects&lt;/td&gt;
&lt;td&gt;Many-to-many, reverse &lt;code&gt;ForeignKey&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;prefetch_related&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A separate query per relationship, joined in Python&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A useful shortcut:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you access the relationship as a &lt;strong&gt;single object&lt;/strong&gt; (&lt;code&gt;book.author&lt;/code&gt;), you almost certainly want &lt;code&gt;select_related&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If you access it as a &lt;strong&gt;collection you loop over&lt;/strong&gt; (&lt;code&gt;author.books.all()&lt;/code&gt;), you want &lt;code&gt;prefetch_related&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And you can combine them freely on one queryset when a page touches both kinds:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select_related&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;author&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;prefetch_related&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;tags&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One JOIN for the single author, one extra query for the many tags. A page that might have run thousands of queries now runs a small, fixed handful, no matter how many books, authors, or tags exist.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Rule I Take Away
&lt;/h2&gt;

&lt;p&gt;Compressed to one line:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Accessing a related object inside a loop fires one query per row, turning one intended query into N+1.&lt;/strong&gt; Fetch the relationships up front: &lt;code&gt;select_related&lt;/code&gt; for single-valued relationships (one JOIN), &lt;code&gt;prefetch_related&lt;/code&gt; for multi-valued ones (a second query joined in Python).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The deeper lesson, the one that outlasts this specific framework, is that &lt;strong&gt;in an ORM, reading an attribute can be a database call&lt;/strong&gt;, and the ones that fire inside loops are the ones that hurt.&lt;/p&gt;

&lt;p&gt;The ORM is wonderful because it hides the database behind ordinary-looking object access. That same hiding is what makes N+1 so easy to write: &lt;code&gt;book.author&lt;/code&gt; looks free, costs a query, and you put it in a loop without a second thought.&lt;/p&gt;

&lt;p&gt;The fix is always the same shape: &lt;strong&gt;load what the loop will need before the loop&lt;/strong&gt;, in a bounded number of queries, instead of letting the loop pull it one row at a time.&lt;/p&gt;

&lt;p&gt;And the usual sting applies. This never shows up when you test, because you test with a handful of rows where N+1 is indistinguishable from one query. It only bites once real data arrives, which is to say in production, at the worst possible time, on the page you were sure was simple.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to Catch It Before It Catches You
&lt;/h2&gt;

&lt;p&gt;Keep these four habits:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Count your queries, do not eyeball your code.&lt;/strong&gt; Install the Django Debug Toolbar (or log queries) and watch the query count for each page. N+1 is invisible in the code and glaringly obvious in a query count that scales with your rows. This single tool catches almost every instance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Suspect every related access inside a loop.&lt;/strong&gt; &lt;code&gt;book.author&lt;/code&gt;, &lt;code&gt;order.customer&lt;/code&gt;, &lt;code&gt;item.product&lt;/code&gt; inside a &lt;code&gt;for&lt;/code&gt; loop, in a template or a serializer, is a query per iteration. The moment you loop and touch a relationship, ask whether you prefetched it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Match the method to the relationship.&lt;/strong&gt; Single object per row (&lt;code&gt;book.author&lt;/code&gt;) → &lt;code&gt;select_related&lt;/code&gt;. Collection per row (&lt;code&gt;author.books.all()&lt;/code&gt;) → &lt;code&gt;prefetch_related&lt;/code&gt;. Combine both on one queryset when a page needs both.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test with realistic data volumes.&lt;/strong&gt; Seed your development or test database with thousands of rows, not twelve. N+1 hides at small scale by definition; the only way to feel it before production does is to give it enough rows to multiply.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I added &lt;code&gt;.select_related("author")&lt;/code&gt; to that one queryset, reloaded, and watched the query count fall from over five thousand to &lt;strong&gt;one&lt;/strong&gt;. The page dropped back under a tenth of a second. The code in the template never changed. I had simply loaded the authors before the loop, instead of letting the loop fetch them five thousand times.&lt;/p&gt;




&lt;h2&gt;
  
  
  The 30-Second Takeaway
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Accessing a related object (&lt;code&gt;book.author&lt;/code&gt;) inside a loop triggers a separate query per row&lt;/strong&gt;, because the ORM lazily fetches the relationship on first access. One list query plus N per-row queries is the N+1 problem.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It is invisible in development&lt;/strong&gt; (a handful of rows means a handful of queries) and &lt;strong&gt;catastrophic in production&lt;/strong&gt; (thousands of rows means thousands of queries). The code never changes; the row count does.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;select_related&lt;/code&gt;&lt;/strong&gt; fixes it for single-valued relationships (forward &lt;code&gt;ForeignKey&lt;/code&gt;, &lt;code&gt;OneToOne&lt;/code&gt;): it does a SQL JOIN and fetches everything in one query. Use it when you access the relation as one object.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;prefetch_related&lt;/code&gt;&lt;/strong&gt; fixes it for multi-valued relationships (many-to-many, reverse &lt;code&gt;ForeignKey&lt;/code&gt;): it runs a separate query and joins in Python, costing a small fixed number of queries (e.g. 2), not one per row. Use it when you loop over a collection.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Choosing:&lt;/strong&gt; one related object per row → &lt;code&gt;select_related&lt;/code&gt;; many per row → &lt;code&gt;prefetch_related&lt;/code&gt;. Combine both on one queryset when needed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Catch it&lt;/strong&gt; with a query counter (Django Debug Toolbar), suspect every related access inside a loop, and test with realistic data volumes, because N+1 only reveals itself at scale.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Your Turn
&lt;/h2&gt;

&lt;p&gt;Install the Django Debug Toolbar and open your busiest list page. If the query count climbs with the number of rows on the page, you have an N+1. Add &lt;code&gt;select_related&lt;/code&gt; or &lt;code&gt;prefetch_related&lt;/code&gt; to the queryset and watch it collapse to a handful.&lt;/p&gt;

&lt;p&gt;What's the worst query count you've ever found on a single page? Tell me in the comments 👇&lt;/p&gt;




&lt;p&gt;&lt;em&gt;If this was useful, follow me here for more stories from the backend trenches, and subscribe to &lt;a href="https://thebackendtechlead.substack.com/" rel="noopener noreferrer"&gt;The Backend Tech Lead&lt;/a&gt; on Substack for weekly blueprints.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>performance</category>
      <category>orm</category>
    </item>
  </channel>
</rss>
