Running with Ruby

Domain-Driven Rails – Mediocrity-Driven Book

Updates

  • This is a review of the Domain-Driver book and its content only. Keep in mind, that this book can be bought also with code, exercises and workshops. Those weren’t the subject of this review.
  • This is a review of the beta version of the book. Arkency didn’t mention that on their site, but this has already been fixed. I highly recommend checking their website for updates and/or fixes, as not all the points from this review might still be valid.
  • The author was kind enough to comment this review, so please go to the comments section for his perspective.

Introduction

It’s been a while since my last technical book review. I’ve decided to have a go at Domain-Driven Rails as this subject is one of the things that I’ve been particularly interested in for quite a while now.

For those who don’t know me, I’m the creator of Karafka framework, a tool that can and is used for simplifying Apache Kafka based Ruby applications development. It is used by multiple teams for implementing the Domain-Driven approach (mainly, for the Domain events / Event sourcing parts of DDD)  within their Ruby and Rails applications.

I’ve been using the DDD approach for both: new and existing systems for over 3 years and I can definitely say, that if you want to build complex systems, which can be maintained and developed, this is the way to go.

But is Domain-Driven Rails the best book to read before tossing yourself into deep end? Unfortunately, I’m not quite sure.

Aim of this review

The aim of this review is to help you decide whether or not this book is a good choice for you and is it worth spending $49 or more.

Who is the book target reader?

I would say, that this book is definitely not for the readers who:

  • just finished some Ruby on Rails tutorials,
  • recently mastered basics of CRUD and scaffolding,
  • didn’t encounter any of the problems that this book aims to solve,
  • your biggest applications contain +/-20 models at most and have only simple business logic,
  • you’ve never used Sidekiq or any other background processing tools.

Of course it doesn’t mean that it won’t be useful for you at all. It’s more about giving the problems and challenges described in this title a deeper, personalized context without which it will be just a next theoretical (from the usage perspective) position in your library.

It’s also not a glossary type of book, which you can open, find a solution and close it again. DDD is a huge subject and cherry-picking of some parts as remedies may not be the best idea.

DD Rails can be a good choice if:

  • you’ve spent 2+ years with Rails built systems,
  • you just need a really quick introduction into the subject, without theoretical base or anything else,
  • you’ve just started a new job in a company where they use DDD and you want to get a grasp of it but don’t have too much time.

In such cases, go for it, but before you do, please read the rest of the review.

Advanced programmer reviewing book for mids? Does it even make sense?

You might say, that I’m not the best guy to review this book. You might say, that I’ve been using the approach and solutions presented in it for a long time, so my expectations may not exactly be met by this book. And I think just the opposite, because I remember myself reading Implementing Domain-Driven Design and trying to adapt knowledge found there into the systems built with Ruby and Rails. I remember times, when there were no decent libraries nor articles for Ruby programmers on that matter. Heck, that was one of the reasons Karafka was born!

The review

Below I present some of the things that I liked/disliked in particular. If you are interested in a TL;DR version, please go to the summary section.

What did I like about this book

Undoubtedly, there are some good aspects of this book. Here are a few that I appreciated the most.

The idea

I’m a big evangelist of DDD and its components. I really appreciate the time and the effort Robert & the Arkency team invested to make DDD more accessible and easier for Ruby and Rails developers.

References

If you strive for more information on a given matter, at the end of each chapter you will find tons of references and links for further exploration.

Sagas

Sagas are one of the things I find hard to explain to people*. We are accustomed too much to the HTTP way of thinking.

Sagas are meant to be asynchronous and they need to be designed that way.

Because saga will be asynchronously processing domain events from many bounded contexts, which can happen at any time, there is potential for race conditions

Luckily the guys behind this publication didn’t forget about it. They embrace it and present a few ways of dealing with this kind of situations.

They also cover the at-least-once delivery case, idempotent sagas and events duplication:

If the message queue that you use gives at-least-once delivery guarantees your sagas should be idempotent and handle duplicated events.

Too bad though, that they didn’t mention the exactly-once Kafka’s feature.

* Especially when you don’t call them process managers

Code examples and business context

The book is rich with examples. Many of them are from finance-related systems that Arkency develops and maintains, which adds an extra value to it. Most of them are clear and stripped out of useless parts that aren’t relevant to the discussed subject.

