hiranya911: According to https://cloud.google.com/apis/design/errors#handling_errors clients should retry on 500 and 503 errors. I do understand that RTDB APIs are slightly different from other cloud REST APIs. But I suppose we can try this out in the wild, and if it becomes a problem use a different retry config just for the RTDB client (the API as implemented here supports it).
rockwotj: That sounds good to me @hiranya911 
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 24190cf..b3bec93 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,8 @@
 # Unreleased
 
--
+- [added] Implemented HTTP retries. The SDK now retries HTTP calls on
+  low-level connection and socket read errors, as well as HTTP 500 and
+  503 errors.
 
 # v2.15.0
 
diff --git a/firebase_admin/_http_client.py b/firebase_admin/_http_client.py
index fe03e4e..740839d 100644
--- a/firebase_admin/_http_client.py
+++ b/firebase_admin/_http_client.py
@@ -19,16 +19,28 @@
 
 from google.auth import transport
 import requests
+from requests.packages.urllib3.util import retry # pylint: disable=import-error
+
+
+# Default retry configuration: Retries once on low-level connection and socket read errors.
+# Retries up to 4 times on HTTP 500 and 503 errors, with exponential backoff. Returns the
+# last response upon exhausting all retries.
+DEFAULT_RETRY_CONFIG = retry.Retry(
+    connect=1, read=1, status=4, status_forcelist=[500, 503],
+    raise_on_status=False, backoff_factor=0.5)
 
 
 class HttpClient(object):
     """Base HTTP client used to make HTTP calls.
 
-    HttpClient maintains an HTTP session, and handles request authentication if necessary.
+    HttpClient maintains an HTTP session, and handles request authentication and retries if
+    necessary.
     """
 
-    def __init__(self, credential=None, session=None, base_url='', headers=None):
-        """Cretes a new HttpClient instance from the provided arguments.
+    def __init__(
+            self, credential=None, session=None, base_url='', headers=None,
+            retries=DEFAULT_RETRY_CONFIG):
+        """Creates a new HttpClient instance from the provided arguments.
 
         If a credential is provided, initializes a new HTTP session authorized with it. If neither
         a credential nor a session is provided, initializes a new unauthorized session.
@@ -38,6 +50,9 @@ class HttpClient(object):
           session: A custom HTTP session (optional).
           base_url: A URL prefix to be added to all outgoing requests (optional).
           headers: A map of headers to be added to all outgoing requests (optional).
