FaaS instance state keeping method based on connection state perception
By setting up a connection status awareness module and activity detection mechanism in the API gateway, the reliability and resource utilization efficiency problems in WebSocket long connection management under the FaaS architecture are solved, and reliable state maintenance and cost reduction are achieved.
Patent Information
- Application Number
- CN202510120644.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-26
- Publication Date
- 2025-05-09
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
Under the FaaS architecture, it is difficult for the prior art to find a balance between maintaining the reliability of WebSocket long connections and efficient utilization of resources, resulting in high operational costs or lost session state.
By setting up a connection status awareness module in the API gateway, maintaining the mapping relationship between the client identity of the WebSocket connection and the FaaS instance, dynamic management of connection status and precise routing of requests is achieved. At the same time, a connection activity detection mechanism based on time window is adopted to optimize the use efficiency of instance resources.
It realizes the reliable state maintenance of long connections of WebSocket under FaaS architecture, significantly reduces operational costs, avoids the continuous occupation of instance resources, ensures the continuity of session state, and improves the scalability and resource utilization efficiency of the system.
Smart Images

Figure BDA0005258848100000041 
Figure BDA0005258848100000071 
Figure BDA0005258848100000072
Abstract
Description
Technical Field
[0001] The present invention relates to the field of cloud computing technology, and in particular to a method and system for maintaining a WebSocket long connection state under a FaaS (Function as a Service) architecture. Background Art
[0002] With the rapid development of cloud computing technology, Serverless architecture has been widely used in the field of cloud computing due to its advantages such as pay-as-you-go and maintenance-free. Among them, FaaS (Function as a Service), as the core component of Serverless architecture, enables developers to focus on the implementation of business logic without paying attention to infrastructure management by providing function-level computing resources. In the fields of real-time communication, online games, and the Internet of Things, long-connection communication based on WebSocket has important applications in FaaS architecture.
[0003] Currently, there are two main technical solutions for implementing WebSocket persistent connections in the FaaS architecture: one is to directly use the FaaS instance to maintain the WebSocket connection by continuously running the function instance; the other is to maintain the WebSocket connection at the API gateway layer, with the gateway responsible for connection management, and the FaaS instance is called only when business logic needs to be processed. Both solutions attempt to achieve efficient resource utilization while maintaining connection reliability.
[0004] However, these solutions all have obvious limitations. Directly using FaaS instances to maintain connections will result in continuous use of computing resources. Since FaaS charges based on resource usage time, this approach will result in higher operating costs. Although the solution of managing connections through an API gateway solves the problem of resource usage, when a WebSocket connection needs to access the previous session state, the FaaS instance may have been released or assigned to other tasks, which will cause the loss of session state and affect the normal operation of the application.
[0005] To solve the above problems, the industry has tried to introduce additional state storage services to save session information or to ensure connection reliability by reserving instance pools. However, these improvements either increase system complexity and maintenance costs or deviate from the original design of FaaS, which is to scale on demand and pay as you go. In addition, these solutions also face performance and scalability challenges when handling a large number of concurrent connections.
[0006] Therefore, how to achieve cost-effective and efficient WebSocket persistent connection management under the FaaS architecture, which can not only ensure the reliability and state consistency of the connection, but also give full play to the advantages of FaaS on-demand scaling, has become a technical problem that needs to be solved urgently in the current cloud computing field. Developing a solution that can balance resource utilization efficiency and connection reliability is of great practical significance. Summary of the invention
[0007] The main purpose of the present invention is to solve the problem of maintaining the long connection state of WebSocket under the FaaS architecture, especially to solve the technical defects of the prior art that either generate too high operating costs or cause the loss of session state. In addition, the present invention also aims to achieve efficient utilization of FaaS instance resources, while ensuring the reliability of WebSocket connections, and maximize the advantages of FaaS on-demand scaling.
[0008] To achieve the above objectives, the present invention provides a FaaS instance state maintenance method based on connection state awareness, the core of which is to set a connection state awareness module in the API gateway, and to achieve dynamic management of the connection state and accurate routing of requests by maintaining the mapping relationship between the client identifier of the WebSocket connection and the FaaS instance. In some embodiments, the method also includes a connection activity detection mechanism based on a time window to optimize the utilization efficiency of instance resources.
[0009] Specifically, the present invention sets a connection status awareness module in the API gateway, which is responsible for maintaining the mapping relationship between the client identifier of the WebSocket connection and the FaaS instance, ensuring that the request can be accurately routed to the specified instance.
[0010] Furthermore, the present invention monitors the data packet transmission of the WebSocket connection within a specified time window through a state detection module, and marks the connection as an active state when data packet transmission is detected, otherwise it is marked as an idle state.
[0011] Preferably, the present invention dynamically determines the running state of the FaaS instance based on the data interaction state of the connection, maintains the instance running when the connection is in an active state, and allows the instance to release resources when it is idle.
[0012] In particular, the present invention assigns a unique client ID to each WebSocket connection, and records and updates the correspondence between the client ID and the FaaS instance that handles the connection in real time. When the state of the FaaS instance changes, the system automatically updates the corresponding mapping relationship to ensure the accuracy and reliability of connection management.
[0013] Optionally, the present invention implements a persistent storage mechanism for the connection state, which saves the state information before the instance is released and can restore the connection state from the storage when needed.
[0014] In some embodiments, the present invention sets a connection activity threshold, and triggers an instance resource release mechanism when the connection activity is lower than the threshold.
[0015] In addition, the present invention can also dynamically adjust the size of the monitoring time window based on the historical connection activity mode, thereby improving the accuracy and efficiency of state detection.
[0016] In other embodiments, the present invention parses the client identifier in the request through the request routing module, and accurately forwards the request to the target FaaS instance according to the mapping relationship, thereby ensuring accurate routing and processing of the request.
[0017] In a preferred embodiment, the present invention realizes reliable maintenance and efficient management of WebSocket connection state through the collaborative work of connection state perception module, state detection module, instance management module and request routing module. In addition, through the cooperation of state persistence module and state recovery module, the reliability and fault tolerance of the system are further enhanced.
[0018] By adopting the above scheme, the present invention has the following beneficial effects: 1. Realizes reliable state maintenance of WebSocket long connections under the FaaS architecture; 2. Significantly reduced operating costs and avoided continuous occupation of instance resources; 3. Ensure the continuity of session status and solve the problem of state loss; 4. Improved the scalability and resource utilization efficiency of the system; 5. Simplified the development and maintenance process and reduced the system complexity.
[0019] In summary, the present invention provides a technical solution that takes into account both resource efficiency and connection reliability, effectively solves the technical problem of WebSocket long connection management under the FaaS architecture, and has important practical value. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] In order to more clearly illustrate the technical solution of the present invention, the following briefly introduces the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present invention, and for those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative work.
[0021] Figure 1It is the overall system architecture diagram, which shows the relationship and data flow between the API gateway and various functional modules (connection status perception module, status detection module, instance management module and request routing module).
[0022] Figure 2 It is a connection state management flow chart that describes the complete life cycle from connection initialization to release, as well as the transition process of the connection state (active / idle).
[0023] Figure 3 It is a flow chart of activity detection, which shows the complete processing flow of packet monitoring, activity calculation, state determination and state transition based on time window.
[0024] Figure 4 It is an instance lifecycle state diagram that describes the state transition process of a FaaS instance from initialization to termination, including detailed state management in the running state and idle state.
[0025] Figure 5 It is a state persistence architecture diagram, which shows the complete process from state generation, encoding, storage to recovery, including key links such as storage layer selection, state synchronization, and consistency check.
[0026] Figure 6 It is a request routing sequence diagram that describes in detail the complete interaction process from the client initiating a request to the final establishment of a connection, including steps such as routing decision, instance status query, and request forwarding. DETAILED DESCRIPTION
[0027] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0028] In the description of the present invention, it should be noted that the terms "center", "up", "down", "left", "right", "vertical", "horizontal", "inside", "outside" and the like indicate directions or positional relationships based on the directions or positional relationships shown in the accompanying drawings, and are only for the convenience of describing the present invention and simplifying the description, and do not indicate or imply that the device or element referred to must have a specific direction, be constructed and operated in a specific direction, and therefore cannot be understood as a limitation on the present invention.
[0029] Example 1: Basic system architecture
[0030] See also Figure 1As shown, the basic architecture of the FaaS instance state maintenance system based on connection state awareness provided by the present invention includes an API gateway 100, a connection state awareness module 200, a state detection module 300, an instance management module 400 and a request routing module 500. This architecture realizes efficient management of WebSocket long connections under the FaaS architecture by introducing a connection state awareness mechanism at the API gateway layer.
[0031] The API gateway 100 is the entry layer of the system and is responsible for receiving and processing all WebSocket connection requests. The connection status perception module 200 is set in the API gateway 100. By establishing a mapping relationship table between the client identifier and the FaaS instance, accurate tracking of the connection status is achieved. The mapping relationship table adopts a key-value pair storage structure, where the key is the client unique identifier and the value is the corresponding FaaS instance identifier and its status information.
[0032] The state detection module 300 works in conjunction with the connection state perception module 200 to continuously monitor the data interaction of the WebSocket connection. window , count the packet transmission frequency during the window period. When a packet transmission is detected, calculate the connection activity index A conn :
[0033] Where N packets Indicates the number of packets within the time window.
[0034] The instance management module 400 performs lifecycle management of FaaS instances based on the activity data provided by the state detection module 300. This module maintains the instance state transition mechanism and defines the instance states such as creation, operation, sleep, and release. State transition follows the following rules: conn Greater than the preset threshold A threshold Keep the instance running when A conn Below A threshold And the duration exceeds the preset time T idle , triggers the instance release process.
[0035] The request routing module 500 is responsible for ensuring that the WebSocket request is accurately forwarded to the specified FaaS instance. This module implements a routing algorithm based on the client identifier and determines the target instance of the request by querying the mapping relationship table maintained by the connection state perception module 200. The routing process includes four steps: request parsing, identifier extraction, instance search, and request forwarding, which ensures the continuity and state consistency of request processing.
[0036] In this architecture, each module communicates with each other through a standardized interface. The connection status perception module 200 provides a unified status query and update interface to other modules, realizing a loose coupling design between modules. The system adopts an asynchronous event processing mechanism and transmits status change notifications through the event bus, which improves the system's response performance and scalability.
[0037] The architecture design in this embodiment supports horizontal expansion. When the system load increases, the system capacity can be increased by adding API gateway nodes and FaaS instances. At the same time, this architecture also provides a basic framework support for the implementation of specific functions described in subsequent embodiments.
[0038] It should be noted that the above architecture is only a preferred embodiment of the present invention, and those skilled in the art can appropriately adjust and optimize the system architecture without violating the design concept of the present invention. For example, additional functional modules can be added or the interaction mode between modules can be adjusted according to the requirements of specific application scenarios.
[0039] Example 2: Connection status management
[0040] Based on the system architecture described in Example 1, this example describes in detail the specific implementation method of connection state management. Figure 2 As shown, connection state management includes three core links: client identity allocation, mapping relationship maintenance and state update.
[0041] The client identifier allocation adopts a distributed unique identifier generation algorithm to ensure the uniqueness of the identifier in a distributed environment. The identifier consists of three parts: timestamp, node identifier and serial number. The specific generation formula is: ClientID=(timestamp<<42)|(nodeID<<10)|sequence(2)
[0042] Among them, timestamp is a timestamp accurate to milliseconds, nodeID is the unique identifier of the API gateway node, and sequence is an increasing sequence number. This design not only ensures the uniqueness of the identifier, but also includes time and node information, which is convenient for subsequent tracking and management.
[0043] The mapping relationship maintenance is implemented through a high-performance distributed key-value storage system. The data structure of the key-value pair is defined as follows: Key:ClientID Value:{instanceID,connectionState,lastActiveTime,metadata}
[0044] Among them, instanceID is the unique identifier of the FaaS instance, connectionState indicates the connection status (including ACTIVE, IDLE, DISCONNECTED, etc.), lastActiveTime records the last active time, and metadata stores additional connection metadata.
[0045] The state update mechanism uses an event-based asynchronous processing mode. When the following events are detected, the mapping relationship update is triggered: 1. WebSocket connection establishment: creating a new mapping record 2. Packet transmission: Update lastActiveTime 3. Connection status change: Update connectionState 4. Instance status changes: Update instanceID and related status
[0046] In order to improve the reliability of the system, this embodiment implements the persistent storage and fault tolerance mechanism of the mapping relationship. By introducing the two-phase commit protocol, the consistency of the mapping relationship in a distributed environment is ensured. The atomicity of the state update is guaranteed by the following formula: UpdateSuccess=Commit(ClientID,NewState)∧Persist(ClientID,NewState)(3)
[0047] When a node failure occurs, the system can restore the mapping relationship through persistent storage and resolve potential data conflicts through the version number mechanism. Version control uses the vector clock algorithm: VersionVector={(nodeID1,counter1),...,(nodeID n ,counter n )}(4)
[0048] In order to optimize performance, this embodiment adopts a multi-level cache strategy. Frequently accessed mapping relationships are stored in the memory cache, and less frequently accessed data is stored in the persistent storage layer. The cache update adopts a write-through strategy to ensure data consistency.
[0049] The cache hit rate is optimized through a dynamically adjusted LRU (Least Recently Used) algorithm: CacheSize=min(base_size+α×connection_count,max_size)(5)
[0050] Among them, α is the elasticity factor and connection_count is the number of currently active connections.
[0051] The innovation of this embodiment is that it realizes accurate tracking and efficient management of WebSocket connection status under the FaaS architecture through distributed unique identifiers and event-driven state management mechanism. This solution not only solves the problem of state consistency in traditional methods, but also provides good scalability and fault tolerance.
[0052] It should be noted that the specific parameters and algorithms in this embodiment can be adjusted according to the actual application scenario. For example, the bit allocation of the identifier can be adjusted according to the system scale, or different cache strategies can be selected according to performance requirements. In addition, the technical solution of this embodiment can also be extended to other distributed systems that require precise state management.
[0053] Example 4: Instance lifecycle management
[0054] Based on the above embodiments, and combined with Figure 4 The state transition diagram shown in FIG. 1 shows a state transition diagram of a FaaS instance. This embodiment describes in detail a method for managing the lifecycle of a FaaS instance. The method achieves an optimal balance between resource utilization and connection reliability by precisely controlling the state transition of the instance.
[0055] The instance lifecycle state machine defines four basic states: INITIALIZING, RUNNING, IDLE, and TERMINATED. State transitions are triggered by connection activity, and the transition probability model is defined as follows:
[0056] Among them, the state transition probability p ij Dynamic calculation based on historical activity patterns: f(A score ) is the activity adjustment function, which is used to adjust the conversion probability according to the current activity:
[0057] Instance resource management uses a predictive scaling strategy. By analyzing historical activity patterns, future resource requirements are predicted:
[0058] Among them, α is the current resource weight, w i is the historical data weight, satisfying
[0059] In order to optimize resource utilization efficiency, a dynamic time window adjustment mechanism is introduced. The window size is dynamically adjusted according to the activity change rate:
[0060] Among them, γ is the adjustment coefficient, is the activity change rate.
[0061] This example innovatively introduces a state preheating mechanism to reduce cold start delays by preheating instances in advance:
[0062] Among them, A trend is the activity trend indicator, and μ is the warm-up sensitivity parameter.
[0063] The resource release strategy adopts a progressive solution, and the release timing is determined by calculating the cost-effectiveness of resources:
[0064] Among them, P reuse is the resource reuse probability, C maintain is the maintenance cost, T prediction Forecast validity period.
[0065] The consistency guarantee during the state migration process is achieved through a two-phase commit protocol: CommitSuccess=PreparePhase(S new )∧CommitPhase(S new )(13)
[0066] Among them, S new Each stage contains status verification and rollback mechanisms.
[0067] The core innovation of this embodiment is to achieve intelligent regulation of the FaaS instance life cycle through probabilistic state machines and predictive resource management. This solution not only ensures the reliability of WebSocket connections, but also maximizes resource utilization efficiency.
[0068] It should be noted that the parameters and thresholds in this embodiment can be adjusted according to the actual scenario. For example, the preheating strategy can be adjusted according to the business peak period, or the resource release strategy can be adjusted according to the cost target. In addition, the lifecycle management method can also be extended to other cloud computing scenarios that require precise resource control.
[0069] Example 5: State persistence
[0070] Combined with the technical basis of the above embodiments, and referring to Figure 5The state persistence architecture shown in the embodiment describes in detail the persistent storage and recovery method of the WebSocket connection state. The method ensures reliable storage and rapid recovery of the connection state through a layered storage architecture and an incremental synchronization mechanism.
[0071] The state information adopts a layered coding structure, including a basic state layer and an incremental state layer. The state coding format is defined as follows:
[0072] The state compression adopts the incremental encoding algorithm, and the compression ratio calculation formula is:
[0073] Persistent storage uses a distributed log structure, and each state change record contains the operation type and state data: LogEntry={OpType,StateData,Timestamp,Checksum}(16)
[0074] Among them, the checksum calculation adopts the weighted hash algorithm: Checksum=Hash(w1×OpType+w2×StateData+w3×Timestamp)(17)
[0075] State synchronization uses an incremental synchronization mechanism based on version vectors:
[0076] The state recovery process consists of three stages, and the completion of each stage is calculated by the following formula:
[0077] Among them, Stage i Indicates the completion status of the i-th stage, w i is the weight coefficient.
[0078] To ensure data consistency, a consensus mechanism based on Paxos is introduced. Status submission must meet the following requirements:
[0079] The state rollback adopts the Copy-on-Write strategy. The rollback cost is calculated as follows:
[0080] Among them, T snapshot is the snapshot recovery time, T undoi Rollback time for a single operation.
[0081] The innovation of this embodiment lies in the design of a hierarchical state storage structure and an incremental synchronization mechanism, which achieves efficient state persistence and recovery. To optimize performance, an adaptive cache strategy is introduced:
[0082] Among them, P access is the access probability, calculated based on historical access patterns:
[0083] State merging uses a timestamp-based eventual consistency solution: MergedState=Merge({State i |timestamp i ≤T current})(twenty four)
[0084] Conflict resolution is achieved through a vector clock algorithm:
[0085] This embodiment also includes a performance optimization mechanism to improve throughput through batch processing:
[0086] It should be noted that the specific parameters in this embodiment can be adjusted according to actual needs. For example, the compression strategy can be adjusted according to the storage capacity, or the synchronization frequency can be adjusted according to the consistency requirements. In addition, the state persistence solution is also applicable to other distributed system scenarios that require reliable state management.
[0087] Example 6: Request routing
[0088] Based on the technical framework constructed in the above embodiments, and referring to Figure 6 The request routing flow chart shown in the figure describes in detail the precise routing method of WebSocket requests in this embodiment. The method realizes precise forwarding and processing of requests through multi-layer routing decision and dynamic load balancing.
[0089] The request routing process first performs client identity resolution. The identity extraction algorithm is defined as follows:
[0090] The routing decision adopts a multi-factor scoring model, and the routing score calculation formula is:
[0091] Among them, routing factors include instance affinity, load status, network latency, etc.:
[0092] The instance affinity score is calculated using historical session data:
[0093] The load balancing strategy uses the weighted minimum connection number algorithm:
[0094] Network latency evaluation is based on exponential moving average: Latency new =β×Latency current +(1-β)×Latency old (31)
[0095] Request forwarding uses an adaptive retry mechanism, and the retry probability is calculated as follows:
[0096] In order to improve routing efficiency, a routing cache mechanism is introduced to evaluate the cache effectiveness: CacheValid = (T current -T cache ) <T threshold ∧StateUnchanged(33)
[0097] The consistency of routing decisions is ensured by distributed locks: LockAcquired=TryLock(ClientID,Timeout)∧ValidateState()(34)
[0098] This embodiment innovatively introduces a routing prediction mechanism to predict the optimal routing path based on historical routing patterns:
[0099] Among them, the routing pattern features are extracted through timing analysis: RoutePattern={timestamp,source,destination,performance}(36)
[0100] To handle burst traffic, an adaptive current limiting algorithm is designed:
[0101] Request priority dynamic adjustment mechanism:
[0102] Among them, the adjustment factors include characteristics such as business importance and response time requirements.
[0103] The core innovation of this embodiment is to achieve accurate forwarding of WebSocket requests through multi-dimensional routing decisions and predictive routing mechanisms. This solution not only ensures the continuity of request processing, but also provides flexible load balancing capabilities.
[0104] It should be noted that the parameter configuration in this embodiment can be adjusted according to the actual scenario. For example, the routing factor weight can be adjusted according to the business characteristics, or the cache strategy can be adjusted according to the system scale. In addition, this routing solution is also applicable to other distributed system scenarios that require precise request scheduling.
Claims
1. A FaaS instance state maintenance method based on connection state awareness, characterized in that: include: (a) Setting a connection status awareness module in the API gateway, wherein the connection status awareness module is used to maintain a mapping relationship between a client identifier of a WebSocket connection and a FaaS instance; (b) Detect the data interaction status of the WebSocket connection; (c) determining the running status of the FaaS instance based on the data interaction status; (d) Routing the WebSocket request to the specified FaaS instance according to the mapping relationship.
2. The method according to claim 1, characterized in that The detection of the data interaction state of the WebSocket connection includes: (a) Monitor the data packet transmission of the WebSocket connection within a specified time window; (b) when a packet transmission is detected, marking the connection as active; (c) When no packet transmission is detected within a specified time window, the connection is marked as idle.
3. The method according to claim 2, characterized in that Determining the running state of the FaaS instance based on the data interaction state includes: (a) When the WebSocket connection is active, maintain the running state of the corresponding FaaS instance; (b) When the WebSocket connection is idle, the corresponding FaaS instance is allowed to release resources.
4. The method according to claim 1, characterized in that: The mapping relationship between the client identifier and the FaaS instance for maintaining the WebSocket connection includes: (a) Assign a unique client identifier to each WebSocket connection; (b) Record the correspondence between the client identifier and the FaaS instance that processes the connection; (c) When the state of the FaaS instance changes, update the mapping relationship.
5. The method according to claim 1, characterized in that Also includes: (a) Setting the connection activity threshold; (b) When the connection activity is lower than the threshold, the FaaS instance resource release mechanism is triggered.
6. The method according to claim 2, characterized in that Also includes: (a) Dynamically adjusting the size of the specified time window, specifically including: (b) Statistics of historical connection activity patterns; (c) adjusting the time window size based on the historical connection activity pattern.
7. The method according to claim 1, characterized in that Also includes: (a) Before the FaaS instance is released, the connection status information is persistently stored; (b) When the connection needs to be restored, the connection status information is read from the persistent storage.
8. The method according to claim 1, characterized in that Routing the WebSocket request to the specified FaaS instance includes: (a) Parse the client identifier in the request; (b) determining a target FaaS instance according to the mapping relationship; (c) Forward the request to the target FaaS instance.
9. A FaaS instance state maintenance system based on connection state awareness, characterized in that: include: (a) A connection status awareness module, which is used to maintain the mapping relationship between the client identifier of the WebSocket connection and the FaaS instance; (b) a status detection module, used to detect the data interaction status of the WebSocket connection; (c) an instance management module, configured to determine the running state of the FaaS instance based on the data interaction state; (d) A request routing module, used to route the WebSocket request to a specified FaaS instance according to the mapping relationship.
10. The system according to claim 9, characterized in that Also includes: (a) State persistence module, used to save connection state information before the FaaS instance is released; (b) A state recovery module, used to restore connection state information from persistent storage when needed.