Skip to content

Redis as our cache

Introduction

We are using Redis as a key-value store to save flow execution related data. We are doing this to improve the performance of the application.

We defined a trait called CachesContextAwareData which creates a PSR-Simplecache compliant cache pool. The cache pool is used to store context-aware data, i.e. based on flow execution id, JDM id and environment id. This way, we can store data that is only relevant for a specific context; If the context is different, the data is not used.

Usage

Add the trait CachesContextAwareData to your class. The trait provides a method called getCache(). This method returns the cache pool. You can use the cache pool to store and retrieve data.

Flow configuration

It is possible to clear the cache after a flow execution. This can be done by adding the following configuration to the flow configuration:

{
  "clearAllCaches": true, 
  "clearFlowExecutionCaches": true
}

The option clearAllCaches clears all caches. The option clearFlowExecutionCaches clears only the caches that are related to the flow execution.

CLI command

We created a CLI command to clear the caches. The command is called bin/console synqup:clear-redis-maintenance. It has the following options:

bin console synqup:clear-redis-maintenance --force

If the option --force is set, the command clears all caches. If the option is not set, it will be checked if flow executions are still running or if the cache contains less than 100MB of data. If both conditions are not met, the command will clear the caches.

Heartbeat Subscriber

We also have a heartbeat subscriber in place. It is called HeartbeatEventRedisSubscriber. The subscriber is responsible for clearing the cache. It listens to the HeartbeatEvent. It checks if the hour after midnight has passed. If it has, it checks if there was a cache cleanup within the last REDIS_FLUSH_INTERVAL seconds (environment variable, defaults to 900). Just the the CLI command, it checks if flow executions are still running or if the cache contains less than 100MB of data. If both conditions are not met, the cache will be cleared.