Skip to content

Proposal: Stateless Session Handoff & Resilience #133

Description

@xhzeem

Currently, MTProto session state (including sequence numbers, message queues, and salt management) is stored in-memory within the session service (app/interface/session). This architecture creates a strong coupling between a client connection and a specific service instance.
If a session service node restarts (during maintenance, auto-scaling, or failure), all associated client sessions are interrupted, often requiring a protocol-level re-handshake or resulting in sequence number mismatches until the client resynchronizes.

Proposed Improvement

Transition to a stateless session architecture by offloading ephemeral session state to a distributed high-performance store (e.g., a Redis Cluster).

Key Components:

  1. Distributed State Store: Move nextSeqNo, firstMsgId, and message queues from app/interface/session/internal/sess/session.go to Redis.
  2. Consistent Hashing / Any-Node Processing: Modify gnetway (app/interface/gnetway) or the internal routing logic to allow any session node to handle a request for a given session_id by fetching the current state from the distributed store.
  3. Session Handoff Logic: Implement a "lock-and-fetch" or "optimistic concurrency" mechanism to ensure that two nodes don't process messages for the same session_id simultaneously in a way that corrupts the sequence numbers.

Benefits

  • Zero-Downtime Updates: Perform rolling updates of the session service without disconnecting users or breaking MTProto state.
  • Horizontal Elasticity: Seamlessly scale the session layer based on real-time load.
  • Improved UX: Users remain "online" and their message delivery remains uninterrupted even during backend infrastructure changes.

Compatibility

This change is strictly server-side and maintains 100% compatibility with the standard MTProto 2.0 protocol. No client-side changes are required.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions