# Backend for Frontend और Aggregation — API Gateway और Service Mesh

Source: https://www.geekswithgeeks.com/hi/api-gateway-service-mesh/p-bff

> Client प्रकार के अनुसार APIs को आकार दें और कई backend calls को एक में जोड़ें।

## एक screen, एक call

मोबाइल home screen को पाँच services का डेटा चाहिए हो सकता है; धीमे network पर हर एक को बुलाना बेकार है। **Backend for Frontend (BFF)** pattern हर client प्रकार (mobile, web, partner) के लिए services के सामने छोटा उद्देश्य-निर्मित API रखता है। वह calls का (समानांतर) **aggregation** करता है, fields छाँटता है और formats ढालता है ताकि हर client एक call करे और सिर्फ़ उतना डाउनलोड करे जितना चाहिए। Gateway हल्का aggregation कर सकता है; भारी orchestration front-end टीम के स्वामित्व वाली समर्पित BFF service में होनी चाहिए। कसी निर्भरता, आंशिक विफलताओं (जो है लौटाएँ, जो नहीं है उसे चिह्नित करें) और BFFs में दोहराए तर्क पर नज़र रखें।

## API को आकार दें और सुरक्षित रूप से release करें

Backend-for-frontend और shadowing जैसे patterns clients और releases की मदद करते हैं।

![तीन patterns: aggregate, shadow, shift।](assets/figures/api-gateway-service-mesh/section-7-map.svg) — चित्र 7.1 — Aggregate, shadow और shift।

## BFF endpoint (उदाहरण)

Calls समानांतर चलती हैं और विफल होती वैकल्पिक dependency पूरी screen विफल करने की जगह शालीनता से घटती है। `get_*` आपके service clients के प्रतीक हैं।

```python
import asyncio

async def home_screen(user_id):
    profile, orders, recs = await asyncio.gather(
        get_profile(user_id),
        get_recent_orders(user_id, limit=3),
        get_recommendations(user_id),
        return_exceptions=True,
    )
    return {
        "profile": profile,                                   # required
        "recent_orders": orders if not isinstance(orders, Exception) else [],
        "recommendations": recs if not isinstance(recs, Exception) else [],
        "degraded": [n for n, v in (("orders", orders), ("recommendations", recs)) if isinstance(v, Exception)],
    }
```

**Quiz:** BFF pattern का उद्देश्य क्या है?

- [x] हर client प्रकार को उसकी ज़रूरत के आकार का API देना, कम round trips के साथ
- [ ] सारी microservices बदलना
- [ ] Gateway हटाना
- [ ] Databases encrypt करना

*Answer:* हर client प्रकार को उसकी ज़रूरत के आकार का API देना, कम round trips के साथ. अनुकूलित aggregation परत बातूनीपन और client जटिलता घटाती है।
