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

Throttling updates to reduce server load #185

Open
1 of 2 tasks
matsuyama-k1 opened this issue Dec 23, 2024 · 2 comments
Open
1 of 2 tasks

Throttling updates to reduce server load #185

matsuyama-k1 opened this issue Dec 23, 2024 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@matsuyama-k1
Copy link

matsuyama-k1 commented Dec 23, 2024

Checklist

[ x] Are you reporting a bug? Use github issues for bug reports and feature requests. For general questions, please use https://discuss.yjs.dev/
[ x ] Try to report your issue in the correct repository. Yjs consists of many modules. When in doubt, report it to https://github.com/yjs/yjs/issues/

Is your feature request related to a problem? Please describe.
Every time a user makes an update, an update is sent to the server. As a result, a large number of database writes occur, which can put significant strain on the server and database.
When central server is written in serverless environment, db writes becomes costly due to the high volume of write operations.

Describe the solution you'd like
I would like to add a throttling option for updates.

Additional context
I customized y-websocket in my project, and it works well. Below is the code I added to y-websocket:

const THROTTLE_TIME_MS = 5000
// y-websocket
export class WebsocketProvider extends Observable<string> {
  constructor(doc, ...) {
    // ...

    this._updates = [];

    this._batchBroadcastUpdates = () => {
      const mergedUpdate = Y.mergeUpdates(this._updates)
      // refresh queue when updates sent
      this._updates = [];

      const encoder = encoding.createEncoder();
      encoding.writeVarUint(encoder, messageSync);
      syncProtocol.writeUpdate(encoder, mergedUpdate);

      broadcastMessage(this, encoding.toUint8Array(encoder), false);
    };

    this._throttledBroadcast = throttle(() => {
      this._batchBroadcastUpdates();
    }, THROTTLE_TIME_MS);

    this._updateHandler = (update, origin) => {
      if (origin !== this) {
        // add update queue
        this._updates.push(update);
        this._throttledBroadcast();
      }
    };

    this.doc.on('update', this._updateHandler);
  }
  disconnect() {
    this._batchBroadcastUpdates();
    // ...
  }
}
  • I'm a sponsor 💖
  • This feature is critical for my project.
@matsuyama-k1 matsuyama-k1 added the enhancement New feature or request label Dec 23, 2024
@matsuyama-k1 matsuyama-k1 closed this as not planned Won't fix, can't repro, duplicate, stale Dec 23, 2024
@matsuyama-k1 matsuyama-k1 reopened this Dec 23, 2024
@dmonad
Copy link
Member

dmonad commented Dec 26, 2024

Wouldn't it be better to cache server operations? I currently see no reason to throttle client-distributed operations.

  • The clients broadcast every individual change
  • The server distributes all messages received by the clients
  • The server stores all received updates after a debounce. It can use Y.mergeUpdates(updates) to merge all received updates into a single entry.

@matsuyama-k1
Copy link
Author

matsuyama-k1 commented Dec 26, 2024

@dmonad
Actually.
I might not be in common situation.
Since I set up yjs server in AWS Lambda functions, I cant throttle in the server side due to their stateless nature.
In this case we have to implement throttling on the client side.
I realized that if this is not common situation we do not have to implement this feature in y-websocket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants