Willem Olding
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Efficient record storage in the Holochain DHT One of the aims of Holochain is to replace a conventional database as the primary storage for web applications. One thing that that databases excel at is dealing with large numbers of records and returning them in smaller pages. For example in MySQL retrieving a subset of posts to a blog could be done by calling: `SELECT * FROM posts LIMIT 50,100;` This will return posts from 50 to 100. The table may contain millions of other posts which can be largely ignored. ## Naïve approach in Holochain There are several problems in replicating similar behaviours in the Holochain DHT. At the moment the most commonly implemented way of implementing a similar query is through the use of anchors and links. A `posts` anchor (B for Base) could act as the base and when a user creates a post (e for entry) it is added as a link. ```graphviz digraph { B -> {e1, e2, e3} } ``` This has two crippling problems as the number of posts becomes large: - calling `get_links` on the anchor must retrieve all of the posts. This may require millions of network requests as well as unbounded memory requirements (in the WASM runtime). - Whichever agents happen to hold the anchor in their neighbourhood also have to store all the links. This leads to 'hotspots' in the DHT where there is vastly different storage requirements for different hash neighbourhoods. A real solution to this problem should meet the following requirements: - No base should have to store more than $N$ links - Can return pages of results of size $n < N$ - Must be able to retrieve all records by iterating over pages - Must work under network partitioning ## Slightly less Naïve (but still disfunctional) approach This candidate solution has been referred to as the 'bucket chain'. This splits the monolithic anchor into an increasing number of buckets which should hold $n \leq N$ entries. ```graphviz digraph R { { rank=same b1 b2 b3 } B -> b1 -> b2 -> b3 b1 -> {e1, e2, e3} b2 -> {e4, e5, e6} b3 -> {e7, e8} } ``` The processes for inserting and retrieving are as follows: ### Inserting 1. Find the base/anchor hash 1. follow the link to the next bucket 1. Check if next bucket exists. If yes follow link and repeat 3. 1. We are now at the end of the bucekt chain. Check number of entries linked - If < $N$ add link to new entry. done. - Else commit new bucket, link to current bucket and link new entry to new bucket ### Retrieving 1. Find the base/anchor hash 1. Follow links to next bucket and call get_links to retrieve entries 1. Repeat 2. until all entries have been retrieved (or the number you want) The retrieval process can be modified to begin at a non-root bucket as well facilitating paging. ### Problems While at first glance this may seem to have solved the problem it does not hold up in a multi-agent setting. There is no guarantee that in-between checking how many entries are in a bucket and commiting your entry that someone else has not also added their entry. This would result in $N+1$ entries in a bucket and therefore this scheme does not meet the first requirement. There is also a more malicious attack where an agent could create a new node that is offline, add $N$ unique entries to the first bucket and then rejoin the network. Upon rejoining their entries would be merged with the existing ones. Repeating this procedure with new agents means there could be an unbounded number of entries on any given bucket. It may seem that proper validation could prevent the above attack but there is no way to discern an attacker from a valid network partition thus validation cannot help in this case (Also validation that makes use of DHT state, like number of links on a base, is a bad idea anyway.) These issues mean this approach is largely uselss in a multi-aget hash table. ## Agent-centric approach As with all things in Holochain the answer is agent centricity! It is not possible to perform deterministic validation on DHT state but it is possible to validate based on the local chain. Therefore to use validation to ensure that no more than $N$ entries can be linked to a given base we need to enforce via validation that only a particular agent can link to that base. This means that all of the entries/add_links are in this agents local chain storage and validation can use this to enforce a count. In this scheme we have a single bucket chain per agent and a query must traverse the tree of all bucket chains to retrieve all the entries. This data structure has the added bonus of being filterable by author. Let $Abi$ be the $i^{th}$ bucket belonging to agent A and $Aei$ be the $i^{th}$ entry created by agent A. ```graphviz digraph R { { rank=same Ab1 Ab2 Ab3 } { rank=same Bb1 Bb2 } root -> {Ab1, Bb1} Ab1 -> Ab2 -> Ab3 Ab1 -> {Ae1, Ae2, Ae3} Ab2 -> {Ae4, Ae5, Ae6} Ab3 -> {Ae7, Ae8} Bb1 -> Bb2 Bb1 -> {Be1, Be2, Be3} Bb2 -> {Be4} } ``` The following validation rules ensure the data structure remains valid: - An agent can only add links to their own buckets - An agent cannot add more than $N$ links to a bucket ### Inserting The insertion algorithm is the same as above but an agent most follow the validation rules above ### Retrieving Retrieval is also the same for a single agents bucket chain. If all agents entries are required then this must be repeated for each agent bucket chain. ### Problems Let us evaluate how this approach meets the requirements defined at the start. The validation ensures that for all bases, except for the root, it is guaranteed that there will be less than $N$ links. If the agent buckets are named in a consistent way then it is possible to jump to a particular bucket (in fact the between bucket links are not required) which enables paging queries. Furthermore it is guaranteed that all but the last bucket will not change between calls (a great property for pagination). The other third requirement of being able to retrieve all records is met (provided there is no network partition but this is always the case in Holochain) although it may be much less efficient to retrieve all the records if there are many agents with underfilled buckets. And finally this approach is eventually consistent provided there is not more than one agent with the same ID (which would be a disaster anyway). The main limitation of this approach is the root anchor. There is no limit to the number of agents that may join and thus this may become a hotspot in the DHT. This could be resolved by not including it and having some other way of discovering agent IDs such as social network graphs or methods outside of the application. This is good practice for scalability anyway and it follows that you then only need to retreive data for agents you are connected to. ## Conclusion The above agent-centric bucket tree approach appears to provide a good solution to the data retrieval and pagination problem for Holochain. There may be something I have not considered so any feedback is appreciated. It seems especially well suited to social network type applications where the network can be used to discover agents without requiring a root agent anchor.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully