# Tracker Stores

All conversations are stored within a tracker store.
Rasa Open Source provides implementations for different store types out of the box.
If you want to use another store, you can also build a custom tracker store by extending the `TrackerStore` class.

## Contents
- [Tracker Stores](https://legacy-docs-v1.rasa.com/1.9.2/api/tracker-stores/#tracker-stores)
  - [InMemoryTrackerStore (default)](https://legacy-docs-v1.rasa.com/1.9.2/api/tracker-stores/#inmemorytrackerstore-default)
  - [SQLTrackerStore](https://legacy-docs-v1.rasa.com/1.9.2/api/tracker-stores/#sqltrackerstore)
  - [RedisTrackerStore](https://legacy-docs-v1.rasa.com/1.9.2/api/tracker-stores/#redistrackerstore)
  - [MongoTrackerStore](https://legacy-docs-v1.rasa.com/1.9.2/api/tracker-stores/#mongotrackerstore)
  - [DynamoTrackerStore](https://legacy-docs-v1.rasa.com/1.9.2/api/tracker-stores/#dynamotrackerstore)
  - [Custom Tracker Store](https://legacy-docs-v1.rasa.com/1.9.2/api/tracker-stores/#custom-tracker-store)

## InMemoryTrackerStore (default)

Description
`InMemoryTrackerStore` is the default tracker store. It is used if no other tracker store is configured. It stores the conversation history in memory.

Note
As this store keeps all history in memory, the entire history is lost if you restart the Rasa server.

Configuration
To use the `InMemoryTrackerStore` no configuration is needed.

## SQLTrackerStore

Description
`SQLTrackerStore` can be used to store the conversation history in an SQL database. Storing your trackers this way allows you to query the event database by sender_id, timestamp, action name, intent name and typename.

Configuration
To set up Rasa Open Source with SQL the following steps are required:
1. Add required configuration to your `endpoints.yml`:

```yaml
   tracker_store:
       type: SQL
       dialect: "postgresql"  # the dialect used to interact with the db
       url: ""  # (optional) host of the sql db, e.g. "localhost"
       db: "rasa"  # path to your db
       username:  # username used for authentication
       password:  # password used for authentication
       query: # optional dictionary to be added as a query string to the connection URL
         driver: my-driver
   ```
2. To start the Rasa server using your SQL backend,
add the `--endpoints` flag, e.g.:

```bash
   rasa run -m models --endpoints endpoints.yml
   ```
3. If deploying your model in Docker Compose, add the service to your `docker-compose.yml`:

```yaml
   postgres:
     image: postgres:latest
   ```

To route requests to the new service, make sure that the `url` in your `endpoints.yml`
references the service name:

```yaml
   tracker_store:
       type: SQL
       dialect: "postgresql"  # the dialect used to interact with the db
       url: "postgres"
       db: "rasa"  # path to your db
       username:  # username used for authentication
       password:  # password used for authentication
       query: # optional dictionary to be added as a query string to the connection URL
         driver: my-driver
   ```

Parameters
- `domain` (default: `None`): Domain object associated with this tracker store
- `dialect` (default: `sqlite`): The dialect used to communicate with your SQL backend.
- `url` (default: `None`): URL of your SQL server
- `port` (default: `None`): Port of your SQL server
- `db` (default: `rasa.db`): The path to the database to be used
- `username` (default: `None`): The username which is used for authentication
- `password` (default: `None`): The password which is used for authentication
- `event_broker` (default: `None`): Event broker to publish events to
- `login_db` (default: `None`): Alternative database name to which initially connect, and create the database specified by `db` (PostgreSQL only)
- `query` (default: `None`): Dictionary of options to be passed to the dialect and/or the DBAPI upon connect

### Officially Compatible Databases
- PostgreSQL
- Oracle > 11.0
- SQLite

### Oracle Configuration
To use the SQLTrackerStore with Oracle, there are a few additional steps. First, create a database `tracker` in your Oracle database and create a user with access to it. Create a sequence in the database with the following command:

```sql
   CREATE SEQUENCE username.events_seq;
   ```
Next you have to extend the Rasa Open Source image to include the necessary drivers.

```dockerfile
   FROM rasa/rasa:1.9.2-full
   USER root
   RUN apt-get update -qq && apt-get install -y --no-install-recommends alien libaio1 && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
   COPY oracle.rpm oracle.rpm
   RUN pip install cx-Oracle
   RUN alien -i oracle.rpm
   USER 1001
   ```

Then build the docker image:

```bash
   docker build . -t rasa-oracle:1.9.2-oracle-full
   ```

Now you can configure the tracker store in the `endpoints.yml` as described above,
and start the container. The `dialect` parameter will be `oracle+cx_oracle`.

## RedisTrackerStore

Description
`RedisTrackerStore` can be used to store the conversation history in [Redis](https://redis.io/).

Configuration
To set up Rasa Open Source with Redis the following steps are required:
1. Start your Redis instance
2. Add required configuration to your `endpoints.yml`:
   ```yaml
   tracker_store:
       type: redis
       url: <url of the redis instance, e.g. localhost>
       port: <port of your redis instance, usually 6379>
       db: <number of your database within redis, e.g. 0>
       password: <password used for authentication>
       use_ssl: <whether or not the communication is encrypted, default `false`>
   ```
3. To start the Rasa server using your configured Redis instance,
add the `--endpoints` flag:
   ```bash
   rasa run -m models --endpoints endpoints.yml
   ```
4. If deploying your model in Docker Compose, add the service to your `docker-compose.yml`:
   ```yaml
   redis:
     image: redis:latest
   ```

To route requests to the new service,
make sure that the `url` in your `endpoints.yml` references the service name:
   ```yaml
   tracker_store:
       type: redis
       url: <url of the redis instance, e.g. localhost>
       port: <port of your redis instance, usually 6379>
       db: <number of your database within redis, e.g. 0>
       password: <password used for authentication>
       use_ssl: <whether or not the communication is encrypted, default `false`>
   ```

Parameters
- `url` (default: `localhost`): The url of your redis instance
- `port` (default: `6379`): The port which redis is running on
- `db` (default: `0`): The number of your redis database
- `password` (default: `None`): Password used for authentication
- `record_exp` (default: `None`): Record expiry in seconds
- `use_ssl` (default: `False`): whether or not to use SSL for transit encryption

## MongoTrackerStore

Description
`MongoTrackerStore` can be used to store the conversation history in [Mongo](https://www.mongodb.com/).

Configuration
1. Start your MongoDB instance.
2. Add required configuration to your `endpoints.yml`
   ```yaml
   tracker_store:
       type: mongod
       url: <url to your mongo instance, e.g. mongodb://localhost:27017>
       db: <name of the db within your mongo instance, e.g. rasa>
       username: <username used for authentication>
       password: <password used for authentication>
       auth_source: <database name associated with the user’s credentials>
   ```
   You can also add more advanced configurations (like enabling ssl) by appending a parameter to the url field:
   ```yaml
   mongodb://localhost:27017/?ssl=true
   ```
3. To start the Rasa server using your configured MongoDB instance,
add the `--endpoints` flag:
   ```bash
   rasa run -m models --endpoints endpoints.yml
   ```
4. If deploying your model in Docker Compose, add the service to your `docker-compose.yml`:
   ```yaml
   mongo:
     image: mongo
     environment:
       MONGO_INITDB_ROOT_USERNAME: rasa
       MONGO_INITDB_ROOT_PASSWORD: example
   mongo-express:
     image: mongo-express
     ports:
       - 8081:8081
     environment:
       ME_CONFIG_MONGODB_ADMINUSERNAME: rasa
       ME_CONFIG_MONGODB_ADMINPASSWORD: example
   ```

To route requests to this database,
make sure to set the `url` in your `endpoints.yml` as the service name:
   ```yaml
   tracker_store:
       type: mongod
       url: mongodb://mongo:27017
       db: <name of the db within your mongo instance, e.g. rasa>
       username: <username used for authentication>
       password: <password used for authentication>
       auth_source: <database name associated with the user’s credentials>
   ```

Parameters
- `url` (default: `mongodb://localhost:27017`): URL of your MongoDB
- `db` (default: `rasa`): The database name which should be used
- `username` (default: `0`): The username which is used for authentication
- `password` (default: `None`): The password which is used for authentication
- `auth_source` (default: `admin`): database name associated with the user’s credentials.
- `collection` (default: `conversations`): The collection name which is used to store the conversations

## DynamoTrackerStore

Description
`DynamoTrackerStore` can be used to store the conversation history in [DynamoDB](https://aws.amazon.com/dynamodb/).

Configuration
1. Start your DynamoDB instance.
2. Add required configuration to your `endpoints.yml`:
   ```yaml
   tracker_store:
       type: dynamo
       tablename: <name of the table to create, e.g. rasa>
       region: <name of the region associated with the client>
   ```
3. To start the Rasa server using your configured `DynamoDB`
add the `--endpoints` flag:
   ```bash
   rasa run -m models --endpoints endpoints.yml
   ```

Parameters
- `tablename (default: states)`: name of the DynamoDB table
- `region` (default: `us-east-1`): name of the region associated with the client

## Custom Tracker Store

Description
If you require a tracker store which is not available out of the box, you can implement your own.
This is done by extending the base class `TrackerStore`.

Class `rasa.core.tracker_store.TrackerStore(domain, event_broker=None, retrieve_events_from_previous_conversation_sessions=False)`

Steps
1. Extend the `TrackerStore` base class. Note that your constructor has to provide a parameter `url`.
2. In your `endpoints.yml` put in the module path to your custom tracker store and the parameters you require:
   ```yaml
   tracker_store:
       type: path.to.your.module.Class
       url: localhost
       a_parameter: a value
       another_parameter: another value
   ```
3. If you are deploying in Docker Compose,
you have two options to add this store to Rasa Open Source:
   - extending the Rasa image to include the module
   - mounting the module as volume

Make sure to add the corresponding service as well.
For example, mounting it as a volume would look like so:
   ```yaml
   rasa:
       <existing rasa service configuration>
       volumes:
           - <existing volume mappings, if there are any>
           - ./path/to/your/module.py:/app/path/to/your/module.py
   custom-tracker-store:
       image: custom-image:tag
   ```
   `endpoints.yml`:
   ```yaml
   tracker_store:
       type: path.to.your.module.Class
       url: custom-tracker-store
       a_parameter: a value
       another_parameter: another value
   ```
