database story by devops

79
A Database story by DevOps exploring SQL Database in Production.

Upload: anton-martynenko

Post on 12-Jul-2015

426 views

Category:

Engineering


5 download

TRANSCRIPT

Page 1: Database story by DevOps

A Database story by DevOpsexploring SQL Database in Production.

Page 2: Database story by DevOps

Anton Martynenko

Dev & QA & OPS = DevOpsHead of Technical Operations at Youscan

.Net, Windows Azure, Sql Server, NoSql…

Twitter: @[email protected]

Page 3: Database story by DevOps

YouScan is a leader of the emerging social media monitoring & analytics market in Russia and Ukraine.

Our products:http://youscan.ruhttp://leadscanner.ru

Page 4: Database story by DevOps

The Reality:

Time is limited

Talk covers a lot of stuff

No time to go into details

If you have a question – make notes and ask later

Page 5: Database story by DevOps

The Production

Main DB

WWWAndroid

appService iOs app

Page 6: Database story by DevOps

The Production with more databases

Main DBClientsTools

WWWAndroid

appService iOs app

Page 7: Database story by DevOps

The Production

Main DB

WWWAndroid

appService iOs app

Page 8: Database story by DevOps

Database as a Black Box

WWWAndroid

appService iOs app

Page 9: Database story by DevOps

Database as a source of problems

Main DB

WWWAndroid

appService iOs app

Page 10: Database story by DevOps
Page 11: Database story by DevOps

Main DB

Firewall for production DB

WWWAndroid

appService iOs app

Page 12: Database story by DevOps

DevOps – Developer with access to Prod

Main DB

WWWAndroid

appService iOs app

Page 13: Database story by DevOps

Typical Database headaches

1.Performance

2.Whatever-SQL mess in DB

3.Maintenance

Main DB

Page 14: Database story by DevOps

Typical Database headaches

1.Performance

2.Whatever-SQL mess in DB

3.Maintenance

Main DB

Page 15: Database story by DevOps

Performance issues

Memory

Disk I/O bottlenecks

High CPU usage

Page 16: Database story by DevOps

Know your Production hardware!

- Memory

- Disk I/O

- CPU usage

Page 17: Database story by DevOps

Understand your load

Page 18: Database story by DevOps

Latencies are important

Page 19: Database story by DevOps

Understand Database “Math”

Tables

Indexes

Queries

Page 20: Database story by DevOps

Understand Transaction Isolation

Serializable

Repeatable reads

Read committed

Read uncommitted

Page 21: Database story by DevOps

Performance: User Experience

WWW

“Place order”

“Success”

Main DB

Page 22: Database story by DevOps

User’s Transaction is complex

WWW

“Place order”

“Success”

Main DB

Page 23: Database story by DevOps

User’s Transaction is complex

WWW

“Place order”

“Success”

Main DB

So Slow!

Page 24: Database story by DevOps

Performance: TOP I/O Queries

Page 25: Database story by DevOps

Performance: TOP CPU Queries

Page 26: Database story by DevOps

Google to find TOP IO/CPU Queries

Page 27: Database story by DevOps

Catch the bottleneck

CPU Score Query

13543541 SELECT * FROM Products WHERE …

7545314 SELECT * FROM Users where …

567541 INSERT INTO Reviews …

IO Score Query

68743513 SELECT * FROM Authors INNER JOIN...

1475214 INSERT INTO OrderQueue …

1246876 INSERT INTO SystemLogs …

Page 28: Database story by DevOps

Analyze Top IO Queries

Analyze Top CPU Queries

Page 29: Database story by DevOps

Understand your query

Page 30: Database story by DevOps

Understand indexes used by query

Page 31: Database story by DevOps

Understand execution plan

Page 32: Database story by DevOps

Slow “SELECT”

Missing Index

Suboptimal Query

Low Memory (or Big Index)

Low disk IOPS (or too many queries)

Page 33: Database story by DevOps

Slow “INSERT”, “UPDATE”

Too many Indexes

Suboptimal Query

Low disk IOPS (or too many inserts)

Page 34: Database story by DevOps

Understand the query

Page 35: Database story by DevOps

Catch!

Page 36: Database story by DevOps

Where is the problem?

Missing Index

Suboptimal Query

Low Memory (or Big Index)

Low disk IOPS (or too many queries)

Too many Indexes

Page 37: Database story by DevOps

Root cause: missing index

Missing Index

Suboptimal Query

Low Memory (or Big Index)

Low disk IOPS (or too many queries)

Too many Indexes

Review existing indexes Add new index Or change existing

index

Page 38: Database story by DevOps

Root cause: query

Missing Index

Suboptimal Query

Low Memory (or Big Index)

Low disk IOPS (or too many queries)

Too many Indexes

Understand execution plan Use Sql query parameters Do select/update minimal

