# Replacement caches

## Introduction

Having [chosen to configure cache replacements for a virtual environment](https://doc.wearepatchworks.com/product-documentation/virtual-environments/implementing-virtual-environments/configuring-a-virtual-environment-version/configuring-virtual-environment-replacements/..#configuring-replacements-for-a-virtual-environment-version), a new replacement rule is displayed, ready for you to configure:

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FoGbdf02KTpaMkdpOZ690%2Freplace%20cache%201.png?alt=media&#x26;token=d53882c8-6d0c-4d28-b978-19f69302fee4" alt=""><figcaption></figcaption></figure>

Having defined the first rule, you can add more - you must create one replacement rule for every cache that you need to replace in every process flow that will run in this environment.&#x20;

## Need to know

Please consider the [cache type](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/cache/add-to-cache-shape) used when replacing caches in [master process flows](https://doc.wearepatchworks.com/product-documentation/virtual-environments/implementing-virtual-environments/creating-a-master-process-flow).

<table data-full-width="true"><thead><tr><th width="141.7890625">Cache type </th><th width="159.76171875">Can be replaced in VE config?</th><th width="166.5625">Can be used in master process flows?</th><th>Cache key notes</th></tr></thead><tbody><tr><td><strong>Company</strong></td><td>Yes</td><td>Yes</td><td><p>We recommend that you always replace <code>company</code> caches in virtual environments, unless you have a good reason for not doing so. This ensures that caches are unique for each environment.</p><p></p><p>Keep in mind that cache keys are NOT replaced. Provided that you replace the <code>company</code> cache in each virtual environment, this is not an issue - the cache in each environment will be unique.</p><p></p><p>If, for some reason, you do not take this approach, please ensure that the given cache key in your master process flow utilises variables to ensure that it's unique at runtime - otherwise, cached data from one environment may overwrite data from another. </p></td></tr><tr><td><strong>Flow</strong> </td><td>Yes</td><td>Yes</td><td><p>We recommend that you always replace <code>flow</code> caches in virtual environments, unless you have a good reason for not doing so. This ensures that caches are unique for each environment.</p><p></p><p>Keep in mind that cache keys are NOT replaced. Provided that you replace the <code>flow</code> cache in each virtual environment, this is not an issue - the cache in each environment will be unique.</p><p></p><p>If, for some reason, you do not take this approach, please ensure that the given cache key in your master process flow utilises variables to ensure that it's unique at runtime - otherwise, cached data from one environment may overwrite data from another. Please see <a href="../../../creating-a-master-process-flow/special-considerations-for-building-master-process-flows/special-considerations-for-building-master-process-flows-caches#referencing-variables-for-unique-cache-keys">Referencing variables for unique cache keys</a> for more information.</p></td></tr><tr><td><strong>Flow run</strong></td><td>No</td><td>Yes</td><td>Behind the scenes, each cache key for a <code>flow run</code> cache is appended with a unique <code>flow run id</code> - although you never see this in the dashboard, it does mean that these caches will never clash between virtual environments. <br><br>However, if required, you can reference an environment variable in the cache key name, so the cache key is immediately identifiable for each virtual environment.  please see <a href="../../../creating-a-master-process-flow/special-considerations-for-building-master-process-flows/special-considerations-for-building-master-process-flows-caches#referencing-variables-for-unique-cache-keys">Referencing variables for unique cache keys</a> for more information.</td></tr></tbody></table>

{% hint style="info" %}
For more information on the different cache types, please refer to our [Add to cache](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/cache/add-to-cache-shape) documentation.
{% endhint %}

## Defining replacement rules for a cache

{% stepper %}
{% step %}
**Select the required ORIGINAL cache**

Use the `original` dropdown list to select the first company cache in a 'master' process flow that must be replaced when this process flow runs in the given virtual environment - for example:

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FAAxMANVA3PDQjpbW9qBn%2Freplace%20instance%202a.png?alt=media&#x26;token=f1b3a6fb-6c99-4b2c-b744-32c712c12519" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
As you work through your replacement configuration, it's helpful to view related ['master' process flows](https://doc.wearepatchworks.com/product-documentation/virtual-environments/implementing-virtual-environments/creating-a-master-process-flow) in another browser tab. You can duplicate a Patchworks browser tab and navigate to a different page whenever needed.
{% endhint %}
{% endstep %}

{% step %}
**Select the required REPLACEMENT cache**

Use the `replacement` dropdown list to select the replacement cache that must be used when this process flow runs in the given virtual environment - for example:

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FzDEmA2wf0B0c5k4Y5G7v%2Freplace%20cache%203.png?alt=media&#x26;token=c4a3a7d0-e46a-4667-9eaa-14429bff1760" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**Confirm your selections**

Click the `create` button (associated with this replacement rule) to confirm your selections:

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FIftlGxmzNHuCSWvq8W9F%2Freplace%20cache%204.png?alt=media&#x26;token=e34c6303-0456-487d-8d4e-586352c08295" alt=""><figcaption></figcaption></figure>

The rule is added:

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F92Xvq6DuZssAsAonCjeV%2Freplace%20cache%205.png?alt=media&#x26;token=b9f8206f-3cf7-4738-8de0-009d7a0e30bd" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Once a rule is confirmed, it can't be edited. If you need to change a replacement rule, use the 'trash' icon to remove it and add a new one.
{% endhint %}
{% endstep %}

{% step %}
**Add more replacement rules for caches**

To add another replacement rule for a cache, click the top-right `create` button:

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2Flvv380FuD8neSZlFSEEb%2Freplace%20cache%206.png?alt=media&#x26;token=56752b09-e7ba-479c-95ab-3b9a2bbc154d" alt=""><figcaption></figcaption></figure>

Update the new rule as required, then click the associated `create` button to confirm your selections:

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FvVsy9mTibWExywIUmQ22%2Freplace%20cache%207.png?alt=media&#x26;token=6b2737ab-caa4-46d3-8440-060d4c44c1c3" alt=""><figcaption></figcaption></figure>

Repeat as many times as needed to define all required cache replacements for all process flows that will be running in this virtual environment.

{% hint style="info" %}
If you don't want to define ALL cache replacements now, that's OK. For example, you might choose to add the minimum number of replacements needed for [testing](https://doc.wearepatchworks.com/product-documentation/virtual-environments/implementing-virtual-environments/testing-a-process-flow-against-a-virtual-environment), then define remaining rules (in a [new draft](https://doc.wearepatchworks.com/product-documentation/virtual-environments/implementing-virtual-environments/configuring-a-virtual-environment-version/understanding-virtual-environment-versions)) later, after initial testing is complete.
{% endhint %}
{% endstep %}
{% endstepper %}
