Event-Driven Webapps That Work Local-Second
You're driving from Germany to Italy for a family holiday, a trip of more than 1500 km, so you plan an overnight stop in Austria. After a whole day at the wheel, you reach the hotel, keen to stretch your legs, let the kids run around a bit, and see what the Austrian city has to offer.
You’re driving from Germany to Italy for a family holiday, a trip of more than 1500 km, so you plan an overnight stop in Austria. After a whole day at the wheel, you reach the hotel, keen to stretch your legs, let the kids run around a bit, and see what the Austrian city has to offer.
Glad to be done with driving for the day, you walk into the hotel and head to the reception desk. And that’s where the trouble begins! The Internet is down, so they can’t check you in. “Look,” you say, “I’ve got it all right here, the booking and the confirmation, couldn’t you hand me the room key now and finish the check-in later?”. “Sorry,” they answer, “the only way to get a key is to check in through the System”. The System is, naturally, a run-of-the-mill web app running in a browser.
You’re asked to wait, and you do; 10 minutes go by, then 20 – the kids are beginning to wreck the lobby – and at last, after 30 minutes, the connection comes back and the check-in goes through. You could argue it’s only 30 minutes. Then again, you’ve been coding since you were about 12, and you can’t stop asking yourself – must we really put up with technology like this? Couldn’t it be better?
Naturally, it could! This is simply poor (though likely cheap) system design that delivers a “sub-optimal” – putting it kindly – user experience whenever the network has problems.
Local-first?
For a long time now, I’ve liked the local-first approach. It’s packed with ideas that just feel “right”: your data belongs to you; a central server is a convenience rather than a requirement; the application keeps working through disruptions. So it occurred to me – could this be the answer to the hotel check-in issue?
Mulling it over during the next day’s long drive, I concluded that a complete local-first app would most likely be overkill for a problem like this. The tools and techniques exist, e.g. CRDTs and automerge, but they shine in areas such as collaborative editing and mobile apps with patchy connectivity.
We can, however, borrow some of those ideas to build a “local-second” solution. Normally, the system would work in the classic, centralized client-server way, i.e. as an entirely standard webapp+HTTP API.
When the network fails, though, it would fall back to an offline mode with reduced functionality. The essential services remain available – and data gets buffered until connectivity returns.
Here, the essential functionality is checking guests in so they can rest after a long journey; other operations may be unavailable.
The event-driven PoC
With the help of AI coding assistants (Claude Code, in case you’re wondering), I built a PoC application that implements the skeleton of such a local-second webapp together with its backend. Before we get to the code, though, a few decisions about the technologies and the architecture itself need to be made.
In a traditional web app, all logic is fully controlled and centralized on the server. Here, by contrast, besides that server-side logic, we sometimes (during offline periods) have data changes made entirely on the client and synchronized only afterwards.
To make such a setup easier to reason about, I chose to build on events. The backend is event-driven: events are the primary source of truth, and they are persisted, which gives us an audit trail and a way to build projections for fast queries.
Event sourcing is frequently associated with NoSQL databases and the CQRS pattern, and it tends to be complex because of eventual consistency. That’s not a necessity, though; event sourcing can be implemented on top of a relational database, giving you the advantages of atomic transactions.
This is the route I chose: transactional event sourcing on PostgreSQL, with projections that are always consistent because they are updated in the same transaction in which the event is inserted.
In online mode, the app behaves like a perfectly ordinary, unexciting CRUD web app. As a user of the backend HTTP API, you never see the events. There are /checkin, /checkout, POST /bookings (creating a booking) endpoints. Internally, they produce events and update the corresponding projections. Current bookings are fetched with a plain GET /bookings request, which queries a table built as a projection of the events. Why, then, bother with events at all?

Because they drastically simplify the architecture and the way you reason about the system once offline mode takes over. While running degraded, we must be able to check guests in locally, buffering the changes (such as room assignment). Once the system is back online, it has to send each of those changes to the backend, which must apply them to the database. And the backend (provided the client is properly authenticated) must always accept such requests: events are immutable facts; the check-in has already taken place.
So offline check-ins are collected as client-side events. When the system comes back online, they get sent one-by-one to the backend. There, each client-side event is translated into a server-side event and handled just like any event produced “normally”.

