Overview
This article describes the differences of session management between the M-Files Desktop Client and REST API based functions and how to ensure REST API calls function correctly in Multi-Server Mode scenarios (such as the M-Files New Cloud).
Details
In the (New) M-Files Cloud all servers are redundant using Multi-Server Mode (this does not apply to the Classic M-Files Cloud). The service consists of multiple identical servers and their incoming traffic goes through the load balancer. The load balancer supports so called sticky sessions that allow the original caller to access only one server. This optimizes performance at the server level. If the node goes to unreachable state for one reason or another, the load balancer diverts the traffic to another node, allowing seamless and invisible transition to end users. Desktop applications utilize this in full, and without any action/configuration needed by the user.
The RESTful interface (the REST API), however, has a different session management system in the M-Files backend. In case of failover, the REST traffic is diverted to other node like with desktop connections, but session data is not shared between nodes. It means that RESTful applications need to re-login in case of failover. A RESTful application should still use sticky sessions even it doesn't support fully automatic recovering yet.
Solution
Sticky sessions for the REST API are based on HTTP Cookies. Cookie information is returned to the caller as part of the "authenticationtokens" endpoint request (see more in the "Multi-Server Mode Considerations" section here) together with the authentication token itself. It is an HTTP response header and you need to add it to all subsequent requests you make in order to enable the sticky session.
If you do not do that, random requests will "work", and random requests will "fail" and get an error (could be "unauthenticated" error, or an error similar to "OAEP padding").
Notes
It is recommended that applications capture errors for the requests they make and handle them accordingly. For example:
- A 500 or 400 server error may contain information that you may want to log for later review.
- You may want to save also information on what action failed so you can execute it again later.
- An "unauthenticated" error (such as HTTP status code 403) should result in the application authenticating again (e.g., to the "authenticationtokens" endpoint) and then using the new token and cookie to execute the request again, and for other subsequent requests.
- You may want to keep an application-wide token cache to avoid having to send the user+password all the time and before every request. So long as the current token works, you can keep using it, when it gets invalidated (e.g., because of age), you will get a request that fails authentication, and then you can authenticate again, and cache the new token+cookies.
