Code Explainers

Code explainers tagged #batching

typescript
import { Injectable, Scope } from '@nestjs/common';
import DataLoader from 'dataloader';
import { InjectRepository } from '@nestjs/typeorm';
import { In, Repository } from 'typeorm';

Batching queries with DataLoader in NestJS

dataloader batching n+1
Intermediate 7 steps
java
@Configuration
public class DataLoaderConfig {
 
    @Bean

Batching GraphQL queries with DataLoaders in Spring

graphql batching n+1-problem
Advanced 8 steps
ruby
class BackfillUsersFullName < ActiveRecord::Migration[7.1]
  disable_ddl_transaction!
 
  BATCH_SIZE = 5_000

How to backfill a column safely in Rails

migrations batching backfill
Intermediate 7 steps
ruby
namespace :counter_cache do
  desc "Recalculate comments_count for posts after a backfill"
  task warm_post_comments: :environment do
    scope = Post.where(comments_count: nil).or(Post.where("comments_count < 0"))

Backfilling counter caches in a Rake task in Rails

counter-cache batching rake-tasks
Intermediate 6 steps
rust
use axum::{extract::State, response::IntoResponse, Json};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::sync::Arc;

Building a JSON-RPC 2.0 handler in Axum

json-rpc serde request-dispatch
Intermediate 8 steps
javascript
async function uploadInBatches(records, uploadFn, { batchSize = 100, concurrency = 3 } = {}) {
  const batches = [];
  for (let i = 0; i < records.length; i += batchSize) {
    batches.push(records.slice(i, i + batchSize));

Uploading records with bounded concurrency

concurrency worker-pool async-await
Advanced 8 steps
go
package batch
 
import "fmt"
 

Splitting a slice into batches in Go

generics slices error-handling
Intermediate 6 steps
ruby
class Debouncer
  def initialize(delay:)
    @delay = delay
    @mutex = Mutex.new

A thread-safe debouncer in Ruby

concurrency debouncing mutex
Advanced 5 steps
ruby
class Admin::OrdersExportController < Admin::BaseController
  include ActionController::Live
 
  def show

Streaming a CSV export in Rails

streaming csv-export batching
Advanced 8 steps
javascript
const MAX_BATCH_SIZE = 20;
const FLUSH_INTERVAL = 5000;
const ENDPOINT = '/api/analytics';
 

Batching analytics events in the browser

batching buffering sendbeacon
Intermediate 10 steps
java
@Controller
public class BookController {
 
    private final BookService bookService;

Solving GraphQL N+1 with batch mapping in Spring

graphql n+1-problem batching
Intermediate 8 steps
go
func (r *EventRepository) BatchInsert(ctx context.Context, events []Event) error {
	if len(events) == 0 {
		return nil
	}

Batch inserting rows in a Go transaction

transactions batching database
Intermediate 7 steps
go
type SensorReading struct {
	DeviceID    string    `json:"device_id" binding:"required"`
	Temperature float64   `json:"temperature"`
	RecordedAt  time.Time `json:"recorded_at" binding:"required"`

Streaming NDJSON ingestion in Gin

streaming batching json-decoding
Advanced 10 steps
ruby
class BulkImporter
  BATCH_SIZE = 500
 
  def initialize(records)

Batching bulk inserts in Rails

batching bulk-insert deduplication
Intermediate 5 steps
java
public class OrderRepository {
 
    private static final int BATCH_SIZE = 500;
    private static final String INSERT_SQL =

Batched JDBC inserts with transaction safety

jdbc batching transactions
Intermediate 8 steps
java
@Component
@Order(20)
public class ProductSearchIndexRunner implements ApplicationRunner {
 

Rebuilding a search index at Spring startup

startup-hook batching streaming
Intermediate 8 steps
typescript
type Event = Record<string, unknown>;
 
interface BatcherOptions {
  endpoint: string;

Batching analytics events in TypeScript

batching buffering async
Intermediate 8 steps
javascript
class DOMBatcher {
  constructor() {
    this.reads = [];
    this.writes = [];

Batching DOM reads and writes to avoid layout thrash

batching requestanimationframe layout-thrashing
Intermediate 8 steps