Band of brothers, building scalable social web apps on windows azure with mvc3, mongo db, rabbitmq

  • Published on

  • View

  • Download


The presentation will be deep dive into how to build scalable social web apps on Windows Azure IAAS by utilizing latest technologies based on document based storage


<ul><li>1.Band of Brothers, building scalable socialweb apps on Windows Azurewith ASP.NET MVC3, MongoDB, RabbitMQMarjan NikolovskiCo-Owner, Senior Software Engineer at EmitKnowledgeSenior Software Engineer at</li></ul> <p>2. Agenda Social web architecture Intro to MongoDB Data modeling for MongoDB Intro to RabbitMQ Preparing your guns for pub/sub with RabbitMQ Data painting with MVC3 Hosting and scaling strategies on Windows Azure 3. Social web architecture By definition a social web must provide connectivity betweenthe users for a common good From functional aspect a social web must provide: Connectivity (Relationships) Privacy Private messaging Notifications and events (both real-time and offline) Recommendations 4. Social web architecture Connectivity, how users are socializing Directional or bidirectional relations ? How are users going to socialize ? Post Sharing Groups Private messaging Gamification 5. Social web architecture Privacy Beware of details Who, When and WhatUserA User BUser CWho can access myY Ndata ?When can my data be After publish Delay with 1 dayaccessed ?What kind of data could Posts and eventsPostsbe accessed ? 6. Social web architecture Private messaging aka Conversations Define messaging types: One to One Group messaging Define messaging strategy When a new conversation starts ? Until when does it last ? Spam detection and prevention Mark as spam strategy Spam filtering strategy Both ? 7. Social web architecture Notifications and events (both real-time and offline) When to notify ? Frequency of notifications To stream or to aggregate? To filter or display all ? 8. Social web architecture Recommendations Define recommendation method: Friends method Recommends the friends of my friends Ease to implement Big miss factor Recommends people that we possibly know Not very practical Sharing interests method Recommend me a friend that we have a starting point to talk about Efficient for connectivity Needs initial user input for building the analytics 9. Social web architecture From technical aspect must respect: Stateless Pluggability Async execution Load distribution Intensive logging and auditing Content delivery strategy 10. Social web architecture Stateless Azure instance load balancing is your enemy We dont know on which instance the request will end (cloud load balancing) So, forget about using server side session or tempdata Yes we can store it in DB, but performance per request ? 11. Social web 12. Social web architecture Pluggability Enable the platform to go down partially Develop your functionalities as plugins Enable real-time plugin load/unload (hotplug) Partial deployment 13. Social web architecture 14. Social web architecture Async execution Sequential processing only at the minimum, everything elsedelegate to background services User registration use case User reset password use case User notification use case Time and data intensive operations must execute as backgroundservices jobs 15. Social web architecture Web serverMessaging infrastructureWorker servicesSend create user request Send confirmation email message User created response Notify for new user registration message 16. Social web architecture Load distribution We hit the processing limit per machine. Now what ? Develop your data and time intensive functionalities with job distribution in mind Event based Producer consumer jobs Each functionality requires Job orchestrator Job processor 17. Social web architectureNotify from 1 100Notify from 101 123Notify all of my friendsNotify all of my friendsNotify from 1 100New user postMESSAGING INFRASTRUCTURENotify from 101 123 18. Social web architecture Intensive logging and auditing You will always want to know what your users are up to Analytics cant help ! Put your intensive logging and auditing so you can playback later Log and audit everything you can get Build your own analytics system 19. Social web architecture{"Username" : "some_username","IsAuthenticated" : false,"RawUri" : "/Account/Login","BaseUri" : "/Account/Login","HttpMethod" : "POST","IpAddress" : "","Refferer" : "http://localhost:42378/","UserAgent" : "mozilla/5.0 (windows nt 6.1) applewebkit/535.12 (khtml, like gecko) maxthon/ safari/535.12","IsCrawler" : false,"IsSecureResource" : false,"Action" :"Emit.Knowledge.Controllers.RequestLogging.UserRequestLoggingController.VerifyUserCredentials(Stringusername, String password, User&amp; verifiedUser)","Data" : {"username" : "test333"},"IsFaulted" : false,"CreatedOn" : {"UtcDateTime" : new Date("3/10/2012 01:40:52")}} 20. Social web architecture Content delivery strategy Reference your scripts with build number .js?ver=1.0.1 to propagate script changes with ease Minify as many js scripts into one Group page images into sprites and add build number .png?ver=1.0.1 Reference user images to the CDN Analyze the average change of user profile image to determinethe image caching period 21. Intro to MongoDB Document store Fast, scalable and available JSon used for data hydratation Dynamic Stores data structured as documents instead of row as seen inRDBMS Uses Query, Insert, Update and Remove for data manipulation Used for adding data at high rates without choking the system 22. Intro to MongoDB Documents in MongoDB Each document can be of size max to 16mb Each document you insert into MongoDB will be assigned an idconsisted of: This is a 12-byte value consisting of 4 parts: timestamp (4 bytes) machine identifier (3 bytes) process id (2 bytes) increment (3 bytes) in MongoDB, each document will contain not only the values, but thekey names (~"column names" in relational db-speak) too. So if youhave keys: Username" and Password", then those alone will add 16bytes to each document. MongoDB automatically adds some padding to the documents toallow for documents to grow in size to try and reduce the need fordocuments to be moved around if they do grow 23. Data modeling for MongoDB Forget what youve learned at school and shift your mind Want to be fast ? Denormalize data that will be static(IAuditable ring any bells ?) Common pitfalls To render a post we need the post content and the username Modeling by the book will get you to model the Content entity tohave Title, Content, UserId Now we need to fire two queries to display the username 24. Data modeling for MongoDB Beware of your queries Analyze all of your queries Set index per query But dont forget the order by in the index Mongodb has limitation for OR queries and sorts (indexing willnot help you here) Think in map/reduce way maybe ? 25. Intro to RabbitMQ Robust and reliable messaging for apps Supports many messaging patterns Publish-subscribe Topic based PubSub Point-to-point Request-reply Store and forward 26. { Publish-subscribePublisherPublish an event Topic Consumes the event Consumes the eventSubscriber Subscriber 27. { Topic based PubSubTopicConsumes the eventConsumes the event SubscriberSubscriberConsumes the event Subscriber Subtopic Publish an event Publisher 28. { Point-to-pointPublisher Queue Subscriber 29. { Request-replySubscriber Publisher/Subscriber Topic SubscriberSubscriber 30. { Store and forward SubscriberPublisher/Subscriber Topic Subscriber Subscriber Data 31. Preparing your guns for pub/sub with RabbitMQ Establishing pub/sub architecture Who will listen on what ? Do we need to intercept messages ? Do we need to deliver one message to many subscribers ? Do we need to distribute the message processing ? Two levels of messaging Job coordination and distribution Server instancing coordination Messaging patterns of interests Pub/Sub Topic based Pub/Sub 32. Preparing your guns for pub/sub pub/sub withRabbitMQ Persist messages that must be delivered, everything else justflush it through the wire Topic subscription is your enemy Dont expand your topics tree in depth Minimize topic root subscriptions 33. Data painting with MVC3 Delegate data render to the client with dynamic data insteadon server side Prepare your static content and the templates in the views(this will help you to reduce the traffic. Dom will be created onfly.) Knockout your dynamic data 34. Data painting with MVC3 </p> <div>${LastEditedDate}</div> <p> {{/each}} 35. Data painting with MVC3 </p> <div> <div> <div>Title 1</div> <div>2012-01-01</div> </div> <div> <div>Title 2</div> <div>2012-01-02</div> </div> <div> <div>Title 3</div> <div>2012-01-03</div> </div></div> <p> 36. Data painting with MVC3Template + Ajax Server render638 bytes - JSON/ 2246 bytes - HTML template 22460 bytes - HTML TOTAL2884 bytes22460 bytes HTML 37. Hosting and scaling strategies on Windows Azure Design deployment and scaling strategy per component: Web server Data server Messaging server Worker server 38. Hosting and scaling strategies on Windows Azure When to scale ? MongoDB instances Indexes are too large to fit in memory First scale vertically with RAM then shard RabbitMQ instances Message delivery slows down Scale vertically with CPU then shard ASP.NET MVC instances Number of users goes large Check what is eating the machine throughtput Usually problem with the notifications long pooling Scale horizontally with more extra small or small instances Worker instances Job processing time goes large Scale horizontally with more extra small or small instances Round robin processing strategy will help you to relax the job processing 39. Hosting and scaling strategies on Windows Azure Two instances per type to meet with the SLA requirements Use CentOS for MongoDB, Linux has better memoryfragmentation than Windows For RabbitMQ choose the OS that you are most comfortablewith. Windows 2K8 OS for the ASP.NET MVC 40. Hosting and scaling strategies on Windows Azure Open SSH and MongoDB ports only Dont forget to disable anonymous login on the MongoDB andset u/p combinations for both server and database 41. Hosting and scaling strategies on Windows Azure Open SSH/Remote Desktop ports and RabbitMQcommunication port Disable anonymous connections to the instance and add u/pcombinations for connection to the RabbitMQ server </p>