Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Running aimicromind using Queue

By default, aimicromind runs in a NodeJS main thread. However, with large number of predictions, this does not scale well. Therefore there are 2 modes you can configure: main (default) and queue.

Queue Mode

With the following environment variables, you can run aimicromind in queue mode.

VariableDescriptionTypeDefault
MODEMode to run AiMicromindEnum String: main, queuemain
WORKER_CONCURRENCYHow many jobs are allowed to be processed in parallel for a worker. If you have 1 worker, that means how many concurrent prediction tasks it can handle. More infoNumber10000
QUEUE_NAMEThe name of the message queueStringaimicromind-queue
QUEUE_REDIS_EVENT_STREAM_MAX_LENEvent stream is auto-trimmed so that its size does not grow too much. More infoNumber10000
REDIS_HOSTRedis hostStringlocalhost
REDIS_PORTRedis portNumber6379
REDIS_USERNAMERedis username (optional)String
REDIS_PASSWORDRedis password (optional)String
REDIS_TLSRedis TLS connection (optional) More infoBooleanfalse
REDIS_CERTRedis self-signed certificateString
REDIS_KEYRedis self-signed certificate key fileString
REDIS_CARedis self-signed certificate CA fileString

In queue mode, the main server will be responsible for processing requests, sending jobs to message queue. Main server will not execute the job. One or multiple workers receive jobs from the queue, execute them and send the results back.

This allows for dynamic scaling: you can add workers to handle increased workloads or remove them during lighter periods.

Here's how it works:

  1. The main server receive prediction or other requests from the web, adding them as jobs to the queue.
  2. These job queues are essential lists of tasks waiting to be processed. Workers, which are essentially separate processes or threads, pick up these jobs and execute them.
  3. Once the job is completed, the worker:
    • Write the results in the database.
    • Send an event to indicate the completion of the job.
  4. Main server receive the event, and send the result back to UI.
  5. Redis pub/sub is also used for streaming data back to UI.

Start Redis

Before starting main server and workers, Redis need to be running first. You can run Redis on a separate machine, but make sure that it's accessible by the server and worker instances.

For example, you can get Redis running on your Docker following this guide.

Configure Main Server

This is the same as you were to run aimicromind by default, with the exceptions of configuring the environment variables mentioned above.

Configure Worker

Same as main server, environment variables above must be configured. We recommend just using the same .env file for both main and worker instances. The only difference is how to run the workers.

{% hint style="warning" %} Main server and worker need to share the same secret key. Refer to #for-credentials. For production, we recommend using Postgres as database for perfomance. {% endhint %}

Running aimicromind locally using NPM

npx aimicromind worker # remember to pass in the env vars!

Docker Compose

You can either use the docker-compose.yml provided here or reuse the same docker-compose.yml you were using for main server, but change the entrypoint from aimicromindstartto aimicromindworker:

version: '3.1'

services:
    aimicromind:
        image: aimicromind/aimicromind
        restart: always
        environment:
            - PORT=${PORT}
            ....
            - MODE=${MODE}
            - WORKER_CONCURRENCY=${WORKER_CONCURRENCY}
            ....
        ports:
            - '${PORT}:${PORT}'
        volumes:
            - ~/.aimicromind:/root/.aimicromind
        entrypoint: /bin/sh -c "sleep 3; aimicromind worker"

Git Clone

Open 1st terminal to run main server

pnpm start

Other terminals to run worker

pnpm start-worker

AWS Terraform

Coming soon

Queue Dashboard

You can view all the jobs, their status, result, data by navigating to <your-aimicromind-url.com>/admin/queues