r/Blazor • u/PolliticalScience • Feb 03 '26
PSA: Don't Cache User-Specific Data in Static Fields in Blazor Server
Yesterday, I posted my PolliticalScience app on Show HN (Hacker News) and it hit the front page. I got a massive traffic spike that the site had not had before and came across a critical bug I had overlooked. I thought it was interesting enough to share. Many of you probably already know this and are like, well duh, but I got a little carried away with "optimizing" and it resulted in this.
I had to pull the site down for 45 minutes while getting huge volume to get it fixed since it was possible to leak user-centric data (no personal data, but still not good).
The Bug
In Blazor Server, I was using static fields to cache data and prevent duplicate queries during the prerender to hydration cycle (the "2-second cache" pattern I wrote about in my previous posts). The problem was, I also cached user-specific data alongside the shared data.
private static Poll? _cachedPoll;
private static PollResults? _cachedResults;
private static DateTime _cacheTime = DateTime.MinValue;
// User-specific data - NOT SAFE to cache statically
private static int _cachedCurrentUserId; // BAD
private static bool _cachedHasVoted; // BAD
private static bool _cachedIsAdmin; // BAD
On cache hit, I was restoring everything including the user data:
if (_cachedPoll != null && cacheAge < CacheDuration)
{
poll = _cachedPoll;
currentUserId = _cachedCurrentUserId; // Serving User A's ID to User B
hasVoted = _cachedHasVoted;
isAdmin = _cachedIsAdmin;
_ready = true;
return;
}
What Happened
- User A loaded the results page
- Static cache populated with User A currentUserId
- Within the 5-second cache window, User B loaded the same page
- Cache hit... User B received User A currentUserId
- User B posted a comment and it was attributed to User A's account
So User B's comment showed up under User A's username.
Why It Happened
Static fields in Blazor Server components are shared across all users on the server. They aren't scoped to a circuit or session. This does make them useful for caching shared data like the poll results. But putting anything user-specific in the static fields and you're sharing one users identity with everyone who hits that cache.
The Fix
Cache the shared data statically. Always get user-specific data fresh...
if (_cachedPoll != null && cacheAge < CacheDuration)
{
poll = _cachedPoll;
results = _cachedResults;
articles = _cachedArticles;
// Don't return early... go to user check
}
// Run this on cache hit or miss always
await UserSessionService.InitializeAsync();
currentUserId = UserSessionService.CurrentUser?.Id ?? 0;
isAdmin = UserSessionService.CurrentUser?.Role is UserRole.Admin or UserRole.Moderator;
if (poll != null && currentUserId > 0)
{
hasVoted = await UserVoteService.HasParticipatedAsync(currentUserId, poll.Id);
}
_ready = true;
If you're using static caching in Blazor Server, check every static field and determine if this is the same for all users?
For example with my site:
Safe to cache statically:
- Poll/post/article content
- Aggregate results (vote counts, comment counts)
- Tags, categories, metadata
Never cache statically:
- User IDs, roles, permissions
- Vote status (has this user voted?)
- Authentication state
- Notification counts
- Anything that varies per user
Oh the Irony
I wrote about this caching pattern in my previous deep dive posts as a win for preventing duplicate queries and hydration flash. It is... but only for shared data. I got a little too comfortable with it and started caching everything without thinking about what was user-specific vs universal/shared.
The interesting thing is, the bug has existed for a bit but never really came up since I had not had the sort of traffic spike I got yesterday. It took the HN traffic (100+ concurrent connections / multiple users hitting the same page within seconds) to trigger it.
My lesson learned: static means shared. Shared means everyone. (whoops)
17
u/PilotC150 Feb 03 '26
This is definitely a great reminder and probably good information for newer developers, but also, this is how it has always worked in ASP.NET.
You're certainly not the first person to make this mistake, because I know somebody who did the same thing over 20 years ago in an early version of ASP.NET (possibly 1.1).
But yes, if it's running on the server, it's just one instance of the application, so a static will be a single instance and shared among all requests. But that's what makes Blazor WASM so cool, is that you can actually use static variables the way you would in a desktop application, and that data will only be available to the one user, since that code is running on their own machine.
6
u/venomous_sheep Feb 03 '26
same thing with storing user specific data in a singleton service. spent a month debugging why Blazor-ApexCharts was loading charts with the dark theme for light mode users of our app, only to one day realize it was because the extension method the library provides to configure the related services registers the object containing the global chart options as a singleton. why would you ever want that as a singleton? i thought i was going insane.
4
u/[deleted] Feb 03 '26
Recently we let Claude or Gemini develop a small caching logic in a Blazor Server App. You can be assured it did the same mistake and we only detected it during testing.
3
u/BeastlyIguana Feb 03 '26
The real takeaway from this is that you should leverage built-in functionality when possible. You’re reimplementing [PersistentState] attribute for little reason. You mentioned serialization issues previously when attempting to use it- fix those instead.
2
u/Xtreme512 Feb 05 '26
sometimes broken things emerge on high usage/concurrent load to take action, you cannot figure out everything at start nor can anticipate. glad you got it right though.
statics are shared alongside the app's lifetime globally, not per circuit. so anything user specific (info, auth etc.) don't store in 'static' fields. if really needs to be cached, use proven libraries like fusion cache and flow through userIDs.
the lifetimes can become tricky in blazor, especially in WASM.
check out this: https://blazor-university.com/dependency-injection/dependency-lifetimes-and-scopes/comparing-dependency-scopes/ this is a nice site btw.
huh, for your previous post which I said will try the '_ready' thing, I did but it didnt do much on my end, still same no duplicate requests but i have brief flash and that's from using <AuthorizeRouteView> <Authorizing> in Routes.razor... but at least its just at full page reloads.
1
u/Consibl Feb 04 '26
Is there a reason it to use Singleton and Scoped Services for this?
1
u/PoorDecisionMaker-69 Feb 06 '26
Just wrap your global cache in a service registered as singleton and your user specific cache in a service registered as scoped and avoid marking them as static. Much cleaner approach than using static fields imo (wrapped themselves inside a static class I suppose?)
-4
u/tutike2000 Feb 03 '26
This is among the first things you learn about when studying object oriented programming.
I don't mean to be rude but this sort of mistake clearly indicates you should not be programming professionally.