Categories:
Aura Bot databases
Description of databases used by Aura Bot
Introduction
Currently, aura-bot uses two databases, described below:
- Mongo DB
- Redis + MongoDB
MongoDB
In order to reuse the code, and clarify the development, all classes related with MongoDB connections are stored in the mongodb-storage.ts file within aura-bot-platform repository.
Here it can be found generic “connection” class, that is mongodb-connection.ts, that will handle the connection request, pooling them
and configuring according the configuration (config is mandatory when initializing).
We can find also several “DAO” files, that will couple with a specific MongoDB database and collection, and will create optionally indexes or any other thing required in a specific domain.
MongoDbConnection
There will be only one instance of MongoDbConnection, creating it in the first call to getInstance (requiring config param) and
returning the same instance in subsequent calls (where the config param is not required).
To create the instance properly, the following configuration variables are used:
| Var name | Description | Required/Optional |
|---|---|---|
| AURA_MONGODB_URI | URI to connect to MongoDB, for example mongodb://localhost:27017/?authSource=admin | Required |
| AURA_MONGODB_USERNAME | User name to authenticate connection to MongoDB | Required |
| AURA_MONGODB_PASSWORD | Password to authenticate connection to MongoDB | Required |
| AURA_MONGODB_POOL_SIZE | MongoDB pool size | Optional (Default: 10) |
| AURA_MONGODB_SSL | Use SSL or not when connecting to MongoDB | Optional (Default: false) |
This singleton module should be loaded the first time automatically by the system, and then could be accessed just by calling getInstance
method, like:
const connection = await MongoDbConnection.getInstance();
const db = connection.dbConnect(DATABASE);
const collection = db.collection(COLLECTION);
MongoDbStorage
This DAO class will be used by Bot Builder v4 to store the conversation and user data. An index is created to delete documents older than 24
hours (specified by config var AURA_MONGODB_BOT_COLLECTION_CONTEXT_INDEX_TTL).
Following config variables are used to initialize this class:
| Var name | Description | Required/Optional |
|---|---|---|
| AURA_MONGODB_BOT_DATABASE | Database name where the documents will be stored | Required |
| AURA_MONGODB_BOT_COLLECTION_CONTEXT | Collection name where the documents will be stored | Required |
| AURA_MONGODB_BOT_COLLECTION_CONTEXT_INDEX_TTL | Time (in seconds) that will exist the documents without updates, before being deleted | Optional (Default: 86440) |
To create and initialize a MongoDbStorage, a code like following could be used:
const storage = await new MongoDbStorage().init(configuration);
Cache module
The cache module allows the generation of different types of cache:
AuraCacheLocal: only used for local data.AuraCacheRemote: only used for remote data.AuraCacheRemoteLocal: used for remote and local data, with priority for remote.AuraCacheLocalRemote: used for local and remote data, with priority for local.
How to use cache module
It is possible to use any of the caches through the following commands:
// cache local.
const auraCacheLocal = new AuraCacheLocal('nameCacheLocal');
// cache remote.
const auraCacheRemote = new AuraCacheRemote('nameCacheRemote');
// cache local-remote
const auraCacheLocalRemote = new AuraCacheLocalRemote('nameCacheLocalRemote');
// cache remote-local.
const auraCacheRemoteLocal = new AuraCacheRemoteLocal('nameCacheRemoteLocal');
Variables for AuraCacheLocal
Only for local cache (AuraCacheLocal), it is possible to define a series of options:
- stdTTL: standard TTL as number in seconds for every generated cache element. By default,
0(0 = unlimited). - compressedData: data is to be compressed in remote cache. By default,
false. - deleteOnExpire: boolean value that indicates whether variables will be deleted automatically when they expire or not. If
true, the variable will be deleted. Iffalse, the variable will remain. By default,true. You are encouraged to handle the variable upon the event expired by yourself.
const options = { stdTTL: 100, encryptData: true };
const auraCacheLocal = new AuraCacheLocal('nameCacheLocal', options);
Store a key
The following code sets a key value pair. It is possible to define a TTL (in seconds).
It returns true on success.
const auraCacheLocal = new AuraCacheLocal('nameCacheLocal');
auraCacheLocal.set('key', 'value');
const auraCacheRemote = new AuraCacheRemote('nameCacheRemote');
await auraCacheRemote.set('key', 'value', 1800); // -> data expire at 30m.
Retrieve a key
The following code gets a saved value from the cache.
It returns undefined if the value is not found or has expired. If the value is found, it returns an object with the corresponding key value pair.
const auraCacheLocal = new AuraCacheLocal('nameCacheLocal');
auraCacheLocal.set('key', 'value');
auraCacheLocal.get('key'); // -> value.
const auraCacheRemote = new AuraCacheRemote('nameCacheRemote');
await auraCacheRemote.set('key', 'value');
await auraCacheRemote.get('key'); // -> value.
For caches such as AuraCacheLocalRemote and AuraCacheRemoteLocal, it is possible to force it to obtain data different from the priority:
// cache local-remote
const auraCacheLocalRemote = new AuraCacheLocalRemote('nameCacheLocalRemote');
await auraCacheLocalRemote.set('key', 'value');
await auraCacheLocalRemote.getRemote('key'); // -> value.
auraCacheRemote.getLocal('key');
// cache remote-local.
const auraCacheRemoteLocal = new AuraCacheRemoteLocal('nameCacheRemoteLocal');
await auraCacheRemoteLocal.set('key', 'value');
auraCacheRemoteLocal.getLocal('key'); // -> value.
await auraCacheRemoteLocal.getRemote('key'); // -> value.
Delete a key
The following code deletes a key. It returns the number of deleted entries. A delete will never fail.
const auraCacheLocal = new AuraCacheLocal('nameCacheLocal');
auraCacheLocal.set('key', 'value');
auraCacheLocal.get('key'); // -> value.
auraCacheLocal.del('key'); // -> 1.
auraCacheLocal.get('key'); // -> undefined.
const auraCacheRemote = new AuraCacheRemote('auraCacheRemote');
await auraCacheRemote.set('key', 'value');
await auraCacheRemote.get('key'); // -> value.
await auraCacheRemote.del('key'); // -> 1.
await auraCacheRemote.get('key'); // -> undefined.
Flush all data
The following code deletes all data from a store.
const auraCacheLocal = new AuraCacheLocal('nameCacheLocal');
auraCacheLocal.set('key', 'value');
auraCacheLocal.get('key'); // -> value.
auraCacheLocal.flush(); // -> true.
auraCacheLocal.get('key'); // -> undefined.
const auraCacheRemote = new AuraCacheRemote('auraCacheRemote');
await auraCacheRemote.set('key', 'value');
await auraCacheRemote.get('key'); // -> value.
await auraCacheRemote.flush(); // -> true.
await auraCacheRemote.get('key'); // -> undefined.
Redis + MongoDB
If the configuration variable AURA_BOT_CONTEXT_DATABASE is set to REDIS-MONGODB, aura-bot uses a two-level cache to store the context through Aura Redis Mongo Synchronizer.
The following environment variables must be set for the two databases.
Variables
| Var name | Description | Required/Optional |
|---|---|---|
| AURA_MONGODB_BOT_COLLECTION_CONTEXT | Name of the collection where the context is stored. By default: aura-context. |
Required |
| AURA_MONGODB_BOT_DATABASE | Name of the database where the bot collections are stored. By default: aura-bot |
Required |
| AURA_MONGODB_PASSWORD | Password of the MongoDB. Required | Required |
| AURA_MONGODB_POOL_SIZE | Pool size of the MongoDB. By default: 60. |
Required |
| AURA_MONGODB_SSL | Boolean value indicating if MongoDB connection uses SSL or not. By default: true. |
Required |
| AURA_MONGODB_URI | URI of the MongoDB | Required |
| AURA_MONGODB_USERNAME | Username of the MongoDB | Required |
| AURA_REDIS_CONFIGURATION_PREFIX | Prefix for Redis configuration keys. By default: aura-config: |
Required |
| AURA_REDIS_CONTEXT_CACHE_PREFIX | Prefix for Redis context cache keys. By default: context: |
Required |
| AURA_REDIS_CONTEXT_CACHE_SHADOW_KEY_PREFIX | Prefix for Redis shadow key cache keys. By default: shadow-key: |
Required |
| AURA_REDIS_CONTEXT_CACHE_ACTIVE_CONTEXT_PREFIX | Prefix for Redis active context cache keys. By default: active-context: |
Required |
| AURA_REDIS_CONTEXT_CACHE_SHARD_COUNT | Number of shards to generate lists for storing unprocessed context. | Required |
| AURA_REDIS_CONTEXT_CACHE_TTL | Time in seconds to store a context in cache. By default: 60. |
Required |
| AURA_REDIS_MODE | Mode of Redis distribution (Cluster, Sentinel or Single). By default: Single. |
Required |
| AURA_REDIS_PREFIX | Prefix that will be used by all redis keys when using redis-connector. This allow mixing in a single redis service messages coming from different environments in the same Azure subscription. | Required |
| AURA_REDIS_SENTINEL_INSTANCE_NAME | Name of the Redis instance. Use in sentinel mode. | Optional |
| AURA_REDIS_HOSTS | A string with list of nodes (host:port) separate by comma. For example: “localhost:port,localhost2:port2”. Default: ‘127.0.0.0:6379’ | Required |
| AURA_REDIS_DATABASE | Database number for Single or Sentinel mode. By default: 0. |
|
| AURA_REDIS_PASSWORD | A string with the password of Redis. | Required |
| AURA_REDIS_MAX_RECONNECT_RETRIES | Number of retries to connect to Redis. By default: 25. |
Required |
| AURA_REDIS_MAX_RECONNECT_INTERVAL | Time in milliseconds to wait before reconnecting to Redis. By default: 5000. |
Required |
| AURA_REDIS_USE_CONNECTION_POOL | Used connection pool for Redis connections. Default: true. |
Required |
| AURA_REDIS_CONNECTION_POOL_MIN | Minimum number of connections in the pool. Default: 2. |
Required |
| AURA_REDIS_CONNECTION_POOL_MAX | Maximum number of connections in the pool. Default: 100. |
Required |