%3CLINGO-SUB%20id%3D%22lingo-sub-1752473%22%20slang%3D%22en-US%22%3ETroubleshooting%20Replication%20latency%20in%20Azure%20Database%20for%20MySQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1752473%22%20slang%3D%22en-US%22%3E%3CP%20class%3D%22lia-align-justify%22%3EUsing%20replication%20in%20a%20MySQL%20setup%2C%20allows%20to%20copy%20data%20from%20a%20master%20server%20to%20one%20or%20more%20MySQL%20replica%20servers.%20There%20are%20a%20couple%20of%20reasons%20of%20using%20MySQL%20replication%3A%3C%2FP%3E%0A%3CUL%20class%3D%22lia-align-justify%22%3E%0A%3CLI%3EIt%20spreads%20read%20load%20across%20replica%20servers%20for%20improving%20performance%3B%3C%2FLI%3E%0A%3CLI%3EEnhance%20business%20security%2C%20continuity%20and%20performance%20using%20cross-region%20read%20replica%3A%20the%20replica%20can%20be%20in%20a%20different%20region%20from%20the%20master%20server%3B%3C%2FLI%3E%0A%3CLI%3EBI%20and%20analytical%20workloads%20can%20be%20used%20on%20a%20replica%20server%20without%20affecting%20the%20performance%20of%20the%20master%20server.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%20class%3D%22lia-align-justify%22%3ERegardless%20of%20scenario%20and%20the%20number%20of%20%3CA%20title%3D%22Azure%20Database%20for%20MySQL%20read%20replicas%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fmysql%2Fconcepts-read-replicas%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Database%20for%20MySQL%20read%20replicas%3C%2FA%3E%20configured%2C%20since%20the%20replication%20is%20asynchronous%20there%20is%20a%20latency%20between%20master%20and%20replica%20server.%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20class%3D%22lia-align-justify%22%20id%3D%22toc-hId--1240010159%22%20id%3D%22toc-hId--1240010159%22%3EReplication%20latency%20due%20to%20IO%20or%20SQL%20errors%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-left%22%3EWhen%20the%20replication%20is%20started%20on%20the%20replica%20servers%202%20threads%20are%20running%3A%3C%2FP%3E%0A%3COL%20class%3D%22lia-align-justify%22%3E%0A%3CLI%20class%3D%22lia-align-left%22%3E%3CSTRONG%3EIO%20thread%3C%2FSTRONG%3E%20that%20connects%20to%20the%20master%20server%2C%20reads%20the%20binary%20logs%20and%20copies%20them%20to%20a%20local%20file%20that%20is%20called%20relay%20log%3B%3C%2FLI%3E%0A%3CLI%20class%3D%22lia-align-left%22%3E%3CSTRONG%3ESQL%20thread%3C%2FSTRONG%3E%20that%20reads%20the%20events%20from%20relay%20log%20and%20applies%20them%20to%20the%20database.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%20class%3D%22lia-align-justify%22%3EFirst%20step%20to%20troubleshoot%20the%20replication%20latency%20is%20to%20check%20if%20the%20latency%20is%20either%20due%20to%20IO%20or%20SQL%20thread.%20As%20such%20on%20the%20replica%20server%20must%20be%20executed%20the%20following%20statement%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESHOW%20SLAVE%20STATUS%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3EA%20typical%20output%20will%20look%20like%3A%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-center%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22show_slave.png%22%20style%3D%22width%3A%20869px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F224732i468DC5568A55D099%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22show_slave.png%22%20alt%3D%22show_slave.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-center%22%3EFigure%201.%20SHOW%20SLAVE%20STATUS%20output%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3EFrom%20above%20if%20Slave_IO_Running%3A%20Yes%20and%20Slave_SQL_Running%3A%20Yes%20then%20the%20replication%20is%20running%2C%20it%20is%20not%20broken.%20Shall%20be%20checked%20Last_IO_Errno%2FLast_IO_Error%20or%20Last_SQL_Errno%2FLast_SQL_Error%20that%20will%20hold%20the%20error%20number%20and%20error%20message%20of%20the%20most%20recent%20error%20that%20caused%20the%20SQL%20thread%20to%20stop.%20An%20error%20number%20of%200%20and%20empty%20message%20means%20there%20is%20no%20error.%20Any%20error%20number%20not%20equal%20with%200%2C%20must%20be%20troubleshooted%2C%20can%20be%20checked%20as%20well%20the%20%3CA%20href%3D%22https%3A%2F%2Fdev.mysql.com%2Fdoc%2Fmysql-errors%2F5.7%2Fen%2Fserver-error-reference.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EMySQL%20server%20error%20message%20reference%3C%2FA%3E%20for%20the%20given%20error%20number.%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20class%3D%22lia-align-justify%22%20id%3D%22toc-hId-1247502674%22%20id%3D%22toc-hId-1247502674%22%3EReplication%20Latency%20due%20to%20increase%20connections%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3EA%20sudden%20spike%20in%20the%20number%20of%20connections%20increases%20the%20CPU%20usage%20for%20the%20simple%20fact%20that%20connections%20must%20be%20processed.%20In%20a%20situation%20when%20there%20is%20already%20a%20workload%20on%20the%20Azure%20Database%20for%20MySQL%20replica%20server%20and%20a%20sudden%20spike%20of%20connections%20is%20occurring%20the%20CPU%20will%20have%20to%20process%20the%20new%20connections%20and%20as%20a%20result%20the%20replication%20latency%20can%20increase.%20In%20such%20case%20the%20first%20step%20is%20to%20leverage%20the%20connectivity%20by%20using%20a%20connection%20pooler%20like%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-mysql%2Fset-up-proxysql-clustering-to-connect-efficiently-to-azure%2Fba-p%2F1490357%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3EProxySQL%3C%2FA%3E.%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22before.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F224733iC9D82DFB460013DE%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22before.png%22%20alt%3D%22before.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-center%22%3EFigure%202.%26nbsp%3BReplication%20latency%20is%20increasing%20on%20new%20connections%20spike%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3EAs%20in%20most%20of%20the%20cases%20ProxySQL%20is%20implemented%20in%20a%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Faks%2Fscale-cluster%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAKS%20cluster%20with%20multiple%20nodes%3C%2FA%3E%20or%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fvirtual-machine-scale-sets%2Foverview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EVM%20scale%20sets%3C%2FA%3E%20to%20avoid%20a%20single%20point%20of%20failure%2C%20sometimes%20connections%20spike%20can%20still%20occur.%20Out%20of%20the%20box%20MySQL%20will%20assign%20a%20thread%20for%20each%20connection%2C%20hence%2C%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-mysql%2Fintroducing-thread-pools-in-azure-database-for-mysql-service%2Fba-p%2F1504173%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Ethread%20pools%3C%2FA%3E%20that%20is%20supported%20from%20MySQL%20version%205.7%20can%20be%20used.%20As%20such%2C%20we%20suggest%20enabling%20thread%20pooling%20on%20each%20MySQL%20replica%20server%20and%20properly%20configure%20%3CSTRONG%3Ethread_pool_min_threads%3C%2FSTRONG%3E%20and%20%3CSTRONG%3Ethread_pool_max_threads%3C%2FSTRONG%3E%20parameters%20based%20on%20the%20workload.%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3EBy%20default%2C%20thread_pool_max_threads%20%3D%2065535%20and%20this%20value%20simply%20will%20not%20gain%20any%20improvement%20as%20it%20will%20allow%20to%20create%20up%20till%2065535%20threads%2C%20which%20has%20no%20difference%20from%20the%20normal%20MySQL%20behavior.%20Instead%2C%20this%20parameter%20should%20be%20lowered%20to%20a%20value%20suitable%20for%20the%20workload.%20As%20an%20example%2C%20if%20this%20parameter%20is%20lowered%20to%20200%2C%20even%20though%20there%20can%20be%20a%20couple%20of%20thousands%20of%20new%20connections%2C%20thread%20pool%20will%20allow%20to%20be%20processed%20a%20maximum%20of%20200%20connection%20threads.%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3EAfter%20proper%20implementation%20of%20thread%20pools%20in%20Azure%20Database%20for%20MySQL%20replica%20servers%2C%20in%20case%20of%20a%20spike%20of%20connections%20the%20thread%20pool%20will%20limit%20the%20threads%20processed%20and%20the%20CPU%20will%20not%20increase%2C%20therefore%20replication%20will%20not%20be%20affected%20by%20the%20high%20number%20of%20new%20connections.%20We%20have%20seen%20big%20improvements%20in%20replication%20latency%20after%20implementing%20thread%20pool.%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22after.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F224734iCD0491B5A8A05033%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22after.png%22%20alt%3D%22after.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-center%22%3EFigure%203.%26nbsp%3BCPU%20and%20Replication%20latency%20not%20increasing%20on%20new%20connections%20spike%20(thread%20pool%20enabled)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3EOther%20keynote%20in%20here%20is%20that%20for%20enabling%20thread%20pools%20in%20%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fen-us%2Fservices%2Fmysql%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Database%20for%20MySQL%3C%2FA%3E%20parameter%20%3CSTRONG%3Ethread_handling%3C%2FSTRONG%3E%20must%20be%20changed%20from%20%E2%80%9Cone-thread-per-connection%E2%80%9D%20to%20%E2%80%9Cpool-of-threads%E2%80%9D%20and%20as%20this%20parameter%20is%20a%20static%20one%2C%20a%20server%20restart%20will%20be%20required%20for%20the%20change%20to%20apply.%20Although%20thread_pool_max_threads%20is%20a%20dynamic%20parameter%2C%20in%20a%20special%20case%20when%20reducing%20the%20value%2C%20for%20example%20from%201000%20to%20200%2C%20the%20MySQL%20server%20will%20not%20enlarge%20the%20thread%20pool%20furthermore%2C%20but%20also%20already%20existing%20threads%20will%20not%20be%20terminated%2C%20thus%20if%20the%20thread%20pool%20already%20contains%201000%20threads%2C%20a%20server%20restart%20will%20be%20needed%20to%20reflect%20the%20new%20settings.%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--559951789%22%20id%3D%22toc-hId--559951789%22%3EReplication%20latency%20due%20to%20no%20Primary%20or%20Unique%20key%20on%20a%20table%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3EAzure%20Database%20for%20MySQL%20uses%20Row-Based%20replication%2C%20meaning%20that%20the%20master%20server%20writes%20events%20to%20the%20binary%20log%20about%20individual%20table%20row%20change%2C%20while%20the%20SQL%20Thread%20from%20the%20replica%20server%20will%20execute%20those%20changes%20to%20the%20corresponding%20table%20rows.%20Having%20no%20primary%20or%20unique%20key%20on%20a%20table%20it%20is%20also%20a%20common%20cause%20of%20replication%20latency%2C%20as%20some%20DML%20queries%20will%20need%20to%20scan%20all%20table%20rows%20to%20apply%20the%20changes%20from%20SQL%20thread.%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3EIn%20MySQL%20the%20primary%20key%20is%20an%20associated%20index%20that%20ensures%20fast%20query%20performance%20as%20it%20cannot%20include%20NULL%20values.%20Also%2C%20for%20InnoDB%20storage%20engine%2C%20the%20table%20data%20is%20physically%20organized%20to%20do%20ultra-fast%20lookups%20and%20sorts%20based%20on%20the%20primary%20key%20column%20or%20columns.%20Therefore%2C%20to%20ensure%20that%20the%20replica%20is%20able%20to%20keep%20up%20with%20changes%20from%20the%20source%2C%20we%20recommend%20adding%20a%20primary%20key%20on%20tables%20in%20the%20source%20server%20before%20creating%20the%20replica%20server%20or%20re-creating%20the%20replica%20server%20if%20you%20already%20have%20one.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20class%3D%22lia-align-justify%22%20id%3D%22toc-hId-1927561044%22%20id%3D%22toc-hId-1927561044%22%3EReplication%20latency%20due%20to%20long%20running%20queries%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3EIt%20is%20possible%20that%20the%20workload%20on%20replica%20server%20to%20be%20the%20cause%20of%20the%20latency%20as%20the%20SQL%20thread%20cannot%20keep%20up%20with%20the%20IO%20thread.%20If%20it%20is%20the%20case%2C%20the%20slow%20query%20log%20should%20be%20enabled%20to%20help%20troubleshooting%20the%20issue.%20Slow%20queries%20can%20increase%20resource%20consumptions%20or%20slow%20down%20the%20server%2C%20thus%20replica%20will%20not%20be%20able%20to%20catch%20up%20with%20the%20master.%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3EOn%20the%20other%20hand%2C%20slow%20queries%20on%20master%20server%20can%20equally%20contribute%20to%20the%20latency%20as%20well.%20For%20instance%2C%20when%20a%20long%20running%20transaction%20on%20the%20master%20will%20complete%2C%20the%20data%20will%20be%20propagated%20to%20the%20replica%20server%2C%20but%20as%20it%20already%20took%20long%20to%20execute%20there%20will%20be%20already%20a%20delay.%20In%20such%20a%20case%20the%20latency%20is%20expected%2C%20thus%20long%20running%20queries%20should%20be%20identified%20and%20where%20possible%20to%20reduce%20the%20timings%20either%20by%20optimizing%20the%20query%20itself%20or%20breaking%20into%20smaller%20queries.%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20class%3D%22lia-align-justify%22%20id%3D%22toc-hId-120106581%22%20id%3D%22toc-hId-120106581%22%3EReplication%20latency%20due%20to%20DDL%20queries%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3ESome%20DDL%20queries%2C%20especially%20ALTER%20TABLE%20can%20require%20a%20lock%20on%20a%20table%2C%20therefore%20should%20be%20used%20with%20caution%20in%20a%20replication%20environment.%20Depending%20on%20the%20ALTER%20TABLE%20statement%20executed%2C%20it%20might%20allow%20concurrent%20DML%20operations%2C%20for%20more%20details%20check%20%3CA%20href%3D%22https%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F5.7%2Fen%2Finnodb-online-ddl-operations.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EMySQL%20Online%20DDL%20Operations%3C%2FA%3E.%20Also%2C%20some%20operations%20like%20CREATE%20INDEX%20will%20only%20finish%20when%20all%20transactions%20that%20accesses%20the%20table%20completed.%20In%20a%20replication%20scenario%20first%20the%20DDL%20command%20will%20be%20executed%20on%20the%20master%20server%20and%20only%20when%20completed%20will%20be%20propagated%20to%20the%20replica%20server(s).%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3EIn%20a%20heavy%20workload%20on%20both%20master%20and%20read%20replica%20servers%20an%20ALTER%20TABLE%20statement%20on%20the%20master%20server%20could%20take%20time%20to%20complete%20and%20the%20size%20of%20the%20table%20might%20be%20a%20contribution%20factor%20as%20well.%20When%20the%20operation%20completes%2C%20it%20will%20be%20propagated%20to%20the%20read%20replica%20server%20and%20depending%20on%20the%20ALTER%20statement%20it%20might%20require%20a%20lock%20on%20the%20table.%20In%20case%20a%20lock%20is%20required%2C%20the%20binary%20log%20will%20not%20be%20written%2C%20obviously%20the%20replication%20lag%20will%20start%20to%20increase%20from%20the%20moment%20the%20ALTER%20TABLE%20statement%20was%20propagated%20to%20the%20replica%20till%20the%20lock%20will%20be%20released.%20In%20such%20a%20case%2C%20the%20completion%20time%20will%20also%20depend%20on%20the%20workload%20on%20the%20replica%20or%20if%20there%20are%20long%20running%20transactions%20on%20the%20table%20that%20will%20increase%20the%20time%20furthermore.%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3EThough%20Index%20dropping%2C%20renaming%20and%20creation%20should%20use%20INPLACE%20algorithm%20for%20the%20ALTER%20TABLE%20it%20will%20avoid%20copying%20table%20data%2C%20but%20may%20rebuild%20the%20table.%20Typically%20for%20INPLACE%20algorithm%20concurrent%20DML%20are%20supported%2C%20but%20an%20exclusive%20metadata%20lock%20on%20the%20table%20may%20be%20taken%20briefly%20during%20preparation%20and%20execution%20phases%20of%20the%20operation.%20As%20such%2C%20for%20CREATE%20INDEX%20statement%20the%20clauses%20ALGORITHM%20and%20LOCK%20may%20be%20used%20to%20influence%20the%20table%20copying%20method%20and%20level%20of%20concurrency%20for%20reading%20and%20writing%2C%20nevertheless%20adding%20a%20FULLTEXT%20or%20SPATIAL%20index%20will%20still%20prevent%20DML%20operations.%20See%20below%20an%20example%20of%20creating%20an%20index%20with%20ALGORITHM%20and%20LOCK%20clauses%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3EALTER%20TABLE%20table_name%20ADD%20INDEX%20index_name%20(column)%2C%20ALGORITHM%3DINPLACE%2C%20LOCK%3DNONE%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3EUnfortunately%2C%20for%20DDL%20statement%20that%20requires%20a%20lock%2C%20replication%20latency%20cannot%20be%20avoided%2C%20instead%20these%20types%20of%20DDL%20operations%20should%20be%20performed%20when%20less%20load%20is%20given%2C%20for%20instance%20during%20nighttime%20to%20reduce%20potential%20customer%20impact.%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-justify%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1687347882%22%20id%3D%22toc-hId--1687347882%22%3EReplication%20latency%20due%20to%20replica%20server%20lower%20SKU%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20Azure%20Database%20for%20MySQL%20read%20replicas%20are%20created%20with%20the%20same%20server%20configuration%20as%20the%20master.%20The%20replica%20server%20configuration%20can%20be%20changed%20after%20it%20has%20been%20created.%20However%2C%20if%20the%20replica%20server%20will%20be%20downgraded%2C%20the%20workload%20can%20cause%20higher%20resource%20consumption%20that%20in%20turn%20can%20lead%20to%20replication%20latency.%20Therefore%2C%20it%20is%20recommended%20that%20the%20replica%20server's%20configuration%20should%20be%20kept%20at%20equal%20or%20greater%20values%20than%20the%20source%20to%20ensure%20the%20replica%20is%20able%20to%20keep%20up%20with%20the%20master.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPlease%20reach%20out%20to%20the%20Azure%20Database%20for%20MySQL%20team%20at%20%3CA%20href%3D%22mailto%3AAskAzureDBforMySQL%40service.microsoft.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAskAzureDBforMySQL%40service.microsoft.com%3C%2FA%3E%20if%20you%20have%20any%20questions%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1752473%22%20slang%3D%22en-US%22%3E%3CP%3EA%20couple%20of%20factors%20can%20impact%20on%20replication%20latency%20and%20in%20this%20article%20I%20will%20point%20out%20what%20to%20avoid%20and%20how%20to%20troubleshoot%20some%20replication%20issues.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1752473%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Emysql%20sql%20azure%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

