Skip to content

Instantly share code, notes, and snippets.

@maxim-ge
Last active January 30, 2026 11:14
Show Gist options
  • Select an option

  • Save maxim-ge/08fffc26375cc5b03a68097edd135b20 to your computer and use it in GitHub Desktop.

Select an option

Save maxim-ge/08fffc26375cc5b03a68097edd135b20 to your computer and use it in GitHub Desktop.
Augment Code vs Copilot
@maxim-ge
Copy link
Author

maxim-ge commented Jan 30, 2026

Prompt for Copilot: which value is used to compare with?

Found it! Looking at impl.go:

ok, err := e.storage.CompareAndSwap(key, val, val, int(ttlSeconds))

Answer

The value being compared is val (the leadership value), and it's compared with itself (valval).

Why compare with the same value?

This pattern is used for TTL renewal during leadership maintenance:

  1. Old value: val - The current leader's identifier that was stored when leadership was first acquired
  2. New value: val - The same identifier (no change)
  3. Purpose:
    • Verify we still hold the leadership (the stored value matches our val)
    • Refresh the TTL to extend the leadership duration
    • Detect if someone else has taken over (if the value changed, CompareAndSwap returns false)

The flow

  1. Acquire leadership: InsertIfNotExist(key, val, ttlSeconds) - Store initial value
  2. Maintain leadership: Every ttlSeconds/2, call CompareAndSwap(key, val, val, ttlSeconds) to:
    • Confirm we're still the leader (value hasn't changed)
    • Reset the TTL clock
  3. If CAS fails: Someone changed the value or it expired → release leadership

So the "comparison" is actually a leadership verification + TTL refresh operation in one atomic step.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment