Skip to content

Throttling updates to reduce server load #185

Open
@matsuyama-k1

Description

@matsuyama-k1

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.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions