Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform queuing and caching using redis for a multi-tenant multi-database architecture application in Laravel 5?

I am working on a multi-tenant multi-database architecture application using Laravel which basically means that every tenant in the application has their own database, own sets of users, resources and so on.

Now I am trying to implement queues and caching in the application, then I am trying to use redis for that. A sample code looks like this:

$mailer->send('emails.welcome', ['user' => $user], function ($message) use ($user) {
    $message->from("[email protected]", "Admin");
    $message->to($user->email, $user->first_name)->subject("Welcome!");
});

This is to send a welcome email to user sign-up. But the queuing is storing all the queues in the same database in the same Redis instance, different tenant emails will get mixed up as far as I can tell.

How do I hook into Laravel 5 and change the queue behavior to either store the jobs for each tenant in a separate database or store extra meta-information about the tenant a particular job belongs to? And then also how would I tell Laravel to parse that extra meta-information and connect to the right tenant database before proceeding with the job?

like image 917
Rohan Avatar asked Dec 16 '15 11:12

Rohan


People also ask

How does Redis work with Laravel?

Laravel supports the use of Redis, which uses caches for temporary data storage to speed up the process of performing database queries and getting feedback, which will, in turn, reduce the amount of time spent pulling up data.

Is Redis a cache or queue?

From there, he developed Redis, which is now used as a database, cache, message broker, and queue. Redis delivers sub-millisecond response times, enabling millions of requests per second for real-time applications in industries like gaming, ad-tech, financial services, healthcare, and IoT.

Is Redis multi tenant?

Redis Enterprise offers software multi-tenancy in which a single deployment of Redis Enterprise Software (often deployed as a cluster of nodes) serves hundreds of tenants. Each tenant has its own Redis database endpoint which is completely isolated from the other Redis databases.

Does Laravel need Redis?

So, Laravel has a built-in caching feature that is supported by Redis. Redis keeps its database completely in memory, using disk only for persistence. So that the response will still be slower when the data is not in Redis yet.


1 Answers

For appropriate work of Queue system, you need to use your own implementation of \Illuminate\Queue\SerializesModels trait inside your Jobs. Wheech will save and init correct DB connection on Job::__sleep() and Job::__wakeup(). Take a look at TenantAwareJob trait of hyn/multi-tenant package.

For appropriate work of Cache system, you need to use a prefix which depends on the current host. Take a look how hyn/multi-tenant developers recommend to implement this.

like image 144
Vasyl Sovyak Avatar answered Sep 19 '22 09:09

Vasyl Sovyak