The Only Thing That Matters – presentation materials

TL;DR

A few days ago I gave a presentation titled The Only Thing That Matters. Below you can find the list of materials one could use to ramp up his knowledge about topics covered in the presentation. Beware! Spoilers ahead!

Sql Server

Event Store

Kafka

Summary

I hope this links help you in pursuing the understanding of logging and append only structures. Let’s flush some data on disk!

 

 

 

 

Service Fabric – KeyValueStoreReplica (1)

TL;DR

In the previous post I introduced Sewing Machine, a helper library for building more on top of strong Service Fabric foundations. Before building a thing, one needs to know the foundations though. That’s where we start our Service Fabric journey.

Actors

One of the paradigms that are supported by Service Fabric is a Virtual Actor pattern. Actors are simple and small single threaded executions, that have their own state and a lifecycle. The Virtual Actor pattern ensures that you never need to instantiate any actor. Once you access an actor via its id, it will be created and from now on hosted somewhere in the cluster. Where? That depends on the balancer. All you need to know is an actor type and its identifier.

More about actors you can read in a good introduction provided in here

Actors state

Service Fabric actors can have multiple states. They are accessible by the StateManager property almost like a regular dictionary. If the actor is marked as Persisted, you have a guarantee that its state will be stored between calls, ensuring that in case of a machine failure, it can be restored on another one without data loss.

[StatePersistence(StatePersistence.Persisted)]
class CountingActor : Actor, ICountingActor
{
  public Task SetCountAsync(int value)
  {
    return this.StateManager
      .SetStateAsync("Counter", value);
  }
}

Additionally, multiple states of the same actor can be changed simultaneously and will be stored in a transaction. Yes, you can’t run into a situation with a partially stored state of a persisted actor.

Why would one store different states then? Think of it as a simple partitioning your actors’ data. If you don’t need the whole state every time, maybe it’s good to split them according to their use cases.

The StateManager behind the curtain implements a simple Unit of Work. If you access, set or delete the state under the same key within one call, the StateManager will track data and flush it properly in a transaction at the end of the call.

But transaction means db

If we say transaction, it means that there must be a database behind it. This is the case of Persisted actors as well. Behind the interface of IActorStateProvider providing the API for Actor state management, its implementation calls a very particular class providing access to a transactional, replicated, associative data storage component to service writers – ready for integration into any Service Fabric service. This is the database responsible for storage. It’s called KeyValueStoreReplica.

Summary

In this first blog post we followed from the high level API of actors’ framework, through the state management to the underlying database. In the following posts we’ll dive deeper into capabilities provided by this storage.

Sewing Machine for Service Fabric

TL;DR

This is an introductory post for my new Open Source journey project, Sewing Machine that aims at extending capabilities of Azure Service Fabric services in various ways. It’s focused on speed and performance, but also aims at delivering more capabilities build on top of the powerful Service Fabric foundations.

Services and actors

Service Fabric provides various capabilities. It can host processes and containers, enabling you to control resources usage on a much more granular level. You can host multiple processes on one VM, if they don’t require that much CPU/memory. Beside this, SF provides following models for writing your applications:

  • stateless services
  • stateful services
  • actor framework

These parts will be covered in the forthcoming posts.

Can we do more

I’ve spent some time reading official docs and decompiling Fabric sources and found that there are possibilities of building more on top of these strong foundations. Does more mean better? Possibly yes. I think that in Sewing Machine I’ll be able to allocate much less, pin no memory and use better serialization. This is low level. On a higher level I hope for:

  • event sourced actors
  • better use of secondary replicas (like using them for running processes etc)
  • multi actor projections

Journey, not path

Sewing Machine is in its initial phase. This is another journey project of mine as it’s a project based on uncovering what’s behind Service Fabric. I will share my findings in following blog posts and work towards reaching the aims above. This also means that version 1.0 is highly unlikely to be published within a month or two. For sure this journey will take a bit more and hopefully findings will be good enough to release it.

I hope you’re eager to do some sewing with this fabric. I am.

 

Async programming model

TL;DR

This is a follow up post to Async pump for better throughput in Azure. Please read the first before moving forward.

Feedback

I’ve been given a lot of feedback about my Async pump post. In a few cases this blog post from Ayende was quoted as it describes exactly the same approach. You can read the post, but more meaningful are comments provided by Kelly Sommers and Clemens Vasters.

The model

The await statement has simple semantics. It breaks your code and schedules the following part as a task continuation. This heavy lifting is done on the C# compiler level, so you don’t have to worry about. The model of this extension is simple: define a task and a continuation. Nothing more, nothing less. With my approach, that was a “trick”

That’s the premise of the “trick” that is allegedly achieving parallel execution of I/O and compute work here. That is, however, not the purpose of the asynchronous programming model and of the Windows IO completion port (IOCP) model. The point of IOCP is to efficiently offload IO work from user code to kernel and driver and hardware and not to bother the user code until the IO work is done

by Clemens Vasters

What it basically says is once you await on IO operation, your code that is run after, is scheduled on the IO thread

