Changes between Version 2 and Version 3 of P8


Ignore:
Timestamp:
01/13/26 18:24:05 (7 days ago)
Author:
193284
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • P8

    v2 v3  
    77In our Wedding Planner prototype (Flask REST + SQLite), the same principles apply even though we use a lightweight database. In a production system, the application would typically use PostgreSQL/MySQL and pooling would be mandatory.
    88
     9== What we cover in this phase ==
     10In this phase we focus on:
     11 * What a database transaction is and why it is needed
     12 * COMMIT vs ROLLBACK
     13 * Isolation and concurrency problems (dirty reads, lost updates, etc.)
     14 * How transactions apply to our Wedding Planner scenarios (bookings, RSVP, conflicts)
     15 * What connection pooling is and why it improves performance
     16 * How pooling would be used in a real version of our application
     17
    918== Transactions in databases ==
    1019
    1120=== What is a transaction? ===
    1221A database transaction is a group of operations that must execute as a single unit.
    13 The transaction is considered successful only if all operations succeed.
    14 
    15 In practice, transactions are needed whenever multiple inserts/updates must happen together, without leaving the database in an inconsistent state.
    16 
    17 
    18 === Why are transactions important? ===
     22The transaction is successful only if all operations succeed.
     23
     24Transactions are needed whenever multiple inserts/updates must happen together, without leaving the database in an inconsistent state.
     25
     26=== Why are transactions important? (ACID) ===
    1927Transactions ensure:
    20 
    21 Atomicity → all operations succeed or none are applied
    22 
    23 Consistency → data constraints remain valid
    24 
    25 Isolation → concurrent operations do not corrupt data
    26 
    27 Durability → once committed, changes remain stored
     28 * Atomicity – all operations succeed or none are applied
     29 * Consistency – data constraints remain valid
     30 * Isolation – concurrent operations do not corrupt data
     31 * Durability – once committed, changes remain stored
    2832
    2933This is commonly summarized as the ACID principles.
    30 
    3134
    3235=== COMMIT and ROLLBACK ===
    3336A transaction typically works like this:
    34 
    35 BEGIN → start transaction
    36 
    37 Execute multiple SQL operations
    38 
    39 COMMIT → save changes permanently if everything succeeded
    40 
    41 ROLLBACK → undo everything if an error occurs
     37 * BEGIN – start transaction
     38 * Execute multiple SQL operations
     39 * COMMIT – save changes permanently if everything succeeded
     40 * ROLLBACK – undo everything if an error occurs
    4241
    4342Example (SQL pseudocode):
     
    5049
    5150COMMIT;
    52 -- If any query fails → ROLLBACK;
    53 }}}
    54 
     51}}}
     52
     53If an error happens during the process, the correct behavior is:
     54
     55{{{
     56ROLLBACK;
     57}}}
    5558
    5659=== Transaction example connected to our project ===
    5760
    5861==== Scenario: Venue booking confirmation ====
    59 In the Wedding Planner application, when the user makes a venue booking we must:
    60 
    61 insert the booking record
    62 
    63 update the wedding to reflect the new booking
    64 
    65 ensure no conflicting booking exists
     62In the Wedding Planner application, when the user books a venue we must:
     63 * insert the booking record
     64 * update wedding information
     65 * ensure there is no overlapping booking for the same venue
    6666
    6767If one step fails, the database must not store partial data.
     
    7676Insert venue_booking
    7777
    78 Update wedding record (venue selected)
     78Update wedding record
    7979
    8080COMMIT
     
    8282}}}
    8383
    84 
    85 ==== Conflict detection and transaction need ====
     84==== Conflict detection and why transaction is needed ====
    8685In our prototype we check conflicts using the endpoint:
    8786
     
    9089}}}
    9190
    92 In a real system, a conflict check MUST be done inside a transaction, because:
    93 
    94 User A checks availability (free)
    95 
    96 User B checks availability (free)
    97 
    98 Both insert booking at same time
    99 
    100 → double booking occurs
     91In a real system, the conflict check MUST be done inside a transaction, because two users could do this at the same time:
     92
     93 * User A checks availability → free
     94 * User B checks availability → free
     95 * Both insert a booking at the same moment
     96 * Result → double-booking
    10197
    10298Correct approach:
    103 
    104 conflict check + insert booking must be executed atomically inside a transaction.
    105 
     99 * conflict check + insert booking must execute atomically inside one transaction.
    106100
    107101== Isolation and concurrency (basic overview) ==
     
    109103=== What is isolation? ===
    110104Isolation defines how multiple users/requests interact with the database at the same time.
    111 If isolation is weak, two concurrent transactions may read or write inconsistent values.
     105If isolation is weak, concurrent transactions may read or write inconsistent values.
    112106
    113107Common concurrency problems:
    114 
    115 Dirty read → reading uncommitted changes
    116 
    117 Non-repeatable read → same query returns different results in same transaction
    118 
    119 Phantom read → new rows appear in repeated query
    120 
    121 Even though SQLite is simplified, in production databases isolation level must be chosen carefully.
    122 
     108 * Dirty read – reading uncommitted changes
     109 * Non-repeatable read – the same query returns different result inside one transaction
     110 * Phantom read – new rows appear in repeated query
     111 * Lost update – two users overwrite each other’s updates
     112
     113Even though SQLite is simplified, production systems must carefully choose the isolation level.
    123114
    124115=== Example connected to our project ===
    125 If two users RSVP at the same time, without proper isolation:
    126 
    127 RSVP counts can become incorrect
    128 
    129 Attendance table could end up with duplicates
    130 
    131 The final result may not reflect correct guest statuses
    132 
    133 Therefore, operations such as RSVP update and attendance update should be transactional.
     116If multiple users RSVP or update attendance at the same time without transaction + isolation:
     117 * Attendance table could end up with duplicates
     118 * RSVP results could be overwritten
     119 * Counts per event may become incorrect
     120
     121Therefore, operations such as RSVP update and attendance update should be handled transactionally.
    134122
    135123== Transactions inside Flask prototype ==
    136124
    137125=== How transactions would be handled in Flask ===
    138 In Flask (with SQLAlchemy or DB sessions), the implementation is usually:
    139 
    140 start session/transaction
    141 
    142 execute queries
    143 
    144 commit
    145 
    146 on exception → rollback
    147 
    148 Pseudocode example (Flask-like):
     126In Flask applications (SQLAlchemy or DB sessions), the usual pattern is:
     127 * start transaction
     128 * execute queries
     129 * commit
     130 * if error → rollback
     131
     132Pseudocode example:
    149133
    150134{{{
    151135try:
    152 db.session.begin()
    153 
    154 # check availability
    155 # insert booking
    156 # update wedding
    157 
    158 db.session.commit()
    159 
    160 
     136    db.session.begin()
     137
     138    # check availability
     139    # insert booking
     140    # update wedding
     141
     142    db.session.commit()
    161143except Exception:
    162 db.session.rollback()
    163 }}}
    164 
    165 Even if our prototype is minimal and uses SQLite, the concept remains the same and would scale directly to PostgreSQL.
     144    db.session.rollback()
     145}}}
     146
     147Even though our prototype is minimal and uses SQLite, the concept stays identical and scales directly to PostgreSQL.
    166148
    167149== Database connection pooling ==
     
    171153
    172154Without pooling:
    173 
    174 each HTTP request opens a new DB connection
    175 
    176 connecting is expensive
    177 
    178 under load this becomes slow and unstable
     155 * each HTTP request opens a new DB connection
     156 * connecting is expensive
     157 * under high load, the system becomes slow and unstable
    179158
    180159With pooling:
    181 
    182 application keeps a pool of reusable connections
    183 
    184 requests take an available connection from the pool
    185 
    186 after request finishes → connection is returned to the pool
     160 * application keeps a pool of reusable connections
     161 * requests take a free connection from the pool
     162 * after finishing, the connection returns to the pool
    187163
    188164=== Why pooling improves performance ===
    189 Pooling gives:
    190 
    191 faster response time (no repeated connection overhead)
    192 
    193 controlled DB resource usage
    194 
    195 scalability for multiple concurrent users
    196 
    197 For example:
    198 
    199 100 users → 100 requests
    200 
    201 without pooling → 100 new connections created
    202 
    203 with pooling → e.g. 10 reusable connections shared
     165Pooling provides:
     166 * faster response time (no repeated connection overhead)
     167 * controlled DB resource usage
     168 * scalability for many concurrent users
     169
     170Example:
     171 * 100 users → 100 requests
     172 * without pooling → 100 separate new connections
     173 * with pooling → e.g. 10 connections reused and shared
    204174
    205175=== Pooling in our Wedding Planner system ===
    206 In the final real system, pooling would be very important for:
    207 
    208 /weddings
    209 /weddings/1/guests
    210 RSVP updates
    211 availability checks (these can be frequent)
    212 Availability check endpoints can receive many calls, so pooling prevents performance degradation.
     176In a production version of our project, pooling would be important for endpoints that can be called frequently:
     177 * /weddings
     178 * /weddings/<id>/guests
     179 * RSVP update operations
     180 * availability checks
     181
     182Availability checks can be called many times, so pooling prevents performance degradation.
    213183
    214184=== Example pooling config (conceptual) ===
    215 In SQLAlchemy a pool configuration might look like:
     185In SQLAlchemy a pool configuration could look like:
    216186
    217187{{{
    218188engine = create_engine(
    219 DB_URL,
    220 pool_size=10,
    221 max_overflow=20
     189    DB_URL,
     190    pool_size=10,
     191    max_overflow=20
    222192)
    223193}}}
    224194
    225 This ensures controlled and reusable DB connections.
    226 
    227 
    228195== Conclusion ==
    229 This phase introduced two key concepts for advanced backend applications: transactions and database connection pooling.
    230 Transactions ensure correctness and consistency, especially when multiple operations must succeed together (such as bookings + conflict checks).
    231 COMMIT and ROLLBACK ensure that the database is not left in a partial or broken state when an error occurs.
    232 
    233 Connection pooling improves performance and scalability by reusing database connections instead of creating new ones for every request.
    234 These principles directly apply to our Wedding Planner system, especially for booking, RSVP and venue availability scenarios, and they represent best practices for real production systems.
     196This phase introduced two key concepts for advanced backend applications: database transactions and database connection pooling.
     197
     198Transactions ensure correctness and consistency when multiple operations must succeed together (for example: booking + conflict check, RSVP updates, attendance updates).
     199COMMIT and ROLLBACK guarantee the database is not left in a partial or inconsistent state.
     200
     201Connection pooling improves scalability and performance by reusing database connections instead of creating a new one per request.
     202These principles apply directly to our Wedding Planner system, especially for booking, RSVP, attendance management and venue availability conflict detection.