Things I didn’t like about that book

The good part is behind us. Now it’s time for things that in my opinion made this book much worse that it could be.

Rails or Ruby, Ruby or Rails?

The first page of the prologue and already a funny contradiction:

One topic that was relevant here is how to grow as a Rails developer.

and a few sentences later we get this:

If you focus too much on the technology dimension,
you will become too reliant on “The Framework Way”

Shouldn’t this approach be applied to writing books as well? If so, then maybe the book should be called DD for Ruby. I’m not saying, that this book is tightly bound to the Rails framework, but it would be good not to become too reliant on “The Framework Tooling”. If you get knowledge from a framework perspective*, it will always be distorted. More or less, but it will be and you need to remember this while reading the book.

* This applies as well for a programming language layer

Read a book to understand a book

As I’ve mentioned, there is almost no pure theoretical knowledge presented in DDR. This is an advantage and disadvantage at the same time. I couldn’t help feeling like something was missing and that I could understand everything because I already understood the matter. It means, that if you’re new to the subject, you might find yourself googling for not yet explained acronyms or terminology that

we will discuss in a moment.

The lack of glossary at the end is also a bummer. Especially since (as mentioned), there are times when the explanation for a term is not during its first  occurrence.

Oversize images?

I’ve counted a number of pages where there would be less than 50% of text and/or some oversize images. There are 23 pages like that. It means, that more than 12% of the book is useless.

Note: this point might no longer be valid. Please visit the Updates section of this review.

It took me 1 day to read it. For me, +/- 120 pages of content is not enough to call it a book.

A limited perspective of DDD the Arkency way

If you decide to buy this book and use concepts and code snippets presented there, keep in mind, that you might become Arkency-dependent.

They present their libraries, their approach and their solutions. There would not be anything bad about that, if they explored other solutions and tools available.

They also seem to be really agnostic about their technological stack. They recommend using same database for storing events:

(…) I strongly recommend at the beginning of your journey into the world of asynchronicity, eventual consistency and async handlers: a message bus with a queue in the same SQL database that you for your app. It really simplifies error handling on many levels.

But what they forgot to mention is that:

  • You might not be able to deserialize YAML events in other technologies that easily.
  • You cut yourself from a set of flexible tools that will allow you to easily expand and replace Ruby based components with those written with Go, Elixir or any other technology you find suitable for doing certain jobs.
  • You might become narrow-minded about potential solutions and tools you can add to your stack (because they will have to play along with the Arkency gems).
  • Going for an all-in-one DB solution, tightly integrating all aspects of your software with it, makes it much harder to replace a message bus like that with a more generic one.

Note: I wouldn’t mind that at all, if the books description stated, that most of the examples and code snippets are related to their gems and libraries.

Outdated before published

Long time ago, when Rails weren’t that stable in terms of concepts and internal API, I used to say, that a released book about Ruby on Rails is already an outdated book. This sentence was true especially for printed versions that could not be easily updated.

But I’ve never seen a book that would use abandoned and outdated gems. I was quite surprised to notice, that there are almost 2 pages about Virtus, a library that was abandoned by its creator Solnic (great explanation on the reasons can be found here). Everyone that used to use it, knew from 2015, that it’s going to be totally dropped in favor of the dry-rb ecosystem. But hey, probably authors explore those in the book as well. But did they?

I am not super familiar with the dry-rb ecosystem but I am pretty sure that dry-types, dry-struct and dry-validations provide a nice experience in building command objects as well.

Published but incomplete

Let the image speak for itself:

I know, that the Lean approach is getting more and more popular, but should it be applied to books? I don’t think so. 

Note: This has already been explained in the Updates section and in the comments section of this review.

Eventide as a bonus

When I buy a programming book, I expect it to be written by people that deal with the subject on a daily basis. What did I get as a bonus in DD Rails? 11 page long description of Robert’s first Eventide encounter. Really? I don’t mind him playing with that set of libraries. I would even appreciate his point of  view on them in a blog post, but spending 11 pages out of a book just to describe his first impressions on it?

Summary / TL;DR

Note: Please read the Updates section as not all of this TL;DR might be up to date.

Believe me, I’m not happy to say it, but I must. This book is mediocre at best. Its content could fit into 10-15 blog posts. Then I would say, that it is a really good starting point for people not familiar with DDD. But to sell it as a book?

