dapr / proposals

Proposals for new features in Dapr
Apache License 2.0
15 stars 33 forks source link

Pubsub delayed scheduling proposal #12

Open yaron2 opened 1 year ago

yaron2 commented 1 year ago

Signed-off-by: yaron2 schneider.yaron@live.com

daixiang0 commented 1 year ago

+1 binding

yaron2 commented 1 year ago

+1 binding

yaron2 commented 1 year ago

One more question @yaron2 is what happens to the publishingScopes and subscribingScopes definition that we have for limiting topic access? https://docs.dapr.io/developing-applications/building-blocks/pubsub/pubsub-scopes/

How will that work with <app>-delayed topic?

It works as is, unchanged. Again, this is an implementation detail but Dapr checks if the app is allowed to publish before it sends it to any topic, delayed or not. On the receiving side, Dapr checks if the app is allowed to subscribe regardless of delayed topics as they only act as an intermediary.

mukundansundar commented 1 year ago

One more question @yaron2 is what happens to the publishingScopes and subscribingScopes definition that we have for limiting topic access? https://docs.dapr.io/developing-applications/building-blocks/pubsub/pubsub-scopes/ How will that work with <app>-delayed topic?

It works as is, unchanged. Again, this is an implementation detail but Dapr checks if the app is allowed to publish before it sends it to any topic, delayed or not. On the receiving side, Dapr checks if the app is allowed to subscribe regardless of delayed topics as they only act as an intermediary.

The scoped topics won't change but in addition to that will the application need access to an <app>-delayed topic?

yaron2 commented 1 year ago

One more question @yaron2 is what happens to the publishingScopes and subscribingScopes definition that we have for limiting topic access? https://docs.dapr.io/developing-applications/building-blocks/pubsub/pubsub-scopes/ How will that work with <app>-delayed topic?

It works as is, unchanged. Again, this is an implementation detail but Dapr checks if the app is allowed to publish before it sends it to any topic, delayed or not. On the receiving side, Dapr checks if the app is allowed to subscribe regardless of delayed topics as they only act as an intermediary.

The scoped topics won't change but in addition to that will the application need access to an <app>-delayed topic?

No, this is a Dapr internally used topic.

ItalyPaleAle commented 1 year ago

I second @artursouza's concerns that it may cause some issues with a variety of brokers. Artur mentioned Kafka. Two more examples:

I also have a question about what happens if the consumer is scaled horizontally. Among all PubSub components we support right now, we observe a variety of behaviors, but many (not all) deliver all messages to all subscribers. When the app is scaled horizontally, then, how do we coordinate this?

I definitely do see the value in having delayed messages and it's a big pain point. However, I share the concerns with this proposed implementation's feasibility (and long term supportability across all brokers). Ideally, we should encourage users to rely on the native support for delayed messages that brokers (like Service Bus) offer whenever possible.

Alternatively, if we need a Dapr-specific implementation, I would consider something that uses a state store to temporarily persist messages until they're ready to be published.

artursouza commented 1 year ago

I would also consider making the state store bound to a particular topic - appID combination to avoid contention between multiple apps.

yaron2 commented 1 year ago

When the app is scaled horizontally, then, how do we coordinate this?

Coordination isn't needed as this would satisfy the at-least-once guarantee.

The concern about queue clogging is real and using a general purpose state store makes sense, I'll update the proposal for that section.

olitomlinson commented 1 year ago

@yaron2

Personally, I would advocate utilising Dapr Workflows to handle the time-bound aspects of business logic, and not push that responsibility into the PubSub broker.

Pros

Cons

image

Assuming that a DurableTimers equivalent make it into the Dapr embedded workflow API surface

Example

PubSub Subscriber starts a workflow instance for each message.

// Dapr subscription in [Topic] routes orders topic to this route
app.MapPost("/orders", [Topic("orderpubsub", "orders")] (Order order) => {
    await workflowClient.ScheduleNewWorkflowAsync(
        name: nameof(OrderProcessingWorkflow),
        instanceId: order.orderId,
        input: order);
    return Results.Ok(order);
});

Use a workflow to host the logic,then use a Timer to defer the work until the desired time.

class OrderProcessingWorkflow : Workflow<OrderPayload, OrderResult>
{
    public override async Task<OrderResult> RunAsync(WorkflowContext context, OrderPayload order)
    {
        // Put the workflow to sleep for 72 hours because thats how the business wants to handle this message type
        DateTime dueTime = context.CurrentUtcDateTime.AddHours(72);
        await context.CreateTimer(dueTime, CancellationToken.None);

        // Notify the user that an order has come through
        await context.CallActivityAsync(
            nameof(NotifyActivity),
            new Notification($"Received order {orderId} for {order.Quantity} {order.Name} at ${order.TotalCost}"));

       ....
    }
}
kayvonkhosrowpour commented 1 year ago

+1 for pubsub publishing with delay

ItalyPaleAle commented 1 year ago

What if we implemented this on top of actors instead?

We could leverage the actor reminder subsystem to publish messages at a specific time. We'd also get "for free" the ability to publish messages with a delay.

yaron2 commented 1 year ago

What if we implemented this on top of actors instead?

We could leverage the actor reminder subsystem to publish messages at a specific time. We'd also get "for free" the ability to publish messages with a delay.

That's an idea I've been looking at recently, but would have to defer this to reminders v2. We're planning a distributed scheduling solution in Dapr to serve higher level APIs like actor reminders, delayed pub/sub and an upcoming outbox pattern API

ItalyPaleAle commented 1 year ago

Yes, agree "reminders v1" would likely not offer the perf this would require.

However, it would be nice to see how this could look like implemented on top of an internal actor, like workflows, even if the implementation weren't possible until "reminders v2".

olitomlinson commented 1 year ago

@yaron2 Does a public forum exist for discussing the 'new distributed scheduling solution' ?

ulf-melin-sveasolar commented 7 months ago

What is happening to this?

yaron2 commented 7 months ago

What is happening to this?

We are working on a distributed scheduling system for Dapr which among other consumers (Actors and Workflows) could also be used to support this delayed messaging feature. This proposal will be updated once we have the new system in place