As IO typically takes very long and compute work is comparatively cheap, the goal of the IO system is to keep the thread count low (ideally one per core) and schedule all callbacks (and thus execution of interleaved user code) on that one thread. That means that, ideally, all work gets serialized and there minimal context switching as the OS scheduler owns the thread. The point of this model is ALREADY to keep the CPU nicely busy with parallel work while there is pending IO.

by Clemens Vasters

So with your code awaiting some IO, you’ll call IO, next the code after await is executed on the IO thread, as it’s assumed to be lightweight, another IO occurs, again the same thread dispatched the callback. With the async pump I proposed, comes a danger, as when we follow this partially awaitless approach the continuation code is

not on a .NET poll thread, but an IO pool thread. The strategy above sits on that IO pool thread past the point where you are supposed to return it (which is the next IO call) and thus you might force the IO pool to grow

by Clemens Vasters

Summary

Measure first. Think. Then think again. The “trick” worked in my case, improving performance for initialization of one app (I needed it in the beginning). Does it work in every case and should be used in general, I’d say no. It’s good to follow the programming model. If you don’t want to, you must have strong reasons for it.

Very good morning

TL;DR

This is one more personal post after my latest description of my learning pattern. I’d like to share how I start my morning and how it improved my overall quality of whole day.

Morning schedule

  • 5:45 AM – it’s time to get up and prepare and eat the oatmeal: oat flakes, a few raisins and hot water
  • 6:00 AM – by this time I’ve got my breakfast eaten and it’s reading time!
  • 7:00 AM – workout time (some weights or a stationary bike)
  • 7:50 AM – post workout shake
  • 7:55 AM – shower
  • 8:10 AM – I’m starting to waking up my child. It takes up to 15 minutes of playing music (sometimes singing is included)
  • 8:40 AM – ready for the kindergarten!
  • 9:00 AM – my second breakfast
  • 9:30 AM – with a cup of coffee I’m starting my work

I’ve been changing this schedule a little bit for 5 months now. The current version might be a subject to change, but the overall shape won’t change. Why?

Why so early?

I’ve always been an early bird, but a few months ago I noticed loosing focus. Keeping a morning like this, introduces some good routine/regime to my life and lets me maintain focus for my whole work day. Waking early means going to bed ~10-11PM, but occasionally I stay awake till the midnight (once in a while). Additionally, before I start my work, I got my workout, some reading, two breakfasts checked. This is a really good start!

Summary

Is this a way to go for everyone? Probably not. On the other hand, living in a world of interruptions and notifications does not improve our ability to focus. Having a morning like this – definitely does.

My learning pattern

TL;DR

This is a personal entry about my framework that I use subconsciously for learning new things. I’ve applied it recently when learning about Azure Service Fabric, and then I realized what I did. Next, I thought that it could be useful to share the way I learn new frameworks/tools/libraries. Are you focused?

Azure Service Fabric is the new black

Azure Service Fabric is a new environment for creating distributed apps for both, cloud and on-premise environments. It provides a lot of tooling with different behaviors (stateful, stateless services, actors). I’ve been working with Azure Storage Services for some time and two weeks ago I started learning Service Fabric. From zero (beside my distributed systems know-how and some exp with a public cloud).

Reading docs till you know it all

One may say that you don’t need to know everything about a technology to use it. Yes, you don’t. My aim is not to use a technology but to know it, to immerse into it, to embrace all the patterns behind it natively. Once I got it, it’s like a new language.

In case of Service Fabric it was simple. There is an official doco page which takes a few hours to read. I did it. In my opinion you truly want to learn vocabulary, patterns, properties to embrace one thing. That’s why I read some of the pages more than once.

F12 till it hurts

It wasn’t a point where I was ready to write my first program. I was following the framework code going with F12 (go to the implementation) as deep as possible. Sometimes it requires taking notes, but moves you forward with an amazing speed. That’s how I get the real knowledge how it works.

Write an extension, no dummy sample

The final step is to either send a PR or maybe write a simple extension to the framework. No mambo-jumbo playing with a dummy sample running one actor. I need to prove that foundations are strong. That’s how I rebuilt Actor’s part of the Service Fabric to be somewhat faster.

Summary

This is the approach I use for learning new technologies. Total immersion and getting as deep as possible, as soon as possible. Sometimes it hurts, but still, gains are tremendous!

Performance matters

TL;DR

This is a short follow-up post about Marten’s performance. It shows that saved allocations are not only about allocations and memory. It’s also about you CPU ticks, hence the speed of your library.

Moaaaaar performance !

Let me present you three pictures comparing performance before and after removing a lot of allocations. They were provided by Jeremy after benchmarking my PRs to Marten. My work was purely focused on allocations, but additionally, as shown below, it improved Marten’s speed of execution.

Events

The speed improvement isn’t that significant, but please take a look at the allocated bytes. Now it takes much less memory required before

events

Documents

The new insert is 10% faster and takes much less memory than before.

docs

Bulk inserts

Here, after enabling npgsql library to accept ArraySegment<char> I was able to reuse the same pooled writer. The new approach not only skips allocations but also leases a pooled writer only once. Just take a look at these numbers!

bulk_loading

Summary

When working on a library or a tool it’s good to think about performance and memory consumption. Even in a managed Garbage Collected world, using pooling for buffers or objects at all might not only reduce a memory consumption but also improve the overall speed of your creation.