pnxtech / hydra

A light-weight library for building distributed applications such as microservices
https://www.hydramicroservice.com
MIT License
645 stars 54 forks source link

Support for cached calls. #162

Closed cjus closed 6 years ago

cjus commented 6 years ago

This PR is for an important optimization in Hydra core.

The _checkServicePresence method is one of the most utilized internal functions in hydra. It's called by functions interested in knowing whether a particular service is available. Under the hood, the function first queries Redis for keys related to a named service. The code then proceeds to query additional information for each of the returned service instances. Because this data is stored in Redis as a stringified JSON object, the returned data must be parsed to a restored JavaScript object. Each returned object is then pushed onto an array and returned.

Although those operations are blazing fast, they're executed everytime presence information is needed. Which has the potential to be quite often!

The place those operations take their toll is when there are a lot of HTTP and socket-based messaging flowing through your microservices. We discovered this while testing our HydraRouter microservice which handles considerably more traffic than the rest of our services.

However, this can happen in any service which calls another service hundreds of times per second. This PR is designed to cache calls to _checkServicePresence so that cached results are returned for all calls within three seconds. This should greatly reduce the burden on chatty microservices and on Redis itself.

Although not strictly required I've added the same cache handling to the _getServiceHealth() call.

There may indeed be other places in Hydra which can benefit from internal caching. However, caching is difficult and can lead to difficult to track errors and edge cases.