Live data from Hacker News

How we sped up Notion in the browser with WASM SQLite

notion.so

51–60 of 107 posts

Re: How we sped up Notion in the browser with WASM SQLite

#51

> Using SQLite improved page navigation times by 20 percent in all modern browsers. Couldn't you cache the data in LocalStorage and get similar speed improvements? I mean, how to run SQLite in the browser is a great topic and surely has a lot of very good use cases. But attributing the performance improvements to it sounds a bit misleading, when you had no client caching strategy before and you do now. Obviously load…

(I work at Notion, but didn't build the WASM sqlite thingy)

We've implemented this same cache using LocalStorage, IndexedDB, and SQLite. The Android & iOS apps used SQLite for this since 2020 (I built the Android version IIRC), and the desktop app used SQLite running on a native thread since 2021.

We migrated away from LocalStorage for two reasons:

1. LocalStorage is limited to 10mb file size. We also use LocalStorage for a bunch of less-durable state like "is this toggle open?" or "which view in this database was open last", and as our customer workspaces grew, we faced mounting errors as the record cache competed with the rest of the app for that space. We'd have a bunch of slowdowns under contention as we tried to delete keys in LocalStorage synchronously, which manifested as major UI lag. No good!

- LocalStorage loves to lose writes if you're writing from multiple tabs. It's just not a reliable or trustworthy API. For a cache that doesn't matter as much, but we'd still end up with cache misses for power users for pages that should really be totally locally loadable.

I implemented the IndexedDB version in 2019, to replace the earlier LocalStorage option. We used the IDB record cache in the desktop app until we switched over to native SQLite there in 2021 (https://www.notion.so/blog/faster-page-load-navigation) but we never shipped it for browser users for a few reasons:

- Performance and reliability problems with IDB in browsers is hard to debug; in the native app we can trust the version of Chromium we ship and remediate issues using Electron APIs, where as in the browser wild we're at the mercy of the user-agent

- Our testing in the browser showed limited performance improvements across all device categories: faster devices & scenarios were even faster with IndexedDB, but slower devices & scenarios could be even slower.

The reason I'd attribute for the performance challenge is that IndexedDB pays a high tax per row written and row read. It can be fine in terms of total throughput for a cache if you have large, coarse-grained cache rows, like caching all of a document as a single object, and you update the cache infrequently.

Notion's data model is tree/graph of very fine-grained records; each paragraph is its own database row. Our cache on IndexedDB would perform great for smaller workspace sizes and for a single tab, but with multiple tabs and medium-to-large workspaces, we'd hit contention in IndexedDB and get major slowdowns.

We should improve our cache architecture to have another layer of cache that does whole-pages, but need to weigh the improvement/complexity there versus other performance opportunities.

Re: How we sped up Notion in the browser with WASM SQLite

#52
post #15

Pretty neat! If notion folks are in this thread, what do you do about lack of SharedWorker in Android Chrome?

This is an optional cache so Notion continues to work the way it did before in browsers that don't support WASM SQLite.

As an alternative we have apps for iOS, Android, Mac, Windows. In those apps we already run SQLite in a native thread talking to the normal filesystem for years.

Re: How we sped up Notion in the browser with WASM SQLite

#53

> OPFS doesn’t come with graceful handling of concurrency out of the box. Developers should be aware of this and design around it. There's a multiple readers and writers proposal [0]. It's been "position: positive" by Firefox [1], implemented in Chrome [2], and ignored by Webkit [3] (of course). 0: https://github.com/whatwg/fs/blob/main/proposals/MultipleReadersWriters.md 1: https://github.com/mozilla/standards-posit…

It sucks that even the newest make-wadm-sqlite-fast FileAccessHandles are still an intermediated virtual file system hosted by the browser. I don't get why we have three different file system APIs on the web and none that just use files. Meanwhile WASI has their own server-side file system APIs but neither WASI nor the browser side seem to have any effort to get on the same page. It'll be 2035 before component-model…

The profusion of underpowered interfaces is frustrating but I don't see how browsers could

"just use files"

What does this mean? Providing the POSIX filesystem C language APIs with file descriptor numbers, etc? I guess the browser makers could go Cosmopolitan-C style and emulate the Linux filesystem interface on all platforms, but I think you'd find there's still a weird intermediary layer in between your code and the underlying OS filesystem. Even on Linux the browser would have a virtual filesystem to provide sandboxing & quality-of-service governance. On other systems you'd need a virtual filesystem to paper over those differences between operating systems. Windows exists!

Re: How we sped up Notion in the browser with WASM SQLite

#54

Honestly, we should just have native SQLite supported by the browser. SQLite has become the defacto local single file database standard. It has a very permissive license. It has support contracts out to the 2050s (service lifetime of Airbus A350 airframe). SQLite will probably have a longer lifetime than whatever browser standard we have now.

Unfortunately WebSQL was ahead of its time, I have no doubt it would have caught on more today with the resurgent popularity of SQLite.

IndexedDB got to ride the NoSQL hype of the 2010's

Re: How we sped up Notion in the browser with WASM SQLite

#55

Earlier quoted context omitted.

I think their real innovation was the community and getting "cosy productivity" during Covid going. They are more of a social phenomenon than a technical one.

Notion is a great example of my hatred of the current "upgrade or die" delivery method of software. Notion 1.0, when it was mostly text with a unique way to view tabular data, was fast and enjoyable to use. Once it shifted to competing with Atlassian et al, it acquired all the bloat those products already have.

Sadly, I don’t think basing a business (with the tech investor desire for growth! growth! growth!) off one product, is best for an app that’s best when predictable.

Re: How we sped up Notion in the browser with WASM SQLite

#56
post #51

> Using SQLite improved page navigation times by 20 percent in all modern browsers. Couldn't you cache the data in LocalStorage and get similar speed improvements? I mean, how to run SQLite in the browser is a great topic and surely has a lot of very good use cases. But attributing the performance improvements to it sounds a bit misleading, when you had no client caching strategy before and you do now. Obviously load…

(I work at Notion, but didn't build the WASM sqlite thingy) We've implemented this same cache using LocalStorage, IndexedDB, and SQLite. The Android & iOS apps used SQLite for this since 2020 (I built the Android version IIRC), and the desktop app used SQLite running on a native thread since 2021. We migrated away from LocalStorage for two reasons: 1. LocalStorage is limited to 10mb file size. We also use LocalStorag…

Sounds like NLS/Augment on a machine with enouh core and modern support APIs.

Re: How we sped up Notion in the browser with WASM SQLite

#57
post #36

Notion takes 15s to load to an empty page. Then another 5 to dismiss the popup about new AI features and the like. I'm glad they are making their app faster, in the meantime I (browser user) have cancelled my team's subscription and will be using something else.

Curious where you're moving? About to begin finding a tool similar to Notion (but not Notion).

I’d love advice too - I’ve become heavily reliant on Notion’s database-lite features, which is what a lot of the markdown-based apps don’t invest in by default. I’m beginning to get frustrated with loading speeds that feel like spinning disks.

Re: How we sped up Notion in the browser with WASM SQLite

#58

This is cool because I haven’t seen WASM SQlite in production. I wasn’t sure whether it’d end up being more performant than something built in like IndexedDB. Seeing that it’s practically feasible is definitely cool.

Same - I've seen plenty of exploratory/experimental posts, but this is the most information I've seen about it being used in production.

Re: How we sped up Notion in the browser with WASM SQLite

#59

Overall I think using SQLite locally to offload database work is incredibly powerful. Given most laptops have an SSD now a days, you can scan an entire 30-100MiB SQLite db in miliseconds. Meanwhile in AWS you would pay $27k a month to have the same IOPS as a Lenovo Thinkpad X1. I just got done with a side project using WASM SQLite as well, it's incredibly powerful, even supports full text search. My project "cluttr"…

If you think those read speeds are great, try DuckDB (which has many SIMD improvements) if you want to blow your socks off.

Isn’t one OLTP and the other OLAP? I don’t understand why DuckDB is often suggested as a drop-in replacement.

Re: How we sped up Notion in the browser with WASM SQLite

#60

Earlier quoted context omitted.

I think their real innovation was the community and getting "cosy productivity" during Covid going. They are more of a social phenomenon than a technical one.

Notion is a great example of my hatred of the current "upgrade or die" delivery method of software. Notion 1.0, when it was mostly text with a unique way to view tabular data, was fast and enjoyable to use. Once it shifted to competing with Atlassian et al, it acquired all the bloat those products already have.

Notion was never fast, and I've been using it on and off for the past 10 years or so.
Post reply on HN