Note N2 · Tadinda
BARCODE DATA PIPELINE
From barcode to product data: the Open Food Facts source chain and a three-layer cache
- Sheet
- TADINDA
- Topic
- FastAPI · Redis · MySQL · Open Food Facts
- Source
- services/product_cache_service.py
- Date
- 2026-11-12
- Reading
- 2 min
Short answer: don't rely on a single product database; look the barcode up in several sources in turn and use the first "useful" record. Cache the result in two layers: Redis (hot) and MySQL (warm), both for 24 hours. If every source fails and you have an old record, return it; stale data beats no data. If you translated the ingredients text into Turkish, store the translation too, so the same product is never translated again.
01Source chain
- The Open Food Facts family: the configured main address, then the Turkey, US and France servers, then Open Products Facts, Open Beauty Facts and Open Pet Food Facts. A product may be registered as a cosmetic rather than a food.
- UPCItemDB: the keyless trial; for products missing from OFF.
- Optional sources: FoodRepo and OpenNutrition; enabled by configuration.
- Last resort: local market data and an AI-assisted fallback.
Each source is asked "is there useful data", not "is there a record"; a record with a name but empty ingredients doesn't stop the chain from moving to the next source. Every attempt's outcome (hit, miss, error) is logged with the source name; how useful each source is can be counted from the logs.
02Three layers
# 1. Hot: Redis if (cached := await redis.get(f"product:{code}")): return json.loads(cached) # 2. Warm: MySQL, if fresher than 24 hours row = await db.get(ProductCache, code) if row and now - row.updated_at <= timedelta(hours=24): ... # 3. The source chain; the result is written to MySQL and Redis fresh = await off_service.fetch_product(code)
If Redis is down the app doesn't stop; it falls back to the MySQL layer, and errors writing to Redis are swallowed. Old-format records in MySQL (without the Turkish fields) don't count as fresh and are fetched again.
03Serving stale data
If the source chain finds nothing but MySQL has a record older than 24 hours, that record is returned and written back to Redis. Open Food Facts being unreachable for a while doesn't stop the analysis of products scanned before. Products the user entered by hand skip the age check entirely; an external source can't overwrite them.
04Translation is a cache too
The risk analysis works on Turkish text. If the ingredients exist only in another language, they are translated into Turkish. The translation is written both to Redis and to a separate column of the product record; the product record in Redis is deleted so the next read picks up the fresh record with the translation. When the same barcode is scanned again, no request goes to the translation service.
→More notes from this project
—End of notewritten by MUHAMMEDPYZ