# Migration

## Overview

This page describes the behavior of migration on Diarkis servers.

## What is Migration?

Diarkis servers are configured in a cluster, and users connect to one of these servers. Consider a scenario where the connected Diarkis server is scaled in and the server to which the user is connected is shut down. For a typical server, users connected at that time would be disconnected because the server stops. However, Diarkis servers can move within the cluster, allowing users to continue receiving services without disconnection.\
This mechanism, where the service is maintained by reconnecting from the server currently connected to another server, is called **migration**.

While we explained it using the example of scaling-in for simplicity, migration can occur due to various factors besides scaling-in. When migration occurs, the server and client application need to collaboratively proceed through the migration process. The following sections explain the specific processes when migration occurs.

If you are participating in a Room, please do not use the features below and refer to the [Room-specific migration](/en/diarkis-modules/room/setup-client.md#room-migration).

## Overall Flow of the Migration Process

Migration begins with a notification from the server.\
When the server determines that migration is necessary due to various factors, it notifies the client of the need for migration, triggering `DiarkisTcpBase::OnOffline`/`DiarkisUdpBase::OnOffline()`. Upon receiving this event, which indicates that migration is necessary, the client executes `DiarkisTcpBase::SendMigrate()`/`DiarkisUdpBase::SendMigrate()` to request the initiation of migration from the server. Once the server receives the migration request, it disconnects the requesting client, moves the user's data on the server to a new server, and the client reconnects to the new server.\
These processes are undertaken during migration.

## Client Response During Migration

As explained in the previous section, on the client side, after receiving `DiarkisTcpBase::OnOffline`/`DiarkisUdpBase::OnOffline()`, it is necessary to proceed with the migration process by executing `DiarkisTcpBase::SendMigrate()`/`DiarkisUdpBase::SendMigrate()`.

In the sample provided with the SDK, `SendMigrate()` is implemented to be called immediately after `OnOffline()` is triggered, as shown below:

```
void DiarkisTcp::OnOffline()
{
    DiarkisTcpBase::OnOffline();

    // Due to server scaling-in, the connected server will go offline.
    // Invoke SendMigrate at an appropriate timing that matches the application's implementation to perform server migration.
    this->SendMigrate();
}
```

```
void DiarkisUdp::OnOffline()
{
    DiarkisUdpBase::OnOffline();

    // Due to server scaling-in, the connected server will go offline.
    // Invoke SendMigrate at an appropriate timing that matches the application's implementation to perform server migration.
    this->SendMigrate();
}
```

Note that when executing `DiarkisTcpBase::SendMigrate()`/`DiarkisUdpBase::SendMigrate()`, the reconnection process starts, temporarily disabling communication as the connection to the server is disconnected briefly. The timing of disconnection can be controlled by the timing of executing `DiarkisTcpBase::SendMigrate()`/`DiarkisUdpBase::SendMigrate()`, so please adjust it according to your application's needs.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.diarkis.io/en/diarkis-client/diarkis-module/migration.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
