package
0.0.0-20240814010835-9fc48a241e25
Repository: https://github.com/surahman/ftex.git
Documentation: pkg.go.dev

# 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:

LocationDetails
/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 variablesFinally, 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.

NameEnvironment Variable KeyTypeDescription
AuthenticationREDIS_AUTHENTICATIONParent key for authentication information.
↳ username.USERNAMEstringUsername for Redis session login.
↳ password.PASSWORDstringPassword for Redis session login.
ConnectionREDIS_CONNECTIONParent key for connection configuration.
↳ addr.ADDRstringThe cluster IPs to bootstrap the connection. Must contain the port number.
↳ maxConnAttempts.MAXCONNATTEMPTSintThe maximum number of times to try to establish a connection.
↳ maxRetries.MAXRETRIESintThe maximum number of times to try an operation.
↳ poolSize.POOLSIZEintThe connection pool size on a per cluster basis.
↳ minIdleConns.MINIDLECONNSintThe number of minimum idle connections per client.
↳ maxIdleConns.MAXIDLECONNSintThe 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

# Functions

NewError is a base error message with no special code.
NewRedis will create a new Redis configuration by loading it.

# Constants

Error codes.
Error codes.
Error codes.
Error codes.
Error codes.

# Variables

ErrCacheDel is returned if a key-value pair cannot be deleted from the cache.
ErrCacheMiss is returned when a cache miss occurs.
ErrCacheSet is returned if a key-value pair cannot be placed in the cache.
ErrCacheUnknown is returned if an unknown error occurs in the cache.
ErrUnhealthy is returned if the cache is unreachable.

# Structs

Error is the base error type.

# Interfaces

Redis is the interface through which the cache server can be accessed.