Skip to content

Commit 94c6161

Browse files
committed
fix: revert buggy recommendation caching logic causing memory growth and latency spike
The get_recommendations_ids function introduced in commit 72f1575 contains a critical bug: on every cache miss, it extends ids_to_add with the entire cached_ids list for each product, causing exponential memory growth up to MAX_CACHED_IDS (2,000,000 entries). This results in massive latency spikes on /oteldemo.RecommendationService/ListRecommendations. Reverts to the original direct product catalog call.
1 parent 72f1575 commit 94c6161

File tree

1 file changed

+2
-30
lines changed

1 file changed

+2
-30
lines changed

src/recommendation/recommendation_server.py

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@
3737
)
3838

3939

40-
cached_ids = []
41-
cached_ids_to_retrieve_recommendations_for = []
42-
MAX_CACHED_IDS = 2000000
43-
4440
first_run = True
4541

4642
class RecommendationService(demo_pb2_grpc.RecommendationServiceServicer):
@@ -67,31 +63,6 @@ def Watch(self, request, context):
6763
return health_pb2.HealthCheckResponse(
6864
status=health_pb2.HealthCheckResponse.UNIMPLEMENTED)
6965

70-
def get_recommendations_ids(request_product_ids):
71-
global cached_ids
72-
73-
with tracer.start_as_current_span("get_recommendations_ids") as span:
74-
can_retrieve_from_cache = True
75-
for p_id in request_product_ids:
76-
if p_id not in cached_ids_to_retrieve_recommendations_for:
77-
can_retrieve_from_cache = False
78-
79-
if not can_retrieve_from_cache:
80-
logger.info("get_recommendations_ids: cache miss")
81-
span.set_attribute("app.cache_hit", False)
82-
cat_response = product_catalog_stub.ListProducts(demo_pb2.Empty())
83-
ids_to_add = []
84-
for x in cat_response.products:
85-
ids_to_add.extend(cached_ids)
86-
ids_to_add.append(x.id)
87-
if len(ids_to_add) + len(cached_ids) < MAX_CACHED_IDS:
88-
cached_ids= cached_ids + ids_to_add
89-
return cached_ids
90-
else:
91-
logger.info("get_recommendations_ids: cache hit")
92-
span.set_attribute("app.cache_hit", True)
93-
return cached_ids
94-
9566
def get_product_list(request_product_ids):
9667
global first_run
9768
with tracer.start_as_current_span("get_product_list") as span:
@@ -101,7 +72,8 @@ def get_product_list(request_product_ids):
10172
request_product_ids_str = ''.join(request_product_ids)
10273
request_product_ids = request_product_ids_str.split(',')
10374

104-
product_ids = get_recommendations_ids(request_product_ids)
75+
cat_response = product_catalog_stub.ListProducts(demo_pb2.Empty())
76+
product_ids = [x.id for x in cat_response.products]
10577

10678
span.set_attribute("app.products.count", len(product_ids))
10779

0 commit comments

Comments
 (0)