The logic is neatly contained in the event handlers, which run both when an event comes from an online HTTP API call and when it comes from the offline catch-up mechanism. On top of that, client and server share the same concept of events as facts that occurred in the system, which gives a consistent framework for reasoning about the application.
Technicalities, and syncing backend data to frontend
We have yet to pick the technologies! For the PoC’s frontend, I chose React+Typescript. For the backend, I went with Rust+Axum+sqlx (because – why not?). Any backend language capable of talking to PostgreSQL would work, and Rust is both fast and strongly type-checked, which very much suits my taste.
As a side quest, I wanted to find out how Claude Code copes when asked to implement a non-mainstream backend pattern (transactional event sourcing), guided by the articles mentioned above, in a language it wasn’t mainly trained on and which is hardly the go-to choice for CRUD backends. The result? A full report will appear in next month’s This Month We AIed, but here’s a spoiler: it did quite well, and nearly all of the backend code is AI-generated, though it needed close supervision.
There are two frontend applications: one lets guests book hotels, the other lets front-desk staff check guests in and out. The implementation is extremely minimal, more of a skeleton; it’s missing key features, such as any kind of security.
One more piece of functionality hasn’t been addressed yet: keeping the frontend state in sync with the backend state. A front-desk clerk would normally keep the application open all the time, so the backend state has to be synchronized to the frontend continuously.
As usual, there are several sensible ways to do this. The frontend might, for instance, open a WebSocket connection to the backend, receive a stream of events (not necessarily the same ones the backend persists, but some transformation of them) and apply them locally. Or it might poll for today’s bookings at regular intervals. Or …
There are, in fact, a few frontend solutions built specifically for local-first applications, so I began my search there. One of them is RxDB, a local-first JavaScript database with functionality for syncing data to the backend. It’s tempting, but a closer look shows it’s geared more towards “full” local-first applications and isn’t necessarily ideal for our local-second approach. In RxDB, the client mostly manages the data, and conflicts are also resolved on the client before the server accepts the writes. A fascinating technology, just not the right match.
Another option is ElectricSQL, which lets you continuously synchronize a PostgreSQL query into a JavaScript app. It runs as a separate application alongside your backend, but clients aren’t supposed to talk to it directly. Your backend should act as a proxy instead, taking care of authentication and authorization before passing a query on to the Electric instance.
On the frontend, it uses long-polling requests to keep receiving data updates. That’s just what we’re after!
Electric has no built-in mechanism for syncing local writes to the backend, but it describes several patterns you could use.
Our event-driven approach resembles “shared persistent optimistic state”, but offers a more general answer to the question of how to store local writes. It consistently maps client-side events onto server-side concepts and keeps a high level of abstraction, instead of operating on the level of collecting low-level insert/update operations.
ElectricSQL in practice
How does the PoC use ElectricSQL? The central concept in Electric is the shape, a query kept in sync with the frontend. The Rust backend app exposes a /hotels/{id}/bookings/shape endpoint, which proxies a specific query to the Electric instance running locally. As a result, the client can’t stream arbitrary queries, only one fixed query.
Here, that query reads data from the bookings projection, which is updated transactionally whenever booking-related events are handled. Each time the current day’s bookings change, every connected client gets the data right away, and it’s then rendered through ordinary React updates.

