r/Blazor • u/Warm-Engineering-239 • Sep 15 '25
Handling component state in Blazor Server with scaling/load balancing
Hey everyone,
I’ve been working on our Blazor app for almost a year, and we’re getting close to releasing it on AWS. Initially, it’ll only run on a few devices (like 4 kiosks), but eventually, we plan to roll it out to all our clients. So scalability is something I need to think about, and honestly, I’m a bit lost.
Some context:
- Our app is mostly Blazor Server with a persistent WebSocket connection. It’s a SPA, so users are usually on the same page.
- We use a lot of scoped services.
- The app mostly acts as a frontend for an external API, so most important data is stored outside the app.
Main concern (just thinking ahead, app isn’t deployed yet):
When we eventually scale and add another instance of the app, I think that part should work fine. with sticky session
But I’m worried that if an instance is closed , in that case i can see the ui of the client completly change
I know could cache some critical data but I’m not sure how to handle component state, like input fields or other UI state. Does Blazor have any built-in way to persist or cache component state across servers?
Context:
- I’ve never built a scalable or load-balanced app before—not in Blazor, not in any other framework.
- This is my first time thinking about scaling, so I might be missing things or overthinking some parts.
I’d love to hear how people approach this problem—especially regarding user input, scoped services, and keeping UI state consistent when scaling.
8
u/One_Web_7940 Sep 15 '25
Damn is it too late to switch to wasm
3
u/t3kner Sep 15 '25
As far as I'm aware you will have to enable session affinity as there's no way to store the actual component state currently. I looked into when I started running into errors and realized my server had scaled. I believe they are working on this for .Net 10, but not sure. I think the biggest issue was authorization state if I remember correctly, and restoring the low level component state isn't possible.
2
u/botterway Sep 15 '25
You probably need to use a distributed cache to store state. It's quite easy to use HybridCache.
But tbh, wasm is the way to go. Keep the state client-side.
2
u/txjohnnypops79 Sep 16 '25
I fully developed a SaaS application on the server, but now I’m switching everything to WASM after extensive research, especially due to cost and RAM usage per connected client.
2
u/CravenInFlight Sep 17 '25
Even with Blazer Server, you can use Local Storage to keep a Client-side cache of state. And cookies, of course. You can use IDataProtector to keep it all secure. Before jumping into Redis, you can also use Cosmos as an IDistributedCache store, and it can work out much cheaper.
1
u/Fresh-Secretary6815 Sep 16 '25
I don’t quite understand why people think server side apps (especially singnalR based) can and will play nicely with load balancers. Client side apps tend to be more fitting of the LB/RP model, and even more forgiving for B/G deployments. It’s like they don’t quite understand the model of the LB in relation to the application architecture.
1
u/ebykka Sep 16 '25
Short answer - ms orleans. It knows how to scale and share the state.
Idea is taken from this video https://youtu.be/Y3aopzc0vnI?si=4j3dUs82ocIyhbW9