Stage 8. Server-side business logic: where a company's rules live

Development in PHP and Yii2, working with MySQL, query optimisation and caching with Memcached. Why business rules belong on the server, not in the interface.

Stage 8. Server-side business logic: where a company's rules live
INGELLO / BUSINESS3032

The server side is where a company's real rules are written down: who can do what, what is incompatible with what, how a price is calculated, how much a customer gets back after a cancellation. Everything else, from the website to the mobile app, is just a way of displaying those rules. If the rules live in the interface, in spreadsheets and in employees' heads, the business loses money on inconsistencies and cannot scale.

This is the eighth stage of our app development strategy. We look at what back-end development includes, what you get, what it costs and why a mistake in the data model is more expensive than any other.

What happens at this stage

  • Development in PHP (Yii2).
  • Working with MySQL, query optimisation, caching (Memcached).
  • Dependency management via Composer.

Behind these lines are concrete things: the database schema and migrations, APIs for interfaces and integrations, access rights, calculations, statuses and an event log. We speed up the standard part with our own Fractal generator: the data schema, a documented API and a basic admin panel are generated, while the code that sets your business apart from the next one is written by hand.

What you get at the end

  • A database whose schema follows the model from the technical specification, plus migrations for changing it.
  • A documented API used by the interfaces, the mobile app and external systems.
  • An admin panel for managing data.
  • Business rules, access rights and calculations checked on the server on every action.
  • An event log: who did what in the system, and when.

How much back-end development costs

In the Ingello price list the server-side business logic stage costs from $0 to $4,700, and the price is fixed before work starts. The lower bound means that not every project needs a separate budget for this stage. Within the range the price is determined by:

  • the number of entities and statuses and the links between them;
  • how many business rules and calculations there are: prices, discounts, refunds, accruals;
  • how many roles there are and how finely permissions are split;
  • whether data must be migrated from an old system;
  • what load is expected and where caching is needed.

Why a rule must live in one place

If the condition "discount no more than 30%" is written into the interface, the report and the import, sooner or later those three copies will drift apart. One rule, one place. The interface displays it, the server enforces it.

The same goes for access rights. Hiding a button is not enough: the action must be locked on the server, otherwise it can be called directly. More on this in our article on production security.

What this looks like on real projects

In the BusTicket admin system for a bus carrier, permissions are split by role down to individual actions: for each section separately "view", "edit" and "full access". A dispatcher cannot delete a route, a carrier cannot see other carriers' bookings, an agent cannot get into payroll. Everything that happens in the system is written to a timeline. When someone asks six months later who did something, the answer is a dated record, not a guess.

The same server calculates refunds. The refund percentage comes from a "Refund conditions" reference table and depends on how long before departure the trip was cancelled. A partner's route goes on sale only after an administrator has reviewed it.

The Lino floor coverings shop has different rules: six price types, including two floor prices below which nothing may be sold, and fractional quantities in the basket to two decimal places. Rules like these cannot be left to the interface: the manager edits prices for whole categories, and a buyer must never see a price below the floor price under any circumstances.

Settings without a programmer

Some rules change more often than new versions ship. The technical specification for the XenFit online training platform stated explicitly that rating coefficients and mailing frequency are changed from the admin panel without a programmer. It also set code requirements: names in the code match the language of the business domain, and the API is documented in Swagger. A new developer reads a server like that as a description of the business.

Data sets the ceiling for the product

The data schema laid down at the technical specification stage becomes code here. A mistake in the relationships is not fixed by an index or a cache. It is fixed by a migration, and a migration on live data always costs more than thinking it through at the start.

When Lino's data was moved from the old structure, every field was examined separately: the client kept manufacturer and brand in one table, and the purchase price had a different name from ours. Along the way we removed unnecessary NOT NULL constraints so the import would not fail on empty values.

When load grows, the techniques from our article on server optimisation will help. But optimisation cures a slow query, not a wrong model.

What breaks if you cut corners

  • Rules get copied into the interface, reports and imports, and drift apart.
  • Permissions are enforced only by hiding buttons, so any action can be called directly.
  • There is no event log, and disputes are settled from employees' memory.
  • Every change to prices or terms needs a programmer, even though it could have been a setting in the admin panel.

What we need from you

  • The rules your company works by today: price lists, discount and refund terms, approval procedures.
  • A list of roles and what each one is allowed to do.
  • An export of your current data, if it has to be moved from an old system or spreadsheets.

Where next

Next comes deployment and infrastructure. The previous step: integrations. The map of all stages is in the app development strategy.

Discuss the architecture. If your company's rules currently live in spreadsheets and people's heads, book a free consultation with an architect: we will go through the data model, and afterwards you will receive a price and timeline estimate broken down by stage.

Frequently asked questions

01How much does back-end development for an app cost?

In the Ingello price list the server-side business logic stage costs from $0 to $4,700. The price depends on the number of entities and statuses, how many business rules and calculations there are (prices, discounts, refunds), how finely roles and permissions are split, whether data must be migrated from an old system and the expected load. The stage price is fixed before work starts, with an exact estimate after a free consultation.

02Which technology stack does Ingello use for the back end?

Server-side development is done in PHP with the Yii2 framework, a MySQL database, Memcached for caching and Composer for dependency management, with Web-Sockets for real-time exchange. The standard part (the data schema, a documented API and a basic admin panel) is accelerated by Ingello's own Fractal generator, while logic unique to the client's business is written by hand.

03Why can't business rules be checked only in the interface?

Because any request can be sent around the interface, and a hidden button does not stop anyone calling the action directly. On top of that, a rule copied into the interface, a report and an import will drift apart over time. The right approach: the rule is written once on the server, the server checks it on every action, and the interface only displays the result and guides the user.

04What does the server-side business logic stage deliver?

The client gets a database whose schema follows the model from the technical specification, with migrations for changing it; a documented API for the interfaces, the mobile app and external systems; an admin panel for managing data; rules, permissions and calculations enforced on the server; and an event log showing who did what and when. This is the foundation every later stage builds on.

05Can data be migrated from an old system to the new one?

Yes. Data migration is planned as part of the server-side stage and affects its price. Every field of the old structure is matched to the new one: in the Lino shop case the manufacturer and brand sat in one table and the purchase price had a different name, so the mappings were worked out by hand and unnecessary constraints were removed so the import would not fail on empty values.