If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Queue on shared webhosting

Started by KiranaTama, Dec 15, 2022, 01:18 AM

Previous topic - Next topic

KiranaTamaTopic starter

Hello everyone! There are many inquiries regarding queues on a toaster, but what about shared hosting?

The issue lies in having to process up to 100 requests in just 10 seconds and not being able to send more than 1 request per second via the API, which is a major problem.

To potentially resolve this issue, I have an idea, but I'm uncertain if it'll be effective. The plan is as follows:
1) When a request comes in, I will check a database table to see if a row (0/1) is currently being worked on. If so, I will serialize/json_encode the request and add it to another table as a request in the queue.
2) Once the initial request is processed, I will implement a method (such as through a destructor or do-while loop) to continuously check if there are any queued requests in the database and immediately process them. It's essential to avoid using cron jobs.

Do you think this solution is flawed? While it may be considered a "crutch," is it an acceptable approach for implementation? Are there any other solutions you would suggest given the limitations of shared hosting?

I understand that there are alternatives like gearman, rabbitmq, etc., but unfortunately they aren't viable options for shared hosting.
  •  


techtrendsit

Have you considered why cron didn't handle it that way? Instead of using cron, we can log all of our tasks in a table and have a script run on Kron that checks for any outstanding tasks and executes them sequentially.

To speed up the task execution process, an infinite loop can be run in the handler script to constantly check for new tasks. While doing so, it's important to block access to avoid any potential issues. To prevent the system from crashing, periodic checks can also be performed using cron.

Regarding blocking access:

if (!flock($fh = fopen(__FILE__, 'r'), LOCK_EX | LOCK_NB)) die('Script is already running!');
  •  

Nealowero

Your proposed solution of using a database table to manage the queue of requests is creative, but it does come with its own set of potential issues.
Firstly, relying on database operations for managing a high-traffic queue can be resource-intensive and may not scale well as the number of requests increases. Additionally, continuously polling the database to check for queued requests could create unnecessary load on the shared hosting environment, potentially leading to performance issues or even service interruptions.

Given these limitations, a more efficient approach could involve leveraging client-side queuing mechanisms to control the rate at which requests are sent to the server. For instance, you could implement a client-side queue that adheres to the rate limit of 1 request per second and then sends the requests to the server at a controlled pace.

Another consideration is to explore the use of caching mechanisms to temporarily store and process incoming requests. By utilizing in-memory caching solutions or lightweight file-based caching, you may be able to offload some of the processing overhead from the database and reduce the impact on the shared hosting environment.

Furthermore, if the nature of the requests allows for it, you might want to look into optimizing the API endpoints to handle batch requests, thereby reducing the overall volume of individual requests that need to be processed within the given time constraints.

While your proposed solution demonstrates ingenuity in working around the limitations of shared hosting, it's important to carefully assess its potential impact on performance and resource utilization. Exploring alternative strategies such as client-side queuing, caching, and API endpoint optimization could offer more sustainable and efficient approaches to handling the influx of requests within the specified constraints.
While your proposal presents a pragmatic workaround, there are alternative approaches worth considering to mitigate the challenges posed by the limitations of shared hosting. As a hosting specialist, it's essential to balance creativity with practicality in order to deliver robust and scalable solutions within the given constraints.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...