Giter Site home page Giter Site logo

simplebank's Introduction

What is Deadlock?

二つのトランザクションが二つ以上の資源の確保をめぐって互いに相手を待つ状態となり、そこから先へ処理が進まなくなることを Deadlock(デッドロック)という。

Entity Relationship Diagram

EntityRelationshipDiagram

例えば、account1 から account2 へ $10 送金することを想定する。

作成されるレコードは下記の三つ。

  1. 【transfers】 from_account_id:1、to_account_id:2、amount:10 という値が入ったレコード
  2. 【entries】 account_id:1、amount:-10 という値が入ったレコード
  3. 【entries】 account_id:2、amount:10 という値が入ったレコード

最後に、account1 の残高を $10 減らし、account2 の残高を $10 増やし、送金が完了する。

Why do we need db transaction

  1. To provide a reliable and consistent unit of work, even in case of system failure.
  2. To provide isolation between programs that access the database concurrently

ACID Properties in DBMS

  1. Atomicity (Either the entire transaction takes place at once or doesn’t happen at all.)
  2. 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.)
  3. Isolation (Multiple transactions can occur concurrently without leading to the inconsistency of the database state)
  4. 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;

RESTful HTTP API in Go using Gin

Gin is an HTTP web framework written in Go that is immensely popular with over 50k stars on Github at the time of posting

少ないコード量で、バリデーションチェックを行うことができる。

What is Viper?

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

Why mock database?

  1. Independent tests isolate tests data to avoid conflicts

  2. Faster test reduce a lot of time taking to the database

  3. 100% coverage easily setup edge cases: unexpected errors

What's the problem of JWT?

  1. Weak algorithms Give developers too many algorithms to choose Some algorithms are known to be vulnerable

  2. Trivial Forgery Set "alg" header to "none"

simplebank's People

Contributors

naokiyazawa avatar

Watchers

 avatar

Forkers

egbordzor

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.