|
|
Testing ADK Agent with Redis Session Persistence
Author: Venkata Sudhakar
Redis-backed session persistence lets ADK agents survive restarts by storing conversation state in a distributed cache rather than in-process memory. ShopMax India uses this pattern in production so that customer support agents can resume mid-session even after a pod restart during peak sale events in Mumbai and Bangalore.
The ADK InMemorySessionService can be swapped for a Redis-based implementation that serializes session state as JSON. In tests, a FakeRedisSessionService wraps a dict to simulate the same read/write contract without needing a live Redis instance. Key parameters are session_id, user_id, and ttl_seconds, which governs how long idle sessions are kept.
The example below defines a FakeRedisSessionService with get and put methods, creates an ADK agent bound to it, runs a tool call, resets the agent to simulate a restart, then restores state from the fake Redis store and asserts the conversation context is intact.
It gives the following output,
Session session-shopmax-mumbai-001 restored, order status: Shipped
1 passed in 0.12s
In production, use Redis TTL to expire idle sessions automatically and avoid unbounded memory growth. Always serialize state with a versioned schema so that a rolling deploy does not corrupt sessions created by the previous pod version. Run persistence tests in CI against a Redis test container using pytest-docker or testcontainers-python to catch serialization regressions early.
|
|