feat: Implement inventory service with stock reservation - #7
Merged
Conversation
…ation design Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…a event layers Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…docker-compose wiring Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
products_dbwith product-service — no separate database, no AutoMigrate (product-service owns theinventorytable)order.created→ reserves stock viaReservedQuantity += n(optimistic atomic UPDATE)order.cancelled→ releases reservation viaGREATEST(reserved_quantity - n, 0)inventory.updatedon every stock change; publishesinventory.low_stockwhen available stock ≤ 10requireRole("admin"))Changes
New service —
services/inventory-service/Inventoryentity (mirrors product-service table schema exactly),AvailableQuantity()helper,LowStockThreshold = 10,UpdateInventoryRequest,OrderCreatedEventwithjson:"id"(matches order-service'sOrder.IDtag)GetByProductID,SetTotalQuantity(PostgreSQL upsert viaINSERT ... ON CONFLICT DO UPDATE),ReserveStock(atomic WHERE clause prevents overbooking, distinguishesErrInventoryNotFoundvsErrInsufficientStock),ReleaseStock(GREATESTclamp prevents negative reservation)inventory:<product_id>key, 5 min TTL (shorter because stock changes on every order)inventory.updatedandinventory.low_stockkafka.Readerinstances — one per topic (order.created,order.cancelled);Start()launches one goroutine per reader;handleMessageswitches on topicHandleOrderCreatedloops items, reserves each, logs and continues on failure (partial reservation acceptable);HandleOrderCancelledreleases each item's reservationGET /inventory/:product_id,PUT /inventory/:product_id; no auth helper needed (gateway handles it)runMigrations— product-service owns the table; Kafka consumer uses groupinventory-service-ordersdocker-compose.ymlREDIS_URL,KAFKA_BROKERStoinventory-serviceenvironmentkafka: condition: service_healthytoinventory-service.depends_onTest plan
docker-compose up inventory-service products-db redis kafkastarts without errorsGET /healthreturns{"status":"healthy","service":"inventory-service"}PUT /api/inventory/:product_id(as admin) sets total stock and returns updated inventoryGET /api/inventory/:product_id(as admin) returns stock levels with computedavailable_quantityorder.created→reserved_quantityincrements for each itemorder.cancelled→reserved_quantitydecrements for each iteminventory.low_stockevent (visible in Kafka UI)🤖 Generated with Claude Code