简体   繁体   中英

Google Cloud Pub/Sub and multiple receivers

My usecase is this:

I have X instances of App Engine NodeJS backend. I use Redis Cloud Memorystore as caching layer. To minimize Redis use, and add performance, each backend instance also has its own local caching.

Rules are like this, when a request comes in to a backend instance:

  • first check local cache
  • if found, use that.
  • if not found, look in Redis cache
  • update local cache with data from Redis

The local cache times out much quicker than the Redis cache, but I would still like to be able to send a "clear cache" message to all instances.

The entire thing works extremely well, but I'd like to be able to update the local caches by sending a pub/sub message which in turn would be received by all backend instances, clearing the local caches.

Yes, I could just opt not to use local caching and just Redis MemoryStore, but that would mean.network traffic for every Redis lookup, slower responses, and more Redis resource use.

Is this possible at all, or is there only 1 receiver for each pushed message? I know the receiver can opt not to ack a message, but how to make sure all instances have received, and how to know that has happened, so ack can be sent by the last instance? Seems impossible

Your use case is uncommon, but IMO, you have 2 solutions:

  1. Use Redis

Put a clear cache datetime in redis. For each request, the App Engine instance call for the clear cache datetime. If the local value (the previous clear cache value, or null if there is no precedent call (new instance case)) is behind the Redis value, clear the local cache. Else continue

  1. Use PubSub

(Not my preferred) When an instance start, create a pull subscription on a PubSub topic. Pull the messages. When you receive one, clear the cache.

With PubSub, when you publish a message in a topic, it is duplicated in all subscription. With that pattern, all the instances will have a subscription and can receive the message.

However, you could reach the number of subscription quotas. To limit that, set the expiration value to 1 day, to clean automatically the subscription after 1 day without any subscriber.

Not sure there is a tear down notice on App Engine instance to let you the time to delete the subscription nicely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM