how to write your database: the story about event store

60
HOW TO WRITE YOUR DATABASE The story about By Victor Haydin, Head of R&D, ELEKS

Upload: victor-haydin

Post on 25-May-2015

5.361 views

Category:

Technology


1 download

DESCRIPTION

This story is about distributed open-source database called Event Store (http://geteventstore.com). Event Store is developed by distributed team, part of which are ELEKS employees. I am going to talk about Event Store purpose and how it works, share some lessons we learned during the development and how it feels when you develop distributed high-performance system of that complexity. The talk will be interesting for technical people: software architects and engineers in general and .NET developers in particular as Event Store is written in C#.

TRANSCRIPT

Page 1: How to write your database: the story about Event Store

HOW TO WRITE YOUR

DATABASEThe story about

By Victor Haydin, Head of R&D, ELEKS

Page 2: How to write your database: the story about Event Store

Agenda

1.Problem definition2.Event Store: introduction3.Under the hood4.Lessons learned5.Q&A

Page 3: How to write your database: the story about Event Store

Few notes before we start1.This story is about the development, technology

and lessons learned, not a product2.Neither about Event Sourcing3.Outsourcing point of view4.I was there on pre-sales and initiation phases.

When the team was formed I left the project.

Page 4: How to write your database: the story about Event Store

Survey

Page 5: How to write your database: the story about Event Store

How many of you can understand code?

Page 6: How to write your database: the story about Event Store

Event Sourcing?

Page 7: How to write your database: the story about Event Store

Event Store?

Page 8: How to write your database: the story about Event Store

SEDA?

Page 9: How to write your database: the story about Event Store

140 characters long definition:

Event sourcing - persisting entities by appending all business events to transaction log. To rebuild the state, we replay this log.

Page 10: How to write your database: the story about Event Store

ExampleBankAccount

GotSallary: +100GotCashFromAtm: -10MadeInternetPayment: -15MadeTerminalPayment: -5PutCashToAccount: +20

Account balance: 100-10-15-5+20=90

Page 11: How to write your database: the story about Event Store

Event SourcingPros Cons

Performance Performance

Simplification Versioning

Audit Trail Querying

Integration with other systems Not common approach

Troubleshooting Better tastes with CQRS

Fixing errors

Testing

Flexibility

Page 12: How to write your database: the story about Event Store
Page 13: How to write your database: the story about Event Store

The marketing says

Page 14: How to write your database: the story about Event Store

and time-series data

Page 15: How to write your database: the story about Event Store

for single-node configuration

Page 16: How to write your database: the story about Event Store

AtomPub protocol for HTTP

Page 17: How to write your database: the story about Event Store

33000 writes per second on single node

Page 18: How to write your database: the story about Event Store

Master-Slave replication for Premium version

Page 19: How to write your database: the story about Event Store

I’ll talk more about Mono later

Page 20: How to write your database: the story about Event Store

web-based

Page 21: How to write your database: the story about Event Store

Client API1.Append to Stream2.Read Events3.Subscribe4.Transactions5.Stream Metadata6.Delete Stream7.System Settings8.Connection Lifecycle

Page 22: How to write your database: the story about Event Store

Under the hood

Page 23: How to write your database: the story about Event Store

Disclaimer

Page 24: How to write your database: the story about Event Store

SEDA

Page 25: How to write your database: the story about Event Store

Time to take a look at code

Page 26: How to write your database: the story about Event Store

SEDA outcome1.Very few locks in code2.Simple state management3.Easy to understand system composition4.Easy to extend5.Easy to monitor (DEMO)

Page 27: How to write your database: the story about Event Store

License is MIT, code is available on github. Enjoy!

Page 28: How to write your database: the story about Event Store

Storage Engine

Page 29: How to write your database: the story about Event Store

All events are stored in one transaction log (splitted into chunks)

Page 30: How to write your database: the story about Event Store

Writer Chaser Readers Scavenger

Storage pipeline

Page 31: How to write your database: the story about Event Store

Writer1.Single-threaded2.Append-only3.Write data in chunks4.Flush depending on load and settings

Page 32: How to write your database: the story about Event Store

Chaser1.Checks what writer has appended to log and

sends acknowledgements 2.Updates index

Page 33: How to write your database: the story about Event Store

Readers1.Read events from transaction log with help of

Read Index

Page 34: How to write your database: the story about Event Store

Read Index1.Maps stream name and version to position in

transaction log2.Index = MemTable + PTables3.MemTable = Dictionary of Lists4.Index entry – record of fixed length5.PTable = file with sorted sequence of entries

and cached midpoints6.Binary search FTW!

Page 35: How to write your database: the story about Event Store

Scavenger1.Background process that go through transaction

log chunks and clean it from obsolete data (e.g. deleted streams, $maxCount, $maxAge etc.)

2.Simply writes data to a new chunk file3.Last one out shut the lights off

Page 36: How to write your database: the story about Event Store

High Availability

Page 37: How to write your database: the story about Event Store

High Availability1.Master-Slave replication2.Automatic master elections (N/2 + 1 quorum)3.Wins the one with most actual state4.Write always to master, read from master or

slave, depending on client’s choice 5.Byte stream replication6.Available in commercial version

Page 38: How to write your database: the story about Event Store

Projections

Page 39: How to write your database: the story about Event Store

Projection – the process of taking an event stream and converting it to some other form (e.g. another event stream or state object)

Page 40: How to write your database: the story about Event Store

Projections Engine1.Built-in query language – Javascript2.Based on Google’s v8

Page 41: How to write your database: the story about Event Store

Demo: Projections Engine

Page 42: How to write your database: the story about Event Store

Lessons Learned

Page 43: How to write your database: the story about Event Store

Immutability is good

Page 44: How to write your database: the story about Event Store

Pure async is fast,although hard for development

Page 45: How to write your database: the story about Event Store

Lockless programming is hard

Page 46: How to write your database: the story about Event Store

Distributed programming is even harder

Page 47: How to write your database: the story about Event Store

Debugger is almost useless

Page 48: How to write your database: the story about Event Store

Unit-testing is a limited tool

Page 49: How to write your database: the story about Event Store

Verbose logs rock

Page 50: How to write your database: the story about Event Store

Real-life testingrocks like nothing else

Page 51: How to write your database: the story about Event Store
Page 52: How to write your database: the story about Event Store

Bugs in .NET[System.Security.SecuritySafeCritical]public virtual void Flush(Boolean flushToDisk) {

// This code is duplicated in Disposeif (_handle.IsClosed) __Error.FileNotOpen();if (_writePos > 0) {FlushWrite(false);if (flushToDisk) {

if (!Win32Native.FlushFileBuffers(_handle)) {__Error.WinIOError();

}}

}else if (_readPos < _readLen && CanSeek) {FlushRead();

}_readPos = 0;_readLen = 0;

}

