Rest – Implementing a caching microservice by avoiding potential bottlenecks

cachingmessage-queuemicroservicesrest

The system I am working on employs quite a sophisticated caching/preloading mechanism for external third-parties, and since it is built on a microservice architecture, I would like to extract the whole caching/preloading functionality from a feature microservice (say, booking/searching) to a dedicated one:

+-----+      +--------------------+            +----------+
| API | <--> | booking | internal | <--------> | external |
|     |      | service | cache    |            | service  |
+-----+      +--------------------+            +----------+

versus

+-----+      +---------+      +---------+      +----------+
| API | <--> | booking | <--> | caching | <--> | external |
|     |      | service |      | service |      | service  |
+-----+      +---------+      +---------+      +----------+

I am concerned with the potential bottlenecks this might introduce: instead of the booking microservice using an internal cache system that talks to a backend, it will communicate with a separate microservice for all get/set calls (that in turn talks to the backend). The bottleneck I see is mainly the added time for communicating through the extra microservice. Should this be a big concern? How shall I tackle communication between the booking microservice and the caching one (REST, message queues)? It should be synchronous communication, as far as I see things. What any other (hidden) bottlenecks can arise from this setup?

Best Answer

My suggestion is that instead of worrying about whether to use an external or internal cache, your first concern should be that your booking-service does not care whether or not your are using an external service.

That is to say, your booking-service should be caching against an interface with the concrete implementation injected in; it would not know or care if it was using:

  1. An external caching service (out of process)
  2. An internal cache (in process)
  3. A pass through cache which just went straight to the external service

At some point, you could even develop a system which used a smart combination of external and internal.

Once this behavior is properly isolated, you are then much more free to explore the advantages and disadvantages of each solution for your particular use case.