Buffering & Flushing
Smart strategies for buffering and flushing logs in Triton for optimal performance and reliability across Apex and LWC.
Understanding Buffering vs Flushing
Real-World Example: Financial Transaction Processing
public class PaymentProcessor {
public static void processPayment(Id accountId, Decimal amount) {
Long startTime = System.now().getTime();
// Set up template for all payment processing logs
Triton.instance.setTemplate(
Triton.makeBuilder()
.category(TritonTypes.Category.Integration)
.type(TritonTypes.Type.Backend)
.area(TritonTypes.Area.PaymentProcessing)
.relatedObject(accountId)
);
// Buffer routine operations using template
Triton.instance.addLog(
Triton.instance.fromTemplate()
.level(TritonTypes.Level.DEBUG)
.summary('Payment processing started')
.details('accountId=' + accountId + ', amount=' + amount)
);
try {
// First, make the callout to payment gateway
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.paymentgateway.com/process');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setBody(JSON.serialize(new Map<String, Object>{
'accountId' => accountId,
'amount' => amount,
'currency' => 'USD'
}));
HttpResponse response = new Http().send(req);
if (response.getStatusCode() == 200) {
// Parse successful response
Map<String, Object> responseData = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
// Now perform DML after successful callout
Payment__c payment = new Payment__c(
Account__c = accountId,
Amount__c = amount,
Status__c = 'Completed'
);
insert payment;
// Buffer success milestone using template
Triton.instance.addLog(
Triton.instance.fromTemplate()
.level(TritonTypes.Level.INFO)
.summary('Payment processed successfully')
.details('paymentId=' + payment.Id)
.relatedObject(payment.Id)
.integrationPayload(req, response)
.duration(System.now().getTime() - startTime)
);
} else {
// Immediately flush critical error using template
Triton.instance.log(
Triton.instance.fromTemplate()
.exception(new PaymentException('Gateway error: ' + response.getStatusCode()))
.summary('Payment gateway failed')
.integrationPayload(req, response)
);
}
} catch (Exception e) {
// Immediately flush critical errors using template
Triton.instance.log(
Triton.instance.fromTemplate()
.exception(e)
.summary('Payment processing failed')
);
throw e;
} finally {
// Flush all buffered logs at transaction boundary
Triton.instance.flush();
}
}
}LWC Buffering Example
When to Buffer vs Flush
Buffer When:
Flush Immediately When:
Performance Considerations
Apex Performance
LWC Performance
Best Practices
Buffer Management
Flush Strategies
Monitoring and Debugging
Advanced Patterns
Conditional Flushing
Smart Buffering with Limits
Last updated