Using replication in a MySQL setup, allows to copy data from a master server to one or more MySQL replica servers. There are a couple of reasons of using MySQL replication:

  • It spreads read load across replica servers for improving performance;
  • Enhance business security, continuity and performance using cross-region read replica: the replica can be in a different region from the master server;
  • BI and analytical workloads can be used on a replica server without affecting the performance of the master server.

Regardless of scenario and the number of Azure Database for MySQL read replicas configured, since the replication is asynchronous there is a latency between master and replica server. 

 

Replication latency due to IO or SQL errors

 

When the replication is started on the replica servers 2 threads are running:

  1. IO thread that connects to the master server, reads the binary logs and copies them to a local file that is called relay log;
  2. SQL thread that reads the events from relay log and applies them to the database.

First step to troubleshoot the replication latency is to check if the latency is either due to IO or SQL thread. As such on the replica server must be executed the following statement:

 

 

SHOW SLAVE STATUS

 

 

A typical output will look like:

show_slave.png

Figure 1. SHOW SLAVE STATUS output

 

From above if Slave_IO_Running: Yes and Slave_SQL_Running: Yes then the replication is running, it is not broken. Shall be checked Last_IO_Errno/Last_IO_Error or Last_SQL_Errno/Last_SQL_Error that will hold the error number and error message of the most recent error that caused the SQL thread to stop. An error number of 0 and empty message means there is no error. Any error number not equal with 0, must be troubleshooted, can be checked as well the MySQL server error message reference for the given error number.

 

