Skip to the page

Testing a feature and estimating work · senior

Testing consumers and big database migrations

Message consumers and migrations fail in ways a normal request test misses: duplicates, order, a database that goes down, long locks and giant backfills.

The notes

What you must know

  • Kafka delivers at least once, so the handler must be idempotent: dedupe by event id, or make the update conditional (UPDATE ... WHERE status = pending).
  • Bad message: after a few attempts it goes to a dead-letter topic with the error, and the partition moves on. A test proves the consumer does not loop forever.
  • Database down: no offset commit until the work is done, retries with backoff. The test stops the database container and starts it again. Crash between write and commit: the event comes again and idempotency makes it harmless.
  • Order holds only within one partition per key; events from two producers can arrive in any order, so the handler checks allowed state changes. Test schema changes with a schema registry or contract tests. Wait for results by polling (Awaitility or similar), never by fixed sleeps.
  • Migrations: test on a production-sized copy with personal data masked, to measure time, locks, WAL volume and replica lag.
  • ALTER TABLE takes an ACCESS EXCLUSIVE lock. If it waits behind a long transaction, every later query waits too and the table stops. Use a short lock_timeout with retries.
  • Postgres 11 and later add a column with a constant default without rewriting the table; a volatile default rewrites it. Safer order: add a nullable column, backfill, then enforce NOT NULL (a CHECK ... NOT VALID constraint validated separately avoids a long locked scan).
  • Expand and contract: the new schema works with both old and new code. Backfill in batches by primary-key range, throttled, resumable and idempotent, watching replica lag. Have a rollback or roll-forward plan, and verify with counts and a query for rows still empty.

The red pen

Common traps: mistake → fix

  • Mistake: Exactly-once delivery means no duplicate handling

    Fix: make handlers idempotent anyway.

  • Mistake: A migration that is fast on staging is fast in production

    Fix: table size and lock waits change everything.

  • Mistake: Rolling back a migration is always possible

    Fix: plan a down step or a roll-forward, and check backups first.

Easy to hard

Questions interviewers ask, easy to hard

  1. How would you test a Kafka consumer?
  2. How do you test what happens when the database is down during processing?
  3. How do you test a migration that adds a column to a 500 million row table?
  4. Why can an ALTER TABLE stop the whole table even if it is fast?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview