Orbit-db: [Question] Database replication

Created on 4 Dec 2017  路  5Comments  路  Source: orbitdb/orbit-db

Hi there,

I am now developing a twitter like web application using orbit-db.
Assume I have a userA who starts a nodeA. He stored all his tweets in a feed type database called tweetsDB. userA posts 1000 tweets in total stored in database tweetsDB.
Now another useruserB on nodeB wants to see the most recent 5 tweets of userA.
So userB would try to connect to tweetsDB.
Does that mean userB has to download (replicate) the whole tweetsDB including those 1000 tweets in order to see the most recent 5 tweets?

Thank you

question

Most helpful comment

Hi @haadcode ,

Thank you much for your reply. It's extremely helpful.
I have a few other questions here. I just want to make sure that I understand everything correctly before I continue my project with the fancy orbit-db library ;)

  1. You said that I could use await db.load(5) to load the database with 5 latest operations (tweets) locally or listen to replicated event for the data update happened remotely. Is there a way to load the 5 oldest operations first instead of the 5 most recent operations?

  2. When database is replicated from another remote database, does it replicate everything randomly or is there an order of replication like starting from the latest to the oldest?

  3. When two users try to update an entry in database at the same time, what would happen? For example, two users click the like button in a tweet at the same time.

  4. For the document store type database, how fast is it to query an entry by its _id. I would assume that in the worst case it would be O(n) because the data is stored in a linked list?

  5. Is there a way to stop replicating the database and then resume from where I stopped?

  6. If I use orbit-db in browser, how will the database be stored? IndexedDB?

Thank you very much for your patience.

All 5 comments

In short: no, they don't need to replicate 1000 tweets to display 5 :)

You can do this by giving the load() function a number of entries to load, eg. await db.load(5). OrbitDB will then load the database with 5 latest operations (tweets) and emit ready event and finish loading. After that, it'll load the rest of the 1000 tweets in the background.

Does that help?

And in case where userB doesn't have any of userA tweets, you don't need to do anything special but to open the database and userB will start replicating userA tweets automatically. You can listen for the replicated event to learn when the database has updated.

Hi @haadcode ,

Thank you much for your reply. It's extremely helpful.
I have a few other questions here. I just want to make sure that I understand everything correctly before I continue my project with the fancy orbit-db library ;)

  1. You said that I could use await db.load(5) to load the database with 5 latest operations (tweets) locally or listen to replicated event for the data update happened remotely. Is there a way to load the 5 oldest operations first instead of the 5 most recent operations?

  2. When database is replicated from another remote database, does it replicate everything randomly or is there an order of replication like starting from the latest to the oldest?

  3. When two users try to update an entry in database at the same time, what would happen? For example, two users click the like button in a tweet at the same time.

  4. For the document store type database, how fast is it to query an entry by its _id. I would assume that in the worst case it would be O(n) because the data is stored in a linked list?

  5. Is there a way to stop replicating the database and then resume from where I stopped?

  6. If I use orbit-db in browser, how will the database be stored? IndexedDB?

Thank you very much for your patience.

@shd101wyy I know this is very, very late but I still think answering your question will be helpful to others as well.

You said that I could use await db.load(5) to load the database with 5 latest operations (tweets) locally or listen to replicated event for the data update happened remotely. Is there a way to load the 5 oldest operations first instead of the 5 most recent operations?

Yes, certain data stores have an iterator function that can probably help you.

When database is replicated from another remote database, does it replicate everything randomly or is there an order of replication like starting from the latest to the oldest?

Replication will start from the exchanged "heads" i.e. the latest entry or entries in the DAG, and replicate from there by following the next field inside of the entries.

When two users try to update an entry in database at the same time, what would happen? For example, two users click the like button in a tweet at the same time.

OrbitDB implements the ipfs-log, which in turn uses something called Lamport Clocks to ensure that even if they press the button a the _exact same microsecond_, they will be properly sorted.

For the document store type database, how fast is it to query an entry by its _id. I would assume that in the worst case it would be O(n) because the data is stored in a linked list?

That sounds right.

Is there a way to stop replicating the database and then resume from where I stopped?

I don't believe so, no...

If I use orbit-db in browser, how will the database be stored? IndexedDB?

Yes, it's stored in IndexedDB via a library called LevelDB.


Finally, we also just released the OrbitDB Field Manual and I encourage everybody to check it out and open new issues / PRs there if you see fit.

@aphelionz Thank you very much for your replies!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RichardLitt picture RichardLitt  路  4Comments

whyrusleeping picture whyrusleeping  路  3Comments

oskarpyke picture oskarpyke  路  6Comments

varcario picture varcario  路  5Comments

Clickative picture Clickative  路  5Comments