Last-mile Delivery Platform — technical appendix
For engineers: the nine services of a delivery platform, hands-off deploys, a hot standby, and the decisions I made, with the cost of each.
TL;DR
Last-mile Delivery Platform is a TypeScript and Python monorepo: a NestJS API on PostgreSQL and Redis, a Next.js website, an admin panel and three Telegram mini apps built with React and Vite, and three aiogram bots. Nine services run in Docker in production. Every push to the main branch runs the tests (4,037 for the API), rebuilds only the affected services and deploys them; there have been 513 such deploys since February 2026. Over the same period the database schema went through 110 migrations. The main server has a hot standby fed by streaming replication. On 12 October the standby was needed: the switch took 52 minutes and no data was lost.
Platform diagram
Every channel goes through one API; behind it sit the database and Redis, with a continuous copy on a standby server. The table below breaks the same picture down by service.
| Service | Used by | Technology |
|---|---|---|
| API | every channel | NestJS 10, TypeORM, PostgreSQL 16, Redis 7, Socket.IO; 65 modules, 79 entities |
| Website | customers | Next.js 14, React 18, three languages |
| Admin panel | owner and managers | React 18, Vite 6, 81 routes |
| Staff mini app | dispatchers and drivers | React 19, Vite 7, Telegram WebApp |
| Previous driver app | drivers, while they move to the shared app | React 19, Vite 7 |
| Shop inside Telegram | customers | React 19, Vite 7 |
| Three bots: dispatchers, drivers, shop | staff and customers | Python, aiogram 3 |
The bots do not poll the database for events. The API publishes events to Redis and each bot subscribes to its own channels. The dispatcher map and customer tracking receive the driver's position over Socket.IO.
How code reaches the server
- Before a push, a local hook type-checks the affected apps and every app that consumes a changed shared package.
- In GitHub Actions, a path filter works out which services are affected. Tests run in parallel: API, bots, database.
- Each affected service is built into a Docker image tagged with the commit.
- A promote step moves the
latesttag for all built images at once. If any move fails, the ones already moved go back to their previous images and the run fails. Success is checked by reading the registry, not by the command's exit code, because the command can return zero without moving the tag. - A deploy step connects to the server and updates only the services from its own run.
Runs are queued strictly one after another and are never cancelled. Re-running an old run cannot move latest back to an old commit; a separate freshness check prevents it. If old code is needed in production, the answer is a revert and a new push.
The pipeline does not touch the database schema. I apply migrations by hand on the primary database before the push, and write them so the old code keeps working with the new schema: expand first, deploy the code, then remove what is no longer needed. The standby receives schema changes through replication.
Hot standby
The standby server receives the database through PostgreSQL streaming replication, and its Redis runs as a replica. Settings, images and environment files are synced every hour, uploads every night. No containers run on the standby until it becomes the primary. Separately, the databases are backed up every day to object storage and to one more server.
Failover is a set of scripts: fence off the old primary, start the services on the standby, switch DNS, reissue certificates, point deploys at the new address. When the old server boots, it asks which server is the primary now and does not start Docker if the answer is not itself.
On 12 October the main server disappeared because the hosting provider lost the physical machine. 52 minutes later the store was running on the standby, and the last transaction had reached the replica. Two days later the old server booted, saw that another server was the primary, and stayed down. A new standby for the new primary was ready the same day.
Decisions and why
One API for every channel
The website, the admin panel, the mini apps, the bots, the other storefronts and the support chatbot all talk to one API. The alternative was a separate backend per channel. I chose one because price, stock, delivery zone and order status have to match everywhere, and duplicated logic drifts apart. The cost: the API is large, 65 modules, and any change to it is checked against the whole test suite.
Telegram instead of mobile apps
In January the repository had two Expo apps started for dispatchers and drivers. I stopped them and moved staff to Telegram mini apps: no app stores or store reviews, sign-in with the Telegram account, notifications as ordinary messages, and updates arrive with each deploy. The cost is depending on Telegram's WebView and its limits; in particular, a background tab goes to sleep.
Location from Telegram live sharing
Because of that sleeping tab, I take the driver's position from Telegram's own live location. The bot receives a point every few seconds and stores it in Redis. While the live location is fresh (no older than 180 seconds), it outranks points from other devices. A point that lies farther from it than anyone could drive in the time between them (at 40 m/s, but never less than 1 km) is dropped entirely: it reaches neither the cache, nor the history, nor the map. If Redis is unavailable, the rule lets the point through, as before the rule existed.
One shared rule picks the warehouse
Which warehouse serves an address is decided by a separate package in the monorepo with no framework dependencies. Both the API and the website import it. The alternative was a copy of the rule in each app. The cost: the package cannot lean on NestJS or Next.js, and any change to it is tested by both consumers.
Products belong to the main warehouse
The main warehouse owns the product card, the price and any sale; the other warehouses inherit them and keep only their own stock. Before this, each warehouse had its own copy of prices, and by the time of the switch one of them had drifted on 8 prices and 35 sales. The cost: a warehouse cannot set its own price, and that limit is deliberate.
Migrations by hand, before the push
There is no migration step in the pipeline, on purpose. Changing the schema on a live database is the moment a person should be present: to check the plan, stop at the first error, and verify the result on both the primary and the standby. The cost is a manual step and the discipline it needs. A migration forgotten before a push would break the deploy, so code that reads new columns ships only after the schema has been checked.
latest moves for every service or for none
Each build used to move latest on its own. If one build failed, its neighbours already pointed at the new commit, and the next green deploy of an unrelated service brought them into production as a side effect. The trap was set by one push and went off on another. Now a dedicated step moves the tag in one operation with rollback, and each deploy updates only its own services. The cost: a service whose promote succeeded but whose deploy failed does not catch up in passing and waits for its own next build.
A hot standby, not just backups
A daily database copy is enough to avoid losing data for good, but restoring from it takes hours and loses the day's orders. A replica takes minutes and loses nothing. The cost is a second server, config syncing, and fencing of the old primary, without which two servers could take orders at the same time.
SMS: own phones first, the cloud as backup
SMS notifications go through a gateway that runs on Android phones. Messages go through our own phones on our own gateway server first; the cloud gateway is used only when our phones fail to send. The cloud gateway used to hang, and the driver waits for the send result on screen. The cost: the gateway's API cannot tell whether a phone is alive, and its statuses are wrong in both directions, so a new phone joins the rotation only after a test message that someone confirms has arrived.
The chatbot integration is a contract with a test
The support chatbot uses the API under a contract: a separate document describes every request, response and rule, with file:line references into the code, 90 of them. In August a check showed these references go stale in about three weeks: 16 out of 44 already pointed to the wrong place. Now a test verifies that every reference points at live code, and that the references an argument relies on point at a line with the expected content. The cost: references have to be recalculated when the code changes, but the document can no longer fall behind the code unnoticed.
How it is checked
- API: 4,037 tests in 235 suites (Jest). Driver bot: 97 tests (pytest). Staff mini app: 32 tests (Vitest). Separate database tests in CI.
- A type check of the affected apps before every push.
- A larger change starts with a plan that a second AI agent reviews independently. Then come the code, the tests and another review.
- A new rule is first run on real data. The location filter was checked on three days of one driver's points before it shipped: 1,480 false points dropped, 39 questionable ones let through.
What the platform does not do
- No microservices: the API is a single NestJS process, split into modules inside it.
- No migrations in the pipeline, by design.
- No native mobile apps.
- No route optimisation across several addresses.