Replication Latency due to increase connections

 

A sudden spike in the number of connections increases the CPU usage for the simple fact that connections must be processed. In a situation when there is already a workload on the Azure Database for MySQL replica server and a sudden spike of connections is occurring the CPU will have to process the new connections and as a result the replication latency can increase. In such case the first step is to leverage the connectivity by using a connection pooler like ProxySQL.

 

before.png

Figure 2. Replication latency is increasing on new connections spike

 

As in most of the cases ProxySQL is implemented in a AKS cluster with multiple nodes or VM scale sets to avoid a single point of failure, sometimes connections spike can still occur. Out of the box MySQL will assign a thread for each connection, hence, thread pools that is supported from MySQL version 5.7 can be used. As such, we suggest enabling thread pooling on each MySQL replica server and properly configure thread_pool_min_threads and thread_pool_max_threads parameters based on the workload.

 

By default, thread_pool_max_threads = 65535 and this value simply will not gain any improvement as it will allow to create up till 65535 threads, which has no difference from the normal MySQL behavior. Instead, this parameter should be lowered to a value suitable for the workload. As an example, if this parameter is lowered to 200, even though there can be a couple of thousands of new connections, thread pool will allow to be processed a maximum of 200 connection threads.

 

After proper implementation of thread pools in Azure Database for MySQL replica servers, in case of a spike of connections the thread pool will limit the threads processed and the CPU will not increase, therefore replication will not be affected by the high number of new connections. We have seen big improvements in replication latency after implementing thread pool.

 

