Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use Deno KV for storing fresh assets #7

Merged
merged 3 commits into from
May 26, 2023
Merged

Conversation

tlgimenes
Copy link

@tlgimenes tlgimenes commented May 26, 2023

This PR adds esbuild assets to Deno KV. This feature uses Deno KV as an optimization and KV is not required for serving assets on both dev and production environments.

How does it work?

All isolate instances have an internal inMemory cache. We use the following algorithm for incoming requests:

if file in in-memory-cache then
  return in-memory-cache[file]
else if file in kv
  return kv[file]
else 
  bundle assets 
  store assets in in-memory-cache
  return in-memory-cache[file]
  store assets in kv in background
  housekeep stale assets in background

With this algorithm, we ensure:

  1. Any isolate is able to serve the website without relying on KV.
  2. If an asset is inside kv, any isolate is able to return it
  3. KV is bounded by housekeeping stale assets

2 is nice because it should remove issues of missing assets when any chunk changes due to esm.sh changes

Consistency

store assets in kv in background means:

  1. Files are split into 64Kb chunks and saved in KV in a kv transaction. This ensures the file is stored consistently on KV
  2. chunks are stored before islands and entrypoints. This ensures that if an island is served, it's corresponding chunks are retrievable from KV. This ensures no more issues with missing assets when serving the store

Note that KV currently has a limit of 10 operations in a transaction. If your asset is bigger than 10 * 64Kb, it will fail saving on KV, but it's ok since the isolates work without KV

Streaming

A nice approach to saving assets in KV in 64Kb chunks is that we are able to stream these chunks when serving assets from KV

Debug

Since this is a difficult feature to get right, I'm adding a bunch of console.logs that may be removed in the future. To make your life more pleasant, I added emojis! 🎉

I hope to make this PR a fresh PR one day.

Comment on lines +188 to +190
await bundleAndCacheOnce();

return inMemoryCache.get(path);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any change that bundleAndCacheOnce will not succeed in putting path in inMemoryCache?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently have the same behavior, so I don't think this is an issue

src/server/bundle.ts Show resolved Hide resolved
Co-authored-by: Lucis <luciannojunior@gmail.com>
@tlgimenes tlgimenes merged commit 7790e86 into main May 26, 2023
@tlgimenes tlgimenes deleted the feat/kv-for-assets branch May 26, 2023 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants