> For the complete documentation index, see [llms.txt](https://amartyushov.gitbook.io/tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://amartyushov.gitbook.io/tech/app-aspects/software-architecture/caching.md).

# Caching

## Caching topologies

![](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FR3fyhIs45Piyz0gCZmuf%2FScreenshot%202022-05-04%20at%2009.34.10.png?alt=media\&token=dba9b8b7-38a2-4071-a76a-c8011063f352)

## Single in-memory cache

![](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FEREhEy1rkNADFd1Hvif7%2FScreenshot%202022-05-04%20at%2009.35.04.png?alt=media\&token=5a5e0137-dbc6-4120-b402-6b779dcaf7ca)

## Distributed (client-server) cache

![](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FKMwqZc0MlVrwIUTno8P5%2FScreenshot%202022-05-06%20at%2008.56.36.png?alt=media\&token=5dc0d128-5dc3-43fd-b168-bfc658b453c3)

Proprietary protocol is usually based on WebSocket.

## Replicated (in-process) cache

![](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FJJTc7WFSRjj8R1Bxi3r3%2FScreenshot%202022-05-06%20at%2009.08.48.png?alt=media\&token=058560dc-5e1f-4338-8f17-3de2661321ea)

Cache on every node contains a replica of cache from another node.&#x20;

If cache size growth this may be a problem (resources).

## Near cache hybrid

![](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FJ3OeBB67tCcOEsZKfTPT%2FScreenshot%202022-05-06%20at%2009.12.56.png?alt=media\&token=64885ec1-7fab-42d0-857e-3f4424b2c210)

* there is a distributed cache (backing cache)
* Each node contains a subset of backing cache (it communicates with backing cache)
  * MRU - most recently used
  * MFU - most frequently used
* client libraries (caches) are not talking to each other
  * they may have different data from one another

## Which cache for microservices?

* Local cache does not fit - because each service will have non consistent cache
* Near-cache does not fit&#x20;
  * as far as every service has a subset of cache which is also non consistent => every service will have different response latency (cache on one service has value\_1 and returns it quick, cache on another service does not have this value in near cache => request to backing cache is fired => latency is higher)
* Replicated cache OR Distributed cache is a choice for microservices

![](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FabkGKpCt2pWlIgPEsahv%2FScreenshot%202022-05-06%20at%2009.41.15.png?alt=media\&token=2b228bbf-7d9f-488c-852d-f1f6508d083e)