after.png

Figure 3. CPU and Replication latency not increasing on new connections spike (thread pool enabled)

 

Other keynote in here is that for enabling thread pools in Azure Database for MySQL parameter thread_handling must be changed from “one-thread-per-connection” to “pool-of-threads” and as this parameter is a static one, a server restart will be required for the change to apply. Although thread_pool_max_threads is a dynamic parameter, in a special case when reducing the value, for example from 1000 to 200, the MySQL server will not enlarge the thread pool furthermore, but also already existing threads will not be terminated, thus if the thread pool already contains 1000 threads, a server restart will be needed to reflect the new settings.

 

Replication latency due to no Primary or Unique key on a table

 

Azure Database for MySQL uses Row-Based replication, meaning that the master server writes events to the binary log about individual table row change, while the SQL Thread from the replica server will execute those changes to the corresponding table rows. Having no primary or unique key on a table it is also a common cause of replication latency, as some DML queries will need to scan all table rows to apply the changes from SQL thread.

 

In MySQL the primary key is an associated index that ensures fast query performance as it cannot include NULL values. Also, for InnoDB storage engine, the table data is physically organized to do ultra-fast lookups and sorts based on the primary key column or columns. Therefore, to ensure that the replica is able to keep up with changes from the source, we recommend adding a primary key on tables in the source server before creating the replica server or re-creating the replica server if you already have one.

 

