Hurry Up, Ends March 14!
Open Closed

ILocalEvent performance and transaction issue #8940


User avatar
0
Yaduraj.Shakti created

Hi,

We need help with Local Events in ABP.IO. We are performing certain notifications when an entity is created or updated. Inside the handleEvent method, we have used a try-catch block and are not throwing any errors. However, this ILocalEvent handling is causing performance issues, and our APIs are taking around 1.5 seconds to respond. One of the handlers performs an insert operation.

We have tried changing our handler code to the following:
`

public async Task HandleEventAsync(EntityCreatedEventData<MyEntity> eventData)
{
// Offload the event handling to a separate thread
await Task.Run(async () =>
{
    // Our event handling logic here, notification or database operation
});
}`

Does this look good to you?

Can you provide any insights or suggestions to improve the performance? is await Task.Run(async ()).. is required? is seperate ouw required inside the handler?

  • ABP Framework version: v8.3.0

  • UI Type: Angular

  • Database System: EF Core (PostgreSQL)

  • Tiered (for MVC) or Auth Server Separated (for Angular): yes

  • Exception message and full stack trace: No exception But Angular APIs needs to refresh again to get updated data so we need to put the call in setTimeout for 1-2 seconds.

  • Steps to reproduce the issue: NA


3 Answer(s)
  • User Avatar
    0
    enisn created
    Support Team .NET Developer

    Hi,

    Can you provide any insights or suggestions to improve the performance? is await Task.Run(async ()).. is required? is seperate ouw required inside the handler?

    If you need to execute main process and the event itself in a same transaction, then no, you cannot make it faster by executing in a different thread since the roll-back won't work whenever you use Task.Run. All the local events are designed to be runned in the same transaction.

    But if you don't need to execute in the same transaction, you can use IDistributedEventBus with RabbitMQ or Kafka. In that case you can use Inbox/Outbox Pattern to ensure all the events are consistently published and consumed. If your application goes bigger, that might be a good scenario for you

  • User Avatar
    0
    Yaduraj.Shakti created

    Thanks @enisn

    One more thing, we are using ILocalEvent to track the entity changes and in the local event handler we are further publishing the IDistributedEventBus. Would this still be causing performance issues.

    `MyEntityStatusChangedEventHandler: ILocalEventHandler<EntityCreatedEventData<MyEntity>>
    {
    	//Handling local event
    	public async Task HandleEventAsync(EntityCreatedEventData<MyEntity> entityCreatedData)
    	{
    		//Publishing Distrubuted event
    		await _distributedEventBus.PublishAsync(new CustomDataEto
    		{
    			//set properties
    			CustomDataEto.Data = MyEntity.Data;
    		}
    	}
    }`
    

    Also, what if we use new transaction scope using IUnitOfWorkManager.

  • User Avatar
    0
    enisn created
    Support Team .NET Developer

    You can create different unit of work scopes and do not wait the second one for returning resposne of the http request. That might be the case.

    Also if you don't need, you can disable unit of work only for your handler side. But still, handling operation will be executed sync in the same thread, whenever you use Task.Run, you detach the the thread from the original one and you don't have to wait it. It logically works as expected but whenever an error occurs in the created task, it'll be harder to track it.

    By the way, you can manually control unit of work according to your requirements:
    https://abp.io/docs/latest/framework/architecture/domain-driven-design/unit-of-work#controlling-the-unit-of-work

Learn More, Pay Less
33% OFF
All Trainings!
Get Your Deal
Mastering ABP Framework Book
Do you need assistance from an ABP expert?
Schedule a Meeting
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v9.2.0-preview. Updated on March 13, 2025, 04:08