Designing an alternative to Replication

Slides:



Advertisements
Similar presentations
Mecanismos de alta disponibilidad con Microsoft SQL Server 2008 Por: ISC Lenin López Fernández de Lara.
Advertisements

SQL Server Replication
Moving data using replication Anthony Brown
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Developing Asynchronous Database Applications with SQL Server 2005 Service Broker Noah Subrin Technical Lead SRA International.
Working with SQL and PL/SQL/ Session 1 / 1 of 27 SQL Server Architecture.
Service Broker Lesson 11. Skills Matrix Service Broker Service Broker, provides a solution to common problems with message delivery and consistency that.
Module 9: SQL Server 2005 Replication. Overview Overview of Replication Enhancements New Types of Replication Configuring Replication.
NovaBACKUP 10 xSP Technical Training By: Nathan Fouarge
SQL Server Replication By Karthick P.K Technical Lead, Microsoft SQL Server.
Database Technical Session By: Prof. Adarsh Patel.
LiveCycle Data Services Introduction Part 2. Part 2? This is the second in our series on LiveCycle Data Services. If you missed our first presentation,
Module 2: Creating Efficient Replication Solutions.
(Business) Process Centric Exchanges
Triggers A Quick Reference and Summary BIT 275. Triggers SQL code permits you to access only one table for an INSERT, UPDATE, or DELETE statement. The.
Module 1: Exploring Replication. Overview Understanding SQL Server Replication Setting Up Replication Understanding Agents in Replication Securing Replication.
Siebel 8.0 Module 5: EIM Processing Integrating Siebel Applications.
Oracle Data Integrator Procedures, Advanced Workflows.
Overview – Chapter 11 SQL 710 Overview of Replication
1 Chapter Overview Introducing Replication Planning for Replication Implementing Replication Monitoring and Administering Replication.
Module 11: Introducing Replication. Overview Introduction to Distributed Data Introduction to SQL Server Replication SQL Server Replication Agents SQL.
Triggers. Why Triggers ? Suppose a warehouse wishes to maintain a minimum inventory of each item. Number of items kept in items table Items(name, number,...)
Chapter 15: Achieving High Availability Through Replication.
 Replication is the process of copying database information  Replication is used for:  Backing up your database  Migrating to a new server  Mirroring.
SQL Server 2005 Implementation and Maintenance Chapter 12: Achieving High Availability Through Replication.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Master Data Management & Microsoft Master Data Services Presented By: Jeff Prom Data Architect MCTS - Business Intelligence (2008), Admin (2008), Developer.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Module 5: Implementing Merge Replication. Overview Understanding Merge Replication Architecture Implementing Conflict Resolution Planning and Deploying.
1 Intro stored procedures Declaring parameters Using in a sproc Intro to transactions Concurrency control & recovery States of transactions Desirable.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Ch 12. Replication. Replication Place copies of data to a different location Use: Reduce locking conflict when multiple sites want to work on same set.
SQL Triggers, Functions & Stored Procedures Programming Operations.
Making Sense of Service Broker Inside the Black Box.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
11 Copyright © 2009, Oracle. All rights reserved. Enhancing ETL Performance.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
SQL Database Management
Platform and Data Migration With Little Downtime
SQL Replication for RCSQL Reporting
With Temporal Tables and More
Core LIMS Training: Project Management
Visual Studio Database Tools (aka SQL Server Data Tools)
Katowice,
SQL Replication for RCSQL 4.5
Transactional Replication Deep Dive
Distributed Database Management Systems
Tables and Triggers.
Glasgow, SQL Server Meetup
Service Broker - Put Me In Coach
Brad Bird System Center & VM Architect Cistel Technology Inc.
Getting Started With SQL Server Replication
A Technical Overview of Microsoft® SQL Server™ 2005 High Availability Beta 2 Matthew Stephen IT Pro Evangelist (SQL Server)
Isolation Levels Understanding Transaction Temper Tantrums
Optimizing Microsoft SQL Server 2008 Applications Using Table Valued Parameters, XML, and MERGE
Auditing in SQL Server 2008 DBA-364-M
Transactional Replication A Deeper Dive Drew Furgiuele, Senior DBA IGS
Traveling in time with SQL Server 2017
Making Sense of Service Broker
Visual Studio Database Tools (aka SQL Server Data Tools)
Fundamentals of Databases
Cloud Data Replication with SQL Data Sync
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Message Queuing Telemetry Transport (Internet of Things)
Relational Database Design
Chapter 11 Managing Databases with SQL Server 2000
Prof. Arfaoui. COM390 Chapter 9
Isolation Levels Understanding Transaction Temper Tantrums
Presentation transcript:

Designing an alternative to Replication A Real World™ Example

About me (Ben Thul) Database professional since 2006 Database Engineer at SurveyMonkey e: ben.thul@gmail.com

Replication Everyone’s Least Favorite Workhorse

REPLICATION Publisher Distributor Subscribers Has one or more Articles DML/DDL against any of the Articles is marked in the transaction log Distributor Runs Log Reader Agent to consume marked records from Publisher, inserts them into Distribution database Subscribers Receives results of actions run at Publisher, replays them at each Subscriber (either via Push or Pull) Picture from: https://technet.microsoft.com/en-us/library/ms151706(v=sql.105).aspx

