An inventory and sales system built from independent gRPC microservices and Protocol Buffers, with a command-line interface on top.
Table of Contents
The system manages products and sales from an interactive terminal menu. The interesting part is how it is split: the logic that any business object needs, formatting a report and keeping an activity log, lives in two small microservices that talk to the rest of the system over gRPC instead of being copied into each object.
- report is stateless. It receives a generic table (a title, column names and rows) and returns it formatted as text or CSV. It knows nothing about products or sales, so any business object can use it.
- audit owns its own SQLite database and stores a log of every operation performed by the business objects.
- The products and sales themselves are stored in a third SQLite database that belongs to the business-objects library, inside the CLI process.
Terminal (keyboard)
|
backend (CLI) menu and error handling, no SQL and no gRPC
| function calls
business objects (lib) Product and Sale: validation, rules, inventory.db
| function calls
clients (lib) gRPC wrapper: deadlines, typed errors, health checks
| gRPC over HTTP/2 + Protocol Buffers <-- the only network boundary
+----+-----------------+
| |
report :50051 audit :50052
(stateless) (audit.db)
The network only appears between the clients wrapper and the two microservices. Everything above that line runs in a single process.
- Products and sales with validation: invalid input, duplicate names, unknown products and insufficient stock are rejected with a specific error code and leave the data untouched.
- Atomic sales: registering a sale and lowering the stock happen in one SQL transaction, with no network calls inside it.
- Reports in plain text or CSV for products or sales, generated by the report service, and CSV export to
backend/exports/. - Activity history per entity, read from the audit service.
- Service status check on startup and on demand.
- Resilient by design: deadlines, a quick reconnection policy and graceful shutdown (see Resilience).
- Two
.protocontracts (report.protoandaudit.proto) shared by client and server. - End-to-end tests that start the real services and make real gRPC calls.
- Node.js 24. The project uses the built-in
node:sqlitemodule, so no native database package has to be compiled. - npm (it ships with Node.js)
- Clone the repository
git clone https://github.com/jerichd4c/grpc-inventory.git
- Navigate to the project directory
cd grpc-inventory - Install the dependencies of every package at once (the project uses npm workspaces)
npm install
- Start both microservices in one terminal
npm run services
- In a second terminal, start the interactive CLI
npm run cli
The CLI prints the status of both services when it starts and then shows this menu:
| Option | Action |
|---|---|
1 |
List products |
2 |
Create product |
3 |
Restock product |
4 |
Register sale |
5 |
List sales |
6 |
Show report (asks for products or sales) |
7 |
Export report as CSV into backend/exports/ |
8 |
Activity history (last 10 events of products or sales) |
9 |
Service status |
0 |
Exit |
A business error, such as selling more units than the stock, shows a message and returns to the menu. A service that is down is reported the same way. Only an unexpected error stops the program, so that real bugs are not hidden.
| Command | Description |
|---|---|
npm run services |
Start the report and audit services together and supervise them |
npm run report |
Start only the report service |
npm run audit |
Start only the audit service |
npm run cli |
Start the interactive CLI |
npm run check:protos |
Load both .proto files and list their services, RPCs and messages |
npm test |
Run the end-to-end tests |
Everything has a default and is optional.
| Variable | Default | Purpose |
|---|---|---|
REPORT_PORT |
50051 |
Port the report service listens on |
AUDIT_PORT |
50052 |
Port the audit service listens on |
REPORT_ADDRESS |
localhost:50051 |
Where the clients look for the report service |
AUDIT_ADDRESS |
localhost:50052 |
Where the clients look for the audit service |
INVENTORY_DB_PATH |
packages/bo/inventory.db |
Products and sales database (:memory: keeps it in RAM) |
AUDIT_DB_PATH |
services/audit/audit.db |
Audit log database |
Because the clients take the address from the environment, a service can run on another machine without changing any code, for example REPORT_ADDRESS=192.168.1.20:50051.
The inventory never stops selling because of a microservice: the main data lives in inventory.db, inside the CLI process.
| Situation | Create, sell, restock | List (1, 5) | Report and export (6, 7) | History (8) |
|---|---|---|---|---|
| Everything up | Works and is audited | Works | Works | Works |
| audit down | Works, with a not audited warning |
Works | Works | UNAVAILABLE error |
| report down | Works and is audited | Works | UNAVAILABLE error |
Works |
| Both down | Works, with a warning | Works | Error | Error |
- audit is best effort. By the time it is called the sale is already confirmed, so failing the operation would be misleading. The system continues and warns that the event was not logged. report is not best effort: there is nothing to show without it, so the error is reported.
- Deadlines: every RPC has 3 seconds to answer, so a hung server cannot hang the CLI.
- Typed errors: the wrapper turns raw gRPC failures into a
ServiceErrorwith a status code, and the business rules into aBusinessErrorwith a code of its own. - Fast reconnection: the gRPC reconnect backoff is limited to between 0.5 and 2 seconds. With the default settings a client took 15 seconds to notice that a restarted service was back; with the limit it takes about 2.
- Health check: a connection check that sends no RPC, with a 1.5 second limit, used at startup and by option
9. - Graceful shutdown: on
Ctrl+Ceach service stops accepting new calls, lets the running ones finish and closes its database. - Supervision:
npm run servicesruns both services as child processes and stops the other one if either exits with an error, so the system is never left half running.
grpc-inventory/
├── protos/ report.proto and audit.proto: the contracts
├── services/
│ ├── report/ Generate: formats a table as text or CSV
│ └── audit/ Log and List: events stored in audit.db
├── packages/
│ ├── clients/ gRPC wrapper (ReportClient, AuditClient, ServiceError)
│ └── bo/ Business objects: Product, Sale, BusinessError, inventory.db
├── backend/ Interactive CLI: menu, actions and error printing
├── scripts/ start-services.js and check-protos.js
└── test/ End-to-end tests
Each folder under services/, packages/ and backend/ is an npm workspace, so the business objects import the wrapper by its package name, @grpc-inventory/clients.
npm test runs 8 end-to-end tests with the built-in node:test runner. They start the real microservices on ports 50061 and 50062 with temporary databases, so they never touch the data of a normal run, and they take a few seconds.
- Creating a product saves it and logs it in the audit service.
- A sale lowers the stock and stores the total.
- A sale larger than the stock fails and changes nothing.
- Invalid input is rejected with the right error code.
- Reports are built by the report service, in text and CSV.
- With audit down, operations still work and warn once.
- With report down, reports fail with
UNAVAILABLE. - A restarted service is used again within a few seconds.
The last three tests stop services on purpose, a small-scale version of chaos engineering.
- No authentication or encryption: the gRPC channels are created without TLS, so the services are meant for a local or trusted network.
- Audit gaps are not recovered: if audit is down during an operation, that event is lost from the log; there is no retry queue.
- Ordered, shared state in tests: the end-to-end tests run in order and share data, so one test depends on the previous one.
- Single machine by default: the services listen on
0.0.0.0, but the CLI looks for them onlocalhostunless the*_ADDRESSvariables are set.
- gRPC and Protocol Buffers
- Node.js and its built-in
node:sqliteandnode:testmodules