A storage abstraction separates session logic from persistence technology. This matters when requirements conflict: PostgreSQL provides durability and auditability, Redis provides speed, and DynamoDB provides automatic scaling. Rather than forcing one backend on every deployment, the service delegates persistence through a replaceable interface. section 4.3 section 4.3.1
The Session Service owns request validation, business rules, and response formatting. A configured storage implementation owns persistence details. The service calls interface methods such as get_or_create_session() and add_messages() without knowing whether they become PostgreSQL queries, Redis commands, or another backend. section 4.3 section 4.4.3
A practical implementation follows a small set of boundaries:
- Define the storage interface around domain operations, such as retrieving or creating sessions and adding messages. section 4.4.3
- Implement the interface for a selected backend; PostgreSQL is suited to durable, consistent, auditable session data, while alternatives support different latency, flexibility, or operations requirements. section 4.3.1
- Inject the chosen implementation into the service constructor instead of instantiating a concrete storage class inside service logic. section 4.4.3
- Reuse the same abstraction pattern for different domains while changing the operations: session storage looks up messages by ID, whereas vector storage searches floating-point embeddings by similarity. section 5.4
Swapping a session backend
A gRPC SessionServiceServicer receives Protocol Buffer requests, calls its injected SessionStorage object, and returns Protocol Buffer responses. Replacing PostgreSQL with Redis changes the configured implementation rather than the service methods or client code. Production migration still requires planning because moving millions of sessions is not automatic. section 4.4.3 section 4.3.1
Figure 4.3 The Session Service architecture. The SessionServiceServicer class receives Protocol Buffer requests from the API gateway and delegates to the abstract SessionStorage interface, enabling different storage backends to be swapped without changing service code.

Use the architecture figure to trace Protocol Buffer requests from the gateway through the service and into the swappable SessionStorage interface. section 4.4.3