I’m disappointed with what I’ve read. I know, that Arkency can do way better. I know, that they have 100% guarantee policy but even then, it doesn’t justify selling a product that is:

  • incomplete (refers to the beta version, please read the Updates section),
  • outdated,
  • without proofreading,
  • too short,
  • badly structured in terms of layout,
  • presenting only a single set of libraries and a single perspective on the subject.

There’s a huge difference between expectations against blog posts and books and I think, sometimes it’s worth reminding some people about it.

Sidekiq Unique Jobs: don’t waste your time waiting – reschedule if busy

Half a year ago, I’ve made a post about Enforcing unique jobs in Karafka and Sidekiq for single resources. This approach is great, however, there’s a particular case in which Sidekiq Unique Jobs can block all of your Sidekiq workers except one. This can significantly limit your computing power without you being aware of it.

When using Sidekiq Unique Jobs with a WhileExecuting strategy, only a single worker can start processing Sidekiq job with a given unique key. This is really helpful when you work with resources for which you cannot perform parallel operations (for example when you work with a database for which there are no atomic operations but you need to increment counters), as doing so could overwrite results from an other worker. A WhileExecuting strategy, with a properly defined unique key can help you prevent that from happening. However…

Problem definition

Sidekiq jobs are being consumed out of a FIFO queue. Without any additional modifications, situation is pretty clear: having 4 single threaded processes each allows you to process 4 jobs at the same time.

Everything changes, when you decide to add Sidekiq Unique Jobs to your stack. In a case, when there are multiple jobs in sequence for a given unique key, Sidekiq will get seriously clogged. In the worst scenario, it won’t matter how many workers and threads you have in your Sidekiq infrastructure. You will get a performance of a single Sidekiq thread. It is because you cannot make Sidekiq skip certain tasks because of FIFO queue processing. To bypass this limitation, authors of Sidekiq Unique Jobs introduced a #sleep that will run up until the resource is free again to be processed or until timeout occurs. This approach means, that if you have more tasks in queue than processors, they will have to wait until all the jobs with a given unique key are processed.

All the workers will actively wait (meaning that in Sidekiq console you will see them marked as busy) up until a lock is released.

Solution: reschedule instead of waiting

Warning: if a similar case occurs in your business logic quite often, probably you will be better taking engine different than Sidekiq. I would recommend this solution for non-frequent edge/corner cases.

Bypassing that behavior is pretty easy: if there’s a lock, put the current job at the end of the queue. That said, your jobs will be checked for possibility of execution and rescheduled back instead of waiting. This will mess up your queue counters a bit (as you will have more jobs enqueued and processed that it should) but on the other hand it means that Sidekiq will “actively” seek for resources on which it can work in a certain moment.

To do so, we can create a new Unique Job strategy that we can later on apply. Apart from rescheduling, our strategy won’t differ from the WhileExecuting, so we can use it as a base.

module SidekiqUniqueJobs
  module Lock
    class WhileExecutingReschedule < WhileExecuting
      MUTEX = Mutex.new

      def synchronize
        MUTEX.lock

        if (@locked = locked?)
          yield
        else
          # We use sleep just to prevent from a pointless, extremely fast looping
          # in case all the jobs have the same unique key
          sleep 0.1
          @item['class'].constantize.perform_async(*@item['args'])
        end
      rescue Sidekiq::Shutdown
        logger.fatal { "the unique_key: #{@unique_digest} needs to be unlocked manually" }
        raise
      ensure
        # If we were able to obtain lock, we need to release it after processing
        if @locked
          SidekiqUniqueJobs.connection(@redis_pool) { |conn| conn.del @unique_digest }
        end

        MUTEX.unlock
      end
    end
  end
end

Applying this strategy is super easy. We just need to replace while_executing strategy with while_executing_reschedule:

class ApplicationWorker
  include Sidekiq::Worker

  # sidekiq_options unique: :while_executing
  sidekiq_options unique: :while_executing_reschedule
end

Cover photo by: Alexandre Duret-Lutz on Creative Commons 2.0 license. Changes made: added an overlay layer with an article title on top of the original picture.

« Older posts

Copyright © 2017 Running with Ruby

Theme by Anders NorenUp ↑