In general, if something isn’t working in Google Cloud, a good first step is to check for missing permissions on a service account, especially if you are using a service account other than the default. You can do this by going to the IAM & Admin section of the console and checking the permissions for the service account. Checking logs for Cloud Run for the service will also help to identify any issues.
You will also need a service account for the profile service itself. As the service will have events pushed to it, it does not need any permissions to access PubSub; however, it will need permissions to read and write to Firestore and to write logs.
Receiving Pub/Sub Events
With the fact service now set up to publish events about user fact changes, let’s focus on the profile service. The profile service’s task is to subscribe to the fact service’s topic and update the relevant user profile when a message is received.
You could consider using the Go Pub/Sub client to pull messages from the topic, but it’s not ideal for this setup. Why? Because it would require the Cloud Run service to be run all the time, causing unnecessary resource consumption and increased costs. It could be scheduled to run at certain times, but this would mean there would be a significant delay in updating profiles.
Instead, you can adopt a more cloud native approach. Since the profile service is another Cloud Run service, you can set up an HTTP endpoint for Pub/Sub to push messages. When Pub/Sub pushes a message to this endpoint, it triggers an instance that processes the message in the HTTP request’s body. This approach relieves the profile service from maintaining a connection with Pub/Sub, allowing it to focus solely on updating user profiles. Furthermore, it optimizes resource usage as service instances are created and billed only when a message needs processing.
You are now ready to create a subscription to the topic that the fact service is publishing. A subscription is a way of linking events with a service. This subscription will be configured to send messages to the profile service.
Creating a Subscription
Even though messages are being published and the profile service is there waiting, nothing will happen until you create a subscription to the topic.
This last setting is useful as it means that if the profile service is temporarily unavailable for any reason, the message will be retried later. However, after that, the message will be sent to the dead letter topic.