How does Blazor carry objects across servers when load balancing

David Thielen 3,211 Reputation points
2023-03-05T01:39:09.9433333+00:00

Hi all;

I have some Blazor server side code that I can reduce down to this little bit:

@foreach (var employee in Employees)
{
    <tr>
        <td class="btn-group-sm">
            <button class="btn btn-outline-danger"
               @onclick="() => HandleDelete(employee)">
               Delete
            </button>
        </td>
    </tr>
}

And that calls HandleDelete(employee) on a click passing the employee object that it has persisted in the session data (I assume) to pass when the click occurs.

However, if I have multiple servers being load balanced, what happens if the submit event is sent to a different server? That different server won't have the employee object (I believe).

Does this all depend on the https session staying connected to the original server and all subsequent communication going to that server?

And if so, in a fail-over situation, what happens. In that case it's definitely a connection to a new server getting that submit action.

??? - thanks - dave

Developer technologies .NET Blazor
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-03-06T16:50:37.05+00:00

    blazor server app has a persistent connection and state data (circuit) stored in the server memory. so events are sent to the same server. load balancing only load balances the initial connection. if load balancing, sticky sessions (cooke identifies the server) are used so in the case of lost connection, the client reconnects to the same server, and the same circuit if it exists.

    in the case of reconnect, if the same circuit can not be found, the app is reloaded and all data is lost. you can check for saved state at starup to restore state, if you wrote code to save the state to a persistent store.

    a common approach is to store state in the browser's local storage. on init reload storage if any and reset state:

    https://gist.github.com/SteveSandersonMS/ba16f6bb6934842d78c89ab5314f4b56

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-03-06T01:57:59.34+00:00

    Hi @David Thielen

    For permanent data persistence that spans multiple users and devices, the app can use server-side storage. Options include:

    • Blob storage
    • Key-value storage
    • Relational database
    • Table storage

    After data is saved, the user's state is retained and available in any new circuit.

    For more information on Azure data storage options, see the following:

    More detail information, see ASP.NET Core Blazor state management.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.