ACID Properties
ACID is a set of properties of relational database transactions.- Atomicity - Each transaction is all or nothing
- Consistency - Any transaction will bring the database from one valid state to another
- Isolation - Executing transactions concurrently has the same results as if the transactions were executed serially
- Durability - Once a transaction has been committed, it will remain so
Replication Patterns
Master-slave replication
The master serves reads and writes, replicating writes to one or more slaves, which serve only reads. Slaves can also replicate to additional slaves in a tree-like fashion. If the master goes offline, the system can continue to operate in read-only mode until a slave is promoted to a master or a new master is provisioned.
Disadvantages: Master-slave replication
Disadvantages: Master-slave replication
Master-master replication
Both masters serve reads and writes and coordinate with each other on writes. If either master goes down, the system can continue to operate with both reads and writes.
Disadvantages: Master-master replication
Disadvantages: Master-master replication
General replication disadvantages
Further reading:Scaling Techniques
Federation

Disadvantages: Federation
Disadvantages: Federation
Sharding

Disadvantages: Sharding
Disadvantages: Sharding
Denormalization
Denormalization attempts to improve read performance at the expense of some write performance. Redundant copies of the data are written in multiple tables to avoid expensive joins. Some RDBMS such as PostgreSQL and Oracle support materialized views which handle the work of storing redundant information and keeping redundant copies consistent. Once data becomes distributed with techniques such as federation and sharding, managing joins across data centers further increases complexity. Denormalization might circumvent the need for such complex joins. In most systems, reads can heavily outnumber writes 100:1 or even 1000:1. A read resulting in a complex database join can be very expensive, spending a significant amount of time on disk operations.Disadvantages: Denormalization
Disadvantages: Denormalization
SQL Tuning
SQL tuning is a broad topic and many books have been written as reference. It’s important to benchmark and profile to simulate and uncover bottlenecks.- Benchmark - Simulate high-load situations with tools such as ab
- Profile - Enable tools such as the slow query log to help track performance issues
Tighten up the schema
Tighten up the schema
- MySQL dumps to disk in contiguous blocks for fast access
- Use
CHARinstead ofVARCHARfor fixed-length fieldsCHAReffectively allows for fast, random access, whereas withVARCHAR, you must find the end of a string before moving onto the next one
- Use
TEXTfor large blocks of text such as blog posts.TEXTalso allows for boolean searches. Using aTEXTfield results in storing a pointer on disk that is used to locate the text block - Use
INTfor larger numbers up to 2^32 or 4 billion - Use
DECIMALfor currency to avoid floating point representation errors - Avoid storing large
BLOBS, store the location of where to get the object instead VARCHAR(255)is the largest number of characters that can be counted in an 8 bit number, often maximizing the use of a byte in some RDBMS- Set the
NOT NULLconstraint where applicable to improve search performance
Use good indices
Use good indices
- Columns that you are querying (
SELECT,GROUP BY,ORDER BY,JOIN) could be faster with indices - Indices are usually represented as self-balancing B-tree that keeps data sorted and allows searches, sequential access, insertions, and deletions in logarithmic time
- Placing an index can keep the data in memory, requiring more space
- Writes could also be slower since the index also needs to be updated
- When loading large amounts of data, it might be faster to disable indices, load the data, then rebuild the indices
Avoid expensive joins
Avoid expensive joins
- Denormalize where performance demands it
Partition tables
Partition tables
- Break up a table by putting hot spots in a separate table to help keep it in memory
Tune the query cache
Tune the query cache
- In some cases, the query cache could lead to performance issues
