synchronize emails in the local db and the server.

I would like to process emails and keep them in my database synchronized with ones on server. Is it any way to do it without using SEEN flag in search? Say can we get list of uids from the server to compare them with list of uids we keep locally to find
unprocessed ones? Or can we set custom flag PROCESSED and and fetch messages without that flag efficiently?