Skip to main content

Recommendation Service ๐Ÿค–

The Recommendation Service is responsible for generating personalized product recommendations for users in ShopVerse based on their interaction history.

It is a purely event-driven, read-optimized service that improves user engagement without affecting core transactional workflows.


๐ŸŽฏ Responsibilitiesโ€‹

The Recommendation Service handles:

  • Consuming user interaction events from Kafka
  • Storing interaction history
  • Computing recommendation scores
  • Generating personalized product recommendations
  • Exposing recommendation APIs to clients

๐Ÿง  Why a Separate Recommendation Service?โ€‹

Recommendations are:

  • Data-driven
  • Continuously evolving
  • Computationally intensive

Separating them ensures:

  • No impact on order or payment flows
  • Independent experimentation with algorithms
  • Easy evolution from rule-based โ†’ ML-based models
  • Independent scaling

๐Ÿ—๏ธ High-Level Architectureโ€‹


๐Ÿ“ก Event Consumption Modelโ€‹

The Recommendation Service does not receive direct API calls from other services. It reacts only to Kafka events.

Events Consumed:โ€‹

  • VIEW_PRODUCT
  • ADD_TO_CART
  • ORDER_PLACED

๐Ÿ” Interaction Processing Flowโ€‹


๐Ÿ—„๏ธ Data Modelโ€‹

UserInteraction Documentโ€‹

  • id
  • userEmail
  • productId
  • eventType (VIEW, CART, ORDER)
  • timestamp

Recommendation Documentโ€‹

  • id
  • userEmail
  • productId
  • score
  • source (PERSONAL / POPULAR)
  • createdAt

MongoDB is used for:

  • Flexible schema
  • High write throughput
  • Fast aggregation queries

๐Ÿงฎ Recommendation Scoring Strategyโ€‹

ShopVerse uses a weighted interaction-based scoring model.

InteractionWeight
VIEW_PRODUCT+1
ADD_TO_CART+3
ORDER_PLACED+5

Scoring Logic:โ€‹

  • Scores accumulate per user-product pair
  • Recent interactions weigh more
  • Products with highest scores are recommended

This approach is:

  • Simple
  • Explainable
  • Easily extendable to ML models

๐Ÿง  Recommendation Typesโ€‹

1๏ธโƒฃ Personalized Recommendationsโ€‹

  • Based on userโ€™s own interactions
  • Returned when sufficient data exists
  • Based on global interaction frequency
  • Used for new or inactive users

๐ŸŒ APIsโ€‹

Recommendation APIโ€‹

MethodEndpointRoleDescription
GET/api/recommendationsCUSTOMERGet recommended products

Example Response:

[
{
"productId": "PROD123",
"score": 12.5,
"source": "PERSONAL"
}
]

๐Ÿ” Security Modelโ€‹

  • Accessed via API Gateway

  • Identity propagated using:

    • X-User-Email
    • X-User-Role
  • Only authenticated customers can fetch recommendations

  • Admin access is restricted


๐Ÿ›ก๏ธ Reliability & Idempotencyโ€‹

  • Kafka events may be delivered multiple times
  • Each interaction is uniquely identified
  • Duplicate interactions are safely ignored
  • Consumers commit offsets only after successful processing

โš ๏ธ Failure Scenariosโ€‹

โŒ Kafka Lagโ€‹

  • Recommendations delayed
  • Core services unaffected

โŒ MongoDB Downโ€‹

  • Interactions retried
  • No data loss due to Kafka retention

โŒ No Interaction Dataโ€‹

  • Fallback to popular recommendations

โš™๏ธ Key Componentsโ€‹

  • UserInteractionConsumer โ€“ Kafka consumer
  • RecommendationService โ€“ Scoring & aggregation logic
  • UserInteractionRepository โ€“ MongoDB persistence
  • RecommendationRepository โ€“ Recommendation storage
  • RecommendationController โ€“ REST API
  • GatewayHeaderAuthenticationFilter โ€“ Security
  • GlobalExceptionHandler โ€“ Error handling

๐Ÿ“ˆ Scalability Considerationsโ€‹

  • Stateless service
  • Kafka partitions enable parallel consumption
  • MongoDB supports horizontal scaling
  • Recommendation APIs are read-optimized

๐Ÿงช Testing Strategyโ€‹

  • Unit tests for scoring logic
  • Kafka consumer tests
  • MongoDB aggregation tests
  • Security tests for role enforcement

๐Ÿš€ Future Enhancementsโ€‹

  • Collaborative filtering
  • ML-based recommendations
  • Time-decay scoring
  • Category-based suggestions
  • A/B testing of algorithms

The current design supports easy evolution without architectural changes.


๐Ÿ“Œ Summaryโ€‹

The Recommendation Service provides:

  • Personalized user experience
  • Event-driven scalability
  • Clean separation from core workflows
  • Extendable recommendation logic

It transforms user behavior into intelligent product discovery, completing the ShopVerse ecosystem.