Blog

Too Many Threads in Cloud Pub/Sub

How to slim down the Publisher to a manageable number of threads.

In general, extra inactive threads in Java are not a problem. But in a resource-constrained environment, and in particular, if your software already uses a lot of threads, too many extra threads can overburden your system.

Google Cloud PubSub is an excellent asynchronous communication framework, known for being lightweight. Yet on first use, the Google PubSub Java client creates 60 threads that stay live permanently. (You can see all threads using Thread.getAllStackTraces().keySet() .) PubSub’s extra threads are used for asynchronous operations as part of publishing messages.

1 4sprim87bvpp it wgdqjq
Too many threads

The threads are useful for a high-volume publisher; but for a low-volume publisher, 60 threads are far too many.

The API for setting the number of threads is not part of the official PubSub API, not well-documented, and quite low-level. So, here is how to make it happen.

1. Set setExecutorThreadCount to a small number like 4 (or even 1), as follows. This reduces the number of threads by about 40.

2. That still leaves about 20 inactive threads. You can reduce these as documented by ajaaym at GitHub (code below).

Here, you create your Publisher, GrpcTransportProvider, and ManagedChannelBuilder with a single shared one-threaded ExecutorProvider, so that in total only one thread is added for the PubSub Publisher.

Subscribe to updates, news and more.

Related blogs