Replication latency due to long running queries

 

It is possible that the workload on replica server to be the cause of the latency as the SQL thread cannot keep up with the IO thread. If it is the case, the slow query log should be enabled to help troubleshooting the issue. Slow queries can increase resource consumptions or slow down the server, thus replica will not be able to catch up with the master. 

 

On the other hand, slow queries on master server can equally contribute to the latency as well. For instance, when a long running transaction on the master will complete, the data will be propagated to the replica server, but as it already took long to execute there will be already a delay. In such a case the latency is expected, thus long running queries should be identified and where possible to reduce the timings either by optimizing the query itself or breaking into smaller queries.

 

Replication latency due to DDL queries

 

Some DDL queries, especially ALTER TABLE can require a lock on a table, therefore should be used with caution in a replication environment. Depending on the ALTER TABLE statement executed, it might allow concurrent DML operations, for more details check MySQL Online DDL Operations. Also, some operations like CREATE INDEX will only finish when all transactions that accesses the table completed. In a replication scenario first the DDL command will be executed on the master server and only when completed will be propagated to the replica server(s).

 

In a heavy workload on both master and read replica servers an ALTER TABLE statement on the master server could take time to complete and the size of the table might be a contribution factor as well. When the operation completes, it will be propagated to the read replica server and depending on the ALTER statement it might require a lock on the table. In case a lock is required, the binary log will not be written, obviously the replication lag will start to increase from the moment the ALTER TABLE statement was propagated to the replica till the lock will be released. In such a case, the completion time will also depend on the workload on the replica or if there are long running transactions on the table that will increase the time furthermore.

 

