Where does this pattern in the feerate distribution per block originate from? While building a tool/visualization that displays the feerate of a transaction according to it's position in a given block I came across a patter in some blocks.

I expect a typical block to start with the highest fee per byte ratio (feerate) transactions and then continue with transactions where the feerate slowly decreases. Often there are some CPFP transactions where a lower feerate is followed by a higher feerate.

However, what I'm seeing is, that in some blocks (from different miners) are mixed 9 sat/B transactions with 5 sat/B transactions in between. At a later position in the block there are 5 sat/B transactions again. This happens over multiple blocks and different miners. I guess it originates from getblocktemplate or something similar.

I've forked a version of my tool an highlight 5 sat/B transactions in blue (Can be found here, feel free to explore). Since this is still under active development, I'll include screenshots of some blocks in case the links stop working. I deliberately selected a set of blocks where I think it's clear what I mean. 



Sample Blocks:


Block #517361 mined by ViaBTC (?)


Block #517363 mined by BTC.TOP (?)


Block #517357 mined by BTC.com (?)

and here is one from Slush too.



Questions: 

1) Is this known behavior of an algorithm that creates block templates? 

2) What motivation is there behind this feature. Is it a bug?

3) Are there commonly used getblocktemplate alternatives?
1) Is this known behavior of an algorithm that creates block templates? 


Yes. This is behavior that is present in Bitcoin Core.

Bitcoin Core bundles transactions into "packages" of one or more transactions. Each package consists of an unconfirmed transaction and its children (if any) to cover the case of Child-Pays-For-Parent. The transaction fee rate is calculated for the entire package (total fees paid by the transactions in the package divided by size of the package).

When transactions are selected, it's really the packages that are selected. Because the packages are put inside the block in the order of package fee rate, you will sometimes get a few low fee rate transactions followed by a really high fee rate transaction because they were all part of the same transaction and the high fee rate transaction was a child that paid for it's parent(s).

This is called "ancestor fee rate ordering".


  2) What motivation is there behind this feature. Is it a bug?


It is neither a feature nor a bug, it is just a quirk of how the transaction selection works.



See also: https://bitcointalk.org/index.php?topic=2058831.0