Skip to content

m1.2 : Added TTL support - #8

Merged
SantanuKar43 merged 12 commits into
mainfrom
ttl
May 13, 2026
Merged

m1.2 : Added TTL support#8
SantanuKar43 merged 12 commits into
mainfrom
ttl

Conversation

@Priyansh61

Copy link
Copy Markdown
Collaborator

No description provided.

Comment thread internal/store/store.go
}
}

func (s *Store) StartExpire(ctx context.Context) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is this method called?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry missed this added in main.go

Comment thread internal/store/store.go
Comment on lines +103 to +105
s.mu.RLock()
snapshot := s.tree
s.mu.RUnlock()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

snapshot := s.tree -> does this create a copy?
If not, what purpose does the Rlock serve?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The iradix returns a reference to the current tree version. The RLock ensures no concurrent writes modify s.tree while we're reading the reference. Once we have the reference, we can safely release the lock because the tree is immutable.

Comment thread internal/store/store.go
Comment on lines +112 to +122
snapshot.Root().Walk(func(key []byte, value entry) bool {
if runs >= sampleSize {
return true
}
if value.expireAt > 0 && value.expireAt < time.Now().Unix() {
expiredKeys = append(expiredKeys, string(key))
expiredCount++
}
runs++
return false
})

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we stop walking when runs >= sampleSize, will we be able to reach expired nodes always? Shouldn't we check expiredCount here instead?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct point, changed the approach to a last pointer tracking which helps us to make sure we cover the entire tree


store.Delete(cmd.Args[0])
return "OK"
case "EXPIRE":

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to handle writing to wal for expire and setex commands. Also replay for these commands needs to take care of the ttl.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is wal replay handled?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WAL entry: "SETEX mykey 60 myvalue" // 60 seconds TTL
Written at: 10:00:00 AM
Server crashes at: 10:00:30 AM (key should expire at 10:01:00 AM)
Server restarts at: 11:00:00 AM (1 hour later)

Problem: We only have "60 seconds" in WAL, not the absolute time "10:01:00 AM"

Iss replay ko kaiser handle karre?? TS bei daale kya??

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of writing the ttl, we can write the absolute time when it is supposed to expire. During replay just compare with current time and process accordingly.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@Priyansh61

Copy link
Copy Markdown
Collaborator Author

@SantanuKar43 can u merge this please?

@SantanuKar43
SantanuKar43 merged commit af2864e into main May 13, 2026
1 check passed
@SantanuKar43
SantanuKar43 deleted the ttl branch May 13, 2026 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants