coinbase / temporal-ruby

Ruby SDK for Temporal
Apache License 2.0
213 stars 81 forks source link

How to pass some information from workflow context to all the activities. #266

Closed Nagesh-Anna closed 7 months ago

Nagesh-Anna commented 8 months ago

I am writing one workflow where I wanted to call 4 activities from this workflow.

I wanted to load the same user object in all these activities. Is there a way that we can pass this object to the activity context from the workflow context? If yes, Could you please help with some examples?

Also, does this get updated with time in the context or do we have to load every time to context?

I am happy to hear if there are any other alternatives.

Thanks in Advance!

jeffschoner commented 8 months ago

Workflows don't directly invoke activities in the same process. Rather a workflow schedules an activity task that will be picked up and executed by any worker polling that task queue. Because of this, you typically need to load the object in each activity based on inputs to the activity.

You can cache the data on local disk to reduce unnecessary reloading of an object. However, because activities can execute on any worker listening to a task queue, the cache miss rate will rapidly increase as your worker fleet size increases. To improve this, you can follow a pattern known as "activity stick queues". There's no sample for this in temporal-ruby, but the concept is illustrated in other Temporal SDKs: .NET, Go, Python, Typescript.

This all will introduce increased complexity in your code. You have to decide if it's worth it or not. Another simple option is to consider combining multiple activities together so that the object only needs to be loaded once or a fewer number of times. There's downsides to this too: notably making the entire activity idempotent or setting tight timeouts may be more difficult, or you can lose parallelism.

Nagesh-Anna commented 7 months ago

Thank you