SQL Practice Logo

SQLPractice Online

SQL Server: T-SQL Features: Real-World

Module: Database-Specific Features

T-SQL procedural extensions enable complex business logic directly in the database. Stripe uses T-SQL stored procedures to process payment workflows with error handling. Microsoft Dynamics leverages control flow for batch operations. Banking systems rely on TRY/CATCH for transaction integrity. Salesforce manages 50K+ stored procedures for CRM logic. E-commerce platforms use MERGE for real-time inventory updates.

Stripe Payment Processing with Retry Logic

**Company**: Stripe

**Challenge**: Stripe processes millions of payments daily across 135+ currencies. Payment processing involves multiple steps: validate card, check fraud score, call payment gateway, update merchant balance, send notifications. External payment gateways have 1-2% failure rate due to network timeouts. Without retry logic, these transient failures would cause legitimate payments to fail, losing revenue for merchants.

**Solution**: Stripe uses T-SQL stored procedures with TRY/CATCH and WHILE loops for retry logic. When payment gateway calls fail, the procedure retries up to 3 times with exponential backoff (2s, 4s, 8s). This handles transient network errors while avoiding overwhelming the gateway.

**Results**: Retry logic reduced payment failure rate from 2.5% to 0.3% (8x improvement). 2.2% of payments succeed on retry (mostly network timeouts). Average retry count: 1.2 attempts per payment. Exponential backoff prevents overwhelming payment gateways. Stripe processes 50K payments/second with 99.99% success rate. Merchants see $2M+ additional revenue monthly from recovered failed payments.

-- Query payment success rates by retry count

SELECT

retry_count,

COUNT(*) AS payment_count,

SUM(CASE WHEN status = 'succeeded' THEN 1 ELSE 0 END) AS successful,

CAST(SUM(CASE WHEN status = 'succeeded' THEN 1 ELSE 0 END) * 100.0 / COUNT(*) AS DECIMAL(5,2)) AS success_rate

FROM payment_logs

WHERE created_at >= DATEADD(day, -7, GETDATE())

GROUP BY retry_count

ORDER BY retry_count;

-- Results:

-- retry_count | payment_count | successful | success_rate

-- 0 | 1,000,000 | 975,000 | 97.50%

-- 1 | 25,000 | 20,000 | 80.00%

-- 2 | 5,000 | 2,000 | 40.00%

-- 3 | 3,000 | 0 | 0.00%

-- Overall success rate: 99.70% (997,000 / 1,000,000)

-- Stored procedure code:

CREATE PROCEDURE ProcessPaymentWithRetry

@PaymentID INT,

@Amount DECIMAL(10,2),

@MerchantID INT,

@MaxRetries INT = 3

AS

BEGIN

SET NOCOUNT ON;

DECLARE @RetryCount INT = 0;

DECLARE @Success BIT = 0;

WHILE @RetryCount < @MaxRetries AND @Success = 0

BEGIN

BEGIN TRY

BEGIN TRANSACTION;

-- Validate, process payment, update balance

COMMIT TRANSACTION;