:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-imports): retry redis connection before falling back in idempotency check (#97014)
Tom Owers203d643841cf
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/idempotency.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/idempotency.pyindex afdb3396a44e..583c77d929ad 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/idempotency.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/idempotency.py@@ -2,8 +2,10 @@ from contextlib import contextmanagerfrom django.conf import settings+import redisimport structlogfrom asgiref.sync import async_to_sync+from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential_jitterfrom posthog.exceptions_capture import capture_exceptionfrom posthog.redis import get_client@@ -18,9 +20,24 @@ IDEMPOTENCY_KEY_PREFIX = "warehouse_pipelines:processed"IDEMPOTENCY_TTL_SECONDS = 72 * 60 * 60 # 3 days (72 hours) same as the topic retention period+@retry(+ retry=retry_if_exception_type((redis.exceptions.ConnectionError, redis.exceptions.TimeoutError)),+ stop=stop_after_attempt(3),+ wait=wait_exponential_jitter(initial=0.1, max=1),+ reraise=True,+)+def _connect_and_ping(redis_client: redis.Redis) -> None:+ redis_client.ping()++@contextmanagerdef get_redis_client():- """Get a Redis client for the data warehouse Redis instance."""+ """Get a Redis client for the data warehouse Redis instance.++ A bare connection blip here would fall through to the delta history scan on+ every batch until it clears, which is not cheap (see `is_batch_already_processed`).+ Absorb a few quick retries first, same as `sync_lock._get_redis_client`.+ """redis_client = Nonetry:if not settings.DATA_WAREHOUSE_REDIS_HOST or not settings.DATA_WAREHOUSE_REDIS_PORT:@@ -29,7 +46,7 @@ def get_redis_client():)redis_client = get_client(f"redis://{settings.DATA_WAREHOUSE_REDIS_HOST}:{settings.DATA_WAREHOUSE_REDIS_PORT}/")- redis_client.ping()+ _connect_and_ping(redis_client)except Exception as e:logger.warning("Redis unavailable for idempotency check — falling back to delta history scan",diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/test_idempotency.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/test_idempotency.pyindex 737d84f74347..4b50036b4c33 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/test_idempotency.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/test_idempotency.py@@ -1,12 +1,14 @@import pytestfrom unittest.mock import AsyncMock, MagicMock, patch+import redisfrom parameterized import parameterizedfrom posthog.temporal.common.errors import NonReportableErrorfrom products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.load.idempotency import (get_idempotency_key,+ get_redis_client,is_batch_already_processed,mark_batch_as_processed,)@@ -197,3 +199,29 @@ class TestMarkBatchAsProcessed:mock_get_client.return_value.__enter__.return_value = Nonemark_batch_as_processed(team_id=1, schema_id="s", run_uuid="r", batch_index=0)# No exception — the function logs a warning and returns+++class TestGetRedisClient:+ """A bare connection blip previously fell through to the delta history scan on+ every batch with no retry (see `sync_lock.TestGetRedisClient` for the sibling+ fix this mirrors)."""++ @patch(f"{_IDEMPOTENCY_MODULE}.get_client")+ def test_recovers_from_transient_connection_error(self, mock_get_client: MagicMock) -> None:+ mock_redis = MagicMock()+ mock_redis.ping.side_effect = [redis.exceptions.TimeoutError("Timeout connecting to server"), None]+ mock_get_client.return_value = mock_redis++ with get_redis_client() as client:+ assert client is mock_redis+ assert mock_redis.ping.call_count == 2++ @patch(f"{_IDEMPOTENCY_MODULE}.get_client")+ def test_fails_closed_after_exhausting_retries(self, mock_get_client: MagicMock) -> None:+ mock_redis = MagicMock()+ mock_redis.ping.side_effect = redis.exceptions.TimeoutError("Timeout connecting to server")+ mock_get_client.return_value = mock_redis++ with get_redis_client() as client:+ assert client is None+ assert mock_redis.ping.call_count == 3