Pub/Sub message triggered from Cloud run hosted application is taking long time

I have a simple Spring boot application hosted in Google cloud run which publishes a Google Pub/Sub message to a topic in the same project. This is taking a long time for about 5 min roughly. Below is the code that I use to publish the Google pub/sub message. But the same was working fine with no delay in Appengine environment.

ApiFuture<String> messageIdFuture = com.google.cloud.pubsub.v1.Publisher.publish(pubsubMessage);
        ApiFutures.addCallback(messageIdFuture, new ApiFutureCallback<String>() {
            @Override
            public void onFailure(Throwable throwable) {
                if (throwable instanceof ApiException) {
                    ApiException apiException = ((ApiException) throwable);
                    // details on the API exception
                    log.error("APIException Status Code: {}", apiException.getStatusCode().getCode());
                    log.error("APIException is Retryable: {}", apiException.isRetryable());
                }
                log.error("Error publishing message: {}", pubSubMsg);
            }
            @Override
            public void onSuccess(String messageId) {
                log.info("Success msg after publish: {}", messageId);
            }
        }, MoreExecutors.directExecutor());

How to overcome this delay in publishing the Pubsub message?

1 answer

  • answered 2020-11-25 14:56 Andrew

    If I remember correctly Spring Boot creates a JAR file that contains nested JARs. In Cloud Run these files get unpacked during startup and this can impact on the run time.

    Try using a Jib plugin to containerise your application and see if that improves your timings.

    I imagine that you will also need to optimise how your app is containerised. Are you using a Multi-Stage Build?

    I am not overly familiar with Spring Boot but this article discusses how to containerise your code for best performance.