dejan pekter / nordeus – reactor design pattern

27
Reactor pattern Event handling pattern Dejan Pekter Software Development Engineer II

Upload: conversionmeetup

Post on 07-Jul-2015

284 views

Category:

Software


0 download

DESCRIPTION

Presentation on reactor design pattern should introduce you a pattern that our server’s core is based on. We will try to give away all pros and cons about it. This pattern is used to simplify development of highly concurrent servers by separating business logic from communication. This is very technical presentation, but it is designed for beginners so it should be understandable to anyone with basic software engineering skills.

TRANSCRIPT

Page 1: Dejan Pekter / Nordeus – Reactor design pattern

Reactor patternEvent handling pattern

Dejan PekterSoftware Development Engineer II

Page 2: Dejan Pekter / Nordeus – Reactor design pattern

Nordeus and me

● Over 150 employees● Top Eleven

○ ~ 12 000 000 MAU○ ~ 5 000 000 DAU○ Cross-platform pioneer ○ Most popular online sports game

● Backend Software Development Engineer○ 250+ servers○ 10+ types○ 70.000 requests / minute○ Every fail is expected

Page 3: Dejan Pekter / Nordeus – Reactor design pattern

Goal

● Share knowledge on Reactor design pattern used on our servers

● In example of server that is ○ Highly concurrent○ Highly responsive○ Well designed

● Consider different solutions○ Incrementally find solution○ Pros○ Cons

Page 4: Dejan Pekter / Nordeus – Reactor design pattern

Prerequisites

● Design pattern ● Thread● Context switch● Request● Response

Page 5: Dejan Pekter / Nordeus – Reactor design pattern

Easy solution

● Wait for clients to connect● For every accepted client

○ Create thread which will take responsibility over that client

Page 6: Dejan Pekter / Nordeus – Reactor design pattern

Easy solution - conclusion

● Good:○ Intuitive○ Readable code○ Easy to understand

● Bad:○ 1000 users ~ 1000 threads○ Memory overhead○ Context switch overhead○ Create/kill overhead

Page 7: Dejan Pekter / Nordeus – Reactor design pattern

Improved solution

● Create thread pool with N threads● Accept client

○ Pass client to thread pool ○ Thread pool assigns thread to that client

Page 8: Dejan Pekter / Nordeus – Reactor design pattern

Improved solution - conclusion

● Good:○ Constrained number of threads○ Threads are reused

● Bad:○ Harder to implement○ Input/output is mixed with business logic○ Possibility of starvation

Page 9: Dejan Pekter / Nordeus – Reactor design pattern

Basic nuclear reactor theory

Page 10: Dejan Pekter / Nordeus – Reactor design pattern

NO! Re-act!

Page 11: Dejan Pekter / Nordeus – Reactor design pattern

How would you react on good joke (event) that I can’t really tell?

Page 12: Dejan Pekter / Nordeus – Reactor design pattern

FunnyThingHandler

Page 13: Dejan Pekter / Nordeus – Reactor design pattern

How would you react onLOGIN_OR_REGISTER

Page 14: Dejan Pekter / Nordeus – Reactor design pattern

You would not!Server would.

Page 15: Dejan Pekter / Nordeus – Reactor design pattern

Event granulation

CLIENT MESSAGE

Page 16: Dejan Pekter / Nordeus – Reactor design pattern

Reactor pattern

Reactor

Events

Callbacks

Page 17: Dejan Pekter / Nordeus – Reactor design pattern

Event handler

EventHandler

+ handleEvent()+ getEventId()

LoginHandler

+ handleEvent()+ getEventId()

RegisterHandler

+ handleEvent()+ getEventId()

Page 18: Dejan Pekter / Nordeus – Reactor design pattern

Reactor detailed design

EventHandler

+ handleEvent()+ getEventId()

LoginHandler

+ handleEvent()+ getEventId()

RegisterHandler

+ handleEvent()+ getEventId()

Server

Reactor

+ handleEvents()+ registerHandler()+ removeHandler()

dispatches 1 *

creates

Communicator

+ getEvents()

1

1

Page 19: Dejan Pekter / Nordeus – Reactor design pattern

Reactor phases

● Initialize phase○ Registration of event handlers

● Event handling phase○ Gather all ready messages○ Process all messages

■ Get requests■ Identify event handler■ Execute callback

Page 20: Dejan Pekter / Nordeus – Reactor design pattern

Reactor - Sequence Diagram

Main Program Event Handler Reactor

EventsregisterHandler()

instantiateHandler()

handleEvents()

handleEvent()

1) Initialize phase

2) Event handling phase

concreteHandler

event

Page 21: Dejan Pekter / Nordeus – Reactor design pattern

Reactor solution - conclusion

● Good○ Logic is separated○ Easy to add new event handler○ Granulation to message level

● Bad○ Harder to debug○ Not intuitive○ Single threaded

Page 22: Dejan Pekter / Nordeus – Reactor design pattern

Our improvements

● One reactor is not enough N reactors

● Separate logic even more○ Server - single thread

■ Accept clients ■ Dispatch to reactor by round robin

○ Reactor■ IO responsibility■ Build application level messages■ Find event handler■ send to dispatcher

○ Dispatcher - thread pool■ Execute callback

Page 23: Dejan Pekter / Nordeus – Reactor design pattern

Our improvements - conclusion

● Good○ Responsibilities are fully separated○ 70.000 requests per minute in production○ Tested even for order of magnitude more○ Good design

● Bad○ Even harder debugging

Page 24: Dejan Pekter / Nordeus – Reactor design pattern

Conclusion

● Reactor pattern○ Great maintainability○ Forget about how messages arrive, they just do○ Single threaded by definition○ Multithreaded in production

● Business○ Invest time and money in good solution○ You get new stuff almost for free

Page 25: Dejan Pekter / Nordeus – Reactor design pattern

Remember...

● Don’t reinvent the wheel● Got the wheel, but you need a plane

○ think○ improve○ build it yourself

Page 26: Dejan Pekter / Nordeus – Reactor design pattern

Don’t forget to...WORK AND PLAY

Page 27: Dejan Pekter / Nordeus – Reactor design pattern

Thanks for your attention!Questions?