Skip to content

Commit

Permalink
Add design document for PubSub (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
dc7303 authored Apr 24, 2021
1 parent c58df97 commit a32da4e
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions design/pub-sub.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: pub-sub
target-version: 0.1.0
---

# PubSub

## Summary
Yorkie needs to share events happening in documents with different clients. For this, we implemented this feature using gRPC server-side stream and the PubSub pattern.

### Goals
We should be able to share events with other clients who are subscribing to Documents.

## Proposal Details

### How does it work?
Yorkie implements WatchDocuments API using [gRPC server-side streaming](https://grpc.io/docs/languages/go/basics/#server-side-streaming-rpc) to deliver the events that have occurred to other clients.

```protobuf
// api/yorkie.proto
service Yorkie {
...
rpc WatchDocuments (WatchDocumentsRequest) returns (stream WatchDocumentsResponse) {}
}
```

And to manage the event delivery target, we are using the [PubSub pattern](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern).
You can learn more by looking at the [sync package](https://github.com/yorkie-team/yorkie/tree/main/yorkie/backend/sync) we are implementing.

The order of operation is as follows.

1. The WatchDocuments API creates a `Subscription` instance with the client and document keys.
2. Subscription instance internally manages the `DocEvent channel`, and the WatchDocuments API uses a select statement to retrieve events passed to the Subscription instance.
3. The client can deliver the event to the Subscription instances that are subscribed to the same document through the `PubSub.Publish(publisherID *time.ActorID, topic string, event DocEvent)` method.
4. In the select statement mentioned earlier, when an event is confirmed, it is sent to the stream server.

### Risks and Mitigation
Currently, Subscription instances are managed in memory. This can be a problem when building a cluster of servers.
To solve this problem, we are planning to support cluster-mode using [etcd](https://github.com/etcd-io/etcd).

0 comments on commit a32da4e

Please sign in to comment.