Replication Pros Cons Has been around for a long time When it works, it works In most cases, fairly simple to set up/configure Has a lot of options for configuration if you need them A robust system for initializing subscribers Cons Has a reputation for being ‘fragile’ Implications for backup/restore strategy DML statement affects 1000 rows at publisher → 1000 individual commands to be run at subscriber Default configuration stops distribution on any error (including trying to delete a non- existent row!) Built-in monitoring isn’t great

Service Broker A new way to solve old problems

Service Broker Service Broker sends messages from one location to one or more other locations Has support for conversations Service A sends a message to service B and it responds Conversations can go back and forth as many times as is necessary

Service Broker In order to build a replication system on top of this, we need A way to generate messages when events happen to Articles at the Publisher A way for subscribers to receive and replay those events How hard could it be?

Configuration

Configuration Before we can talk about pushing messages from one place to another, there’s some plumbing we need to do Tell each side about where the other side is (i.e. routes) Tell each side what types of messages it can expect (i.e. message types, contracts) If the Publisher and Subscriber are on different servers, configure authentication between them

Configuration (Routing) Service Broker routing can wildcard on any of: Service name Broker instance (sys.databases.service_broker_guid) In our implementation, both Service name and Broker instance are specified Routing is used to determine what server the destination service is on when the conversation is initiated and what database to deliver the message to once it gets there.

Configuration (Message Types) Each side of the conversation has to know what types of messages are to be sent Message types are merely a name and what type of validation is done on the message (either NONE, WELL_FORMED_XML, or VALID_XML (validated against an XML schema) Message Type is specified when a SEND statement is issued Message types are bundled into Contracts Each Message Type in a Contract is specified as being able to be sent by either the initiator of the conversation, the target, or either. Contract name is specified when a BEGIN DIALOG statement is issued

Configuration (Endpoints) If the two Services are in databases that are on separate servers, a Service Broker endpoint will need to be set up on both the Publisher and Subscriber Authentication can be any of Windows (NTLM) Window (Kerberos) Certificate (both servers share a common server-level cert)

Publication

Publication Each table considered for replication via Service Broker has a trigger on it. The trigger Examines the DML operation performed (Insert, Update, Delete) Creates an XML message to represent the change using the UPDATE() and COLUMNS_UPDATED() functions Sends the message

Publication “Sending the message“ warrants further discussion In a Microsoft whitepaper*, they suggest re-using conversation handles and also to use ≈ 1 in 150 of them In order to abstract this from the application, procedures were made to create handles and send messages using these stored conversation handles * https://technet.microsoft.com/en-us/library/dd576261(v=sql.100).aspx

Publication The first implementation of this used a naïve round-robin method We encountered blocking on the conversation handle if anything did a DML and didn’t commit right away We moved to a READPAST strategy where the select from the handles table just gets a handle that has the right contract type and isn’t locked right now We try this five times and if we still haven’t gotten a handle, then resign ourselves to the round-robin strategy

Publication DEMO

Subscription

Subscription In the subscriber database, messages are processed via Service Broker Activation A background process determines when to start a stored procedure to process messages Multiple “activated procedures” can be running at once (up to a user-configured maximum)

Subscription The Activation procedure Grabs a bunch of messages off of the queue Classifies each message by what table the message affects Passes the message to a procedure that only processes messages for that table

Subscription Each table-specific procedure Parses the message back out into its constituent columns Parses the update mask (generated by COLUMNS_UPDATED() in the trigger In the case of an insert or delete does the simple operation (i.e. insert all the columns or delete by primary key) In the case of an update, interprets the update mask to determine which columns need to be updated

Subscription DEMO

caveats Or “Lessons Learned”

Caveats (Text Columns) Any columns that admit arbitrary text can produce a message that will not be replayed correctly at the subscriber e.g. “I <3 SQL” will become “I <3 SQL” As a workaround, (var)(n)char columns are cast to an equivalent length (var)binary type which is then base-64 encoded with the BINARY BASE64 clause to FOR XML The subscriber also needs to take steps to base-64 decode this information and then produce the original value from the publisher

CAVEATS (Update Mask) The first version of the function that decodes the update mask used sys.columns at the subscriber to turn the bit mask into column names If the columns were enumerated differently at the publisher and the subscriber, the message would be applied incorrectly How does this happen? In development: Add two columns at the publisher Remove the first of the two Add the second at the subscriber

CAVEATS (Update Mask) As a way around this, a copy of the publisher enumeration of the columns for replicated tables is sent to the subscriber so that it can be referenced when the bitmask is decoded This is also accomplished via sending a message periodically via Service Broker

Caveats (Message Replay Order) Our implementation uses Broker Prioritization to say that some messages should be consumed before others For the purposes of this discussion, prioritization is done at a Contract level (i.e. each Contract can be given a higher or lower priority) Initially, prioritization was done at the table level This had a side effect of updates occasionally being consumed before their corresponding insert Which leads to “non-convergence”

Caveats (Message Replay Order) To get around this, we reorganized the contracts into logical operations (i.e. a contract for each of insert, update, and delete) Inserts are prioritized highest Deletes are next highest priority Deletes are rare in our environment Updates are the lowest priority

Resources Code can be downloaded from GitHub https://github.com/ben-thul/BrokerReplication