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

# Custom lock store

> Write a lock store that keeps conversation locks in your own backend, or change the built-in Redis lock store.

A lock store keeps one turn of a conversation running at a time, so user
messages are processed in the order they arrive. If a second message for the
same conversation arrives while the first is still running, it waits for its
ticket. Mantle ships `in_memory`, `redis`, and `concurrent_redis` lock stores.
Write your own when your backend is none of those, or subclass the Redis store
to change how it behaves.

## Before you start

Put your code in a package next to `agent.yml`: a folder named `addons/` with an
empty `__init__.py` file. Start `rasa` from the project root so the package can
be imported.

## Subclass `BaseLockStore`

Import `BaseLockStore` from `rasa.api.locks.v1` and subclass it. Do not subclass
`LockStore`. That is the storage port `BaseLockStore` is built on. `BaseLockStore`
adds the ticket ordering, so you only write storage methods.

```python addons/custom_lock_store.py theme={null}
from rasa.api.locks.v1 import BaseLockStore, LockStoreSettings, TicketLock


class MyLockStore(BaseLockStore):
    def __init__(self) -> None:
        super().__init__()
        self.locks: dict[str, TicketLock] = {}

    def connect(self, settings: LockStoreSettings) -> None:
        """Read settings from integrations.yml. Mantle calls this once at startup."""
        self.endpoint = settings.params["endpoint"]

    def get_lock(self, conversation_id: str) -> TicketLock | None:
        """Return the stored lock for a conversation, or None if it has none."""
        return self.locks.get(conversation_id)

    def save_lock(self, lock: TicketLock) -> None:
        """Store the lock under its conversation ID, replacing any earlier lock."""
        self.locks[lock.conversation_id] = lock

    def delete_lock(self, conversation_id: str) -> None:
        """Remove the lock for a conversation. A missing lock is ignored."""
        self.locks.pop(conversation_id, None)
```

This example keeps locks in a dictionary, so it runs as written. A dictionary
exists in one process only, so with more than one worker or replica, replace the
dictionary calls with calls to a shared backend. Keep the four method names and
signatures. `connect` runs once at startup. Use it to open your client with the
settings from `integrations.yml`.

Register the class under `lock_store`:

```yaml integrations.yml theme={null}
lock_store:
  type: addons.custom_lock_store.MyLockStore
  endpoint: ${LOCK_STORE_ENDPOINT}
```

Every key under `lock_store` except `type` arrives in `settings.params`. Use
`${VAR}` for secrets so they stay out of the file.

| Method | Required | Purpose |
| - | - | - |
| `connect(settings)` | yes | Open your connection, using `settings.params` |
| `get_lock(conversation_id)` | yes | Return the stored `TicketLock`, or `None` |
| `save_lock(lock)` | yes | Store the `TicketLock` under `lock.conversation_id` |
| `delete_lock(conversation_id)` | yes | Remove the lock for that conversation |

## Subclass the built-in Redis store

To change one behavior of `redis` and keep the rest, subclass `RedisLockStore`
from the same package. This example logs each save at debug level, using only
the conversation ID:

```python addons/custom_redis_lock_store.py theme={null}
import logging

from rasa.api.locks.v1 import RedisLockStore, TicketLock

logger = logging.getLogger(__name__)


class MyRedisLockStore(RedisLockStore):
    def save_lock(self, lock: TicketLock) -> None:
        """Log the conversation ID, then save the lock with the built-in Redis store."""
        logger.debug("Saving lock for conversation %s", lock.conversation_id)
        super().save_lock(lock)
```

```yaml integrations.yml theme={null}
lock_store:
  type: addons.custom_redis_lock_store.MyRedisLockStore
  host: ${REDIS_HOST}
  port: 6379
  password: ${REDIS_PASSWORD}
```

The Redis keys (`host`, `port`, `username`, `password`, and the others in
[`lock_store`](https://rasa.com/docs/mantle/reference/integrations-yml#lock_store)) work the same way for
your subclass. The built-in names `redis` and `concurrent_redis` still refer to
the built-in classes. Use a module path only for your own class.

## Versioning

The import `rasa.api.locks.v1` is the version. YAML names only the class. A
later version ships with its own `BaseLockStore`, and your import keeps working
while `v1` is supported. See
[Custom integrations: Versioning](https://rasa.com/docs/mantle/reference/custom-integrations.md#versioning).


## Related topics

- [Lock Stores](https://rasa.com/docs/reference/integrations/lock-stores.md)
- [Custom integrations](https://rasa.com/docs/mantle/reference/custom-integrations.md)
- [integrations.yml](https://rasa.com/docs/mantle/reference/integrations-yml.md)
- [Rasa Pro Changelog](https://rasa.com/docs/reference/changelogs/rasa-pro-changelog.md)
- [Mantle Changelog](https://rasa.com/docs/mantle/changelog.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.