All projects

BABIS

Builds a risk report on a company from its financial statements, company records and what the news says about it.

Built at MDN Labs, 2026

What it is

  • BABIS, the Bond Assessment and Business Intelligence System, is a platform that assesses a company’s credit, liquidity and insolvency risk, and reads news coverage for bad press.
  • It pulls balance sheets, directors, shareholders and corporate events from CreditSafe or RIBES, picked by the company’s country, scores them, and lets underwriters review and override the figures.
  • Odyssey, an insurance underwriting platform, registers companies with it and reads back their reports, articles and sentiment.
  • The backend is shared work, and I did much of it. Its infrastructure and releases are mine.

The problem

One analysis draws on very different sources: a REST API, a SOAP service, and a headless browser searching news sites in several languages. They run at very different speeds, and any of them can fail.

Run naively, a slow browser crawl holds up financial ingestion, a repeated request starts the same analysis twice, and a retry can pay twice for a report that is billed per request.

How I built it

  • I refactored the analysis into a state machine stored in the database. Each stage is a row with declared dependencies: ingestion, then indexing, then credit and liquidity scoring side by side, with news scraping running alongside from the start. Asking for one stage pulls in everything it depends on.
  • Starting an analysis while one is queued or running returns 409 Conflict. The check runs again under a row lock, so two requests at the same moment cannot both get through. Registering a company Odyssey has already sent returns the existing record.
  • Work is split across four Celery workers on isolated queues, for the pipeline, crawl discovery, crawl fetching and news analysis, so slow browser crawls never hold up the financial stages.
  • I built the balance sheet ingestion for CreditSafe’s REST API and the RIBES SOAP service, with extractors for directors, shareholders, corporate events and industry codes aligned across both providers’ schemas.
  • I wired credit and liquidity scoring into the pipeline, built the insolvency study, and built manual financial statements: XLSX import, multi-year saves, and an override layer where changing provider data requires a reason.
  • A headless Chromium browser driven by Playwright clears Google’s consent page, searches Google News and Google web search with search terms in several languages, and widens the date window step by step when a company’s first crawl finds nothing.
  • Extracted articles go to the inference gateway as async jobs, and only articles judged to be about the company, by its name or a known alias, count toward its sentiment score.
  • For production I added structured exception handling, JSON logs with secrets redacted that carry the request ID from an HTTP call into every Celery task it starts, and Docker images that run as a non-root user.

The interesting parts

A pipeline that survives duplicate and lost tasks

Finishing a stage marks the next one QUEUED in the same transaction, and the Celery message goes out through transaction.on_commit, so no worker can pick up a stage before its row says it is ready. Tasks are acknowledged late, so one can arrive twice: a worker only runs a stage that is still QUEUED and ignores an outcome for a stage that already finished. Locks are always taken in the same order, the analysis first and then its stages. A scheduled task re-queues stages stuck past their time limit, using SKIP LOCKED so it never touches a row a live worker holds. When a stage fails, everything that depends on it is marked skipped and the analysis closes as failed.

Paying for a report once

RIBES bills for every ticket, the handle used to download a report. The moment a ticket is bought, it is saved to the database in its own committed write, before the download starts. If the download fails, the next attempt finds the ticket on the latest retrieval run and reuses it. The raw provider response is stored as well, so an analysis can be re-run from it without calling the provider again. The zeep client is built only on first real use, so parsing a saved payload never even fetches the WSDL, and each SOAP call is capped at 120 seconds.

One rate limit shared by every browser

All crawl workers draw on one Google request budget kept in Redis. Two small Lua scripts run atomically: one counts requests in the current minute, the other hands out evenly spaced time slots, with jitter added on top. When Google pushes back, every worker cools down together, for at least as long as its Retry-After asks. If Redis cannot be reached, the limiter fails closed and stops crawling instead of sending unthrottled traffic.

A release has to prove itself before it counts

The deploy runner I wrote is a standard-library Python script that runs on the host in explicit stages. It checks every manifest field against an allowlist, refuses a latest tag or images from two different builds, and stops if the log directory is missing or owned by the wrong user. Migrations run in a one-shot container before any live service is touched. It then waits for the API to report healthy and pings each Celery worker by name, because a broadcast ping passes as soon as any one worker answers. The scheduler is judged by its restarts during this deployment, not its lifetime count. Only then does .env.versions.next atomically replace .env.versions, with the old file kept as .env.versions.previous; a failure after the new containers start brings the previous images back automatically.

How it runs

  • Production and development run on a dedicated subnet of the private cloud, and the API binds only to that subnet’s address.
  • Images are built with uv in multiple stages on a base image tagged by a hash of its own inputs, with the API and Celery images built in parallel. Chromium and its libraries go only into the worker image.
  • Azure DevOps CI fails any image that would run as root, writes a release.env manifest, and ships the release over SSH with pinned host keys. Production deploys wait for an approval.
  • Workers run with an init process that cleans up browser child processes, extra shared memory for Chromium, and a grace period on shutdown so in-flight tasks can finish.
  • Developer scripts generate secrets into files readable only by their owner without printing them, start the stack by profile (full, minimal, infra, app, db, cache or broker), and ask for a typed confirmation before a clean.
  • Logs are written as JSON files that Grafana Alloy ships to the central log stack, and BABIS has its own provisioned Grafana dashboard.