# Task recipes

Follow practical paths for replay, failed messages, authentication, and consumer lag

Package: @cookiemonsterdev/kafka-core 2.8.1
Canonical: https://cookiemonsterdev.github.io/kafka/docs/core/guides/recipes/
Source revision: b112a684d2b3cdc7deadb3513041fcdbdc1c18b5

These recipes connect the client, CLI, and Studio documentation. Start on a local cluster
or a dedicated profile when an operation changes stored state.

## Replay recent messages

For a new replay group, use the consumer's duration-based starting policy:

```ts
const consumer = kafka.consumer({ groupId: 'replay-last-day', autoOffsetReset: 'by_duration:P1D' });
await consumer.connect();
await consumer.subscribe({ topics: ['events'] });
```

This is a fragment using the quickstart's `kafka` instance; call `run()` to process records.
The policy applies when the group has no usable commit, not as an override of an existing commit.
For an existing group, use an explicitly planned reset. See [Consumer](https://cookiemonsterdev.github.io/kafka/docs/core/guides/consumer/#subscribe).

## Handle a failed message

Perform the side effect before marking processing successful. On failure, throw to surface
it to the consumer lifecycle; do not catch and silently return success. Persist an event id
at the destination so a redelivery does not repeat the side effect. If routing failed records
to another topic, decide when that write and the source offset commit become durable.
See [Errors](https://cookiemonsterdev.github.io/kafka/docs/core/guides/errors/) and [Transactions](https://cookiemonsterdev.github.io/kafka/docs/core/reference/producer/).

## Reset a consumer group

Stop its consumers first. Preview the reset, inspect the target offsets, then execute:

```sh
kafka group reset-offsets docs-demo-reader --topic docs-demo-events --to earliest
kafka group reset-offsets docs-demo-reader --topic docs-demo-events --to earliest --execute
```

Restart the consumers and inspect `kafka group offsets docs-demo-reader`. Moving backward
reprocesses records; moving forward skips records. See [reset-offsets](https://cookiemonsterdev.github.io/kafka/docs/cli/reference/commands/group/reset-offsets/).

## Connect to an authenticated cluster

Follow [Security](https://cookiemonsterdev.github.io/kafka/docs/core/guides/security/) to configure TLS and SASL; then use `kafka doctor --profile staging`
to verify configuration resolution before using that profile for administration. The CLI and
Studio use the same [profile configuration](https://cookiemonsterdev.github.io/kafka/docs/cli/guides/configuration/#profiles).

## Investigate consumer lag

Use `kafka group describe <group>` and `kafka group offsets <group>` to identify affected
partitions. Open the group in Studio to compare membership and lag. Correlate the change
with application processing times and rebalance events before tuning concurrency.
See [Troubleshooting](https://cookiemonsterdev.github.io/kafka/docs/core/guides/troubleshooting/#lag-grows-continuously) and [Studio walkthrough](https://cookiemonsterdev.github.io/kafka/docs/studio/start/getting-started/).
