# Is publishing events thread safe?

**URL:** https://discuss.elastic.co/t/is-publishing-events-thread-safe/102877
**Category:** Beats
**Tags:** beats-development
**Created:** [October 5, 2017, 4:43pm UTC](https://discuss.elastic.co/t/is-publishing-events-thread-safe/102877 "2017-10-05T16:43:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jmegin](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@jmegin](https://discuss.elastic.co/u/jmegin)
#### Post date: [October 5, 2017, 4:43pm UTC](https://discuss.elastic.co/t/is-publishing-events-thread-safe/102877/1 "2017-10-05T16:43:39Z")

</div>

The [developer guide](https://www.elastic.co/guide/en/beats/libbeat/5.5/beater-interface.html#run-method) shows how to create an instance of [publisher.Client](https://github.com/elastic/beats/blob/5.5/libbeat/publisher/client.go), but I'm wondering if it's [PublishEvent](https://github.com/elastic/beats/blob/5.5/libbeat/publisher/client.go#L98) and [PublishEvents](https://github.com/elastic/beats/blob/5.5/libbeat/publisher/client.go#L124) methods are thread safe.

I'm currently working on a beat that receives HTTP requests and uses the request body to generate events. Since each request in handled in a separate go routine, is it safe to have these go routines call the PublishEvents method of the same instance of a publisher.Client at the same time? If not, are there any alternatives for publishing events in a thread safe way? I can always use a mutex if there is no thread safe method to publish events.

---

<div class="post-metadata">

### Author: ![steffens](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/steffens/32/79630_2.png) [@steffens](https://discuss.elastic.co/u/steffens)
#### Post date: [October 6, 2017, 10:22am UTC](https://discuss.elastic.co/t/is-publishing-events-thread-safe/102877/2 "2017-10-06T10:22:52Z")

</div>

In 5.x they are thread-safe. In 6.x the publish interfaces are somewhat reworked. In 6.x they are still thread-safe (for now), but there is no actual guarantee. In general it is good practice to create a `publisher.Client` instance per worker. All clients publish events to a shared queue.

Some beats use another tactic. They have some 'publisher' workers instead of a mutex. The data collectors forward events to the publisher workers (using a go channel). The publisher workers might add some more data to events or do some additional filtering and finally Publish the events to libbeat. e.g. packetbeat uses this tactic in order to drop events if the publisher workers are blocked to due back-pressure in the outputs.

---

<div class="post-metadata">

### Author: ![jmegin](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@jmegin](https://discuss.elastic.co/u/jmegin)
#### Post date: [October 6, 2017, 1:56pm UTC](https://discuss.elastic.co/t/is-publishing-events-thread-safe/102877/3 "2017-10-06T13:56:14Z")

</div>

thanks for the detailed response @steffens, that was exactly what I was looking for!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [November 3, 2017, 1:56pm UTC](https://discuss.elastic.co/t/is-publishing-events-thread-safe/102877/4 "2017-11-03T13:56:28Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
