One of the nice things about using Redis via Elasticache is that it gives you a Primary endpoint and a Reader endpoint which means we can split our load more easily (in addition to having the redundancy that they provides.) Unfortunately, Laravel doesn’t have this out of the box which is forever a paper cut I experience. (My only guess is that it is because it really is an AWS thing and by-and-large things are pretty agnostic. Going all-in on AWS is a rant for another day.)

Anyhow, we’re re-examining our caching strategy and this finally bothered me enough to do something about it.

Our main way of interacting with the cache is currently through remember() and rememberForever() … which uses only the Primary endpoint. Now, these functions just wrap a get and if nothing is returned, runs the closure output as a put. So there is our roadmap.

First, we need to do some setup.

In config/database.php I copied the cache block and created a cache_read and cache_write one as well. I left the original one as well as a transition … though let’s be honest, I’m never going to delete it.

<pre class="wp-block-code">```
    'cache_read' => [
        'url' => env('REDIS_URL'),
        'host' => env('REDIS_HOST_READ', '127.0.0.1'),
        'username' => env('REDIS_USERNAME'),
        'password' => env('REDIS_PASSWORD'),
        'port' => env('REDIS_PORT', '6379'),
        'database' => env('REDIS_CACHE_DB', '1'),
    ],

    'cache_write' => [
        'url' => env('REDIS_URL'),
        'host' => env('REDIS_HOST_WRITE', '127.0.0.1'),
        'username' => env('REDIS_USERNAME'),
        'password' => env('REDIS_PASSWORD'),
        'port' => env('REDIS_PORT', '6379'),
        'database' => env('REDIS_CACHE_DB', '1'),
    ],

You’ll also notice that the only thing that changed in there is the host is being populated from a different environment variable, so you’ll need to update your various .env processes as well.

Now that the connections are configured, we need to teach the caching system how to access these new stores in *config/cache.php* by, again, copy and pasting the existing redis blocks and update the connection to use.

```
    'redis_read' => [
        'driver' => 'redis',
        'connection' => 'cache_read',
        'lock_connection' => 'default',
    ],

    'redis_write' => [
        'driver' => 'redis',
        'connection' => 'cache_write',
        'lock_connection' => 'default',
    ],
```
```

All that is left now is to start pulling our code towards acting-like-remember-but-not.

```
```
`$value = Cache::store('redis_read')->get('key');if (! $value) {    $value = 'foo'; // or whatever you would do in the remember closure    Cache::store('redis_write')->put('key');}`
```
```

And just like that, your hit percentage starts to go up on your Replica.

Though I really do think Laravel should be smart enough that if you give a Redis connection read and write options that things automatically route accordingly…