+          retries: A urllib retry configuration. Default settings would retry once for low-level
+              connection and socket read errors, and up to 4 times for HTTP 500 and 503 errors.
+              Pass a False value to disable retries (optional).
         """
         if credential:
             self._session = transport.requests.AuthorizedSession(credential)
@@ -48,6 +63,9 @@ class HttpClient(object):
 
         if headers:
             self._session.headers.update(headers)
+        if retries:
+            self._session.mount('http://', requests.adapters.HTTPAdapter(max_retries=retries))
+            self._session.mount('https://', requests.adapters.HTTPAdapter(max_retries=retries))
         self._base_url = base_url
 
     @property

Automatically retry 503 errors for RTDB
RTDB occasionally will service [503][1] status codes if the service is temporarily overloaded - currently the HTTPClient doesn't retry those operations, which are safe to be retried.


[1]: https://httpstatuses.com/503
diff --git a/tests/test_http_client.py b/tests/test_http_client.py
index db35951..34549bc 100644
--- a/tests/test_http_client.py
+++ b/tests/test_http_client.py
@@ -13,12 +13,17 @@
 # limitations under the License.
 
 """Tests for firebase_admin._http_client."""
+import pytest
+from pytest_localserver import plugin
 import requests
 
 from firebase_admin import _http_client
 from tests import testutils
 
 
+# Fixture for mocking a HTTP server
+httpserver = plugin.httpserver
+
 _TEST_URL = 'http://firebase.test.url/'
 
 
@@ -59,8 +64,57 @@ def test_base_url():
     assert recorder[0].method == 'GET'
     assert recorder[0].url == _TEST_URL + 'foo'
 
+def test_credential():
+    client = _http_client.HttpClient(
+        credential=testutils.MockGoogleCredential())
+    assert client.session is not None
+    recorder = _instrument(client, 'body')
+    resp = client.request('get', _TEST_URL)
+    assert resp.status_code == 200
+    assert resp.text == 'body'
+    assert len(recorder) == 1
+    assert recorder[0].method == 'GET'
+    assert recorder[0].url == _TEST_URL
+    assert recorder[0].headers['Authorization'] == 'Bearer mock-token'
+
 def _instrument(client, payload, status=200):
     recorder = []
     adapter = testutils.MockAdapter(payload, status, recorder)
     client.session.mount(_TEST_URL, adapter)
     return recorder
+
+
+class TestHttpRetry(object):
+    """Unit tests for the default HTTP retry configuration."""
+
+    @classmethod
+    def setup_class(cls):
+        # Turn off exponential backoff for faster execution
+        _http_client.DEFAULT_RETRY_CONFIG.backoff_factor = 0
+
+    def test_retry_on_503(self, httpserver):
+        httpserver.serve_content({}, 503)
+        client = _http_client.JsonHttpClient(
+            credential=testutils.MockGoogleCredential(), base_url=httpserver.url)
+        with pytest.raises(requests.exceptions.HTTPError) as excinfo:
+            client.request('get', '/')
+        assert excinfo.value.response.status_code == 503
+        assert len(httpserver.requests) == 5
+
+    def test_retry_on_500(self, httpserver):
+        httpserver.serve_content({}, 500)
+        client = _http_client.JsonHttpClient(
+            credential=testutils.MockGoogleCredential(), base_url=httpserver.url)
+        with pytest.raises(requests.exceptions.HTTPError) as excinfo:
+            client.request('get', '/')
+        assert excinfo.value.response.status_code == 500
+        assert len(httpserver.requests) == 5
+
+    def test_no_retry_on_404(self, httpserver):
+        httpserver.serve_content({}, 404)
+        client = _http_client.JsonHttpClient(
+            credential=testutils.MockGoogleCredential(), base_url=httpserver.url)
+        with pytest.raises(requests.exceptions.HTTPError) as excinfo:
+            client.request('get', '/')
+        assert excinfo.value.response.status_code == 404
+        assert len(httpserver.requests) == 1

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest
3.9
requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
astroid==1.4.9
CacheControl==0.14.2
cachetools==5.5.2
certifi==2025.1.31
chardet==5.2.0
charset-normalizer==3.4.1
colorama==0.4.6
coverage==7.8.0
distlib==0.3.9
exceptiongroup==1.2.2
filelock==3.18.0
-e git+https://github.com/firebase/firebase-admin-python.git@b199dfa6bf3399a05fdf8e631b4996a09e3684ff#egg=firebase_admin
google-api-core==1.34.1
google-auth==2.38.0
google-cloud-core==2.4.3
google-cloud-firestore==2.20.1
google-cloud-storage==2.14.0
google-crc32c==1.7.1
google-resumable-media==2.7.2
googleapis-common-protos==1.69.2
grpcio==1.71.0
grpcio-status==1.49.0rc1
idna==3.10
iniconfig==2.1.0
isort==6.0.1
lazy-object-proxy==1.10.0
MarkupSafe==3.0.2
mccabe==0.7.0
msgpack==1.1.0
packaging==24.2
platformdirs==4.3.7
pluggy==1.5.0
proto-plus==1.26.1
protobuf==3.20.3
pyasn1==0.6.1
pyasn1_modules==0.4.2
pylint==1.6.4
pyproject-api==1.9.0
pytest==8.3.5
pytest-cov==6.0.0
pytest-localserver==0.9.0.post0
requests==2.32.3
rsa==4.9
six==1.17.0
tomli==2.2.1
tox==4.25.0
typing_extensions==4.13.0
urllib3==2.3.0
virtualenv==20.29.3
Werkzeug==3.1.3
wrapt==1.17.2

name: firebase-admin-python
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tzdata=2025a=h04d1e81_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - astroid==1.4.9
      - cachecontrol==0.14.2
      - cachetools==5.5.2
      - certifi==2025.1.31
      - chardet==5.2.0
      - charset-normalizer==3.4.1
      - colorama==0.4.6
      - coverage==7.8.0
      - distlib==0.3.9
      - exceptiongroup==1.2.2
      - filelock==3.18.0
      - google-api-core==1.34.1
      - google-auth==2.38.0
      - google-cloud-core==2.4.3
      - google-cloud-firestore==2.20.1
      - google-cloud-storage==2.14.0
      - google-crc32c==1.7.1
      - google-resumable-media==2.7.2
      - googleapis-common-protos==1.69.2
      - grpcio==1.71.0
      - grpcio-status==1.49.0rc1
      - idna==3.10
      - iniconfig==2.1.0
      - isort==6.0.1
      - lazy-object-proxy==1.10.0
      - markupsafe==3.0.2
      - mccabe==0.7.0
      - msgpack==1.1.0
      - packaging==24.2
      - platformdirs==4.3.7
      - pluggy==1.5.0
      - proto-plus==1.26.1
      - protobuf==3.20.3
      - pyasn1==0.6.1
      - pyasn1-modules==0.4.2
      - pylint==1.6.4
      - pyproject-api==1.9.0
      - pytest==8.3.5
      - pytest-cov==6.0.0
      - pytest-localserver==0.9.0.post0
      - requests==2.32.3
      - rsa==4.9
      - six==1.17.0
      - tomli==2.2.1
      - tox==4.25.0
      - typing-extensions==4.13.0
      - urllib3==2.3.0
      - virtualenv==20.29.3
      - werkzeug==3.1.3
      - wrapt==1.17.2
prefix: /opt/conda/envs/firebase-admin-python

tests/test_http_client.py::TestHttpRetry::test_retry_on_503 tests/test_http_client.py::TestHttpRetry::test_retry_on_500 tests/test_http_client.py::TestHttpRetry::test_no_retry_on_404
tests/test_http_client.py::test_http_client_default_session tests/test_http_client.py::test_http_client_custom_session tests/test_http_client.py::test_base_url tests/test_http_client.py::test_credential
firebase_admin/_http_client.py CHANGELOG.md
firebase_admin/_http_client.py CHANGELOG.md