Sajal Halder

Inventory Service

A hexagonal-architecture microservice for inventory with concurrency-safe stock adjustments.

By Sajal Halder

  • Java 17
  • Spring Data JPA
  • Gradle
  • JUnit 5

Inventory Service is a Spring Boot microservice by Sajal Halder for managing inventory items. It supports create, read, update and delete, quantity increases and decreases, pagination and sorting, and it uses optimistic locking so concurrent edits cannot silently overwrite each other.

It is a compact example of hexagonal architecture: the domain has no dependency on Spring or the database, and everything technical lives in adapters around it.

Structure

  • domain/modelInventory entities and business rules.
  • domain/portThe interfaces the domain uses and exposes.
  • domain/serviceDomain logic that implements those ports.
  • infrastructure/adapterInbound adapters (the REST API) and outbound adapters (persistence).
  • infrastructure/config, metricsSpring configuration and metrics collection.

Handling concurrent updates

Two people editing the same item at once is the classic lost-update problem. Updates carry a version through an If-Match header, and if the stored version has changed the service rejects the request with a concurrent-modification error instead of overwriting the newer data.

Distributed tracing

Every response includes an X-Trace-ID header. If the caller supplies one it is reused, and otherwise a UUID is generated, then the ID is propagated through the request. That lets a single request be followed through logs across services.

API

  • POST /api/inventoryCreates an item with product name, SKU, quantity and price.
  • GET /api/inventoryLists items with page, size and sort parameters, for example sort=createdDate,desc.
  • PUT /api/inventory/{id}Updates an item, guarded by the If-Match version header.
  • Error responsesEach error carries an HTTP status, an error code, a descriptive message and extra details where relevant. Cases covered are invalid input, missing resources, concurrent-modification conflicts, business-rule violations and system errors.

Testing and monitoring

Tests are written with JUnit 5 and mirror the main source structure, and JaCoCo produces an HTML coverage report. Spring Boot Actuator exposes health checks and metrics, and separate staging and production profiles keep configuration apart.

More projects by Sajal Halder

  • Spring RAG AI

    A retrieval-augmented generation system where agents route each question, retrieve context and check the answer before it is returned.

  • Expense Tracker AI

    A full-stack expense tracker that reads receipt photos with AI, answers questions about spending in chat, and charts where the money goes.

  • Hybrid Fanout Feed System

    A Twitter-style feed that switches strategy by audience size: fan-out on write below 10K followers, fan-out on read above.

  • Banking Microservices

    A banking platform designed around six services (users, accounts, transactions, cards, payments and notifications) behind an API gateway.