> ## Documentation Index
> Fetch the complete documentation index at: https://docs.conduktor.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Cache Kafka records — Conduktor Gateway

> Cache Kafka records with a Conduktor Gateway interceptor to reduce broker load and improve consumer fetch performance.

<Badge stroke color="blue" icon="sparkle" size="lg">Enterprise</Badge>

## Overview

The cache <Tooltip tip="Conduktor Interceptors are Gateway plugins that transform and manipulate data.">Interceptor</Tooltip> is designed to improve the performance of record retrieval in a Kafka <Tooltip tip="A group of Kafka brokers working together to handle data streams.">cluster</Tooltip> by caching records. It intercepts produce and fetch requests, caching produced records and serving fetched records from the cache, if available.

## Benefits

Benefits include:

* **Improved performance**. By serving fetched records from the cache, subsequent fetch requests can be served faster, reducing the overall
  latency and improving the response time for clients.

* **Reduced load on Kafka cluster**. With the cache Interceptor in place, the Kafka cluster experiences reduced load during fetch requests since a portion
  of the requests can be satisfied from the cache directly, reducing the number of requests hitting the cluster.

* **Enhanced scalability**. The cache Interceptor provides an additional layer of scalability by distributing the workload between the cache and
  the Kafka cluster. It can handle a higher volume of fetch requests without overwhelming the Kafka cluster.

## Configuration options

| Config      | Type                         | Default | Description                                                                                                                       |
| :---------- | ---------------------------- | :------ | :-------------------------------------------------------------------------------------------------------------------------------- |
| topic       | String                       | `.*`    | Topic regex, topic that match this regex will have the Interceptor applied. If no value is set, it will be applied to all topics. |
| cacheConfig | [Cache config](#cacheconfig) |         | Configuration for cache.                                                                                                          |

### CacheConfig

| Config        | Type                             | Description                        |
| :------------ | -------------------------------- | :--------------------------------- |
| type          | enum (IN\_MEMORY, ROCKSDB)       | Default: ROCKSDB.                  |
| rocksdbConfig | [Rocksdb config](#rocksdbconfig) | Configuration for RocksDB cache.   |
| inMemConfig   | [InMem config](#inmemconfig)     | Configuration for in-memory cache. |

### RocksdbConfig

| Config    | Type   | Default           | Description                                    |
| :-------- | ------ | :---------------- | :--------------------------------------------- |
| dbPath    | String | `caching_storage` | Path to RocksDB database.                      |
| cacheSize | long   | `104857600`       | RocksDB cache size in bytes (default: 100 MB). |

### InMemConfig

| Config       | Type | Default  | Description                                        |
| :----------- | ---- | :------- | :------------------------------------------------- |
| cacheSize    | int  | `10000`  | Maximum number of entries in the in-memory cache.  |
| expireTimeMs | long | `300000` | Cache expiration time in milliseconds (5 minutes). |

## Example

<Tabs>
  <Tab title="curl">
    ```bash theme={null}
    curl \
      --request PUT \
      --url 'http://localhost:8888/gateway/v2/interceptor' \
      --header 'Authorization: Basic YWRtaW46Y29uZHVrdG9y' \
      --header 'Content-Type: application/json' \
      --data-raw '{
      "name": "myCacheInterceptor",
      "pluginClass": "io.conduktor.gateway.interceptor.CacheInterceptorPlugin",
      "priority": 100,
      "config": {
        "topic": ".*",
        "cacheConfig": {
          "type": "ROCKSDB",
          "rocksdbConfig": {
            "dbPath": "/caching_storage",
            "cacheSize": 104857600
          }
        }
      }
    }'
    ```
  </Tab>

  <Tab title="Conduktor CLI">
    ```yaml theme={null}
    apiVersion: gateway/v2
    kind: Interceptor
    metadata:
      name: myCacheInterceptor
      scope:
        vCluster: passthrough
    spec:
      pluginClass: io.conduktor.gateway.interceptor.CacheInterceptorPlugin
      priority: 100
      config:
        topic: ".*"
        cacheConfig:
          type: ROCKSDB
          rocksdbConfig:
            dbPath: /caching_storage
            cacheSize: 104857600
    ```

    Apply with:

    ```bash theme={null}
    conduktor apply -f cache-interceptor.yaml
    ```
  </Tab>
</Tabs>
