Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create another Redis server instance on same server (managed by Laravel Forge)

Laravel Horizon is coming, so I'm looking to switch my queues from beanstalkd to Redis to take advantage of better queue monitoring.

However, occasionally I have to perform php artisan cache:clear. But if I do so, since my queue is now in Redis, it will wipe out all my jobs, as discussed in this GitHub issue.

The suggestion was to make a separate Redis instance. Here are my questions:

  1. How do I create multiple Redis server instances in my Laravel Forge managed server?
  2. What should I change in my config files so that session and cache point to one Redis server instance, and my queue points to a different Redis server instance? (now know the answer to this, see link in edit)
  3. Is it correct that php artisan cache:clear will now only wipe out data in the Redis instance that my cache is using, and my queue worker Redis server will not be affected?

Edit

Upon searching the internet further, I've come across this discussion.

  1. So I think I do not really need to create a separate Redis server instance, just use a different Redis DB number. Is this correct?
  2. Does php artisan cache:clear wipe out only that DB number? Other DB numbers on the same instance are not affected?
like image 395
jcsoriano Avatar asked Aug 10 '17 18:08

jcsoriano


People also ask

How do I set up a Redis instance?

On the Create a Redis instance page, select your desired configurations for your new instance. Give your instance an Instance ID and, if desired, a Display name. The Instance ID must use only lowercase letters, numbers and hyphens. It must also start with a letter and be unique in its region. For example: my-instance-1.

How do I Choose A Redis version for memorystore for Redis?

To choose one of the available OSS Redis versions for Memorystore for Redis, use the --redis-version flag with one of the following values: You can create Redis instances using one of two connection modes : --connect-mode=PRIVATE_SERVICE_ACCESS or --connect-mode=DIRECT_PEERING .

How to create a Redis instance in gibibytes?

Create a Redis instance using the network on which you established a private services access connection: INSTANCE_ID is the ID assigned to the instance. SIZE is the size of the instance, in gibibytes (GiB). REGION_ID is the region ID. PROJECT_ID is the project ID. NETWORK_NAME is the name of the network with which to create the Redis instance.

How do I configure Redis to work with VPCs?

Select Redis Version 6.x (recommended). Under Capacity, enter a value between 1 and 300 GBs. If you want to enable Redis AUTH select Enable AUTH. If you want to enable in-transit encryption select Enable in-transit encryption. Under Authorized network, select the default network, or one of your available VPC networks. Optional.


1 Answers

Does php artisan cache:clear wipe out only that DB number?

This is accurate. The cache:clear command sends a FLUSHDB command to Redis under the hood which only erases the data in the Redis database selected for storing cache data.

The standard way to isolate data in a single Redis instance for each of Laravel's services is to assign a different database for each service in config/database.php:

'redis' => [ 
    'default' => [
        ... 
        'database' => 0, 
    ], 
    'cache' => [ 
        ...
        'database' => 1, 
    ], 
], 

The configuration above instructs Laravel to use database 1 for storing cache data, and database 0 for all other Redis operations. Then, when we want to clear the cache—using php artisan cache:clear or app('cache')->flush(), etc.—Laravel won't erase the other data stored in Redis.

We can extend this configuration pattern by creating separate connections that specify different Redis databases for each of the session, queue, and broadcasting APIs. This provides more flexibility to manage the data as our application grows.

Of course, for large applications, we might even use different Redis servers for each of these services to improve scalability as our application data exceeds the limitations of a single Redis server.

like image 172
Cy Rossignol Avatar answered Sep 28 '22 07:09

Cy Rossignol