This app accepts a large JSON array, queues it in Redis, and processes it in a background worker. PostgreSQL stores the batch status, saved records, and invalid-record errors.
flowchart LR
Client --> API
API --> PostgreSQL
API --> RedisQueue[Redis / BullMQ]
RedisQueue --> Worker
Worker --> PostgreSQL
- Send a JSON array to the API.
- The API creates a batch with status
PENDINGand queues it. - The worker validates and saves each record.
- The batch becomes
COMPLETEDorFAILED.
PENDING- waiting in the queue or being processed.COMPLETED- all rows were handled.FAILED- the batch could not be queued or processed.
- Node.js and TypeScript
- Express
- PostgreSQL and Prisma
- Redis and BullMQ
- Docker
Install dependencies and generate the Prisma client:
npm install
npx prisma generate
npm run prisma:deployStart the API and worker in separate terminals:
npm run dev:apinpm run dev:workerdocker compose up --buildPORT=3001
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/distributed_loader
REDIS_URL=redis://127.0.0.1:6379POST /api/batch
Content-Type: application/jsonSend a raw JSON array as the request body:
[{"name":"A"},{"name":"B"}]The API responds immediately:
{
"success": true,
"message": "Batch accepted and queued",
"jobId": "uuid"
}GET /api/batch/:jobIdThe response contains the batch status, row counts, and counts of valid records and errors.