Though Index dropping, renaming and creation should use INPLACE algorithm for the ALTER TABLE it will avoid copying table data, but may rebuild the table. Typically for INPLACE algorithm concurrent DML are supported, but an exclusive metadata lock on the table may be taken briefly during preparation and execution phases of the operation. As such, for CREATE INDEX statement the clauses ALGORITHM and LOCK may be used to influence the table copying method and level of concurrency for reading and writing, nevertheless adding a FULLTEXT or SPATIAL index will still prevent DML operations. See below an example of creating an index with ALGORITHM and LOCK clauses:

 

 

ALTER TABLE table_name ADD INDEX index_name (column), ALGORITHM=INPLACE, LOCK=NONE;

 

 

Unfortunately, for DDL statement that requires a lock, replication latency cannot be avoided, instead these types of DDL operations should be performed when less load is given, for instance during nighttime to reduce potential customer impact.

 

Replication latency due to replica server lower SKU

 

In Azure Database for MySQL read replicas are created with the same server configuration as the master. The replica server configuration can be changed after it has been created. However, if the replica server will be downgraded, the workload can cause higher resource consumption that in turn can lead to replication latency. Therefore, it is recommended that the replica server's configuration should be kept at equal or greater values than the source to ensure the replica is able to keep up with the master.

 

Please reach out to the Azure Database for MySQL team at AskAzureDBforMySQL@service.microsoft.com if you have any questions