Skip to content

Commit 89899d7

Browse files
authored
kip 848 updates + faq cleanup (#182)
1 parent f22e146 commit 89899d7

File tree

4 files changed

+70
-35
lines changed

4 files changed

+70
-35
lines changed

FAQ.md

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
1. [Does Karafka require Ruby on Rails?](#does-karafka-require-ruby-on-rails)
2-
1. [Why there used to be an ApplicationController mentioned in the Wiki and some articles?](#why-there-used-to-be-an-applicationcontroller-mentioned-in-the-wiki-and-some-articles)
32
1. [Does Karafka require Redis and/or Sidekiq to work?](#does-karafka-require-redis-andor-sidekiq-to-work)
43
1. [Could an HTTP controller also consume a fetched message through the Karafka router?](#could-an-http-controller-also-consume-a-fetched-message-through-the-karafka-router)
54
1. [Does Karafka require a separate process running?](#does-karafka-require-a-separate-process-running)
@@ -8,7 +7,6 @@
87
1. [Why Karafka does not pre-initializes consumers prior to first message from a given topic being received?](#why-karafka-does-not-pre-initializes-consumers-prior-to-first-message-from-a-given-topic-being-received)
98
1. [Does Karafka restart dead PG connections?](#does-karafka-restart-dead-pg-connections)
109
1. [Does Karafka require gems to be thread-safe?](#does-karafka-require-gems-to-be-thread-safe)
11-
1. [When Karafka is loaded via railtie in test env, SimpleCov does not track code changes](#when-karafka-is-loaded-via-a-railtie-in-test-env-simplecov-does-not-track-code-changes)
1210
1. [Can I use Thread.current to store data in between batches?](#can-i-use-threadcurrent-to-store-data-between-batches)
1311
1. [Why Karafka process does not pick up newly created topics until restarted?](#why-karafka-process-does-not-pick-up-newly-created-topics-until-restarted)
1412
1. [Why is Karafka not doing work in parallel when I started two processes?](#why-is-karafka-not-doing-work-in-parallel-when-i-started-two-processes)
@@ -40,7 +38,6 @@
4038
1. [Why, despite setting `initial_offset` to `earliest`, Karafka is not picking up messages from the beginning?](#why-despite-setting-initial_offset-to-earliest-karafka-is-not-picking-up-messages-from-the-beginning)
4139
1. [Should I TSTP, wait a while, then send TERM or set a longer `shutdown_timeout` and only send a TERM signal?](#should-i-tstp-wait-a-while-then-send-term-or-set-a-longer-shutdown_timeout-and-only-send-a-term-signal)
4240
1. [Why am I getting `error:0A000086:SSL routines::certificate verify failed` after upgrading Karafka?](#why-am-i-getting-error0a000086ssl-routinescertificate-verify-failed-after-upgrading-karafka)
43-
1. [Why am I seeing a `karafka_admin` consumer group with a constant lag present?](#why-am-i-seeing-a-karafka_admin-consumer-group-with-a-constant-lag-present)
4441
1. [Can I consume the same topic independently using two consumers within the same application?](#can-i-consume-the-same-topic-independently-using-two-consumers-within-the-same-application)
4542
1. [Why am I seeing Broker failed to validate record (invalid_record) error?](#why-am-i-seeing-broker-failed-to-validate-record-invalid_record-error)
4643
1. [How can I make polling faster?](#how-can-i-make-polling-faster)
@@ -153,7 +150,6 @@
153150
1. [Is it possible to exclude `karafka-web` related reporting counts from the web UI dashboard?](#is-it-possible-to-exclude-karafka-web-related-reporting-counts-from-the-web-ui-dashboard)
154151
1. [Can I log errors in Karafka with topic, partition, and other consumer details?](#can-i-log-errors-in-karafka-with-topic-partition-and-other-consumer-details)
155152
1. [Why did our Kafka consumer start from the beginning after a 2-week downtime, but resumed correctly after a brief stop and restart?](#why-did-our-kafka-consumer-start-from-the-beginning-after-a-2-week-downtime-but-resumed-correctly-after-a-brief-stop-and-restart)
156-
1. [Why am I experiencing a load error when using Karafka with Ruby 2.7, and how can I fix it?](#why-am-i-experiencing-a-load-error-when-using-karafka-with-ruby-27-and-how-can-i-fix-it)
157153
1. [Why am I getting `+[NSCharacterSet initialize] may have been in progress in another thread when fork()` error when forking on macOS?](#why-am-i-getting-nscharacterset-initialize-may-have-been-in-progress-in-another-thread-when-fork-error-when-forking-on-macos)
158154
1. [How does Karafka handle messages with undefined topics, and can they be routed to a default consumer?](#how-does-karafka-handle-messages-with-undefined-topics-and-can-they-be-routed-to-a-default-consumer)
159155
1. [What happens if an error occurs while consuming a message in Karafka? Will the message be marked as not consumed and automatically retried?](#what-happens-if-an-error-occurs-while-consuming-a-message-in-karafka-will-the-message-be-marked-as-not-consumed-and-automatically-retried)
@@ -225,11 +221,7 @@
225221

226222
## Does Karafka require Ruby on Rails?
227223

228-
**No**. Karafka is a fully independent framework that can operate in a standalone mode. It can be easily integrated with any Ruby-based application, including those written with Ruby on Rails. Please follow the [Integrating with Ruby on Rails and other frameworks](https://github.com/karafka/karafka/wiki/Integrating-with-Ruby-on-Rails-and-other-frameworks) Wiki section.
229-
230-
## Why there used to be an ApplicationController mentioned in the Wiki and some articles?
231-
232-
You can name the main application consumer with any name. You can even call it ```ApplicationController``` or anything else you want. Karafka will sort that out, as long as your root application consumer inherits from the ```Karafka::BaseConsumer```. It's not related to Ruby on Rails controllers. Karafka framework used to use the ```*Controller``` naming convention up until Karafka 1.2 where it was changed because many people had problems with name collisions.
224+
**No**. Karafka is a fully independent framework that can operate in a standalone mode. It can be easily integrated with any Ruby-based application, including those written with Ruby on Rails. Please follow the [Integrating with Ruby on Rails and other frameworks](Integrating-with-Ruby-on-Rails-and-other-frameworks) documentation.
233225

234226
## Does Karafka require Redis and/or Sidekiq to work?
235227

@@ -241,7 +233,7 @@ You can name the main application consumer with any name. You can even call it `
241233

242234
## Does Karafka require a separate process running?
243235

244-
No, however, it is **recommended**. By default, Karafka requires a separate process (Karafka server) to consume and process messages. You can read about it in the [Consuming messages](https://github.com/karafka/karafka/wiki/Consuming-Messages) section of the Wiki.
236+
No, however, it is **recommended**. By default, Karafka requires a separate process (Karafka server) to consume and process messages. You can read about it in the [Consuming messages](Consuming-Messages) section of the documentation.
245237

246238
Karafka can also be embedded within another process so you do not need to run a separate process. You can read about it [here](Embedding).
247239

@@ -271,16 +263,12 @@ Because Karafka does not have knowledge about the whole topology of a given Kafk
271263

272264
## Does Karafka restart dead PG connections?
273265

274-
Karafka, starting from `2.0.16` will automatically release no longer used ActiveRecord connections. They should be handled and reconnected by the Rails connection reaper. You can implement custom logic to reconnect them yourself if needed beyond the reaping frequency. More details on that can be found [here](Active-Record-Connections-Management#dealing-with-dead-database-connections).
266+
Karafka will automatically release no longer used ActiveRecord connections. They should be handled and reconnected by the Rails connection reaper. You can implement custom logic to reconnect them yourself if needed beyond the reaping frequency. More details on that can be found [here](Active-Record-Connections-Management#dealing-with-dead-database-connections).
275267

276268
## Does Karafka require gems to be thread-safe?
277269

278270
Yes. Karafka uses multiple threads to process data, similar to how Puma or Sidekiq does it. The same rules apply.
279271

280-
## When Karafka is loaded via a railtie in test env, SimpleCov does not track code changes
281-
282-
Karafka hooks with railtie to load `karafka.rb`. Simplecov **needs** to be required [before](https://github.com/simplecov-ruby/simplecov#getting-started=) any code is loaded.
283-
284272
## Can I use Thread.current to store data between batches?
285273

286274
**No**. The first available thread will pick up work from the queue to better distribute work. This means that you should **not** use `Thread.current` for any type of data storage.
@@ -372,7 +360,7 @@ Upon rebalance, all uncommitted offsets will be committed before a given partiti
372360

373361
## Can I use Karafka with Ruby on Rails as a part of an internal gem?
374362

375-
Karafka 2.x has [Rails auto-detection](https://github.com/karafka/karafka/blob/78ea23f7044b81b7e0c74bb02ad3d2e5a5fa1b7c/lib/karafka/railtie.rb#L19), and it is loaded early, so some components may be available later, e.g., when ApplicationConsumer inherits from BaseConsumer that is provided by the separate gem that needs an initializer.
363+
Karafka has Rails auto-detection and loads early, so some components may be available later, e.g., when ApplicationConsumer inherits from BaseConsumer that is provided by the separate gem that needs an initializer.
376364

377365
Moreover, despite the same code base, some processes (`rails s`, `rails db:migrate`, `sidekiq s`) may not need to know about karafka, and there is no need to load it.
378366

@@ -576,7 +564,7 @@ To make Kafka accept messages bigger than 1MB, you must change both Kafka and Ka
576564

577565
To increase the maximum accepted payload size in Kafka, you can adjust the `message.max.bytes` and `replica.fetch.max.bytes` configuration parameters in the server.properties file. These parameters controls the maximum size of a message the Kafka broker will accept.
578566

579-
To allow [WaterDrop](https://github.com/karafka/waterdrop) (Karafka producer) to send bigger messages, you need to:
567+
To allow WaterDrop (Karafka producer) to send bigger messages, you need to:
580568

581569
- set the `max_payload_size` config option to value in bytes matching your maximum expected payload.
582570
- set `kafka` scoped `message.max.bytes` to the same value.
@@ -792,10 +780,6 @@ class KarafkaApp < Karafka::App
792780
end
793781
```
794782

795-
## Why am I seeing a `karafka_admin` consumer group with a constant lag present?
796-
797-
The `karafka_admin` consumer group was created when using certain admin API operations. After upgrading to karafka `2.0.37` or higher, this consumer group is no longer needed and can be safely removed.
798-
799783
## Can I consume the same topic independently using two consumers within the same application?
800784

801785
Yes. You can define independent consumer groups operating within the same application. Let's say you want to consume messages from a topic called `event` using two consumers. You can do this as follows:
@@ -1400,7 +1384,23 @@ The `range` strategy has some advantages over the `round-robin` strategy, where
14001384

14011385
Since data is often related within the same partition, `range` can keep related data processing within the same consumer, which could lead to benefits like better caching or business logic efficiencies. This can be useful, for example, to join records from two topics with the same number of partitions and the same key-partitioning logic.
14021386

1403-
The assignment strategy is not a one-size-fits-all solution and can be changed based on the specific use case. If you want to change the assignment strategy in Karafka, you can set the `partition.assignment.strategy` configuration value to either `range`, `roundrobin` or `cooperative-sticky`. It's important to consider your particular use case, the number of consumers, and the nature of your data when choosing your assignment strategy.
1387+
The assignment strategy is not a one-size-fits-all solution and can be changed based on the specific use case.
1388+
1389+
**Recommended approaches:**
1390+
1391+
1. **KIP-848 Consumer Protocol (Kafka 4.0+)** - This is the recommended approach for new deployments:
1392+
- Set `group.protocol` to `consumer` to use the new protocol
1393+
- Configure `group.remote.assignor` (e.g., `uniform` or `range`)
1394+
- Benefits: Faster rebalancing, less disruption, simpler operation, better static membership handling
1395+
1396+
2. **Cooperative-Sticky (for older Kafka versions)** - Use when KIP-848 is not available:
1397+
- Set `partition.assignment.strategy` to `cooperative-sticky`
1398+
- Provides incremental rebalancing benefits over eager protocols
1399+
- Good fallback option for teams on older infrastructure
1400+
1401+
3. **Legacy strategies** - `range` or `roundrobin` for specific use cases or compatibility requirements
1402+
1403+
It's important to consider your Kafka broker version, particular use case, the number of consumers, and the nature of your data when choosing your assignment strategy.
14041404

14051405
For Kafka 4.0+ with KRaft mode, you can also use the [next-generation consumer group protocol (KIP-848)](Kafka-New-Rebalance-Protocol) with `group.protocol: 'consumer'`, which offers significantly improved rebalance performance.
14061406

@@ -1457,7 +1457,7 @@ Karafka provides ways to implement password protection, and you can find detaile
14571457

14581458
Yes, it's possible to use a Karafka producer without a consumer in two ways:
14591459

1460-
1. You can use [WaterDrop](https://github.com/karafka/waterdrop), a standalone Karafka component for producing Kafka messages. WaterDrop was explicitly designed for use cases where only message production is required, with no need for consumption.
1460+
1. You can use WaterDrop, a standalone Karafka component for producing Kafka messages. WaterDrop was explicitly designed for use cases where only message production is required, with no need for consumption.
14611461

14621462
1. Alternatively, if you have Karafka already in your application, avoid running the `karafka server` command, as it won't make sense without any topics to consume. You can run other processes and produce messages from them. In scenarios like that, there is no need to define any routes. `Karafka#producer` should operate without any problems.
14631463

@@ -1888,13 +1888,13 @@ It is indicative of a connectivity issue. Let's break down the meaning and impli
18881888
18891889
1. **Implications for Karafka Web UI**:
18901890
1891-
- If you're experiencing this issue with topics related to Karafka Web UI, it's essential to note that Karafka improved its error handling in version 2.2.2. If you're using an older version, upgrading to the latest Karafka and Karafka Web UI versions might alleviate the issue.
1891+
- If you're experiencing this issue with topics related to Karafka Web UI, upgrading to the latest Karafka and Karafka Web UI versions is recommended as error handling has been continuously improved.
18921892
18931893
- Another scenario where this error might pop up is during rolling upgrades of the Kafka cluster. If the Karafka Web UI topics have a replication factor 1, there's no redundancy for the partition data. During a rolling upgrade, as brokers are taken down sequentially for upgrades, there might be brief windows where the partition's data isn't available due to its residing broker being offline.
18941894
18951895
Below, you can find a few recommendations in case you encounter this error:
18961896
1897-
1. **Upgrade Karafka**: If you're running a version older than `2.2.2`, consider upgrading both Karafka and Karafka Web UI. This might resolve the issue if it's related to previous error-handling mechanisms.
1897+
1. **Upgrade Karafka**: Always use the latest stable versions of Karafka and Karafka Web UI to benefit from improved error handling and bug fixes.
18981898
18991899
1. **Review Configurations**: Examine your Karafka client configurations, especially timeouts and broker addresses, to ensure they're set appropriately.
19001900
@@ -2130,15 +2130,6 @@ This issue is likely due to the `offsets.retention.minutes` setting in Kafka. Ka
21302130
21312131
You can read more about this behavior [here](Operations-Development-vs-Production#configure-your-brokers-offsetsretentionminutes-policy).
21322132
2133-
## Why am I experiencing a load error when using Karafka with Ruby 2.7, and how can I fix it?
2134-
2135-
If you're experiencing a load error with Karafka on Ruby 2.7, it's due to a bug in Bundler. To fix this:
2136-
2137-
- **Install Bundler v2.4.22**: Run `gem install bundler -v 2.4.22 --no-document`.
2138-
- **Update RubyGems to v3.4.22**: Run `gem update --system 3.4.22 --no-document`.
2139-
2140-
Note: Ruby 2.7 is EOL and no longer supported. For better security and functionality, upgrading to Ruby 3.0 or higher is highly recommended.
2141-
21422133
## Why am I getting `+[NSCharacterSet initialize] may have been in progress in another thread when fork()` error when forking on macOS?
21432134
21442135
When running a Rails application with Karafka and Puma on macOS, hitting the Karafka dashboard or endpoints can cause crashes with an error related to fork() and Objective-C initialization. This is especially prevalent in Puma's clustered mode.

Operations/Deployment.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,48 @@ When deploying Karafka consumers using Kubernetes, it's generally not recommende
553553
554554
For larger deployments with many consumer processes, it's especially important to be mindful of the rebalancing issue.
555555
556-
Overall, when deploying Karafka consumers using Kubernetes, it's important to consider the deployment strategy carefully and to choose a strategy that will minimize the risk of rebalancing issues. By using the `Recreate` strategy and configuring Karafka static group memberships and `cooperative.sticky` rebalance strategy settings, you can ensure that your Karafka application stays reliable and performant, even during large-scale deployments.
556+
Overall, when deploying Karafka consumers using Kubernetes, it's important to consider the deployment strategy carefully and to choose a strategy that will minimize the risk of rebalancing issues. By using the `Recreate` strategy and configuring Karafka with appropriate rebalancing strategies, you can ensure that your Karafka application stays reliable and performant.
557+
558+
### Choosing the Right Rebalance Strategy
559+
560+
**For teams running Kafka 4.0+:**
561+
562+
- Use the new **KIP-848 consumer protocol** (`group.protocol` set to `consumer`) as your primary choice
563+
- Benefits: Faster rebalancing, less disruption, simpler operation, improved static membership handling
564+
- This is the recommended approach for all new deployments with compatible infrastructure
565+
566+
**For teams on older Kafka versions or with compatibility constraints:**
567+
568+
- Use **`cooperative-sticky`** rebalance strategy
569+
- Still provides incremental rebalancing benefits over the older eager protocols
570+
- Migrate to KIP-848 when your infrastructure supports it
571+
572+
Example configuration for KIP-848:
573+
574+
```ruby
575+
class KarafkaApp < Karafka::App
576+
setup do |config|
577+
config.kafka = {
578+
'bootstrap.servers': '127.0.0.1:9092',
579+
# Use the new consumer protocol (KIP-848)
580+
'group.protocol': 'consumer'
581+
}
582+
end
583+
end
584+
```
585+
586+
Example configuration for cooperative-sticky (fallback option):
587+
588+
```ruby
589+
class KarafkaApp < Karafka::App
590+
setup do |config|
591+
config.kafka = {
592+
'bootstrap.servers': '127.0.0.1:9092',
593+
'partition.assignment.strategy': 'cooperative-sticky'
594+
}
595+
end
596+
end
597+
```
557598
558599
### Liveness
559600

Operations/Development-vs-Production.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class KarafkaApp < Karafka::App
8686
setup do |config|
8787
config.kafka = {
8888
'bootstrap.servers': '127.0.0.1:9092',
89+
# Fallback to cooperative-sticky for older brokers
8990
'partition.assignment.strategy': 'cooperative-sticky'
9091
}
9192
end

Pro/Long-Running-Jobs.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ class KarafkaApp < Karafka::App
117117
end
118118
```
119119

120+
Both strategies help avoid unnecessary partition revocations when partitions would be re-assigned back to the same process.
121+
120122
### Revocation and re-assignment
121123

122124
In the case of scenario `2`, there is nothing you need to do. Karafka will continue processing your messages and resume partition after it is done with the work.

0 commit comments

Comments
 (0)