Maison > Article > développement back-end > Implémentation d'un système de traitement des commandes : opérations de base de données avancées en partie
Bienvenue dans le troisième volet de notre série sur la mise en œuvre d'un système sophistiqué de traitement des commandes ! Dans nos articles précédents, nous avons jeté les bases de notre projet et exploré les flux de travail temporels avancés. Aujourd'hui, nous plongeons en profondeur dans le monde des opérations de bases de données à l'aide de sqlc, un outil puissant qui génère du code Go de type sécurisé à partir de SQL.
Dans la première partie, nous avons mis en place la structure de notre projet, implémenté une API CRUD de base et intégré une base de données Postgres. Dans la deuxième partie, nous avons élargi notre utilisation de Temporal, en mettant en œuvre des flux de travail complexes, en gérant des processus de longue durée et en explorant des concepts avancés tels que le modèle Saga.
Dans une architecture de microservices, en particulier celle qui gère des processus complexes comme la gestion des commandes, des opérations de base de données efficaces sont cruciales. Ils ont un impact direct sur les performances, l’évolutivité et la fiabilité de notre système. Une mauvaise conception de base de données ou des requêtes inefficaces peuvent devenir des goulots d'étranglement, entraînant des temps de réponse lents et une mauvaise expérience utilisateur.
sqlc est un outil qui génère du code Go de type sécurisé à partir de SQL. Voici quelques avantages clés :
À la fin de cet article, vous pourrez :
Plongeons-nous !
Avant de commencer la mise en œuvre, passons en revue quelques concepts clés qui seront cruciaux pour nos opérations avancées de base de données.
L'optimisation des performances SQL implique plusieurs techniques :
Les transactions garantissent qu'une série d'opérations de base de données sont exécutées comme une seule unité de travail. Les niveaux d'isolement déterminent la façon dont l'intégrité des transactions est visible pour les autres utilisateurs et systèmes. Les niveaux d'isolement courants incluent :
Le partage est une méthode de partitionnement horizontal des données sur plusieurs bases de données. Il s’agit d’une technique clé pour faire évoluer les bases de données afin de gérer de grandes quantités de données et des charges de trafic élevées. Le partitionnement, quant à lui, consiste à diviser une table en morceaux plus petits au sein de la même instance de base de données.
Les opérations par lots nous permettent d'effectuer plusieurs opérations de base de données en une seule requête. Cela peut améliorer considérablement les performances lors du traitement de grands ensembles de données en réduisant le nombre d'allers-retours vers la base de données.
Database migrations are a way to manage changes to your database schema over time. Effective migration strategies allow you to evolve your schema while minimizing downtime and ensuring data integrity.
Now that we’ve covered these concepts, let’s start implementing advanced database operations in our order processing system.
Let’s start by implementing some complex queries and transactions using sqlc. We’ll focus on our order processing system, adding some more advanced querying capabilities.
First, let’s update our schema to include a new table for order items:
-- migrations/000002_add_order_items.up.sql CREATE TABLE order_items ( id SERIAL PRIMARY KEY, order_id INTEGER NOT NULL REFERENCES orders(id), product_id INTEGER NOT NULL, quantity INTEGER NOT NULL, price DECIMAL(10, 2) NOT NULL );
Now, let’s define some complex queries in our sqlc query file:
-- queries/orders.sql -- name: GetOrderWithItems :many SELECT o.*, json_agg(json_build_object( 'id', oi.id, 'product_id', oi.product_id, 'quantity', oi.quantity, 'price', oi.price )) AS items FROM orders o JOIN order_items oi ON o.id = oi.order_id WHERE o.id = $1 GROUP BY o.id; -- name: CreateOrderWithItems :one WITH new_order AS ( INSERT INTO orders (customer_id, status, total_amount) VALUES ($1, $2, $3) RETURNING id ) INSERT INTO order_items (order_id, product_id, quantity, price) SELECT new_order.id, unnest($4::int[]), unnest($5::int[]), unnest($6::decimal[]) FROM new_order RETURNING (SELECT id FROM new_order); -- name: UpdateOrderStatus :exec UPDATE orders SET status = $2, updated_at = CURRENT_TIMESTAMP WHERE id = $1;
These queries demonstrate some more advanced SQL techniques:
Now, let’s generate our Go code:
sqlc generate
This will create Go functions for each of our queries. Let’s use these in our application:
package db import ( "context" "database/sql" ) type Store struct { *Queries db *sql.DB } func NewStore(db *sql.DB) *Store { return &Store{ Queries: New(db), db: db, } } func (s *Store) CreateOrderWithItemsTx(ctx context.Context, arg CreateOrderWithItemsParams) (int64, error) { tx, err := s.db.BeginTx(ctx, nil) if err != nil { return 0, err } defer tx.Rollback() qtx := s.WithTx(tx) orderId, err := qtx.CreateOrderWithItems(ctx, arg) if err != nil { return 0, err } if err := tx.Commit(); err != nil { return 0, err } return orderId, nil } func (s *Store) UpdateOrderStatusTx(ctx context.Context, id int64, status string) error { tx, err := s.db.BeginTx(ctx, nil) if err != nil { return err } defer tx.Rollback() qtx := s.WithTx(tx) if err := qtx.UpdateOrderStatus(ctx, UpdateOrderStatusParams{ID: id, Status: status}); err != nil { return err } // Simulate some additional operations that might be part of this transaction // For example, updating inventory, sending notifications, etc. if err := tx.Commit(); err != nil { return err } return nil }
In this code:
These examples demonstrate how to use sqlc to implement complex queries and handle transactions effectively. In the next section, we’ll look at how to optimize the performance of these database operations.
Optimizing database performance is crucial for maintaining a responsive and scalable system. Let’s explore some techniques to improve the performance of our order processing system.
PostgreSQL’s EXPLAIN command is a powerful tool for understanding and optimizing query performance. Let’s use it to analyze our GetOrderWithItems query:
EXPLAIN ANALYZE SELECT o.*, json_agg(json_build_object( 'id', oi.id, 'product_id', oi.product_id, 'quantity', oi.quantity, 'price', oi.price )) AS items FROM orders o JOIN order_items oi ON o.id = oi.order_id WHERE o.id = 1 GROUP BY o.id;
This will provide us with a query plan and execution statistics. Based on the results, we can identify potential bottlenecks and optimize our query.
Indexes can dramatically improve query performance, especially for large tables. Let’s add some indexes to our schema:
-- migrations/000003_add_indexes.up.sql CREATE INDEX idx_order_items_order_id ON order_items(order_id); CREATE INDEX idx_orders_customer_id ON orders(customer_id); CREATE INDEX idx_orders_status ON orders(status);
These indexes will speed up our JOIN operations and filtering by customer_id or status.
Choosing the right data types can impact both storage efficiency and query performance. For example, using BIGSERIAL instead of SERIAL for id fields allows for a larger range of values, which can be important for high-volume systems.
When dealing with large datasets, it’s important to implement pagination to avoid loading too much data at once. Let’s add a paginated query for fetching orders:
-- name: ListOrdersPaginated :many SELECT * FROM orders ORDER BY created_at DESC LIMIT $1 OFFSET $2;
In our Go code, we can use this query like this:
func (s *Store) ListOrdersPaginated(ctx context.Context, limit, offset int32) ([]Order, error) { return s.Queries.ListOrdersPaginated(ctx, ListOrdersPaginatedParams{ Limit: limit, Offset: offset, }) }
For data that’s frequently accessed but doesn’t change often, implementing a caching layer can significantly reduce database load. Here’s a simple example using an in-memory cache:
import ( "context" "sync" "time" ) type OrderCache struct { store *Store cache map[int64]*Order mutex sync.RWMutex ttl time.Duration } func NewOrderCache(store *Store, ttl time.Duration) *OrderCache { return &OrderCache{ store: store, cache: make(map[int64]*Order), ttl: ttl, } } func (c *OrderCache) GetOrder(ctx context.Context, id int64) (*Order, error) { c.mutex.RLock() if order, ok := c.cache[id]; ok { c.mutex.RUnlock() return order, nil } c.mutex.RUnlock() order, err := c.store.GetOrder(ctx, id) if err != nil { return nil, err } c.mutex.Lock() c.cache[id] = &order c.mutex.Unlock() go func() { time.Sleep(c.ttl) c.mutex.Lock() delete(c.cache, id) c.mutex.Unlock() }() return &order, nil }
This cache implementation stores orders in memory for a specified duration, reducing the need to query the database for frequently accessed orders.
Batch operations can significantly improve performance when dealing with large datasets. Let’s implement some batch operations for our order processing system.
First, let’s add a batch insert operation for order items:
-- name: BatchCreateOrderItems :copyfrom INSERT INTO order_items ( order_id, product_id, quantity, price ) VALUES ( $1, $2, $3, $4 );
In our Go code, we can use this to insert multiple order items efficiently:
func (s *Store) BatchCreateOrderItems(ctx context.Context, items []OrderItem) error { return s.Queries.BatchCreateOrderItems(ctx, items) }
When dealing with very large batches, it’s important to process them in chunks to avoid overwhelming the database or running into memory issues. Here’s an example of how we might do this:
func (s *Store) BatchCreateOrderItemsChunked(ctx context.Context, items []OrderItem, chunkSize int) error { for i := 0; i < len(items); i += chunkSize { end := i + chunkSize if end > len(items) { end = len(items) } chunk := items[i:end] if err := s.BatchCreateOrderItems(ctx, chunk); err != nil { return err } } return nil }
When performing batch operations, it’s important to handle partial failures gracefully. One approach is to use transactions and savepoints:
func (s *Store) BatchCreateOrderItemsWithSavepoints(ctx context.Context, items []OrderItem, chunkSize int) error { tx, err := s.db.BeginTx(ctx, nil) if err != nil { return err } defer tx.Rollback() qtx := s.WithTx(tx) for i := 0; i < len(items); i += chunkSize { end := i + chunkSize if end > len(items) { end = len(items) } chunk := items[i:end] _, err := tx.ExecContext(ctx, "SAVEPOINT batch_insert") if err != nil { return err } err = qtx.BatchCreateOrderItems(ctx, chunk) if err != nil { _, rbErr := tx.ExecContext(ctx, "ROLLBACK TO SAVEPOINT batch_insert") if rbErr != nil { return fmt.Errorf("batch insert failed and unable to rollback: %v, %v", err, rbErr) } // Log the error or handle it as appropriate for your use case fmt.Printf("Failed to insert chunk %d-%d: %v\n", i, end, err) } else { _, err = tx.ExecContext(ctx, "RELEASE SAVEPOINT batch_insert") if err != nil { return err } } } return tx.Commit() }
This approach allows us to rollback individual chunks if they fail, while still committing the successful chunks.
As our system evolves, we’ll need to make changes to our database schema. Managing these changes in a production environment requires careful planning and execution.
To achieve zero-downtime migrations, we can follow these steps:
Let’s look at an example of a backwards compatible migration:
-- migrations/000004_add_order_notes.up.sql ALTER TABLE orders ADD COLUMN notes TEXT; -- migrations/000004_add_order_notes.down.sql ALTER TABLE orders DROP COLUMN notes;
This migration adds a new column, which is a backwards compatible change. Existing queries will continue to work, and we can update our application to start using the new column.
We’re already using golang-migrate for our migrations, which keeps track of the current schema version. We can query this information to ensure our application is compatible with the current database schema:
func (s *Store) GetDatabaseVersion(ctx context.Context) (int, error) { var version int err := s.db.QueryRowContext(ctx, "SELECT version FROM schema_migrations ORDER BY version DESC LIMIT 1").Scan(&version) if err != nil { return 0, err } return version, nil }
Sometimes we need to not only change the schema but also transform existing data. Here’s an example of a migration that does both:
-- migrations/000005_split_name.up.sql ALTER TABLE customers ADD COLUMN first_name TEXT, ADD COLUMN last_name TEXT; UPDATE customers SET first_name = split_part(name, ' ', 1), last_name = split_part(name, ' ', 2) WHERE name IS NOT NULL; ALTER TABLE customers DROP COLUMN name; -- migrations/000005_split_name.down.sql ALTER TABLE customers ADD COLUMN name TEXT; UPDATE customers SET name = concat(first_name, ' ', last_name) WHERE first_name IS NOT NULL OR last_name IS NOT NULL; ALTER TABLE customers DROP COLUMN first_name, DROP COLUMN last_name;
This migration splits the name column into first_name and last_name, transforming the existing data in the process.
It’s crucial to test both the up and down migrations thoroughly before applying them to a production database. Always have a rollback plan ready in case issues are discovered after a migration is applied.
In the next sections, we’ll explore database sharding for scalability and ensuring data consistency in a distributed system.
As our order processing system grows, we may need to scale beyond what a single database instance can handle. Database sharding is a technique that can help us achieve horizontal scalability by distributing data across multiple database instances.
For our order processing system, we’ll implement a simple sharding strategy based on the customer ID. This approach ensures that all orders for a particular customer are on the same shard, which can simplify certain types of queries.
First, let’s create a sharding function:
const NUM_SHARDS = 4 func getShardForCustomer(customerID int64) int { return int(customerID % NUM_SHARDS) }
This function will distribute customers (and their orders) evenly across our shards.
Now, let’s implement a sharding layer that will route queries to the appropriate shard:
type ShardedStore struct { stores [NUM_SHARDS]*Store } func NewShardedStore(connStrings [NUM_SHARDS]string) (*ShardedStore, error) { var stores [NUM_SHARDS]*Store for i, connString := range connStrings { db, err := sql.Open("postgres", connString) if err != nil { return nil, err } stores[i] = NewStore(db) } return &ShardedStore{stores: stores}, nil } func (s *ShardedStore) GetOrder(ctx context.Context, customerID, orderID int64) (Order, error) { shard := getShardForCustomer(customerID) return s.stores[shard].GetOrder(ctx, orderID) } func (s *ShardedStore) CreateOrder(ctx context.Context, arg CreateOrderParams) (Order, error) { shard := getShardForCustomer(arg.CustomerID) return s.stores[shard].CreateOrder(ctx, arg) }
This ShardedStore maintains connections to all of our database shards and routes queries to the appropriate shard based on the customer ID.
Cross-shard queries can be challenging in a sharded database setup. For example, if we need to get all orders across all shards, we’d need to query each shard and combine the results:
func (s *ShardedStore) GetAllOrders(ctx context.Context) ([]Order, error) { var allOrders []Order for _, store := range s.stores { orders, err := store.ListOrders(ctx) if err != nil { return nil, err } allOrders = append(allOrders, orders...) } return allOrders, nil }
Cross-shard transactions are even more complex and often require a two-phase commit protocol or a distributed transaction manager. In many cases, it’s better to design your system to avoid the need for cross-shard transactions if possible.
As your data grows, you may need to add new shards or rebalance existing ones. This process can be complex and typically involves:
Here’s a simple example of how we might update our sharding function to handle a growing number of shards:
var NUM_SHARDS = 4 func updateNumShards(newNumShards int) { NUM_SHARDS = newNumShards } func getShardForCustomer(customerID int64) int { return int(customerID % int64(NUM_SHARDS)) }
In a production system, you’d want to implement a more sophisticated approach, possibly using a consistent hashing algorithm to minimize data movement when adding or removing shards.
Maintaining data consistency in a distributed system like our sharded database setup can be challenging. Let’s explore some strategies to ensure consistency.
While sqlc doesn’t directly support distributed transactions, we can implement a simple two-phase commit protocol for operations that need to span multiple shards. Here’s a basic example:
func (s *ShardedStore) CreateOrderAcrossShards(ctx context.Context, arg CreateOrderParams, items []CreateOrderItemParams) error { // Phase 1: Prepare var preparedTxs []*sql.Tx for _, store := range s.stores { tx, err := store.db.BeginTx(ctx, nil) if err != nil { // Rollback any prepared transactions for _, preparedTx := range preparedTxs { preparedTx.Rollback() } return err } preparedTxs = append(preparedTxs, tx) } // Phase 2: Commit for _, tx := range preparedTxs { if err := tx.Commit(); err != nil { // If any commit fails, we're in an inconsistent state // In a real system, we'd need a way to recover from this return err } } return nil }
This is a simplified example and doesn’t handle many edge cases. In a production system, you’d need more sophisticated error handling and recovery mechanisms.
In some cases, it may be acceptable (or necessary) to have eventual consistency rather than strong consistency. For example, if we’re generating reports across all shards, we might be okay with slightly out-of-date data:
func (s *ShardedStore) GetOrderCountsEventuallyConsistent(ctx context.Context) (map[string]int, error) { counts := make(map[string]int) var wg sync.WaitGroup var mu sync.Mutex errCh := make(chan error, NUM_SHARDS) for _, store := range s.stores { wg.Add(1) go func(store *Store) { defer wg.Done() localCounts, err := store.GetOrderCounts(ctx) if err != nil { errCh <- err return } mu.Lock() for status, count := range localCounts { counts[status] += count } mu.Unlock() }(store) } wg.Wait() close(errCh) if err := <-errCh; err != nil { return nil, err } return counts, nil }
This function aggregates order counts across all shards concurrently, providing a eventually consistent view of the data.
In distributed systems, it’s important to have mechanisms to handle partial failures. Compensating transactions can help restore the system to a consistent state when a distributed operation fails partway through.
Here’s an example of how we might implement a compensating transaction for a failed order creation:
func (s *ShardedStore) CreateOrderWithCompensation(ctx context.Context, arg CreateOrderParams) (Order, error) { shard := getShardForCustomer(arg.CustomerID) order, err := s.stores[shard].CreateOrder(ctx, arg) if err != nil { return Order{}, err } // Simulate some additional processing that might fail if err := someProcessingThatMightFail(); err != nil { // If processing fails, we need to compensate by deleting the order if err := s.stores[shard].DeleteOrder(ctx, order.ID); err != nil { // Log the error, as we're now in an inconsistent state log.Printf("Failed to compensate for failed order creation: %v", err) } return Order{}, err } return order, nil }
This function creates an order and then performs some additional processing. If the processing fails, it attempts to delete the order as a compensating action.
Maintaining referential integrity across shards can be challenging. One approach is to denormalize data to keep related entities on the same shard. For example, we might store a copy of customer information with each order:
type Order struct { ID int64 CustomerID int64 // Denormalized customer data CustomerName string CustomerEmail string // Other order fields... }
This approach trades some data redundancy for easier maintenance of consistency within a shard.
Thorough testing is crucial when working with complex database operations and distributed systems. Let’s explore some strategies for testing our sharded database system.
sqlc generates code that’s easy to unit test. Here’s an example of how we might test our GetOrder function:
func TestGetOrder(t *testing.T) { // Set up a test database db, err := sql.Open("postgres", "postgresql://testuser:testpass@localhost:5432/testdb") if err != nil { t.Fatalf("Failed to connect to test database: %v", err) } defer db.Close() store := NewStore(db) // Create a test order order, err := store.CreateOrder(context.Background(), CreateOrderParams{ CustomerID: 1, Status: "pending", TotalAmount: 100.00, }) if err != nil { t.Fatalf("Failed to create test order: %v", err) } // Test GetOrder retrievedOrder, err := store.GetOrder(context.Background(), order.ID) if err != nil { t.Fatalf("Failed to get order: %v", err) } if retrievedOrder.ID != order.ID { t.Errorf("Expected order ID %d, got %d", order.ID, retrievedOrder.ID) } // Add more assertions as needed... }
Integration tests can help ensure that our sharding logic works correctly with real database instances. Here’s an example:
func TestShardedStore(t *testing.T) { // Set up test database instances for each shard connStrings := [NUM_SHARDS]string{ "postgresql://testuser:testpass@localhost:5432/testdb1", "postgresql://testuser:testpass@localhost:5432/testdb2", "postgresql://testuser:testpass@localhost:5432/testdb3", "postgresql://testuser:testpass@localhost:5432/testdb4", } shardedStore, err := NewShardedStore(connStrings) if err != nil { t.Fatalf("Failed to create sharded store: %v", err) } // Test creating orders on different shards order1, err := shardedStore.CreateOrder(context.Background(), CreateOrderParams{CustomerID: 1, Status: "pending", TotalAmount: 100.00}) if err != nil { t.Fatalf("Failed to create order on shard 1: %v", err) } order2, err := shardedStore.CreateOrder(context.Background(), CreateOrderParams{CustomerID: 2, Status: "pending", TotalAmount: 200.00}) if err != nil { t.Fatalf("Failed to create order on shard 2: %v", err) } // Test retrieving orders from different shards retrievedOrder1, err := shardedStore.GetOrder(context.Background(), 1, order1.ID) if err != nil { t.Fatalf("Failed to get order from shard 1: %v", err) } retrievedOrder2, err := shardedStore.GetOrder(context.Background(), 2, order2.ID) if err != nil { t.Fatalf("Failed to get order from shard 2: %v", err) } // Add assertions to check the retrieved orders... }
Performance testing is crucial, especially when working with sharded databases. Here’s an example of how to benchmark our GetOrder function:
func BenchmarkGetOrder(b *testing.B) { // Set up your database connection db, err := sql.Open("postgres", "postgresql://testuser:testpass@localhost:5432/testdb") if err != nil { b.Fatalf("Failed to connect to test database: %v", err) } defer db.Close() store := NewStore(db) // Create a test order order, err := store.CreateOrder(context.Background(), CreateOrderParams{ CustomerID: 1, Status: "pending", TotalAmount: 100.00, }) if err != nil { b.Fatalf("Failed to create test order: %v", err) } // Run the benchmark b.ResetTimer() for i := 0; i < b.N; i++ { _, err := store.GetOrder(context.Background(), order.ID) if err != nil { b.Fatalf("Benchmark failed: %v", err) } } }
This benchmark will help you understand the performance characteristics of your GetOrder function and can be used to compare different implementations or optimizations.
As we implement and operate our sharded database system, there are several challenges and considerations to keep in mind:
Managing Database Connection Pools : With multiple database instances, it’s crucial to manage connection pools efficiently to avoid overwhelming any single database or running out of connections.
Handling Database Failover and High Availability : In a sharded setup, you need to consider what happens if one of your database instances fails. Implementing read replicas and automatic failover can help ensure high availability.
Consistent Backups Across Shards : Backing up a sharded database system requires careful coordination to ensure consistency across all shards.
Query Routing and Optimization : As your sharding scheme evolves, you may need to implement more sophisticated query routing to optimize performance.
Data Rebalancing : As some shards grow faster than others, you may need to periodically rebalance data across shards.
Cross-Shard Joins and Aggregations : These operations can be particularly challenging in a sharded system and may require implementation at the application level.
Maintaining Data Integrity : Ensuring data integrity across shards, especially for operations that span multiple shards, requires careful design and implementation.
Monitoring and Alerting : With a distributed database system, comprehensive monitoring and alerting become even more critical to quickly identify and respond to issues.
In diesem Beitrag haben wir uns eingehend mit erweiterten Datenbankoperationen mit SQLC befasst und dabei alles von der Optimierung von Abfragen und der Implementierung von Batch-Operationen bis hin zur Verwaltung von Datenbankmigrationen und der Implementierung von Sharding für Skalierbarkeit abgedeckt.
Im nächsten Teil unserer Serie konzentrieren wir uns auf die Überwachung und Alarmierung mit Prometheus. Wir behandeln Folgendes:
Bleiben Sie dran, während wir unser ausgefeiltes Auftragsabwicklungssystem weiter ausbauen und uns als Nächstes darauf konzentrieren, sicherzustellen, dass wir unser System in einer Produktionsumgebung effektiv überwachen und warten können!
Stehen Sie vor herausfordernden Problemen oder benötigen Sie eine externe Perspektive auf eine neue Idee oder ein neues Projekt? Ich kann helfen! Ganz gleich, ob Sie einen Technologie-Proof of Concept erstellen möchten, bevor Sie eine größere Investition tätigen, oder ob Sie Beratung bei schwierigen Themen benötigen, ich bin hier, um Ihnen zu helfen.
Wenn Sie an einer Zusammenarbeit mit mir interessiert sind, wenden Sie sich bitte per E-Mail an hungaikevin@gmail.com.
Lassen Sie uns Ihre Herausforderungen in Chancen verwandeln!
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!