Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Caching and batching in Rabbit

Syusui Moyatani
April 21, 2023
89

Caching and batching in Rabbit

"Caching and batching in Rabbit"

Rabbit is a nostr client which I made. It has several features to reduce the amount of traffic and the number of requests -- caching and batching. This presentation describes how it works and implementation details.

The original version is here. It has some animations which will help you to understand details.
https://docs.google.com/presentation/d/1J-UQuewSRwFWH7aoMYzWfPEdXsNhY9CEMvz2uXLFigw/edit#slide=id.g21961a69e05_0_5

日本語版: https://speakerdeck.com/syusui/rabbitnokiyatusiyubatutihua-noshi-zu-mi

Rabbitのキャッシュ・バッチ化の仕組み © 2023 by Syusui Moyatani is licensed under CC BY-SA 4.0. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/

Syusui Moyatani

April 21, 2023
Tweet

Transcript

  1. Caching and Batching in Rabbit🐰 @syusui_s Rabbitのキャッシュ・バッチ化の仕組み © 2023 by

    Syusui Moyatani is licensed under CC BY-SA 4.0 I'm not really good at English. It would be grateful if you could let me know problems about translation
  2. Showing a post is hard Icon Profile (kind0) The number

    of reposts (kind 6) The number of reactions (kind 7) Several informations are needed to show post 1:00 +20
  3. Fetching data each time is slow • slow to show

    profile • consumes a large amount of monthly mobile communication capacity • reaches rate-limit of relay easily 1:20 +20
  4. Solutions Rabbit uses these solutions: • Caching ◦ Save data

    for a while and reuse it • Batching ◦ Do several requests at once 1:40 +10
  5. Caching Save data for a while and reuse it. •

    Profile ◦ The data is not often changed, so reusable. ◦ User can see name and icon soon if it is saved. There is some other cacheable data: follow list (kind 3), the number of reposts (kind 6), the event which is reposted (kind 1) 1:55 +15
  6. TanStack Query A library for caching mainly HTTP responses •

    Based on SWR strategy • Supports SolidJS 1:15 +15
  7. Simple caching strategy The data will be expired in the

    future. It should be deleted. Define the valid period and remove it after the time passed. Fresh (The data is considered as fresh) Del Vaid period Save 1:30 +30 User need to wait for new data to be retrieved if we remove data immediately after it expires
  8. SWR strategy (stale-while-revalidate) Refetch the data while showing the expired

    data. Fresh Expired Del Valid period (staleTime) Period until deleting from cache (cacheTime) Save User can see data immediately even if it is expired 2:00 +30
  9. Sequence of communication Client Relay Cache I want Alice's profile

    Here is a profile I want Alice's profile Here is profile Here is a data. I want Alice's profile Here is a data saved in a cache I want Alice's profile stale I want Alice's profile Here is a new data. Valid Expired Here is a profile 3:00 +60
  10. Rate-limiting problem Client Relay I want Bob's profile I want

    Carol's profile I want Alice's profile I want David's profile I want Eve's profile Too many subscriptions! (failed to get Eve's profile) 5:00 +10
  11. Motivation Most relays have rate-limit for subscriptions to reduce load.

    How can I keep the number of subscription small? 5:10 +10
  12. Batching Merge requests into a single requests kinds: [0] authors:

    ["8234…e6a2"] kinds: [0] authors: ["3bf0…459d"] kinds: [0] authors: [ "8234…e6a2", "3bf0…459d" ] Example of requesting profiles: 5:20 +20
  13. Sequence of communication Client Relay Batching I want Alice's profile

    I want Bob's profile I want Carol's profile I need profiles for Alice, Bob and Carol Here is Alice's profile 3 sec Here is Alice's profile Here is Bob's profile Here is Carol's profile The number of subscription is reduced Here is Bob's profile Here is Carol's profile Sorting responses 5:40 +60
  14. Sorting responses kind: 0 pubkey: "8234…e6a2" content: {"name":"jack"} kind: 0

    pubkey: "3bf0…459d" content: {"name":"fiatjaf"} pubkey resolver "8234…e6a2" () => { … } "3bf0…459d" () => { … } Promise Promise Pairs of pubkey and resolve functions new Promise((resolve) => …) Find a corresponding promise and fulfill it 6:40 +60
  15. Conclusion Caching and batching contribute to data efficiency Future prospects

    - Optimistic mutation - Subscribe profile change, reposts and reactions to update cache - Speculative caching - Save received events recently for a short time for possible future repost - Remember timelines by using InfiniteQuery - I want to create a library which supports these features…
  16. Cache time for profile staleTime: 5 min Acceptable if name

    and icon are a bit old. cacheTime: 1 day We want to quickly see profiles for users who post frequently. 3:00 +30
  17. Cache time for reposted event staleTime: 4 hours The post

    cannot be changed. We don't need to expire the data. cacheTime: 4 hours The post tend not to be reposted as time passes. We don't need to save the data for a long time. ▪▪さんがリポスト ••さんがリポスト