# विफलताएँ, Retries और Dead-Letter Topics — Apache Kafka: बुनियाद से Production तक Event Streaming

Source: https://www.geekswithgeeks.com/hi/kafka/kc-failures-dlq

> Partition को रोके बिना poison messages सँभालें।

## एक ख़राब record पूरी पंक्ति न रोके

Partition क्रम से प्रोसेस होता है, इसलिए जो record हमेशा विफल हो (**poison message**: ख़राब डेटा, अनपेक्षित schema) वह हमेशा retry करने पर अपने पीछे सब कुछ रोक सकता है। **अस्थायी** विफलताओं (database timeout, जिन्हें backoff के साथ कुछ retries चाहिए) को **स्थायी** विफलताओं (ख़राब आकार का डेटा, जिसे retries ठीक नहीं कर सकते) से अलग करें। स्थायी विफलताओं को error कारण और मूल निर्देशांक (topic, partition, offset) के साथ **dead-letter topic** (DLT) में भेजें, offset commit करें और आगे बढ़ें। DLT का आकार monitor करें और कारण ठीक करने के बाद उसके records जाँचने व replay करने की प्रक्रिया रखें। जहाँ क्रम मायने रखता है, record को कहीं और ले जाने की जगह वहीं retry करें।

## विफलता को dead-letter topic भेजना (उदाहरण)

`TransientError` को आपका कोड retry करता है; बाक़ी स्थायी माना जाता है और संदर्भ के साथ `orders.dlt` में रखा जाता है।

```python
try:
    handle(msg)
except TransientError:
    raise                                  # let the retry policy handle it
except Exception as exc:                   # permanent: park it, keep going
    producer.produce("orders.dlt", key=msg.key(), value=msg.value(),
                     headers=[("error", str(exc).encode()),
                              ("src", f"{msg.topic()}[{msg.partition()}]@{msg.offset()}".encode())])
commit(msg)
```

**Quiz:** Dead-letter topic क्यों उपयोग करें?

- [ ] Broker तेज़ करने के लिए
- [x] स्थायी रूप से विफल records को अलग रखने के लिए ताकि partition चलता रहे
- [ ] सारे errors चुपचाप हटाने के लिए
- [ ] Partitions बढ़ाने के लिए

*Answer:* स्थायी रूप से विफल records को अलग रखने के लिए ताकि partition चलता रहे. अलग रखे records को स्वस्थ traffic रोके बिना जाँचा और replay किया जा सकता है।
