Skip to content

Crack SDE

Most of the content are generated by AI, with human being reviewed, edited, and revised

Menu
  • Home
  • Daily English Story
  • Tech Interviews
  • Cloud Native
  • DevOps
  • Artificial Intelligence
Menu

Understand Transaction in Database

Posted on 11/11/202311/18/2023 by user

In computing, a transaction typically refers to a group of operations that are executed as a single unit. The concept is widely used in database management systems and other applications where data integrity and consistency are crucial. Some key aspects of transactions are:

1. Atomicity

  • Atomicity ensures that all the operations within a transaction are treated as a single unit, which either all succeed or all fail. There is no in-between state. If any operation in the transaction fails, the transaction is aborted, and the system is left unchanged as if the transaction was never initiated.

2. Consistency

  • Transactions must ensure that the data remains consistent before and after the transaction. This means that the transaction transforms the system from one valid state to another valid state, without violating any data integrity or consistency rules of the system.

3. Isolation

  • Isolation means that transactions are executed independently of one another. Even if multiple transactions are executed concurrently, each transaction should not be aware of other transactions running in parallel. Isolation prevents transactions from interfering with each other and ensures that their concurrent execution leads to a state that would be obtained if transactions were executed serially.

4. Durability

  • Once a transaction is committed, its effects are permanent and must persist even if there’s a system failure. This is referred to as durability. Systems usually achieve this by logging transactions and their effects in non-volatile storage.

Types of Transactions

  • Flat Transactions: The most basic form, which doesn’t allow other transactions to be nested within.
  • Nested Transactions: Allow sub-transactions within a main transaction. Sub-transactions can succeed or fail independently, but their effects are only committed if the main transaction commits.
  • Distributed Transactions: Span multiple systems or databases, requiring coordination across different networked resources.

Transaction Management

Managing transactions often involves the following:

  • Concurrency Control: Mechanisms like locks or timestamps to ensure transactions don’t interfere with each other.
  • Recovery Management: Procedures to recover from failures like system crashes, ensuring durability and consistency.

ACID Properties

The key properties of transactions are often summarized by the acronym ACID:

  1. Atomicity: Ensuring the all-or-nothing property.
  2. Consistency: Ensuring data integrity.
  3. Isolation: Keeping transactions independent of each other.
  4. Durability: Making sure changes are permanent.

Transactions are a fundamental concept in database systems and other applications where data integrity and consistency are paramount. They ensure that a series of operations are executed in a controlled manner, maintaining the integrity and consistency of the data even in the presence of errors, failures, or concurrent access.

Let’s delve deeper into each aspect, followed by an example in Go.

1. Atomicity

  • Context: Atomicity ensures that a series of operations within a transaction are treated as a single unit. It’s an “all or nothing” approach. If any part of the transaction fails, the entire transaction is rolled back, leaving the database state unchanged.
  • Example: Consider a banking application where a transfer from Account A to Account B involves two steps: (1) Deduct amount from Account A, (2) Add amount to Account B. Atomicity ensures that if either step fails, neither of them takes effect.

2. Consistency

  • Context: Consistency ensures the database transitions from one valid state to another. Any transaction must leave the database in a consistent state, adhering to all defined rules, including integrity constraints.
  • Example: If a database has a rule that the total amount across all accounts must remain constant, a transaction transferring money between accounts must ensure this rule holds before and after its completion.

3. Isolation

  • Context: Isolation ensures that the transactions are executed in a way as if they are the only ones running in the system. The effects of an incomplete transaction are not visible to other transactions.
  • Example: If two transactions are transferring money from Account A to Account B and Account B to Account C, respectively, isolation ensures that the second transaction sees the effect of the first only after it’s completed.

4. Durability

  • Context: Durability guarantees that once a transaction has been committed, it will remain so, even in the event of power loss, crashes, or errors. The changes are recorded in non-volatile memory.
  • Example: If a transaction commits a data change and the system crashes immediately after, the change should be present when the system restarts.

