# README
Redis
Configuration loading is designed for containerization in mind. The container engine and orchestrator can mount volumes (secret or regular) as well as set the environment variables as outlined below.
You may set configurations through both files and environment variables. Please note that environment variables will
override the settings in the configuration files. The configuration files are all expected to be in YAML
format.
Table of contents
Case Study and Justification
Redis will be leveraged as a session store for conversion rate offers for both Fiat and Cryptocurrency purchases. Whenever a user requests the purchase or conversion of a currency an offer will be generated by retrieving the exchange rate and generating an offer. This offer will be placed in the Redis cache with an expiration TTL. The offer details and ID will be returned to the user. If the user chooses to accept the offer, they will make an API call to retrieve the offer from the cache using its ID.
Redis is ideal for the use case here:
- Highly performant cache.
- Built-in data replication for high-availability across a cache cluster.
- On-disk persistence protects against cold cache scenarios which may see the backend database get overwhelmed with requests in the event of a cache failure.
- Automatic failover.
- Keys are evicted using an LRU policy.
- Keys can have an expiration time set via a time-to-live.
Storing the conversion rates is another potential use for the Redis cache, but it is far from ideal since we enjoy access to live exchange rate data. Providing rates that are fixed over a period of time can result in large financial losses. Nonetheless, the cached currency exchange rate use case is outlined below.
To reduce latency and API calls to a third-party data provider a cache must be used. It is assumed for this demonstration application that price quotes update at evenly spaced intervals over an hour (i.e. every fifteen minutes or four times an hour).
There is a risk of a cache stampede where all keys get evicted at the same time. Fiat currencies are finite in number (~197) and it is extremely likely that there are just one or two dozen prevailing ones. There are many Cryptographic currencies but only a few are assumed to be popular. Both of these situations mean that API calls for price quotes will likely be limited. Any evictions from the cache will be easily refilled without overloading the third-party API.
Cache Policy:
Quotes will be evicted from the cache at the following intervals, plus a random delta of a few seconds above.
- hour + 00 minutes
- hour + 15 minutes
- hour + 30 minutes
- hour + 45 minutes
File Location(s)
The configuration loader will search for the configurations in the following order:
Location | Details |
---|---|
/etc/FTeX.conf/ | The etc directory is the canonical location for configurations. |
$HOME/.FTeX/ | Configurations can be located in the user's home directory. |
./configs/ | The config folder in the root directory where the application is located. |
Environment variables | Finally, the configurations will be loaded from environment variables and override configuration files |
Configuration File
The expected file name is RedisConfig.yaml
. Unless otherwise specified, all the configuration items below are required.
Name | Environment Variable Key | Type | Description |
---|---|---|---|
Authentication | REDIS_AUTHENTICATION | Parent key for authentication information. | |
↳ username | ↳ .USERNAME | string | Username for Redis session login. |
↳ password | ↳ .PASSWORD | string | Password for Redis session login. |
Connection | REDIS_CONNECTION | Parent key for connection configuration. | |
↳ addr | ↳ .ADDR | string | The cluster IPs to bootstrap the connection. Must contain the port number. |
↳ maxConnAttempts | ↳ .MAXCONNATTEMPTS | int | The maximum number of times to try to establish a connection. |
↳ maxRetries | ↳ .MAXRETRIES | int | The maximum number of times to try an operation. |
↳ poolSize | ↳ .POOLSIZE | int | The connection pool size on a per cluster basis. |
↳ minIdleConns | ↳ .MINIDLECONNS | int | The number of minimum idle connections per client. |
↳ maxIdleConns | ↳ .MAXIDLECONNS | int | The maximum number idle connections per client. |
Example Configuration File
authentication:
username: ftex_service
password: ZoF1bncLLyYT1agKfWQY
connection:
addr: 127.0.0.1:7379
maxConnAttempts: 5
maxRetries: 3
poolSize: 4
minIdleConns: 1
maxIdleConns: 20
Example Environment Variables
export REDIS_AUTHENTICATION.USERNAME=admin
export REDIS_AUTHENTICATION.PASSWORD=root