Cloud Intelligence™Cloud Intelligence™

Cloud Intelligence™

Too Many Threads in Cloud Pub/Sub

This page is also available in Deutsch, Español, Français, Italiano, 日本語, and Português.

By Joshua FoxJul 5, 20201 min read
Joshua Fox

About Joshua Fox

Joshua Fox has been a software architect in innovative technology companies for 20 years. Now, he advises tech startups and growth companies on architecture and cost optimization for Google Cloud Platform and Amazon Web Services; also publishing articles, and speaking to cloud engineers.

He has a PhD from Harvard University and a BA in math from Brandeis.

My personal page

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.

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.

PerfectScale™ for Kubernetes

Ready to optimize?

Get your free Kubernetes savings analysis

Your cloud bill shouldn't be a mystery

Optimization, automation, expertise. In one platform.