Category: Education

Kafka on Rails: Using Kafka with Ruby on Rails – Part 2 – Getting started with Rails and Kafka

  1. Kafka on Rails: Using Kafka with Ruby on Rails – Part 1 – Kafka basics and its advantages
  2. Kafka on Rails: Using Kafka with Ruby on Rails - Part 2 - Getting started with Rails and Kafka

Kafka Docker local setup

Before we proceed with combining Kafka with Ruby, it would be good to have a workable local Kafka process. The easiest way to do that is by using our docker-compose.yml present in Karafka:

git clone git@github.com:karafka/karafka.git
cd karafka
docker-compose up

To check that it works, you can just telnet to it:

telnet 127.0.0.1 9092
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.

Getting started with Karafka framework

Karafka is a framework used to simplify Apache Kafka based Ruby and Rails applications development. It provides a higher-level abstraction, that allows you to focus on your business logic development, instead of focusing on implementing lower level abstraction layers. It provides developers with a set of tools that are dedicated for building multi-topic applications similarly to how Rails applications are being built.

As README states:

  • You can integrate Karafka with any Ruby-based application.
  • Karafka does not require Sidekiq or any other third party software (apart from Kafka itself).
  • Karafka works with Ruby on Rails but it is a standalone framework that can work without it.
  • Karafka has a minimal set of dependencies, so adding it won't be a huge burden for your already existing applications.
  • It handles processing, using multiple threads, so it will utilize your CPU better (especially for IO-bound applications).

The way you should start with Kafka and Karafka heavily depends on your system state. I always recommend a different approach for tackling the already existing complex systems and for greenfield applications, especially those that don't use Rails at all.

Brownfield system initial integration

Note: This introduction aims to get you going as fast as possible with sending messages. A broad description on decomposing an already existing Rails application will be provided in one of the upcoming posts in this series.

One of the easiest ways to get started with Kafka and Karafka in an already existing (and often complex) system is by introducing a simple messaging layer that will broadcast events to the Kafka cluster. This approach has several advantages:

  • You can get familiar with the stack without bigger changes to your system.
  • It's easier.
  • It does not require much configuration and setup.
  • You won't have to change your deployment process as messaging can happen from any Ruby process you run, like: Puma processing, Sidekiq process, Resque process, etc.


To do so, you need to install Karafka and use it initially only to produce messages. You can consider this to be an intermediate step in between not having Karafka and having it running on a full-scale.

In order to use it, you need to add this to your Gemfile:

bundle add karafka

and run

bundle exec karafka install

The above command will create all the necessary files and directories to get you started.

After that, you should be able to send messages. To check, that everything works as expected, just try to deliver a single message with a sync producer:

# Karafka producer is thread-safe and can be used from any place in the code
Karafka.producer.produce_sync(topic: 'my-topic', payload: 'message')

Note: It's a really good idea to disable a topic auto-creation for the Kafka production cluster. Typos happen to everyone. You can read more about Kafka brokers configuration options here.

Fresh start with a greenfield system

When you don't need integration with your current stack or you already send messages and want to consume them from a separate application, you can start easily with a clean installation:

mkdir app_dir
cd app_dir
echo "source 'https://rubygems.org'" > Gemfile
echo "gem 'karafka'" >> Gemfile

bundle install
bundle exec karafka install

The karafka install command will create all the files and directories that are required to run Karafka server process. The most interesting file is the karafka.rb file that contains all the configuration details and will contain your routing details to match consumers with proper Kafka topics.

Summary - Getting started is easy!

This part of the series wasn't really long. Karafka is well written and adding it to the stack is not a big problem. And because Kafka messages are immutable, sending messages is a great way to start working with it.

One thing that I can suggest to you at the end of this article, is not to throw yourself in at the deep end by implementing producing and consuming at the same time (especially if you don't have experience with Kafka). Quite often, the initial concept and vision related to the processing flow may change after some modeling. Broadcasting without consumption gives you a really good playground to test your ideas without any risk.

Stay tuned :-)

Read more:

Grill.rb 2016 – Poland’s first outdoor conference review

grillrbIt's about time to summarize some of the conferences that I've recently attended to. This post will be about Grill.rb that organizers claim to be "Poland's first outdoor conference". I was quite skeptical about that claim, but after I couldn't find any other Poland's outdoor conferences, I must admit that :).

And as many "first of a kind" conferences, this one was quite intimate. 60 attendees, 6 talks, open space, lightning talks, 35 kg of meet and a huge amount of great attitude.

Location

Day 1

On Saturday, we had a great weather. More than 30 Celsius degrees and gentle breeze. Anders Hill was definitely a great choice - peaceful and quiet place, with some trees giving us a bit of shadow to hide from the sun.

20160625_191809

Day 2

Organizers on Facebook called it the "Dungeon.rb" and I couldn't agree more. The weather was not too kind for us the next day and we had to move to an indoor location. But even then, you could still feel the outdoor, "non commercial" vibe.

13495094_1705525553034950_1080983640665734947_n

Speakers/presentations/talks

One could say, that 6 presentations for 2 days is not enough. And it would be true, if it wasn't for the Poland vs Switzerland game, barbecue time, open space and some other activities that were presented. If you ask me, I would say, that it was well-balanced. Especially, that it was suppose to be an outdoor, chill conference, not the "most professional, top knowledge !!!1111oneone" IT event ever. It's not that it didn't have pro talks ;), it's more that its main goal was to provide a solid technical base in an open and friendly environment, where one could easily talk with one another. Think of it as a constant 2 day afterparty ;)

I've enjoyed all the talks and what strikes me is that more and more speakers seem to notice and speak about software quality (even when their talks are not directly related to it). I believe that it is a great trend. Achieving (at least) a decent quality is really important and unfortunately especially unexperienced programmers don't see huge advantages that they gain reaching a certain point. The most interesting talk for me, was Krzysztof's Tarnowski "Functional programming in Ruby". It showed a different approach that aims to solve some of the Ruby programmers issues with an Erlang/Elixir approach.

If you're interested in more details about the talks, ping organizers, hopefully they will publish presentations at some point, because it is definitely worth reviewing them, if you haven't attended.

Open space - freedom of choice

What if you could propose a subject on which all the attendees can discuss? What if many people would do that, and you could pick about what you want to talk with others? This is exactly what happened during the 1st day. 3 groups, 3 topics, 3 rounds. Open Source gems vs Law and licences, ReactJS + Rails, Code Quality, DDD and many more. The only downside is that you cannot attend more than 1 at the same time. The more people, the more points of view you can see and understand. Great idea and great execution.

Food/grill

See for yourself :-) Yummy!

13497982_1705825163004989_5415868372842741787_o

Organization

It is hard to describe something that is good. There were only minor things that I could whine about, but it didn't change my  perception of the whole conference so I consider  them irrelevant. Good job, guys!

Summary

Was it worth attending? Yes! Definitely yes! Was it good? Even more. Does the outdoor approach fits into IT conferences? I would say, that we need way more people with that approach (and attitude) than indoor, strict meetings. IT community is a community of people with open minds, and I feel that we integrate and feel way better in this type of environment, than inside of tiny, dark rooms with only projectors light.

Copyright © 2024 Closer to Code

Theme by Anders NorenUp ↑