In microservices or any other event-based architecture, in some use cases, a service might require us to make changes to their own local database and also publish an event. That event is then consumed by other services. To have a consistent software system, it is mandatory that these two actions get performed atomically. Both actions need to complete successfully, or none of them need to complete. There should not be another way.
An elegant way to solve this is by using the Outbox Pattern. This works by using a database table (if your service uses a relational database), usually called the outbox table, to store the events. In this case, you are able to include the insert SQL statement of the event into the use case local transaction. Another runner can periodically check if the outbox table is not empty and process the events by publishing them into a message broker.