*Appears to be fixed in 4.5

Page 53: How to write your database: the story about Event Store

Bugs in Mono

TCP/IP Stack deadlocks Concurrent Stack/Queue

XML Serializer

File Stream issuesGeneral slowness:

20K w/s

Page 54: How to write your database: the story about Event Store

Disk caches: fake flush and reordering

*By default, can be disabled

Page 55: How to write your database: the story about Event Store

Mind the HDD/SSD difference

*It is possible to optimize for both

Page 56: How to write your database: the story about Event Store

There is a long way to simplicity

Page 57: How to write your database: the story about Event Store

Experiments:The way it works

Page 58: How to write your database: the story about Event Store

Further plans1.Horizontal scalability2.Hosted service3.Adding other features

Page 59: How to write your database: the story about Event Store

Links1. GetEventStore.com – official web site

2. GitHub.com/EventStore/EventStore – source code

3. OreDev.org/2012/sessions/a-deep-look-into-the-event-store – A deep look into The Event Store by Greg Young

4. MartinFowler.com/eaaDev/EventSourcing.html – Event Sourcing overview by Martin Fowler

5. Google <Event Sourcing|Event Store>

Page 60: How to write your database: the story about Event Store

Got a question?Ask!

@victor_haydin

linkedin.com/in/victorhaydin

[email protected]