Delphi Backend Architecture

This is the applied case of native systems work: a backend compiled to native code, serving JSON over HTTP, authenticating with ECC-signed JWTs and persisting to PostgreSQL, running on ordinary Linux hosting. It is a perfectly ordinary modern backend that happens to have no runtime underneath it.

When this is the right choice. There is one condition that does most of the work: you already have a substantial Object Pascal codebase and a team that maintains it. In that situation the domain logic can move behind a service boundary instead of being rewritten, and you do not add a second language and a second set of hiring requirements to a company that is already short-staffed on the first one. The native properties — small footprint, no runtime dependency, fast start — come along with it, and are a genuine advantage where deployment is constrained.

When it is not. If you have no existing Pascal codebase and no Pascal engineers, this is very unlikely to be the right architecture for a new backend, and I will say so. The ecosystem around .NET, Java, Go and Node is larger, the hiring market is incomparably better, and for most services those advantages outweigh everything on this page. This service exists for the specific situation described above — not as a general recommendation.

Interface-based SOA with mORMot2

mORMot2 services are defined by Pascal interfaces. You declare an IInvokable interface, implement it once, and the framework handles JSON marshalling, routing, authentication, logging and interface-level access rules. The consequences of that design are what make it worth using:

  • The interface is the contract. The same declaration serves the server implementation and a Delphi or FreePascal client, so a signature change is a compile error rather than a runtime surprise in production.
  • Service instance lifetime is an explicit decision — per-thread, per-session, per-client, or a single shared instance — which is exactly the choice that determines whether your service is safe under concurrency. I make that decision deliberately per service rather than accepting a default.
  • Business logic lives behind the interface boundary and can be exercised by a test client without an HTTP server in the loop.
  • Interfaces can be exposed over HTTP for external clients and consumed in-process by other parts of the same application, without two code paths.

Server hosting depends on the target: HTTP.sys on Windows, the asynchronous socket server on Linux, or behind nginx as a reverse proxy where TLS termination and rate limiting belong to the infrastructure rather than the application.

ORM and PostgreSQL schema design

The mORMot ORM is not an abstraction that hides SQL from you, and it should not be used as one. I design the schema first — as a relational schema, in PostgreSQL terms — and then map TOrm classes onto it.

  • External database mapping through TSQLDBPostgresConnectionProperties, including mapping ORM classes onto tables that already exist and whose column names you are not free to change.
  • Batch operations for bulk insert and update paths, which is the difference between an import that takes four seconds and one that takes four minutes, because it is the difference between one round trip and ten thousand.
  • Explicit transaction boundaries around multi-table writes, and an honest decision about isolation level rather than whatever the connection defaulted to.
  • Indexing and query plans reviewed with EXPLAIN ANALYZE against realistic data volumes — a device-event table grows faster than almost anything else in the system, and partitioning or retention has to be designed before it is 400 million rows, not after.
  • Dropping to hand-written SQL wherever the ORM is the wrong tool. Reporting queries and analytical aggregates should be SQL. Mixing the two is not a design failure; pretending one covers both is.

Authentication: JWT with ECC signatures

mORMot2 supports JWT with ES256 — ECDSA over NIST P-256 — which gives you asymmetric token signing: the authentication service holds the private key, every other service verifies with the public key and cannot mint tokens. For a system split across several processes, or where a device gateway must verify a token without being trusted to issue one, that separation is the whole point.

  • Key handling, rotation and the kid header, so that rotating a key does not invalidate every live session at once.
  • Claim design: what actually belongs in the token, what must be looked up server-side, and a token lifetime that reflects the fact that revocation of a stateless token is a system design problem, not a flag.
  • The framework session model as an alternative where a stateful session genuinely fits better than a bearer token — chosen on the merits, not by habit.

Delphi on Linux

Running Pascal server processes on Linux is normal now, and it changes deployment economics: containers, ordinary Linux hosting, no Windows Server licence in the per-instance cost. It also brings real differences that are better handled at design time than at deployment time — path and case sensitivity, character encoding at the OS boundary, service supervision under systemd, signal handling and clean shutdown, logging to journald or to files that something else rotates. Depending on the project this is Delphi Linux64 or FPC; mORMot2 builds under both, and which one you should target is an early decision that depends on your existing dependencies.

Putting a service layer in front of an existing system

Most of this work is not greenfield. The usual shape is a working Delphi desktop application talking directly to a database, and a requirement for a web or mobile client, or a partner integration, or a second site. The approach I take:

  1. Define the service interfaces around the operations the business actually performs, not around the tables.
  2. Implement them over the existing schema, so the new layer is live and testable while the old client keeps running untouched.
  3. Move the desktop client onto those interfaces one screen at a time. Both paths coexist during the transition; there is no cutover weekend.
  4. Once every writer goes through the service layer, the schema itself becomes free to change — this is the point where a PostgreSQL migration or a redesign becomes possible at all.

The compiler-level side of that work is covered on Legacy Delphi Maintenance & Modernization.

Reliability under real failure

A backend is judged on what it does when its dependencies misbehave, and databases misbehave in specific, recognisable ways. I build for the failures I have actually had to handle: a SQL Server database in recovery or in transition after a failover, connection pools full of stale handles pointing at a node that is gone, login timeouts and transient errors that should be retried with backoff rather than surfaced, and long-running client sessions that assumed a connection opened at 08:00 would still be valid at 17:00. Retry with bounded backoff, health checks that verify a pooled connection before use, and idempotent write paths so that a retry after an ambiguous failure does not duplicate the work.

Reference implementation

Vi, the project management platform listed in the portfolio section of this site, runs on a mORMot2 backend that I designed and wrote. It is the most direct answer to the question of what this architecture looks like when it is finished rather than described: the service decomposition, the ORM and PostgreSQL schema, and the authentication model are all visible there. It is shown as an engineering reference — it is not offered for sale on this site.

Typical starting points

  • We have a Delphi desktop application and now need a mobile client against the same data.
  • We want to move off SQL Server to PostgreSQL without rewriting the application.
  • Our backend is a set of stored procedures and we need a real API in front of it.
  • We want to deploy on Linux containers instead of Windows Server.
  • We chose mORMot2 and want a second opinion on the architecture before we build on it.

Book a free 30-minute technical call

Bring your current architecture, your database, and the constraint that is forcing the change — a new client platform, a hosting cost, a scaling limit, a partner integration. Thirty minutes is enough to sketch a service boundary and tell you where the difficult part will be.

Book a free 30-minute technical call

Tell me what is broken.

No sales call. You describe the problem, I tell you whether it is something I can solve and roughly what it takes. If it sits outside what I do, I will say so on that call.

Book a free 30-minute technical call