field set

Page 39: Database story by DevOps

Root cause: low memory

Missing Index

Suboptimal Query

Low Memory (or Big Index)

Low disk IOPS (or too many queries)

Too many Indexes

Review fields in index Archive old data Check DB Engine config Add more memory

Page 40: Database story by DevOps

Root cause: low IOPS

Missing Index

Suboptimal Query

Low Memory (or Big Index)

Low disk IOPS (or too many queries)

Too many Indexes

Use partitioning Go to SSD Get rid of queries

Page 41: Database story by DevOps

Root cause: too many indexes

Missing Index

Suboptimal Query

Low Memory (or Big Index)

Low disk IOPS (or too many queries)

Too many Indexes Remove unused Remove features

Page 42: Database story by DevOps

Many indexes = many features

Remove uselessfeatures

Page 43: Database story by DevOps

Analyze user behavior

Page 44: Database story by DevOps

Big Table

Page 45: Database story by DevOps

Table Partitioning

Jan 2014

Feb 2014

Mar 2014

Apr 2014

May 2014

June 2014

Table Orders, All Data

Partitioned table Orders, Partition = Month

Page 46: Database story by DevOps

Database Sharding Multiple instances,Shard per country

Main DB

GB Main DB

DE Main DB

FR Main DB

Single instance,All Data

Page 47: Database story by DevOps

Multitenancy Multiple instances,Shard per client

Main DB

Coca Cola Main DB

BMWMain DB

ShellMain DB

Single instance,All Data

NestleMain DB

Macdonald’sMain DB

SiemensMain DB

Page 48: Database story by DevOps

DB License very expensive

Coca Cola Main DB

BMWMain DB

ShellMain DB

NestleMain DB

Macdonald’sMain DB

SiemensMain DB

Page 49: Database story by DevOps

Typical Database headaches

1.Performance

2.Whatever-SQL mess in DB

3.Maintenance

Main DB

Page 50: Database story by DevOps

Databases do a few things really well:

• They store and retrieve data

• They enforce relationships between data entities

• They provide the means to query the data for answers

Page 51: Database story by DevOps

T-SQL, PL-SQL, Whatever-SQL are poor as programming languages

Page 52: Database story by DevOps

SQL programming tools sux

Page 53: Database story by DevOps

Debugging and testing is not easy…

Typical T-SQL or PL-SQL mess in Database

Page 54: Database story by DevOps

What about Refucktoring your SQL code?

Typical T-SQL or PL-SQL mess in Database

Page 55: Database story by DevOps

Java/C#/etc. with modern tools

Typical T-SQL or PL-SQL mess in Database

Page 56: Database story by DevOps

WTF your code is doing in Database?

Page 57: Database story by DevOps

There are no reasons to put your Business Logic

in Database

Page 58: Database story by DevOps

Data -> Database

Data access -> DAL

Transaction -> (not sql)Code

Page 59: Database story by DevOps

Deadlocks

Page 60: Database story by DevOps

Complex code --> Deadlocks

Page 61: Database story by DevOps

ORM

ORM

Main DB

Application

Page 62: Database story by DevOps

In Legacy systems more often than notORM adds complexity to the system

Page 63: Database story by DevOps

ORM

ORM is magic

?

Main DB

Application

Page 64: Database story by DevOps

Main DB

Lightweight ORM

Application

Page 65: Database story by DevOps

Typical Database headaches

1.Performance

2.Whatever-SQL mess in DB

3.Maintenance

Main DB

Page 66: Database story by DevOps
Page 67: Database story by DevOps

Manage schema changes

Page 68: Database story by DevOps

Change = Script

Page 69: Database story by DevOps

Two-staged schema changes

Page 70: Database story by DevOps

Antipattern: Configuration in DB

WWWAndroid

appService iOs app

Page 71: Database story by DevOps

Antipattern: Lookup tables in DB

Page 72: Database story by DevOps

Antipattern: Queuing in Database

Page 73: Database story by DevOps

Keep Schema clean and simple

Get rid of configuration in DB

Get rid of lookup tables in DB

Get rid of write contention

Long term goals:

Page 74: Database story by DevOps

Database size

Page 75: Database story by DevOps

Restore/backup

10 minutes

1 Hour

12 Hours

X 3 days

Page 76: Database story by DevOps

Do Archive old data

Page 77: Database story by DevOps

Trust is foundation for everything

Page 78: Database story by DevOps

Thanks! Questions?

Page 79: Database story by DevOps

Me: https://www.linkedin.com/pub/anton-martynenko/10/289/9b5

YouScan - http://youscan.ru/LeadScanner - http://leadscanner.ru/

Monitoring & Metrics:Zabbix - http://www.zabbix.com/KissMetrics - https://www.kissmetrics.com/Kibana - http://www.elasticsearch.org/overview/kibana/