Example in Go: Key-Value Store with Transaction

Let’s implement a basic in-memory key-value store in Go with transaction support. This will be a simplified model to demonstrate the concept:

package main

import (
    "errors"
    "sync"
)

// KeyValueStore struct holds the key-value pairs in memory and 
// a lock for concurrent access.
type KeyValueStore struct {
    data map[string]string
    lock sync.RWMutex
}

// Transaction struct holds a reference to the KeyValueStore and 
// records changes to be applied.
type Transaction struct {
    kvStore *KeyValueStore
    changes map[string]*string // A nil value in this map indicates a deletion.
}

// NewKeyValueStore initializes a new KeyValueStore.
func NewKeyValueStore() *KeyValueStore {
    return &KeyValueStore{data: make(map[string]string)}
}

// Begin initializes and returns a new transaction associated with 
// the KeyValueStore.
func (kv *KeyValueStore) Begin() *Transaction {
    return &Transaction{kvStore: kv, changes: make(map[string]*string)}
}

// Set records a new value or an update to an existing value in the transaction.
func (t *Transaction) Set(key, value string) {
    t.changes[key] = &value // Store a pointer to the value.
}

// Delete marks a key for deletion in the transaction.
func (t *Transaction) Delete(key string) {
    var nilValue *string = nil // Use nil pointer to represent deletion.
    t.changes[key] = nilValue
}

// Commit applies all recorded changes in the transaction to the KeyValueStore.
func (t *Transaction) Commit() error {
    t.kvStore.lock.Lock() // Ensure exclusive access to the KeyValueStore.
    defer t.kvStore.lock.Unlock()

    for key, value := range t.changes {
        if value == nil {
            // If value pointer is nil, delete the key.
            delete(t.kvStore.data, key)
        } else {
            // Otherwise, update the value in the store.
            t.kvStore.data[key] = *value
        }
    }
    return nil
}

// Get retrieves a value for a key from the KeyValueStore.
func (kv *KeyValueStore) Get(key string) (string, error) {
    kv.lock.RLock() // Acquire a read lock.
    defer kv.lock.RUnlock()

    value, ok := kv.data[key]
    if !ok {
        return "", errors.New("key not found")
    }
    return value, nil
}

func main() {
    kvStore := NewKeyValueStore()

    // Demonstrating transaction usage
    txn := kvStore.Begin() // Begin a new transaction
    txn.Set("key1", "value1") // Set key-value pairs in the transaction
    txn.Set("key2", "value2")
    txn.Commit() // Commit the transaction to apply changes

    // Retrieving values from the key-value store
    if value, err := kvStore.Get("key1"); err == nil {
        println("Key1:", value)
    }
    if value, err := kvStore.Get("key2"); err == nil {
        println("Key2:", value)
    }
}

In this code:

  • KeyValueStore is a simple key-value store.
  • Transaction holds changes made during the transaction.
  • Begin starts a new transaction.
  • Set and Delete record changes in the transaction without affecting the main store.
  • Commit applies changes to the main store.

This implementation is a basic example for illustrative purposes. Real-world transaction systems involve more complex features like rollback mechanisms, concurrency control, and durability guarantees (like logging changes for recovery).

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related

Recent Posts

  • LC#622 Design Circular Queue
  • Started with OpenTelemetry in Go
  • How Prometheus scrap works, and how to find the target node and get the metrics files
  • How to collect metrics of container, pods, node and cluster in k8s?
  • LC#200 island problem

Recent Comments

  1. another user on A Journey of Resilience

Archives

  • May 2025
  • April 2025
  • February 2025
  • July 2024
  • April 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • May 2023

Categories

  • Artificial Intelligence
  • Cloud Computing
  • Cloud Native
  • Daily English Story
  • Database
  • DevOps
  • Golang
  • Java
  • Leetcode
  • Startups
  • Tech Interviews
©2025 Crack SDE | Design: Newspaperly WordPress Theme
Manage Cookie Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
View preferences
{title} {title} {title}