GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(dbapi): add retry_aborts_internally option to disable internal statement-replay retry · Issue #16491 · googleapis/google-cloud-python

feat(dbapi): add retry_aborts_internally option to disable internal statement-replay retry #16491

New issue
Copy link
New issue
Copy link
Open
#16498
Open
feat(dbapi): add retry_aborts_internally option to disable internal statement-replay retry#16491
#16498
Copy link
Assignees
sinhasubham
Labels
api: spannerIssues related to the Spanner API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Description

@waiho-gumloop
waiho-gumloop
opened on Mar 31, 2026
Issue body actions

Summary

The Spanner DBAPI layer (spanner_dbapi) always retries aborted transactions internally by replaying all recorded statements and validating checksums. There is no way to disable this behavior. Applications that implement their own transaction retry logic (re-invoking a callable with a fresh session on abort) experience nested retry loops that cause severe contention amplification under concurrent writes.

Background

When commit() receives an Aborted exception from Spanner, the DBAPI enters an internal retry loop in TransactionRetryHelper.retry_transaction(). This loop replays all statements recorded during the transaction and validates checksums of read results to ensure consistency. It retries up to 50 times with exponential backoff.

This mechanism was designed for Django and other PEP 249 ORMs that build transactions incrementally through individual cursor.execute() calls (original motivation: googleapis/python-spanner-django#34). In this model, the DBAPI layer is the only component that can retry — the ORM has no concept of "re-run this transaction from scratch."

However, many applications use a different pattern: wrapping the entire transaction in a callable and re-invoking it on abort (similar to Session.run_in_transaction). For these applications, the internal retry is unnecessary and harmful.

The nested retry problem

When an application wraps transactions in its own retry loop and the DBAPI also retries internally, the two layers interfere:

  1. Contention amplification (thundering herd): The internal replay re-acquires locks on the same rows that caused the original abort. Under concurrent writes, each replay attempt can abort another thread's replay, leading to exponential retry growth across threads.

  2. Wasted wall-clock time: The internal retry loop accumulates 13–19 seconds of lock wait time (observed in production with 10 concurrent writers) before finally raising RetryAborted. The outer application retry then starts fresh, having wasted all that time.

  3. Checksum mismatches on contended rows: For read-modify-write patterns, replayed reads almost always return different data (because another transaction committed in between), causing _compare_checksums() to fail. The internal retry is structurally unable to succeed in this scenario — it always falls through to RetryAborted after exhausting retries.

Relevant code paths

File Function Role
connection.py L505-515 Connection.commit() Catches Aborted, calls retry_transaction(), then recursively calls commit()
transaction_helper.py L165-210 TransactionRetryHelper.retry_transaction() The internal retry loop — replays statements, validates checksums
checksum.py L64-80 _compare_checksums() Raises RetryAborted on checksum mismatch
exceptions.py L165-172 RetryAborted Exception raised when internal retry fails validation

Timeline

Date Commit / PR Event
Oct 2020 googleapis/python-spanner-django#34 Original request — Django needs transparent transaction retry
Nov 2020 PR googleapis/python-spanner#156, googleapis/python-spanner#160, googleapis/python-spanner#168 DBAPI created with built-in statement replay and checksum validation
Feb 2021 JDBC RETRY_ABORTS_INTERNALLY JDBC driver adds opt-out flag for the same reason
2021+ Go client Go provides NewReadWriteStmtBasedTransaction (with internal retry) vs ReadWriteTransaction (without) as separate APIs
Mar 2026 This issue Python DBAPI still has no way to disable internal retry

Proposed Change

Add a retry_aborts_internally parameter to Connection and connect(), following the same pattern used for read_only and request_priority:

  • Default True — preserves existing behavior; no breaking change
  • When False — commit() wraps Aborted in RetryAborted and raises immediately, bypassing the statement-replay loop

Files changed

  1. connection.py — Add retry_aborts_internally parameter to __init__ and connect(), add property getter/setter, modify commit() to check the flag
  2. test_connection.py — 8 new unit tests

Usage

from google.cloud.spanner_dbapi import connect

# Default (unchanged) — internal retry enabled
conn = connect(instance_id, database_id, project=project)

# Disable internal retry for application-managed retries
conn = connect(instance_id, database_id, project=project,
               retry_aborts_internally=False)

# SQLAlchemy via connect_args
engine = create_engine("spanner:///...",
                       connect_args={"retry_aborts_internally": False})

Production impact

In our workload (10 concurrent writers updating JSON array columns on the same row):

Configuration Success rate Abort-to-recovery time
Default (nested retries) ~55% 13–19 seconds
retry_aborts_internally=False + app retry 98–100% 0.01–0.08 seconds

Related

  • PR: feat(dbapi): add retry_aborts_internally option to Connection python-spanner#1538
  • JDBC equivalent: RETRY_ABORTS_INTERNALLY
  • Go equivalent: NewReadWriteStmtBasedTransaction vs ReadWriteTransaction
  • Django original motivation: googleapis/python-spanner-django#34
Reacted by mhama

Activity

  1. product-auto-label
    added
    api: spannerIssues related to the Spanner API.
    on Mar 31, 2026
  2. blunderbuss-gcf
    assigned
    bhatt4982
    on Mar 31, 2026
  3. parthea commented on Mar 31, 2026

    @parthea
    parthea
    on Mar 31, 2026
    Contributor
    More actions

    Transferring to google-cloud-python as the code in this repository has moved to https://github.com/googleapis/google-cloud-python/tree/main/packages/google-cloud-spanner.

  4. parthea
    transferred this issue fromgoogleapis/python-spanneron Mar 31, 2026
  5. waiho-gumloop
    added a commit that references this issue on Apr 1, 2026

    feat(dbapi): add retry_aborts_internally option to Connection

    This issue will close once commit 1fac066 is merged into the 'main' branch.
    1fac066
  6. waiho-gumloop commented on Apr 1, 2026

    @waiho-gumloop
    waiho-gumloop
    on Apr 1, 2026
    ContributorAuthor
    More actions

    Fix re-submitted for the monorepo: #16498

  7. daniel-sanche
    assigned
    sakthivelmanii
    and unassigned
    bhatt4982
    on May 27, 2026
  8. mhama commented on Jun 7, 2026

    @mhama
    mhama
    on Jun 7, 2026
    More actions

    I believe this is a very important problem for users of Spanner DB-API.

    When the internal statement-replay retry cannot succeed due to checksum mismatches, especially in workloads involving inserts, generated values, timestamps, or read-modify-write patterns, the application can spend more than several seconds inside a transaction failure path before the error is finally surfaced to the application-level retry logic.

    From the user's point of view, this makes Spanner appear as if it were a low-performance service that takes many seconds to recover from transaction aborts, even though the real issue is the DB-API layer's internal retry behavior. This can be very misleading when evaluating Spanner's performance and reliability in production workloads.

    I would appreciate it if this could be triaged as a production-impacting performance issue rather than only as a feature request. I hope this can be prioritized and addressed as soon as possible.

  9. sakthivelmanii
    assigned
    sinhasubham
    and unassigned
    sakthivelmanii
    on Jun 10, 2026
  10. daniel-sanche
    added
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    on Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @sinhasubham
    sinhasubham

Labels

api: spannerIssues related to the Spanner API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.