The drawback is that using Electric the way the PoC does introduces a small eventual consistency gap. Take checking out a guest: the backend handles the request first, generating an event and updating the bookings projection. At that point the request is complete, and so is the client-side interaction.
It’s worth pointing out that in architectures like CQRS, the eventual consistency gap usually sits on the server. Here, the eventual consistency is on the client. That difference may or may not matter. It’s no issue in this application, but it could be in others, so keep it in mind.
Electric then notices the change and pushes the modifications to the clients, which refreshes the view. Normally the update looks instantaneous, but there can be a short delay between the HTTP API request completing and the update arriving.
On the plus side, when offline events are synced to the backend (once it’s back online), the frontend doesn’t need to do anything – the view just refreshes when new data arrives from the bookings shape. This makes the synchronisation logic much simpler.
Offline support
Our local-second webapp is nearly done, but one detail is still missing. Everything behaves well when the Internet goes down: the webapp switches to a degraded mode that permits only offline check-ins until the connection returns.
But what happens if the front-desk clerk presses refresh and the app reloads? Hitting refresh is practically a reflex when something goes wrong. Instead of our web app, they’d see a “This site can’t be reached” screen.
The fix is to build a Progressive Web App, PWA for short. This standard is by now widely supported and lets you cache resources and query results so they’re available offline. Exactly what we need!
Turning our app into a PWA is fairly easy: it comes down to installing the vite-pwa plugin and supplying some configuration (such as … an app icon, which is mandatory; without it the PWA app fails “silently”, that is, without reporting any errors). We have to list the resources to be cached for offline use (here, all html, css, js files). You can choose whether these resources are network-first, where the browser always looks for newer versions before falling back to the cached ones, or local-first, where the browser always serves locally cached resources for quicker loading, at the expense of harder upgrades. In the PoC hotel application, all resources are network-first.
We also have to indicate which backend query results should be cached. Here, those are the queries returning the list of hotels (shown to the clerk when they start using the app) and the details of the selected hotel (name, number of rooms).
Caching the results of Electric’s bookings shape will not work. They are incremental, and what interests us is the combined result (the initial query results plus updates), not just the most recently received data. For that reason, we have to cache the output of the Electric React hook (which is always the latest, combined data) in the browser’s local storage.
No centralized server
Building a local-second application like this (one that normally runs as a traditional client-server system and falls back to a local-only mode when offline) means taking a few things into account, since there is no longer a central server validating and coordinating every data change.
It could happen, for instance, that two people get checked into the same room. Such a situation needs to be handled somehow. The PoC doesn’t implement this, but the backend should accept conflicting check-in requests like these. The clerks should then see a visual cue about the conflict, sort it out by hand, and be able to check the guest in again to a different room.
This, too, follows from the event-driven approach: an event, once it exists, must be treated as a fact; there’s no arguing with it. It has already happened, and all you can do is try to remedy the situation (possibly by creating compensating events).
And what about several check-in desks? In degraded mode, either just one of them should be in operation, or the clerks would need to agree manually on which rooms they assign to their guests.
Ideally, the local web apps could communicate with each other across the local network. The browser’s security model makes this impossible, though. WebRTC is one thing to try: it offers local connectivity, but still needs a central signalling server to coordinate the initial connections. Another possibility is to run a local server outside the browser that talks to the other local clients and keeps a common view of offline events.
TL;DR
Network outages are simply part of life, whether caused by service failures or by local problems with Internet providers. Hotel check-ins might not be critical enough to warrant the extra system complexity, but then again, shouldn’t user experience come before the technical limits of the hotel’s system?
In any case, a local-second app aims to unite two things: the convenience of a centralized database and server that coordinate the system’s logic, prevent conflicts and keep everyone up to date; and keeping the core functionality available offline through a “degraded mode”, in which only the critical logic runs purely on the client side and is synchronized afterwards.
Building both server & client logic around events gives a consistent framework for understanding and reasoning about the system. Events are immutable facts that the system must always handle. Transactional event sourcing gives you the benefits of event sourcing along with the convenience of running SQL queries against projections that are always consistent. Using events everywhere makes it possible to synchronize local writes to a server while keeping a high level of abstraction.
Lastly, ElectricSQL offers a clean way of syncing backend database changes to the frontend, no matter whether the changes were made online or during catch-up after an offline period.
In online mode, rooms are assigned by the server. In offline mode, a screen for choosing the room manually shows up; once the connection is back, the changes are synced to the other instances of the front-desk application.
Have you run into similar issues in applications you’ve worked on? How did you deal with them?