二つのトランザクションが二つ以上の資源の確保をめぐって互いに相手を待つ状態となり、そこから先へ処理が進まなくなることを Deadlock(デッドロック)という。
例えば、account1 から account2 へ $10 送金することを想定する。
作成されるレコードは下記の三つ。
- 【transfers】 from_account_id:1、to_account_id:2、amount:10 という値が入ったレコード
- 【entries】 account_id:1、amount:-10 という値が入ったレコード
- 【entries】 account_id:2、amount:10 という値が入ったレコード
最後に、account1 の残高を $10 減らし、account2 の残高を $10 増やし、送金が完了する。
- To provide a reliable and consistent unit of work, even in case of system failure.
- To provide isolation between programs that access the database concurrently
- Atomicity (Either the entire transaction takes place at once or doesn’t happen at all.)
- Consistency (Integrity constraints must be maintained so that the database is consistent before and after the transaction. It refers to the correctness of a database.)
- Isolation (Multiple transactions can occur concurrently without leading to the inconsistency of the database state)
- Durability (This property ensures that once the transaction has completed execution, the updates and modifications to the database are stored in and written to disk and they persist even if a system failure occurs. These updates now become permanent and are stored in non-volatile memory. The effects of the transaction, thus, are never lost.)
BEGIN;
INSERT INTO transfers (from_account_id, to_account_id, amount) VALUES (1, 2, 10) RETURNING *;
-- 1つのテーブルへのINSERTが他のテーブルからのSELECTをブロックできるのはなぜか。
-- 外部キー制約があるから
-- 「FOR NO KEY UPDATE;」とする
-- name: GetAccountForUpdate :one
-- SELECT * FROM accounts
-- WHERE id = $1 LIMIT 1
-- FOR NO KEY UPDATE;
INSERT INTO entries (account_id, amount) VALUES (1, -10) RETURNING *;
INSERT INTO entries (account_id, amount) VALUES (2, 10) RETURNING *;
SELECT * FROM accounts WHERE id = 1 FOR UPDATE;
UPDATE accounts SET balance = 90 WHERE id = 1 RETURNING *;
SELECT * FROM accounts WHERE id = 2 FOR UPDATE;
UPDATE accounts SET balance = 110 WHERE id = 2 RETURNING *;
ROLLBACK;
Gin is an HTTP web framework written in Go that is immensely popular with over 50k stars on Github at the time of posting
少ないコード量で、バリデーションチェックを行うことができる。
Viper is a complete configuration solution for Go applications including 12-Factor apps. It is designed to work within an application, and can handle all types of configuration needs and formats
-
Independent tests isolate tests data to avoid conflicts
-
Faster test reduce a lot of time taking to the database
-
100% coverage easily setup edge cases: unexpected errors
-
Weak algorithms Give developers too many algorithms to choose Some algorithms are known to be vulnerable
-
Trivial Forgery Set "alg" header to "none"