Skip to content
This repository was archived by the owner on Aug 7, 2025. It is now read-only.
This repository was archived by the owner on Aug 7, 2025. It is now read-only.

[RFC] Add third party caching support in TorchServe #2465

@agunapal

Description

@agunapal

🚀 The feature

Proposal

This proposal is for adding third party caching support in TorchServe.

Conceptually, we can add caching support in either frontend or backend

Frontend Caching Support

Screenshot 2023-07-13 at 11 01 01 AM

Backend Caching Support

Screenshot 2023-07-13 at 11 02 47 AM

From a design point of view, though it would make sense to add caching support in the frontend, the proposal is to add caching support for the backend for 2 reasons:

  1. Easy of use with Python
  2. Ability to extend the base class and customize the caching logic.

We start by adding support for Redis.

The implementation is based on the example proposed by @GavinPHR in #1952

Design Considerations

Two main considerations when designing this solution are

  1. How do you specify the config needed to connect to RedisCache
  2. Where do we plugin Caching class in TorchServe

Specify Cache Config

We could pass the config using config.properties, model-config.yaml, extra-files.
Since we are adding cache support for Python backend, it makes sense to add the config in model-config.yaml
This also gives users the flexibility to use different caches for different model.

The necessary config to access your Redis Cache is defined in the model-config.yaml

cache:
  host: localhost
  port: 6379
  db: 0

TorchServe Implementation

The basic idea is to wrap the handle method with the RedisCache class.
We use the context as the key , hash the context and use this as a key to store the inference result

Where do we wrap the handle method?
Option A

In model_loader.py, load method

User specifies config in yaml file. Based on the config, the entry_point is wrapped with RedicCache

Pro:
Minimal effort
Con:
Spaghetti code

Option B

In custom_handler.py , initialize method

Pro:
User can customize easily
Con:
User needs to define the initialize method

The below code is with Option B

To use Redis Cache with TorchServe, include the following in your handler
from ts.handler_utils.cache.redis import RedisCache

and define your handler as follows for an Image Classifier.
The key idea is to wrap the handle method with the class RedisCache

from ts.torch_handler.image_classifier import ImageClassifier


class CacheHandler(ImageClassifier):
    def __init__(self):
        super(CacheHandler, self).__init__()
        self.initialized = False

    def initialize(self, ctx):
        super().initialize(ctx)
        self.handle = RedisCache(ctx)(self.handle)
        self.initialized = True

Motivation, pitch

Implementing caching support will result in cost savings for customers in inference costs.

Alternatives

No response

Additional context

No response

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions